index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <view class="jnpf-wrap jnpf-wrap-workflow">
  3. <JnpfParser :formConf="formConf" ref="dynamicForm" v-if="!loading" @submit="sumbitForm" :key="key" />
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. props: {
  9. config: {
  10. type: Object,
  11. default: () => {}
  12. },
  13. },
  14. data() {
  15. return {
  16. loading: true,
  17. key: +new Date(),
  18. setting: {},
  19. formConf: {},
  20. formData: {},
  21. eventType: '',
  22. flowUrgent: 1,
  23. dataForm: {
  24. id: '',
  25. flowId: ''
  26. },
  27. isAdd: false,
  28. userInfo: {}
  29. }
  30. },
  31. mounted() {
  32. this.init(this.config)
  33. },
  34. methods: {
  35. init(data) {
  36. this.userInfo = uni.getStorageSync('userInfo') || {}
  37. this.setting = data
  38. this.formConf = data.formConf ? JSON.parse(data.formConf) : {}
  39. this.dataForm.id = data.id || null;
  40. this.dataForm.flowId = data.flowId;
  41. this.loading = true;
  42. this.formData = {};
  43. this.$nextTick(() => {
  44. let extra = {}
  45. if (data.id) {
  46. this.isAdd = false;
  47. extra = {
  48. modelId: data.flowId,
  49. id: this.dataForm.id,
  50. type: data.type,
  51. flowId: data.flowId,
  52. processId: data.id,
  53. opType: data.opType,
  54. taskId: data.taskId
  55. }
  56. uni.setStorageSync('dynamicModelExtra', extra)
  57. const formData = data.draftData || data.formData || {}
  58. this.formData = {
  59. ...formData,
  60. flowId: data.flowId
  61. }
  62. } else {
  63. this.isAdd = true;
  64. }
  65. if (data.previewType == 'initiationForm') this.formData = data.formData || {}
  66. this.fillFormData(this.formConf, this.formData)
  67. this.$nextTick(() => {
  68. this.loading = false
  69. })
  70. this.dataForm.flowId = data.flowId
  71. this.key = +new Date()
  72. })
  73. },
  74. fillFormData(form, data) {
  75. form.disabled = this.setting.readonly
  76. const loop = (list, parent) => {
  77. for (let i = 0; i < list.length; i++) {
  78. let item = list[i]
  79. let vModel = item.__vModel__
  80. let config = item.__config__
  81. if (vModel) {
  82. let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
  83. if (!config.isSubTable) config.defaultValue = val
  84. if (this.isAdd || config.isSubTable) { //新增时候,默认当前
  85. if (config.defaultCurrent) {
  86. if (config.jnpfKey === 'datePicker') {
  87. if (!data.hasOwnProperty(vModel)) {
  88. let format = this.jnpf.handelFormat(item.format)
  89. let dateStr = this.jnpf.toDate(new Date().getTime(), format)
  90. let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
  91. '-01 00:00:00' : format === 'yyyy-MM-dd' ?
  92. ' 00:00:00' : ''
  93. val = new Date(dateStr + time).getTime()
  94. config.defaultValue = val
  95. }
  96. }
  97. if (config.jnpfKey === 'timePicker') {
  98. if (!data.hasOwnProperty(vModel)) {
  99. config.defaultValue = this.jnpf.toDate(new Date(), item.format)
  100. }
  101. }
  102. const organizeIdList = this.userInfo.organizeIdList
  103. if (config.jnpfKey === 'organizeSelect' && Array.isArray(organizeIdList) &&
  104. organizeIdList.length) {
  105. config.defaultValue = item.multiple ? [organizeIdList] : organizeIdList
  106. }
  107. const departmentId = this.userInfo.departmentId
  108. if (config.jnpfKey === 'depSelect' && departmentId) {
  109. config.defaultValue = item.multiple ? [departmentId] : departmentId;
  110. }
  111. const positionIds = this.userInfo.positionIds
  112. if (config.jnpfKey === 'posSelect' && Array.isArray(positionIds) && positionIds
  113. .length) {
  114. config.defaultValue = item.multiple ? positionIds.map(o => o.id) : positionIds[
  115. 0].id
  116. }
  117. const roleIds = this.userInfo.roleIds
  118. if (config.jnpfKey === 'roleSelect' && Array.isArray(roleIds) && roleIds.length) {
  119. config.defaultValue = item.multiple ? roleIds : roleIds[0];
  120. }
  121. const groupIds = this.userInfo.groupIds
  122. if (config.jnpfKey === 'groupSelect' && Array.isArray(groupIds) && groupIds
  123. .length) {
  124. config.defaultValue = item.multiple ? groupIds : groupIds[0];
  125. }
  126. const userId = this.userInfo.userId
  127. if (config.jnpfKey === 'userSelect' && userId) {
  128. config.defaultValue = item.multiple ? [userId] : userId;
  129. }
  130. if (config.jnpfKey === 'usersSelect' && userId) {
  131. config.defaultValue = item.multiple ? [userId + '--user'] : userId + '--user';
  132. }
  133. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  134. config.defaultValue = this.userInfo.signImg
  135. }
  136. }
  137. }
  138. let noShow = item.__config__.noShow || false,
  139. isDisabled = item.disabled || false,
  140. required = item.__config__.required || false,
  141. isVisibility = false
  142. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  143. .__config__.visibility.includes('app'))) isVisibility = true
  144. if (this.setting.formOperates && this.setting.formOperates.length) {
  145. let id = item.__config__.isSubTable ? parent?.__vModel__ + '-' + item?.__vModel__ :
  146. item
  147. .__vModel__
  148. let arr = this.setting.formOperates.filter(o => o.id === id) || []
  149. if (arr.length) {
  150. let obj = arr[0]
  151. noShow = !obj.read
  152. isDisabled = !obj.write
  153. required = obj.required ? obj.required : item.__config__.required
  154. }
  155. }
  156. isDisabled = item.readonly ? item.readonly : isDisabled;
  157. if (this.setting.readonly || config.disabled) isDisabled = true
  158. if (this.setting.origin === 'scan') isDisabled = true
  159. this.$set(item, 'disabled', isDisabled)
  160. this.$set(item.__config__, 'noShow', noShow)
  161. this.$set(item.__config__, 'required', required)
  162. this.$set(item.__config__, 'isVisibility', isVisibility)
  163. } else {
  164. let noShow = item.__config__.noShow ? item.__config__.noShow : false,
  165. isVisibility = false
  166. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  167. .__config__.visibility.includes('app'))) isVisibility = true
  168. this.$set(item.__config__, 'isVisibility', isVisibility)
  169. this.$set(item.__config__, 'noShow', noShow)
  170. }
  171. if (item.__config__ && item.__config__.children && Array.isArray(item.__config__.children)) {
  172. loop(item.__config__.children, item)
  173. }
  174. }
  175. }
  176. loop(form.fields)
  177. form.formData = data
  178. },
  179. sumbitForm(data, callback) {
  180. if (!data) return
  181. const formData = {
  182. ...this.formData,
  183. ...data
  184. }
  185. this.dataForm.formData = formData
  186. if (callback && typeof callback === "function") callback()
  187. this.$emit('eventReceiver', this.dataForm, this.eventType)
  188. },
  189. submit(eventType, flowUrgent) {
  190. if (this.setting.isPreview == '1') return this.$u.toast('功能预览不支持数据保存')
  191. this.eventType = eventType
  192. this.flowUrgent = flowUrgent
  193. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  194. },
  195. }
  196. }
  197. </script>