index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. getConfigData,
  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. getConfigData(this.config.modelId).then(res => {
  59. if (res.code !== 200 || !res.data) {
  60. uni.showToast({
  61. title: res.msg || '请求出错,请重试',
  62. icon: 'none'
  63. })
  64. return
  65. }
  66. this.formConf = JSON.parse(res.data.formData)
  67. const setDataFun = (formData) => {
  68. if (this.config.formOptions.length) {
  69. for (let k in formData) {
  70. for (let i = 0; i < this.config.formOptions.length; i++) {
  71. const e = this.config.formOptions[i]
  72. if (e.currentField == '@formId') this.formData[e.field] = formData.id;
  73. if (e.currentField == k) this.formData[e.field] = formData[k]
  74. }
  75. }
  76. }
  77. this.fillFormData(this.formConf, this.formData)
  78. this.key = +new Date()
  79. this.loading = false
  80. }
  81. if (this.id) {
  82. getModelInfo(this.modelId, this.id).then(res => {
  83. let dataForm = res.data
  84. if (!dataForm.data) return
  85. const formData = JSON.parse(dataForm.data)
  86. this.formData = {}
  87. setDataFun({
  88. ...formData,
  89. id: this.id
  90. })
  91. })
  92. } else {
  93. const formData = row
  94. setDataFun(formData)
  95. }
  96. }).catch(() => {})
  97. },
  98. fillFormData(form, data) {
  99. const loop = list => {
  100. for (let i = 0; i < list.length; i++) {
  101. let item = list[i]
  102. let vModel = item.__vModel__
  103. let config = item.__config__
  104. if (vModel) {
  105. let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
  106. if (!config.isSubTable) config.defaultValue = val
  107. if (config.defaultCurrent) {
  108. if (config.jnpfKey === 'datePicker') {
  109. if (!data.hasOwnProperty(vModel)) {
  110. let format = this.jnpf.handelFormat(item.format)
  111. let dateStr = this.jnpf.toDate(new Date().getTime(), format)
  112. let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
  113. '-01 00:00:00' : format === 'yyyy-MM-dd' ?
  114. ' 00:00:00' : ''
  115. val = new Date(dateStr + time).getTime()
  116. config.defaultValue = val
  117. }
  118. }
  119. if (config.jnpfKey === 'timePicker') {
  120. if (!data.hasOwnProperty(vModel)) {
  121. config.defaultValue = this.jnpf.toDate(new Date(), item.format)
  122. }
  123. }
  124. const organizeIdList = this.userInfo.organizeIdList
  125. if (config.jnpfKey === 'organizeSelect' && Array.isArray(organizeIdList) &&
  126. organizeIdList.length) {
  127. config.defaultValue = item.multiple ? [organizeIdList] : organizeIdList
  128. }
  129. const departmentId = this.userInfo.departmentId
  130. if (config.jnpfKey === 'depSelect' && departmentId) {
  131. config.defaultValue = item.multiple ? [departmentId] : departmentId;
  132. }
  133. const positionIds = this.userInfo.positionIds
  134. if (config.jnpfKey === 'posSelect' && Array.isArray(positionIds) && positionIds
  135. .length) {
  136. config.defaultValue = item.multiple ? positionIds.map(o => o.id) : positionIds[
  137. 0].id
  138. }
  139. const roleIds = this.userInfo.roleIds
  140. if (config.jnpfKey === 'roleSelect' && Array.isArray(roleIds) && roleIds.length) {
  141. config.defaultValue = item.multiple ? roleIds : roleIds[0];
  142. }
  143. const groupIds = this.userInfo.groupIds
  144. if (config.jnpfKey === 'groupSelect' && Array.isArray(groupIds) && groupIds
  145. .length) {
  146. config.defaultValue = item.multiple ? groupIds : groupIds[0];
  147. }
  148. const userId = this.userInfo.userId
  149. if (config.jnpfKey === 'userSelect' && userId) {
  150. config.defaultValue = item.multiple ? [userId] : userId;
  151. }
  152. if (config.jnpfKey === 'usersSelect' && userId) {
  153. config.defaultValue = item.multiple ? [userId + '--user'] : userId + '--user';
  154. }
  155. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  156. config.defaultValue = this.userInfo.signImg
  157. }
  158. }
  159. let noShow = !item.__config__.noShow ? false : item.__config__.noShow
  160. let isVisibility = false
  161. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  162. .__config__.visibility.includes('app'))) isVisibility = true
  163. this.$set(item.__config__, 'isVisibility', isVisibility)
  164. this.$set(item.__config__, 'noShow', noShow)
  165. } else {
  166. let noShow = false,
  167. isVisibility = false
  168. if (!item.__config__.visibility || (Array.isArray(item.__config__.visibility) && item
  169. .__config__.visibility.includes('app'))) isVisibility = true
  170. this.$set(item.__config__, 'isVisibility', isVisibility)
  171. this.$set(item.__config__, 'noShow', noShow)
  172. }
  173. if (item.__config__ && item.__config__.children && Array
  174. .isArray(item.__config__.children)) {
  175. loop(item.__config__.children)
  176. }
  177. }
  178. }
  179. loop(form.fields)
  180. },
  181. cancel() {
  182. uni.navigateBack();
  183. },
  184. sumbitForm(data, callback) {
  185. if (!data) return
  186. this.btnLoading = true
  187. const successFun = (res, callback) => {
  188. if (callback && typeof callback === "function") callback()
  189. uni.showToast({
  190. title: res.msg,
  191. complete: () => {
  192. setTimeout(() => {
  193. this.btnLoading = false
  194. if (this.config.isRefresh) uni.$emit('refresh')
  195. uni.navigateBack()
  196. }, 1500)
  197. }
  198. })
  199. }
  200. if (this.config.customBtn) {
  201. const query = {
  202. paramList: this.jnpf.getParamList(this.config.templateJson, {
  203. ...data,
  204. id: this.id
  205. }) || []
  206. };
  207. getDataInterfaceRes(this.config.interfaceId, query).then(res => {
  208. successFun(res, callback)
  209. }).catch(() => {
  210. this.btnLoading = false
  211. })
  212. } else {
  213. this.dataForm.data = JSON.stringify(data)
  214. createModel(this.config.modelId, this.dataForm).then(res => {
  215. successFun(res, callback)
  216. }).catch(() => {
  217. this.btnLoading = false
  218. })
  219. }
  220. },
  221. submit() {
  222. if (this.isPreview) {
  223. uni.showToast({
  224. title: '功能预览不支持数据保存',
  225. icon: 'none'
  226. })
  227. return
  228. }
  229. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  230. },
  231. }
  232. }
  233. </script>
  234. <style scoped lang="scss">
  235. page {
  236. background-color: #f0f2f6;
  237. }
  238. </style>