mixin.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {
  2. getBillNumber
  3. } from '@/api/common'
  4. const includeList = ['crmOrder', 'salesOrder', 'leaveApply']
  5. export default {
  6. props: {
  7. config: {
  8. type: Object,
  9. default: () => {}
  10. },
  11. },
  12. data() {
  13. return {
  14. flowUrgentOptions: [{
  15. id: 1,
  16. fullName: '普通'
  17. }, {
  18. id: 2,
  19. fullName: '重要'
  20. }, {
  21. id: 3,
  22. fullName: '紧急'
  23. }],
  24. fileList: [],
  25. setting: {},
  26. userInfo: {},
  27. eventType: '',
  28. paymentMethodOptions: [],
  29. requiredList: {},
  30. requiredObj: []
  31. }
  32. },
  33. mounted() {
  34. this.init(this.config)
  35. this.$refs.dataForm.setRules(this.rules)
  36. this.userInfo = uni.getStorageSync('userInfo') || {}
  37. },
  38. methods: {
  39. checkChildRule() {
  40. let list = {}
  41. this.requiredObj.forEach((data) => {
  42. if (data.required) {
  43. list[data.id] = data.name + '不能为空'
  44. }
  45. })
  46. let title = [];
  47. for (let k in list) {
  48. let num = k.split("-");
  49. let childKey = num[0];
  50. num.forEach((model, i) => {
  51. if (i == 1) {
  52. let childData = this.dataForm[childKey]
  53. childData.forEach((child, i) => {
  54. if (child[model] instanceof Array) {
  55. if (child[model].length == 0) {
  56. title.push(list[childKey + "-" + model])
  57. }
  58. } else {
  59. if (!child[model] && child[model] !== 0) {
  60. title.push(list[childKey + "-" + model])
  61. }
  62. }
  63. })
  64. }
  65. })
  66. }
  67. let _regList = this.regList
  68. for (let k in _regList) {
  69. let childData = this.dataForm[k]
  70. for (let n in _regList[k]) {
  71. for (let i = 0; i < _regList[k][n].length; i++) {
  72. const element = _regList[k][n][i]
  73. if (element.pattern) {
  74. element.pattern = element.pattern.toString()
  75. let start = element.pattern.indexOf('/')
  76. let stop = element.pattern.lastIndexOf('/')
  77. let str = element.pattern.substring(start + 1, stop)
  78. let reg = new RegExp(str)
  79. element.pattern = reg
  80. }
  81. childData.forEach((item, index) => {
  82. if (item[n] && !element.pattern.test(item[n])) {
  83. title.push(element.message)
  84. }
  85. })
  86. }
  87. }
  88. }
  89. if (title.length > 0) {
  90. return title[0]
  91. }
  92. },
  93. /* 初始化处理 */
  94. init(data) {
  95. this.dataForm.id = data.id || ''
  96. this.dataForm.flowId = data.flowId
  97. this.setting = data
  98. if (data.formEnCode !== "revoke") this.updateDataRule()
  99. this.$nextTick(() => {
  100. this.$refs.dataForm.resetFields()
  101. if (this.beforeInit) this.beforeInit()
  102. if (data.id) {
  103. let dataForm = JSON.parse(JSON.stringify(data.draftData)) || JSON.parse(JSON.stringify(data
  104. .formData))
  105. if (this.selfGetInfo && typeof this.selfGetInfo === "function") {
  106. this.selfGetInfo(dataForm)
  107. } else {
  108. this.dataForm = JSON.parse(JSON.stringify(dataForm))
  109. }
  110. if (includeList.includes(data.formEnCode) && this.dataForm.fileJson) {
  111. this.fileList = JSON.parse(this.dataForm.fileJson)
  112. }
  113. return
  114. } else {
  115. if (this.selfInit) this.selfInit(data)
  116. if (!this.billEnCode) return
  117. getBillNumber(this.billEnCode).then(res => {
  118. if (data.formEnCode === 'crmOrder') {
  119. this.dataForm.orderCode = res.data
  120. } else {
  121. this.dataForm.billNo = res.data
  122. }
  123. })
  124. }
  125. })
  126. },
  127. /* 提交 */
  128. submit(eventType, flowUrgent) {
  129. this.eventType = eventType
  130. this.$refs.dataForm.setRules(this.rules)
  131. this.$refs.dataForm.validate((valid) => {
  132. if (valid) {
  133. if (includeList.includes(this.setting.formEnCode)) {
  134. this.dataForm.fileJson = !!this.fileList.length ? JSON.stringify(this.fileList) : ''
  135. }
  136. if (!!this.checkChildRule()) return this.$u.toast(`${this.checkChildRule()}`)
  137. if (this.exist && !!this.exist()) return this.$u.toast(`${this.exist()}`)
  138. let dataForm = {}
  139. if (this.beforeSubmit && typeof this.beforeSubmit === "function") {
  140. dataForm = this.beforeSubmit()
  141. } else {
  142. dataForm = this.dataForm
  143. }
  144. if (includeList.includes(this.setting.formEnCode)) {
  145. dataForm.fileJson = JSON.stringify(this.fileList)
  146. }
  147. if (eventType === 'save' || eventType === 'submit') {
  148. if (this.selfSubmit && typeof this.selfSubmit === "function") {
  149. this.selfSubmit(this.dataForm, flowUrgent)
  150. return
  151. }
  152. }
  153. this.$emit('eventReceiver', {
  154. formData: dataForm,
  155. id: this.dataForm.id
  156. }, eventType)
  157. }
  158. })
  159. },
  160. updateDataRule() {
  161. let newRules = {}
  162. for (let i = 0; i < this.setting.formOperates.length; i++) {
  163. const item = this.setting.formOperates[i]
  164. if (item.required) {
  165. this.$set(this.requiredList, item.id, item.required)
  166. if (item.jnpfKey != 'rate' && item.jnpfKey != 'slider') this.requiredObj.push(item)
  167. }
  168. const newRulesItem = {
  169. required: item.required || false,
  170. message: item.name + '不能为空',
  171. trigger: item.trigger || ['blur', 'change'],
  172. type: ''
  173. }
  174. const numList = ['inputNumber', 'datePicker', 'switch', 'rate', 'slider']
  175. if (item.dataType === 'array') newRulesItem.type = item.dataType
  176. if (numList.includes(item.jnpfKey)) newRulesItem.type = 'number'
  177. if (['relationForm', 'popupSelect'].includes(item.jnpfKey)) {
  178. newRulesItem.type = 'any',
  179. newRulesItem.validator = (rule, value, callback) => {
  180. if (value || value === 0) {
  181. callback();
  182. } else {
  183. callback(new Error(item.name + '不能为空'));
  184. }
  185. }
  186. }
  187. if (!this.rules.hasOwnProperty(item.id)) {
  188. if (item.required) this.$set(newRules, item.id, [newRulesItem])
  189. } else {
  190. let withoutRequiredItem = true
  191. for (let i = 0; i < this.rules[item.id].length; i++) {
  192. if (this.rules[item.id][i].hasOwnProperty('required')) {
  193. this.rules[item.id][i].required = item.required || false
  194. withoutRequiredItem = false
  195. }
  196. }
  197. if (withoutRequiredItem && item.required) this.rules[item.id].push(newRulesItem)
  198. }
  199. }
  200. this.rules = {
  201. ...this.rules,
  202. ...newRules
  203. }
  204. this.$refs.dataForm.setRules(this.rules)
  205. },
  206. /* 可见 */
  207. judgeShow(id) {
  208. if (this.setting.opType == 4) return true
  209. if (!this.setting.formOperates || !this.setting.formOperates.length) return true
  210. let arr = this.setting.formOperates.filter(o => o.id === id) || []
  211. if (!arr.length) return true
  212. let item = arr[0]
  213. return item.read
  214. },
  215. /* 可写 */
  216. judgeWrite(id) {
  217. if (this.setting.readonly) return true
  218. if (!this.setting.formOperates || !this.setting.formOperates.length) return false
  219. let arr = this.setting.formOperates.filter(o => o.id === id) || []
  220. if (!arr.length) return true
  221. let item = arr[0]
  222. return !item.write
  223. }
  224. }
  225. }