index.vue 6.5 KB

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