index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. clone,
  3. errorMsg,
  4. staticName
  5. } from "@/utils/tools"
  6. export default {
  7. computed: {
  8. isAdd() {
  9. return this.action === 'add'
  10. },
  11. isView() {
  12. return this.action === 'view'
  13. },
  14. isEdit() {
  15. return this.action === 'edit'
  16. },
  17. readonly() {
  18. if (!this.action) return true
  19. return this.isView
  20. }
  21. },
  22. data() {
  23. return {
  24. rowStyle: 'margin-bottom: 10px',
  25. action: null,
  26. editData: null,
  27. canSave: false,
  28. form: {},
  29. beforeHooks: [],
  30. afterHooks: [],
  31. rules: {}
  32. }
  33. },
  34. onReady() {
  35. this.$refs.editForm.setRules(
  36. this.rules
  37. )
  38. },
  39. onLoad({
  40. action,
  41. json
  42. }) {
  43. this.action = action
  44. if (action !== 'add' && !json) {
  45. return uni.navigateBack()
  46. } else if (json) {
  47. this.editData = JSON.parse(json)
  48. }
  49. },
  50. created() {
  51. this.resetForm()
  52. },
  53. mounted() {
  54. this.init()
  55. },
  56. methods: {
  57. init() {
  58. this.beforeHooks = []
  59. this.afterHooks = []
  60. this.setUp && this.setUp()
  61. this.resetForm()
  62. this.afterReset && this.afterReset()
  63. Promise.all(this.beforeHooks.map(f => f(this.action))).then(_ => {
  64. if (this.loadDataCondition()) {
  65. uni.showLoading({
  66. title: '加载数据 ...'
  67. })
  68. this.getData().then(resp => {
  69. const d = this.parseResp(resp)
  70. if (d) {
  71. this.form = d
  72. return Promise.all(this.afterHooks.map(f => f(this.action, resp, d)))
  73. } else {
  74. return Promise.reject('获取数据失败')
  75. }
  76. }).then(_ => {
  77. if (!this.readonly) {
  78. this.canSave = true
  79. }
  80. }).catch(err => {
  81. if (err) {
  82. errorMsg(err)
  83. }
  84. this.canSave = false
  85. }).finally(_ => {
  86. uni.hideLoading()
  87. })
  88. }
  89. }).catch(err => {
  90. errorMsg('初始化失败')
  91. if (err) {
  92. console.error(err)
  93. }
  94. })
  95. },
  96. resetForm() {
  97. this.canSave = false
  98. this.form = {}
  99. if ('defaultForm' in this) {
  100. this.form = clone(this.defaultForm)
  101. }
  102. if ('tableData' in this) {
  103. this.tableData = []
  104. }
  105. if ('uploadFiles' in this) {
  106. this.uploadFiles = []
  107. }
  108. },
  109. loadDataCondition() {
  110. return this.isView || this.isEdit
  111. },
  112. addBeforeHook(hook) {
  113. this.beforeHooks.push(hook)
  114. },
  115. addAfterHook(hook) {
  116. this.afterHooks.push(hook)
  117. },
  118. afterReset() {},
  119. getDataParam() {
  120. return this.editData.id
  121. },
  122. parseResp(resp) {
  123. return resp.data
  124. },
  125. getData() {
  126. return this.getDataFun()(this.getDataParam())
  127. },
  128. getDataFun() {
  129. return Promise.resolve({})
  130. },
  131. staticName,
  132. back(refresh = false) {
  133. console.log(this, this.listUrl, refresh);
  134. if (this.listUrl) {
  135. const url = `${this.listUrl}?refresh=${refresh}`
  136. uni.redirectTo({
  137. url
  138. })
  139. } else {
  140. uni.navigateBack()
  141. }
  142. },
  143. getSaveFun() {
  144. },
  145. getLocalData() {
  146. return {
  147. ...this.form
  148. }
  149. },
  150. saveSuccessful(resp) {
  151. return resp.code === 200
  152. },
  153. beforeSaveCheck() {
  154. return Promise.resolve()
  155. },
  156. save() {
  157. console.log(this.$refs.editForm.validate);
  158. this.beforeSaveCheck().then(_ => {
  159. this.$refs.editForm.validate().then(res => {
  160. this.getSaveFun()(this.getLocalData()).then(resp => {
  161. console.log('save callback', resp);
  162. if (this.saveSuccessful(resp)) {
  163. this.back(true)
  164. } else {
  165. errorMsg('操作失败')
  166. }
  167. })
  168. })
  169. }).catch(err => {
  170. if(err) {
  171. errorMsg(err)
  172. }
  173. })
  174. }
  175. },
  176. }