form.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="dynamicModel-form-v jnpf-wrap jnpf-wrap-form" v-if="showPage">
  3. <JnpfParser :formConf="formConf" ref="dynamicForm" @submit="sumbitForm" :key="key" v-if="!loading" />
  4. <view class="buttom-actions" v-if="btnType === 'btn_edit' || btnType === 'btn_add'">
  5. <CustomButton class="u-flex buttom-btn-left-inner" :btnText="getCancelText"
  6. btnIcon="icon-ym icon-ym-add-cancel" customIcon :btnLoading="btnLoading" />
  7. <u-button class="buttom-btn" type="primary" @click.stop="submit" :loading="btnLoading">
  8. {{getOkText}}
  9. </u-button>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import CustomButton from '@/components/CustomButton'
  15. import {
  16. getConfigData,
  17. createModel,
  18. updateModel,
  19. getModelInfo
  20. } from '@/api/apply/visualDev'
  21. export default {
  22. components: {
  23. CustomButton
  24. },
  25. data() {
  26. return {
  27. webType: '',
  28. showPage: false,
  29. btnLoading: false,
  30. loading: true,
  31. isPreview: '0',
  32. modelId: '',
  33. formConf: {},
  34. formData: {},
  35. dataForm: {
  36. id: '',
  37. data: ''
  38. },
  39. btnType: '',
  40. formPermissionList: {},
  41. formList: [],
  42. key: +new Date(),
  43. config: {},
  44. clickType: 'submit',
  45. prevDis: false,
  46. nextDis: false,
  47. index: 0,
  48. userInfo: {},
  49. isAdd: false
  50. }
  51. },
  52. computed: {
  53. getOkText() {
  54. const text = this.formConf.confirmButtonTextI18nCode ?
  55. this.$t(this.formConf.confirmButtonTextI18nCode, this.formConf.confirmButtonText) :
  56. this.formConf.confirmButtonText;
  57. return text || this.$t('common.okText');
  58. },
  59. getCancelText() {
  60. const text = this.formConf.cancelButtonTextI18nCode ?
  61. this.$t(this.formConf.cancelButtonTextI18nCode, this.formConf.cancelButtonText) :
  62. this.formConf.cancelButtonText;
  63. return text || this.$t('common.cancelText');
  64. }
  65. },
  66. onLoad(option) {
  67. this.init(option)
  68. },
  69. methods: {
  70. init(option) {
  71. // 使用解构赋值提取配置项
  72. const parseConfig = (rawConfig) => {
  73. try {
  74. return JSON.parse(this.jnpf.base64.decode(rawConfig)) || {}
  75. } catch (error) {
  76. return {}
  77. }
  78. }
  79. const config = parseConfig(option.config)
  80. const {
  81. index,
  82. currentMenu,
  83. btnType = '',
  84. modelId,
  85. isPreview = '0',
  86. id = ''
  87. } = config
  88. const formPermissionList = currentMenu ? JSON.parse(decodeURIComponent(currentMenu)) : []
  89. Object.assign(this, {
  90. userInfo: uni.getStorageSync('userInfo') || {},
  91. config,
  92. index,
  93. formPermissionList,
  94. formList: formPermissionList?.formList || [], // 添加安全访问
  95. btnType,
  96. modelId,
  97. isPreview,
  98. dataForm: {
  99. id
  100. }
  101. })
  102. const getNavigationTitle = () => this.dataForm.id ? this.$t('common.editText') : this.$t('common.addText')
  103. uni.setNavigationBarTitle({
  104. title: getNavigationTitle()
  105. })
  106. this.getConfigData()
  107. },
  108. getConfigData() {
  109. getConfigData(this.modelId, this.config.menuId).then(res => {
  110. if (res.code !== 200 || !res.data) {
  111. uni.showToast({
  112. title: '暂无此页面',
  113. icon: 'none',
  114. complete: () => {
  115. setTimeout(() => {
  116. uni.navigateBack()
  117. }, 1500)
  118. }
  119. })
  120. return
  121. }
  122. this.formConf = res.data.formData ? JSON.parse(res.data.formData) : {};
  123. this.showPage = true
  124. this.initData()
  125. })
  126. },
  127. initData() {
  128. this.$nextTick(() => {
  129. if (this.dataForm.id) {
  130. let extra = {
  131. modelId: this.modelId,
  132. id: this.dataForm.id,
  133. type: 1
  134. }
  135. uni.setStorageSync('dynamicModelExtra', extra)
  136. getModelInfo(this.modelId, this.dataForm.id, this.config.menuId).then(res => {
  137. this.dataForm = res.data
  138. if (!this.dataForm.data) return
  139. this.formData = {
  140. ...JSON.parse(this.dataForm.data),
  141. id: this.dataForm.id
  142. }
  143. this.fillFormData(this.formConf, this.formData)
  144. this.$nextTick(() => {
  145. this.loading = false
  146. })
  147. })
  148. } else {
  149. this.isAdd = true
  150. this.formData = {}
  151. this.loading = false
  152. this.fillFormData(this.formConf, this.formData)
  153. }
  154. this.key = +new Date()
  155. })
  156. },
  157. fillFormData(form, data) {
  158. this.key = +new Date()
  159. const loop = (list, parent) => {
  160. for (let i = 0; i < list.length; i++) {
  161. let item = list[i]
  162. let vModel = item.__vModel__
  163. let config = item.__config__
  164. if (vModel) {
  165. let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
  166. if (!config.isSubTable) config.defaultValue = val
  167. if (this.isAdd || config.isSubTable) { //新增时候,默认当前
  168. if (config.defaultCurrent) {
  169. if (config.jnpfKey === 'datePicker') {
  170. if (!data.hasOwnProperty(vModel)) {
  171. let format = this.jnpf.handelFormat(item.format)
  172. let dateStr = this.jnpf.toDate(new Date().getTime(), format)
  173. let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
  174. '-01 00:00:00' : format === 'yyyy-MM-dd' ?
  175. ' 00:00:00' : ''
  176. val = new Date(dateStr + time).getTime()
  177. config.defaultValue = val
  178. }
  179. }
  180. if (config.jnpfKey === 'timePicker') {
  181. if (!data.hasOwnProperty(vModel)) {
  182. config.defaultValue = this.jnpf.toDate(new Date(), item.format)
  183. }
  184. }
  185. if (config.jnpfKey === 'organizeSelect' && this.userInfo.organizeIds?.length) {
  186. config.defaultValue = item.multiple ? this.userInfo.organizeIds :
  187. this.userInfo.organizeId
  188. }
  189. if (config.jnpfKey === 'posSelect' && this.userInfo.positionIds?.length) {
  190. config.defaultValue = item.multiple ? this.userInfo.positionIds :
  191. this.userInfo.positionId
  192. }
  193. const userId = this.userInfo.userId
  194. if (config.jnpfKey === 'userSelect' && userId) {
  195. config.defaultValue = item.multiple ? [userId] : userId;
  196. }
  197. if (config.jnpfKey === 'usersSelect' && userId) {
  198. config.defaultValue = [userId + '--user'];
  199. }
  200. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  201. config.defaultValue = this.userInfo.signImg
  202. }
  203. }
  204. }
  205. const btn_detail = this.$permission.hasBtnP('btn_detail', this.formPermissionList
  206. .menuId)
  207. const btn_edit = this.$permission.hasBtnP('btn_edit', this.formPermissionList
  208. .menuId)
  209. if (!!this.dataForm.id && !btn_edit && btn_detail) item.disabled = btn_detail
  210. let noShow = !config.noShow ? false : config.noShow
  211. let isVisibility = false
  212. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  213. 'app'))) isVisibility = true
  214. this.$set(config, 'isVisibility', isVisibility)
  215. if (this.formPermissionList.useFormPermission) {
  216. let id = config.isSubTable ? parent.__vModel__ + '-' + vModel : vModel
  217. noShow = true
  218. if (this.formList && this.formList.length) {
  219. noShow = !this.formList.some(o => o.enCode === id)
  220. }
  221. noShow = config.noShow ? config.noShow : noShow
  222. this.$set(config, 'noShow', noShow)
  223. }
  224. } else {
  225. let noShow = config.noShow ? config.noShow : false,
  226. isVisibility = false
  227. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  228. 'app'))) isVisibility = true
  229. this.$set(config, 'isVisibility', isVisibility)
  230. this.$set(config, 'noShow', noShow)
  231. }
  232. if (config && config.children && Array.isArray(config.children)) {
  233. loop(config.children, item)
  234. }
  235. }
  236. }
  237. loop(form.fields)
  238. form.formData = data
  239. this.key = +new Date()
  240. },
  241. sumbitForm(data, callback) {
  242. if (!data) return
  243. this.btnLoading = true
  244. const formData = {
  245. ...this.formData,
  246. ...data
  247. }
  248. this.dataForm.data = JSON.stringify(formData)
  249. this.dataForm.menuId = this.config.menuId
  250. if (callback && typeof callback === "function") callback()
  251. const formMethod = this.dataForm.id ? updateModel : createModel
  252. formMethod(this.modelId, this.dataForm).then(res => {
  253. uni.showToast({
  254. title: res.msg,
  255. complete: () => {
  256. setTimeout(() => {
  257. if (this.clickType == 'save_add') {
  258. this.key = +new Date()
  259. this.$nextTick(() => {
  260. this.$refs.dynamicForm && this.$refs
  261. .dynamicForm.resetForm()
  262. })
  263. }
  264. this.btnLoading = false
  265. this.initData()
  266. if (this.clickType != 'save_proceed' && this.clickType !=
  267. 'save_add') {
  268. uni.$emit('refresh')
  269. uni.navigateBack()
  270. }
  271. }, 1500)
  272. }
  273. })
  274. }).catch(() => {
  275. this.btnLoading = false
  276. })
  277. },
  278. commonSubmit(type) {
  279. this.clickType = type
  280. this.submit(type)
  281. },
  282. submit(type) {
  283. this.clickType = type
  284. if (this.isPreview == '1') return this.$u.toast('功能预览不支持数据保存')
  285. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  286. }
  287. }
  288. }
  289. </script>
  290. <style lang="scss">
  291. page {
  292. background-color: #f0f2f6;
  293. }
  294. </style>