index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="jnpf-wrap jnpf-wrap-form">
  3. <JnpfParser :formConf="formConf" ref="dynamicForm" v-if="!loading" @submit="sumbitForm" :key="key" />
  4. <view class="buttom-actions">
  5. <u-button class="buttom-btn" @click.stop="cancel">取消</u-button>
  6. <u-button class="buttom-btn" type="primary" @click.stop="submit" :loading="btnLoading">
  7. {{config.confirmButtonText||'确定'}}
  8. </u-button>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import {
  14. getConfigDataByMenuId,
  15. getModelInfo,
  16. createModel
  17. } from '@/api/apply/visualDev'
  18. import {
  19. getDataInterfaceRes
  20. } from '@/api/common'
  21. export default {
  22. data() {
  23. return {
  24. config: {},
  25. id: "",
  26. modelId: "",
  27. formConf: {},
  28. dataForm: {},
  29. key: +new Date(),
  30. loading: false,
  31. btnLoading: false,
  32. isPreview: true,
  33. formData: {},
  34. isAdd: false,
  35. userInfo: {}
  36. }
  37. },
  38. onLoad(e) {
  39. this.userInfo = uni.getStorageSync('userInfo') || {}
  40. this.loading = true
  41. let data = e.data ? JSON.parse(decodeURIComponent(e.data)) : {}
  42. this.config = data.config
  43. this.id = data.id
  44. this.modelId = data.modelId
  45. this.isPreview = data.isPreview
  46. if (this.id != null && this.id != undefined && this.id != '') {
  47. this.isAdd = false
  48. } else {
  49. this.isAdd = true
  50. }
  51. uni.setNavigationBarTitle({
  52. title: this.config.popupTitle
  53. })
  54. if (this.config.modelId) this.getConfigData(data.row)
  55. },
  56. methods: {
  57. getConfigData(row) {
  58. getConfigDataByMenuId({
  59. menuId: this.config.modelId
  60. }).then(res => {
  61. this.config.modelId = res.data.id
  62. if (res.code !== 200 || !res.data) {
  63. uni.showToast({
  64. title: res.msg || '请求出错,请重试',
  65. icon: 'none'
  66. })
  67. return
  68. }
  69. this.formConf = JSON.parse(res.data.formData)
  70. const setDataFun = (formData) => {
  71. if (this.config.formOptions.length) {
  72. for (let k in formData) {
  73. for (let i = 0; i < this.config.formOptions.length; i++) {
  74. const e = this.config.formOptions[i]
  75. if (e.currentField == '@formId') this.formData[e.field] = formData.id;
  76. if (e.currentField == k) this.formData[e.field] = formData[k]
  77. }
  78. }
  79. }
  80. this.fillFormData(this.formConf, this.formData)
  81. this.key = +new Date()
  82. this.loading = false
  83. }
  84. if (this.id) {
  85. getModelInfo(this.modelId, this.id).then(res => {
  86. let dataForm = res.data
  87. if (!dataForm.data) return
  88. const formData = JSON.parse(dataForm.data)
  89. this.formData = {}
  90. setDataFun({
  91. ...formData,
  92. id: this.id
  93. })
  94. })
  95. } else {
  96. const formData = row
  97. setDataFun(formData)
  98. }
  99. }).catch(() => {})
  100. },
  101. fillFormData(form, data) {
  102. const loop = list => {
  103. for (let i = 0; i < list.length; i++) {
  104. let item = list[i]
  105. let vModel = item.__vModel__
  106. let config = item.__config__
  107. if (vModel) {
  108. let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
  109. if (!config.isSubTable) config.defaultValue = val
  110. if (config.defaultCurrent) {
  111. if (config.jnpfKey === 'datePicker') {
  112. if (!data.hasOwnProperty(vModel)) {
  113. let format = this.jnpf.handelFormat(item.format)
  114. let dateStr = this.jnpf.toDate(new Date().getTime(), format)
  115. let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
  116. '-01 00:00:00' : format === 'yyyy-MM-dd' ?
  117. ' 00:00:00' : ''
  118. val = new Date(dateStr + time).getTime()
  119. config.defaultValue = val
  120. }
  121. }
  122. if (config.jnpfKey === 'timePicker') {
  123. if (!data.hasOwnProperty(vModel)) {
  124. config.defaultValue = this.jnpf.toDate(new Date(), item.format)
  125. }
  126. }
  127. if (config.jnpfKey === 'organizeSelect' && this.userInfo.organizeIds?.length) {
  128. config.defaultValue = item.multiple ? this.userInfo.organizeIds :
  129. this.userInfo.organizeId
  130. }
  131. if (config.jnpfKey === 'posSelect' && this.userInfo.positionIds?.length) {
  132. config.defaultValue = item.multiple ? this.userInfo.positionIds :
  133. this.userInfo.positionId
  134. }
  135. const userId = this.userInfo.userId
  136. if (config.jnpfKey === 'userSelect' && userId) {
  137. config.defaultValue = item.multiple ? [userId] : userId;
  138. }
  139. if (config.jnpfKey === 'usersSelect' && userId) {
  140. config.defaultValue = [userId + '--user'];
  141. }
  142. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  143. config.defaultValue = this.userInfo.signImg
  144. }
  145. }
  146. let noShow = !item.__config__.noShow ? false : item.__config__.noShow
  147. let isVisibility = false
  148. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  149. .__config__.visibility.includes('app'))) isVisibility = true
  150. this.$set(item.__config__, 'isVisibility', isVisibility)
  151. this.$set(item.__config__, 'noShow', noShow)
  152. } else {
  153. let noShow = false,
  154. isVisibility = false
  155. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  156. .__config__.visibility.includes('app'))) isVisibility = true
  157. this.$set(item.__config__, 'isVisibility', isVisibility)
  158. this.$set(item.__config__, 'noShow', noShow)
  159. }
  160. if (item.__config__ && item.__config__.children && Array
  161. .isArray(item.__config__.children)) {
  162. loop(item.__config__.children)
  163. }
  164. }
  165. }
  166. loop(form.fields)
  167. },
  168. cancel() {
  169. uni.navigateBack();
  170. },
  171. sumbitForm(data, callback) {
  172. if (!data) return
  173. this.btnLoading = true
  174. const successFun = (res, callback) => {
  175. if (callback && typeof callback === "function") callback()
  176. uni.showToast({
  177. title: res.msg,
  178. complete: () => {
  179. setTimeout(() => {
  180. this.btnLoading = false
  181. if (this.config.isRefresh) uni.$emit('refresh')
  182. uni.navigateBack()
  183. }, 1500)
  184. }
  185. })
  186. }
  187. if (this.config.customBtn) {
  188. const query = {
  189. paramList: this.jnpf.getParamList(this.config.templateJson, {
  190. ...data,
  191. id: this.id
  192. }) || []
  193. };
  194. getDataInterfaceRes(this.config.interfaceId, query).then(res => {
  195. successFun(res, callback)
  196. }).catch(() => {
  197. this.btnLoading = false
  198. })
  199. } else {
  200. this.dataForm.data = JSON.stringify(data)
  201. createModel(this.config.modelId, this.dataForm).then(res => {
  202. successFun(res, callback)
  203. }).catch(() => {
  204. this.btnLoading = false
  205. })
  206. }
  207. },
  208. submit() {
  209. if (this.isPreview) {
  210. uni.showToast({
  211. title: '功能预览不支持数据保存',
  212. icon: 'none'
  213. })
  214. return
  215. }
  216. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  217. },
  218. }
  219. }
  220. </script>
  221. <style scoped lang="scss">
  222. page {
  223. background-color: #f0f2f6;
  224. }
  225. </style>