index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (config.jnpfKey === 'organizeSelect' && this.userInfo.organizeIds?.length) {
  105. config.defaultValue = item.multiple ? this.userInfo.organizeIds :
  106. this.userInfo.organizeId
  107. }
  108. if (config.jnpfKey === 'posSelect' && this.userInfo.positionIds?.length) {
  109. config.defaultValue = item.multiple ? this.userInfo.positionIds :
  110. this.userInfo.positionId
  111. }
  112. const userId = this.userInfo.userId
  113. if (config.jnpfKey === 'userSelect' && userId) {
  114. config.defaultValue = item.multiple ? [userId] : userId;
  115. }
  116. if (config.jnpfKey === 'usersSelect' && userId) {
  117. config.defaultValue = [userId + '--user'];
  118. }
  119. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  120. config.defaultValue = this.userInfo.signImg
  121. }
  122. }
  123. }
  124. if (this.origin === 'scan') this.$set(item, 'disabled', true)
  125. let noShow = !config.noShow ? false : config.noShow
  126. let isVisibility = false
  127. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  128. 'app'))) isVisibility = true
  129. this.$set(config, 'isVisibility', isVisibility)
  130. this.$set(config, 'noShow', noShow)
  131. } else {
  132. let noShow = false,
  133. isVisibility = false
  134. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  135. 'app'))) isVisibility = true
  136. this.$set(config, 'isVisibility', isVisibility)
  137. this.$set(config, 'noShow', noShow)
  138. }
  139. if (config && config.children && Array.isArray(config.children)) loop(config.children)
  140. }
  141. }
  142. loop(form.fields)
  143. },
  144. sumbitForm(data, callback) {
  145. if (!data) return
  146. this.btnLoading = true
  147. this.dataForm.data = JSON.stringify(data)
  148. if (callback && typeof callback === "function") callback()
  149. createModel(this.modelId, this.dataForm).then(res => {
  150. uni.showToast({
  151. title: res.msg,
  152. complete: () => {
  153. setTimeout(() => {
  154. this.btnLoading = false
  155. uni.navigateBack()
  156. }, 1500)
  157. }
  158. })
  159. }).catch(() => {
  160. this.btnLoading = false
  161. })
  162. },
  163. submit() {
  164. if (this.isPreview) return this.$u.toast('功能预览不支持数据保存')
  165. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  166. },
  167. resetForm() {
  168. this.loading = true
  169. this.$nextTick(() => {
  170. this.loading = false
  171. this.$refs.dynamicForm && this.$refs.dynamicForm.resetForm()
  172. this.init()
  173. this.key = +new Date()
  174. })
  175. }
  176. }
  177. }
  178. </script>