form.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <view class="dynamicModel-form-v jnpf-wrap jnpf-wrap-form" v-if="showPage">
  3. <JnpfParser :formConf="formConf" ref="dynamicForm" v-if="!loading" @submit="sumbitForm" :key="key" />
  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.userInfo = uni.getStorageSync('userInfo') || {}
  68. this.config = JSON.parse(this.jnpf.base64.decode(option.config))
  69. this.index = this.config.index
  70. this.formPermissionList = !this.config.currentMenu ? [] : JSON.parse(decodeURIComponent(this.config
  71. .currentMenu))
  72. this.formList = this.formPermissionList.formList
  73. this.btnType = this.config.btnType || ''
  74. this.modelId = this.config.modelId;
  75. this.isPreview = this.config.isPreview || '0';
  76. this.dataForm.id = this.config.id || ''
  77. uni.setNavigationBarTitle({
  78. title: this.dataForm.id ? this.$t('common.editText') : this.$t('common.addText')
  79. })
  80. this.getConfigData()
  81. },
  82. methods: {
  83. getConfigData() {
  84. getConfigData(this.modelId).then(res => {
  85. if (res.code !== 200 || !res.data) {
  86. uni.showToast({
  87. title: '暂无此页面',
  88. icon: 'none',
  89. complete: () => {
  90. setTimeout(() => {
  91. uni.navigateBack()
  92. }, 1500)
  93. }
  94. })
  95. return
  96. }
  97. this.formConf = res.data.formData ? JSON.parse(res.data.formData) : {};
  98. this.showPage = true
  99. this.initData()
  100. })
  101. },
  102. initData() {
  103. this.$nextTick(() => {
  104. if (this.dataForm.id) {
  105. let extra = {
  106. modelId: this.modelId,
  107. id: this.dataForm.id,
  108. type: 1
  109. }
  110. uni.setStorageSync('dynamicModelExtra', extra)
  111. getModelInfo(this.modelId, this.dataForm.id).then(res => {
  112. this.dataForm = res.data
  113. if (!this.dataForm.data) return
  114. this.formData = {
  115. ...JSON.parse(this.dataForm.data),
  116. id: this.dataForm.id
  117. }
  118. this.fillFormData(this.formConf, this.formData)
  119. this.$nextTick(() => {
  120. this.loading = false
  121. })
  122. })
  123. } else {
  124. this.isAdd = true
  125. this.formData = {}
  126. this.loading = false
  127. this.fillFormData(this.formConf, this.formData)
  128. }
  129. this.key = +new Date()
  130. })
  131. },
  132. fillFormData(form, data) {
  133. this.key = +new Date()
  134. const loop = (list, parent) => {
  135. for (let i = 0; i < list.length; i++) {
  136. let item = list[i]
  137. let vModel = item.__vModel__
  138. let config = item.__config__
  139. if (vModel) {
  140. let val = data.hasOwnProperty(vModel) ? data[vModel] : config.defaultValue
  141. if (!config.isSubTable) config.defaultValue = val
  142. if (this.isAdd || config.isSubTable) { //新增时候,默认当前
  143. if (config.defaultCurrent) {
  144. if (config.jnpfKey === 'datePicker') {
  145. if (!data.hasOwnProperty(vModel)) {
  146. let format = this.jnpf.handelFormat(item.format)
  147. let dateStr = this.jnpf.toDate(new Date().getTime(), format)
  148. let time = format === 'yyyy' ? '-01-01 00:00:00' : format === 'yyyy-MM' ?
  149. '-01 00:00:00' : format === 'yyyy-MM-dd' ?
  150. ' 00:00:00' : ''
  151. val = new Date(dateStr + time).getTime()
  152. config.defaultValue = val
  153. }
  154. }
  155. if (config.jnpfKey === 'timePicker') {
  156. if (!data.hasOwnProperty(vModel)) {
  157. config.defaultValue = this.jnpf.toDate(new Date(), item.format)
  158. }
  159. }
  160. const organizeIdList = this.userInfo.organizeIdList
  161. if (config.jnpfKey === 'organizeSelect' && Array.isArray(organizeIdList) &&
  162. organizeIdList.length) {
  163. config.defaultValue = item.multiple ? [organizeIdList] : organizeIdList
  164. }
  165. const departmentId = this.userInfo.departmentId
  166. if (config.jnpfKey === 'depSelect' && departmentId) {
  167. config.defaultValue = item.multiple ? [departmentId] : departmentId;
  168. }
  169. const positionIds = this.userInfo.positionIds
  170. if (config.jnpfKey === 'posSelect' && Array.isArray(positionIds) && positionIds
  171. .length) {
  172. config.defaultValue = item.multiple ? positionIds.map(o => o.id) : positionIds[
  173. 0].id
  174. }
  175. const roleIds = this.userInfo.roleIds
  176. if (config.jnpfKey === 'roleSelect' && Array.isArray(roleIds) && roleIds.length) {
  177. config.defaultValue = item.multiple ? roleIds : roleIds[0];
  178. }
  179. const groupIds = this.userInfo.groupIds
  180. if (config.jnpfKey === 'groupSelect' && Array.isArray(groupIds) && groupIds
  181. .length) {
  182. config.defaultValue = item.multiple ? groupIds : groupIds[0];
  183. }
  184. const userId = this.userInfo.userId
  185. if (config.jnpfKey === 'userSelect' && userId) {
  186. config.defaultValue = item.multiple ? [userId] : userId;
  187. }
  188. if (config.jnpfKey === 'usersSelect' && userId) {
  189. config.defaultValue = item.multiple ? [userId + '--user'] : userId + '--user';
  190. }
  191. if (config.jnpfKey === 'sign' && this.userInfo.signImg) {
  192. config.defaultValue = this.userInfo.signImg
  193. }
  194. }
  195. }
  196. const btn_detail = this.$permission.hasBtnP('btn_detail', this.formPermissionList
  197. .menuId)
  198. const btn_edit = this.$permission.hasBtnP('btn_edit', this.formPermissionList
  199. .menuId)
  200. if (!!this.dataForm.id && !btn_edit && btn_detail) item.disabled = btn_detail
  201. let noShow = !config.noShow ? false : config.noShow
  202. let isVisibility = false
  203. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  204. 'app'))) isVisibility = true
  205. this.$set(config, 'isVisibility', isVisibility)
  206. if (this.formPermissionList.useFormPermission) {
  207. let id = config.isSubTable ? parent.__vModel__ + '-' + vModel : vModel
  208. noShow = true
  209. if (this.formList && this.formList.length) {
  210. noShow = !this.formList.some(o => o.enCode === id)
  211. }
  212. noShow = config.noShow ? config.noShow : noShow
  213. this.$set(config, 'noShow', noShow)
  214. }
  215. } else {
  216. let noShow = config.noShow ? config.noShow : false,
  217. isVisibility = false
  218. if (!config.visibility || (Array.isArray(config.visibility) && config.visibility.includes(
  219. 'app'))) isVisibility = true
  220. this.$set(config, 'isVisibility', isVisibility)
  221. this.$set(config, 'noShow', noShow)
  222. }
  223. if (config && config.children && Array.isArray(config.children)) {
  224. loop(config.children, item)
  225. }
  226. }
  227. }
  228. loop(form.fields)
  229. form.formData = data
  230. this.key = +new Date()
  231. },
  232. sumbitForm(data, callback) {
  233. if (!data) return
  234. this.btnLoading = true
  235. const formData = {
  236. ...this.formData,
  237. ...data
  238. }
  239. this.dataForm.data = JSON.stringify(formData)
  240. if (callback && typeof callback === "function") callback()
  241. const formMethod = this.dataForm.id ? updateModel : createModel
  242. formMethod(this.modelId, this.dataForm).then(res => {
  243. uni.showToast({
  244. title: res.msg,
  245. complete: () => {
  246. setTimeout(() => {
  247. if (this.clickType == 'save_add') {
  248. this.key = +new Date()
  249. this.$nextTick(() => {
  250. this.$refs.dynamicForm && this.$refs
  251. .dynamicForm.resetForm()
  252. })
  253. }
  254. this.btnLoading = false
  255. this.initData()
  256. if (this.clickType != 'save_proceed' && this.clickType !=
  257. 'save_add') {
  258. uni.$emit('refresh')
  259. uni.navigateBack()
  260. }
  261. }, 1500)
  262. }
  263. })
  264. }).catch(() => {
  265. this.btnLoading = false
  266. })
  267. },
  268. commonSubmit(type) {
  269. this.clickType = type
  270. this.submit(type)
  271. },
  272. submit(type) {
  273. this.clickType = type
  274. if (this.isPreview == '1') return this.$u.toast('功能预览不支持数据保存')
  275. this.$refs.dynamicForm && this.$refs.dynamicForm.submitForm()
  276. }
  277. }
  278. }
  279. </script>
  280. <style lang="scss">
  281. page {
  282. background-color: #f0f2f6;
  283. }
  284. </style>