index.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <template>
  2. <u-form class="jnpf-wrap-form" ref="dataForm" :rules="rules" :model="formData" :errorType="['toast']"
  3. :label-position="formConf.labelPosition==='top'?'top':'left'"
  4. :label-width="formConf.labelWidth?formConf.labelWidth*1.5:150"
  5. :label-align="formConf.labelPosition==='right'?'right':'left'" :class="formClass+' '+formConfCopy.className">
  6. <view v-for="(item, index) in formConfCopy.fields" :key="item.__config__.renderKey">
  7. <Item v-if="!item.__config__.noShow && item.__config__.isVisibility" :itemData="item" :formConf="formConf"
  8. :formData="formData" :ref="'ref'+item.__config__.formId" :class="item.__config__.className"
  9. @clickIcon='clickIcon' @clickFun="onClick" @collapse-change="onCollapseChange" @tab-change="onTabChange"
  10. @input="setValue" />
  11. </view>
  12. <u-modal v-model="showTipsModal" width='70%' border-radius="16" :content-style="contentStyle"
  13. :content="tipsContent" :titleStyle="titleStyle" :title="tipsTitle" :confirm-style="confirmStyle"
  14. :confirm-text="$t('common.okText')" />
  15. </u-form>
  16. </template>
  17. <script>
  18. import Item from './Item'
  19. import {
  20. getDataInterfaceRes,
  21. getDictionaryDataSelector
  22. } from '@/api/common'
  23. const dyOptionsList = ['radio', 'checkbox', 'select', 'cascader', 'treeSelect']
  24. export default {
  25. components: {
  26. Item
  27. },
  28. props: {
  29. formConf: {
  30. type: Object,
  31. required: true
  32. },
  33. loading: {
  34. type: Boolean,
  35. default: false
  36. },
  37. isShortLink: {
  38. type: Boolean,
  39. default: false
  40. },
  41. },
  42. data() {
  43. const data = {
  44. formClass: 'form-' + this.jnpf.idGenerator(),
  45. formConfCopy: this.$u.deepClone(this.formConf),
  46. formData: {},
  47. rules: {},
  48. options: {},
  49. tableRefs: {},
  50. relations: {},
  51. refList: [],
  52. contentStyle: {
  53. fontSize: '28rpx',
  54. padding: '20rpx',
  55. lineHeight: '44rpx',
  56. textAlign: 'left'
  57. },
  58. titleStyle: {
  59. padding: '20rpx'
  60. },
  61. confirmStyle: {
  62. height: '80rpx',
  63. lineHeight: '80rpx',
  64. },
  65. tipsContent: '',
  66. tipsTitle: this.$t('common.tipTitle'),
  67. showTipsModal: false
  68. }
  69. this.beforeInit(data.formConfCopy.fields)
  70. this.initRelationForm(data.formConfCopy.fields)
  71. this.initFormData(data.formConfCopy.fields, data.formData, data.tableRefs)
  72. this.buildRules(this.$u.deepClone(data.formConfCopy.fields), data.rules)
  73. this.buildOptions(data.formConfCopy.fields, data.options, data.formData)
  74. this.buildRelations(data.formConfCopy.fields, data.relations)
  75. this.$nextTick(() => {
  76. this.onLoadFunc(data.formConfCopy)
  77. this.getRefList()
  78. })
  79. return data
  80. },
  81. provide() {
  82. return {
  83. parameter: this.parameter,
  84. relations: this.relations,
  85. isShortLink: this.isShortLink
  86. }
  87. },
  88. computed: {
  89. parameter() {
  90. const oldFormData = this.formConfCopy.formData ? this.formConfCopy.formData : {}
  91. this.formData.id = oldFormData.id || null
  92. this.formData.flowId = oldFormData.flowId || ''
  93. return {
  94. formData: this.formData,
  95. setFormData: this.setFormData,
  96. setShowOrHide: this.setShowOrHide,
  97. setRequired: this.setRequired,
  98. setDisabled: this.setDisabled,
  99. onlineUtils: this.jnpf.onlineUtils,
  100. }
  101. }
  102. },
  103. mounted() {
  104. this.$refs.dataForm.setRules(this.rules);
  105. this.initRelationData()
  106. uni.$on('subChange', (field) => {
  107. this.handleRelation(field.__vModel__)
  108. })
  109. this.initCss(this.formConfCopy)
  110. },
  111. methods: {
  112. beforeInit(fields) {
  113. const loop = (list) => {
  114. for (var index = 0; index < list.length; index++) {
  115. const config = list[index].__config__
  116. if (config.children && config.children.length) loop(config.children)
  117. if (config.jnpfKey == 'tableGrid') {
  118. let newList = []
  119. for (var i = 0; i < config.children.length; i++) {
  120. let element = config.children[i]
  121. for (var j = 0; j < element.__config__.children.length; j++) {
  122. let item = element.__config__.children[j]
  123. newList.push(...item.__config__.children)
  124. }
  125. }
  126. list.splice(index, 1, ...newList)
  127. }
  128. }
  129. }
  130. loop(fields)
  131. },
  132. initRelationForm(componentList) {
  133. componentList.forEach(cur => {
  134. const config = cur.__config__
  135. if (config.jnpfKey == 'relationFormAttr' || config.jnpfKey == 'popupAttr') {
  136. const relationKey = cur.relationField.split("_jnpfTable_")[0]
  137. componentList.forEach(item => {
  138. const noVisibility = Array.isArray(item.__config__.visibility) && !item
  139. .__config__.visibility.includes('app')
  140. if ((relationKey == item.__vModel__) && (noVisibility || !!item.__config__
  141. .noShow) && !cur.__vModel__) {
  142. cur.__config__.noShow = true
  143. }
  144. })
  145. }
  146. if (cur.__config__.children && cur.__config__.children.length) this.initRelationForm(cur
  147. .__config__.children)
  148. })
  149. },
  150. initFormData(componentList, formData, tableRefs) {
  151. componentList.forEach(cur => {
  152. const config = cur.__config__
  153. if (cur.__vModel__) {
  154. formData[cur.__vModel__] = config.defaultValue
  155. if (cur.__config__.jnpfKey == 'table' && !cur.__config__.noShow) {
  156. tableRefs[cur.__vModel__] = cur
  157. }
  158. }
  159. if (config.children && cur.__config__.jnpfKey !== 'table') {
  160. this.initFormData(config.children, formData, tableRefs)
  161. }
  162. })
  163. },
  164. buildOptions(componentList, data, formData) {
  165. componentList.forEach(cur => {
  166. const config = cur.__config__
  167. if (dyOptionsList.indexOf(config.jnpfKey) > -1) {
  168. if (config.dataType === 'dictionary' && config.dictionaryType) {
  169. cur.options = []
  170. getDictionaryDataSelector(config.dictionaryType).then(res => {
  171. cur.options = res.data.list || []
  172. data[cur.__vModel__ + 'Options'] = cur.options
  173. this.setFieldOptions(cur.__vModel__, cur.options)
  174. this.$nextTick(() => {
  175. uni.$emit("initCollapse")
  176. })
  177. })
  178. } else if (config.dataType === 'dynamic' && config.propsUrl) {
  179. cur.options = []
  180. let query = {
  181. paramList: this.jnpf.getParamList(config.templateJson, formData),
  182. }
  183. getDataInterfaceRes(config.propsUrl, query).then(res => {
  184. cur.options = Array.isArray(res.data) ? res.data : []
  185. data[cur.__vModel__ + 'Options'] = cur.options
  186. this.setFieldOptions(cur.__vModel__, cur.options)
  187. uni.$emit("initCollapse")
  188. })
  189. } else {
  190. data[cur.__vModel__ + 'Options'] = cur.options
  191. }
  192. }
  193. if (config.children && config.jnpfKey !== 'table') this.buildOptions(config.children, data,
  194. formData)
  195. })
  196. },
  197. buildRules(componentList, rules) {
  198. componentList.forEach(cur => {
  199. const config = cur.__config__
  200. const jnpfKey = config.jnpfKey
  201. const useNumList = ['inputNumber', 'switch', 'datePicker', 'rate', 'slider', 'calculate']
  202. const useArrayList = ['select', 'depSelect', 'posSelect', 'userSelect', 'usersSelect',
  203. 'treeSelect', 'popupTableSelect'
  204. ]
  205. config.regList = !config.regList ? [] : config.regList
  206. if (config.required) {
  207. const label = config.labelI18nCode ?
  208. this.$t(config.labelI18nCode, config.label) : config.label;
  209. let requiredItem = {
  210. required: config.required,
  211. message: `${label} ${this.$t('sys.validate.textRequiredSuffix')}`
  212. };
  213. config.regList.push(requiredItem)
  214. }
  215. const rule = config.regList.map(item => {
  216. if (item.pattern) {
  217. item.pattern = item.pattern.toString()
  218. let start = item.pattern.indexOf('/')
  219. let stop = item.pattern.lastIndexOf('/')
  220. let str = item.pattern.substring(start + 1, stop)
  221. let reg = new RegExp(str)
  222. item.pattern = reg
  223. }
  224. item.trigger = config.trigger || 'change, blur'
  225. if (Array.isArray(config.defaultValue)) item.type = 'array'
  226. if (useNumList.includes(jnpfKey)) item.type = 'number'
  227. if (useArrayList.includes(jnpfKey) && cur.multiple) item.type = 'array'
  228. if (jnpfKey === 'organizeSelect' || jnpfKey === 'areaSelect' || jnpfKey ===
  229. 'checkbox' || jnpfKey === 'cascader') item.type = 'array'
  230. if (jnpfKey === 'relationForm' || jnpfKey === 'popupSelect') {
  231. item.type = 'any',
  232. item.validator = (rule, value, callback) => {
  233. if (value || value === 0) {
  234. callback();
  235. } else {
  236. callback(new Error(config.label + '不能为空'));
  237. }
  238. }
  239. }
  240. if (item.messageI18nCode) {
  241. item.message = this.$t(item.messageI18nCode, item.message);
  242. }
  243. return item
  244. })
  245. if (rule.length) rules[cur.__vModel__] = rule
  246. if (config.children && jnpfKey !== 'table') this.buildRules(this.$u.deepClone(config.children),
  247. rules)
  248. })
  249. },
  250. buildRelations(componentList, relations) {
  251. componentList.forEach(cur => {
  252. const config = cur.__config__
  253. const vModel = cur.__vModel__
  254. const selectType = ['dep', 'pos', 'role', 'group']
  255. if (config.jnpfKey === 'userSelect' && selectType.includes(cur.selectType)) {
  256. if (cur.relationField) {
  257. let item = {
  258. ...cur,
  259. realVModel: config.isSubTable ? config.parentVModel + '-' + vModel : vModel,
  260. opType: 'setUserOptions'
  261. }
  262. if (relations.hasOwnProperty(cur.relationField)) {
  263. let boo = relations[cur.relationField].some(o => o.realVModel === cur.realVModel)
  264. if (!boo) relations[cur.relationField].push(item)
  265. } else {
  266. relations[cur.relationField] = [item]
  267. }
  268. }
  269. }
  270. if (dyOptionsList.indexOf(config.jnpfKey) > -1 && config.dataType === 'dynamic' && config
  271. .templateJson && config.templateJson.length) {
  272. for (let i = 0; i < config.templateJson.length; i++) {
  273. const e = config.templateJson[i];
  274. if (e.relationField) {
  275. let item = {
  276. ...cur,
  277. realVModel: config.isSubTable ? config.parentVModel + '-' + vModel :
  278. vModel,
  279. opType: 'setOptions'
  280. }
  281. if (relations.hasOwnProperty(e.relationField)) {
  282. let boo = relations[e.relationField].some(o => o.realVModel === cur
  283. .realVModel)
  284. if (!boo) relations[e.relationField].push(item)
  285. } else {
  286. relations[e.relationField] = [item]
  287. }
  288. }
  289. }
  290. }
  291. if (config.jnpfKey === 'datePicker') {
  292. if (config.startTimeRule) {
  293. let startTimeValue = Number(config.startTimeValue)
  294. if (config.startTimeType == 1) {
  295. cur.startTime = startTimeValue
  296. } else if (config.startTimeType == 3) {
  297. cur.startTime = new Date().getTime()
  298. } else {
  299. if (config.startTimeType == 4) {
  300. if (config.startTimeTarget == 1) cur.startTime = new Date(new Date()
  301. .setFullYear((new Date().getFullYear() - startTimeValue))).getTime()
  302. if (config.startTimeTarget == 2) cur.startTime = new Date(new Date().setMonth((
  303. new Date().getMonth() - startTimeValue))).getTime()
  304. if (config.startTimeTarget == 3) cur.startTime = new Date(new Date().setDate((
  305. new Date().getDate() - startTimeValue))).getTime()
  306. } else {
  307. if (config.startTimeTarget == 1) cur.startTime = new Date(new Date()
  308. .setFullYear((new Date().getFullYear() + startTimeValue))).getTime()
  309. if (config.startTimeTarget == 2) cur.startTime = new Date(new Date().setMonth((
  310. new Date().getMonth() + startTimeValue))).getTime()
  311. if (config.startTimeTarget == 3) cur.startTime = new Date(new Date().setDate((
  312. new Date().getDate() + startTimeValue))).getTime()
  313. }
  314. }
  315. }
  316. if (config.endTimeRule) {
  317. let endTimeValue = Number(config.endTimeValue)
  318. if (config.endTimeType == 1) {
  319. cur.endTime = endTimeValue
  320. } else if (config.endTimeType == 3) {
  321. cur.endTime = new Date().getTime()
  322. } else {
  323. if (config.endTimeType == 4) {
  324. if (config.endTimeTarget == 1) cur.endTime = new Date(new Date()
  325. .setFullYear((new Date().getFullYear() - endTimeValue))).getTime()
  326. if (config.endTimeTarget == 2) cur.endTime = new Date(new Date().setMonth((
  327. new Date().getMonth() - endTimeValue))).getTime()
  328. if (config.endTimeTarget == 3) cur.endTime = new Date(new Date().setDate((
  329. new Date().getDate() - endTimeValue))).getTime()
  330. } else {
  331. if (config.endTimeTarget == 1) cur.endTime = new Date(new Date()
  332. .setFullYear((new Date().getFullYear() + endTimeValue))).getTime()
  333. if (config.endTimeTarget == 2) cur.endTime = new Date(new Date().setMonth((
  334. new Date().getMonth() + endTimeValue))).getTime()
  335. if (config.endTimeTarget == 3) {
  336. cur.endTime = new Date(new Date().setDate((new Date().getDate() +
  337. endTimeValue))).getTime()
  338. }
  339. }
  340. }
  341. }
  342. if (cur.__config__.startRelationField) {
  343. let item = {
  344. ...cur,
  345. realVModel: cur.__config__.isSubTable ? cur.__config__.parentVModel + '-' + cur
  346. .__vModel__ : cur.__vModel__,
  347. opType: 'setDate'
  348. }
  349. if (relations.hasOwnProperty(cur.__config__.startRelationField)) {
  350. let boo = relations[cur.__config__.startRelationField].some(o => o.realVModel ===
  351. cur.realVModel)
  352. if (!boo) {
  353. relations[cur.__config__.startRelationField].push(item)
  354. }
  355. } else {
  356. relations[cur.__config__.startRelationField] = [item]
  357. }
  358. }
  359. if (cur.__config__.endRelationField) {
  360. let item = {
  361. ...cur,
  362. realVModel: cur.__config__.isSubTable ? cur.__config__.parentVModel + '-' + cur
  363. .__vModel__ : cur.__vModel__,
  364. opType: 'setDate'
  365. }
  366. if (relations.hasOwnProperty(cur.__config__.endRelationField)) {
  367. let boo = relations[cur.__config__.endRelationField].some(o => o.realVModel === cur
  368. .realVModel)
  369. if (!boo) {
  370. relations[cur.__config__.endRelationField].push(item)
  371. }
  372. } else {
  373. relations[cur.__config__.endRelationField] = [item]
  374. }
  375. }
  376. }
  377. if (config.jnpfKey === 'timePicker') {
  378. let format = cur.format === 'HH:mm' ? 'HH:mm:00' : cur.format
  379. if (config.startTimeRule) {
  380. let startTime = ''
  381. if (config.startTimeType == 1) {
  382. cur.startTime = config.startTimeValue || '00:00:00'
  383. if (cur.startTime.split(':').length == 3) {
  384. cur.startTime = cur.startTime
  385. } else {
  386. cur.startTime = cur.startTime + ':00'
  387. }
  388. } else if (config.startTimeType == 3) {
  389. cur.startTime = this.jnpf.toDate(new Date(), format)
  390. } else {
  391. let startTimeValue = Number(config.startTimeValue)
  392. if (config.startTimeType == 4) {
  393. if (config.startTimeTarget == 1) startTime = new Date(new Date().setHours((
  394. new Date().getHours() - startTimeValue))).getTime()
  395. if (config.startTimeTarget == 2) startTime = new Date(new Date()
  396. .setMinutes((new Date().getMinutes() - startTimeValue))).getTime()
  397. if (config.startTimeTarget == 3) startTime = new Date(new Date()
  398. .setSeconds((new Date().getSeconds() - startTimeValue))).getTime()
  399. } else {
  400. if (config.startTimeTarget == 1) startTime = new Date(new Date().setHours((
  401. new Date().getHours() + startTimeValue))).getTime()
  402. if (config.startTimeTarget == 2) startTime = new Date(new Date()
  403. .setMinutes((new Date().getMinutes() + startTimeValue)))
  404. .getTime()
  405. if (config.startTimeTarget == 3) startTime = new Date(new Date()
  406. .setSeconds((new Date().getSeconds() + startTimeValue))).getTime()
  407. }
  408. cur.startTime = this.$u.timeFormat(startTime, 'hh:MM:ss')
  409. }
  410. }
  411. if (config.endTimeRule) {
  412. let endTime = ''
  413. if (config.endTimeType == 1) {
  414. cur.endTime = config.endTimeValue || '23:59:59'
  415. if (cur.endTime.split(':').length == 3) {
  416. cur.endTime = cur.endTime
  417. } else {
  418. cur.endTime = cur.endTime + ':00'
  419. }
  420. } else if (config.endTimeType == 3) {
  421. cur.endTime = this.jnpf.toDate(new Date(), format)
  422. } else {
  423. let endTimeValue = Number(config.endTimeValue)
  424. if (config.endTimeType == 4) {
  425. if (config.endTimeTarget == 1) {
  426. endTime = new Date(new Date().setHours((new Date().getHours() -
  427. endTimeValue))).getTime()
  428. }
  429. if (config.endTimeTarget == 2) {
  430. endTime = new Date(new Date().setMinutes((new Date().getMinutes() -
  431. endTimeValue))).getTime()
  432. }
  433. if (config.endTimeTarget == 3) {
  434. endTime = new Date(new Date().setSeconds((new Date().getSeconds() -
  435. endTimeValue))).getTime()
  436. }
  437. } else {
  438. if (config.endTimeTarget == 1) {
  439. endTime = new Date(new Date().setHours((new Date().getHours() +
  440. endTimeValue))).getTime()
  441. }
  442. if (config.endTimeTarget == 2) {
  443. endTime = new Date(new Date().setMinutes((new Date().getMinutes() +
  444. endTimeValue))).getTime()
  445. }
  446. if (config.endTimeTarget == 3) {
  447. endTime = new Date(new Date().setSeconds((new Date().getSeconds() +
  448. endTimeValue))).getTime()
  449. }
  450. }
  451. cur.endTime = this.$u.timeFormat(endTime, 'hh:MM:ss')
  452. }
  453. }
  454. if (cur.__config__.startRelationField) {
  455. let item = {
  456. ...cur,
  457. realVModel: cur.__config__.isSubTable ? cur.__config__.parentVModel + '-' + cur
  458. .__vModel__ : cur.__vModel__,
  459. opType: 'setTime'
  460. }
  461. if (relations.hasOwnProperty(cur.__config__.startRelationField)) {
  462. let boo = relations[cur.__config__.startRelationField].some(o => o.realVModel ===
  463. cur.realVModel)
  464. if (!boo) {
  465. relations[cur.__config__.startRelationField].push(item)
  466. }
  467. } else {
  468. relations[cur.__config__.startRelationField] = [item]
  469. }
  470. }
  471. if (cur.__config__.endRelationField) {
  472. let item = {
  473. ...cur,
  474. realVModel: cur.__config__.isSubTable ? cur.__config__.parentVModel + '-' + cur
  475. .__vModel__ : cur.__vModel__,
  476. opType: 'setTime'
  477. }
  478. if (relations.hasOwnProperty(cur.__config__.endRelationField)) {
  479. let boo = relations[cur.__config__.endRelationField].some(o => o.realVModel === cur
  480. .realVModel)
  481. if (!boo) {
  482. relations[cur.__config__.endRelationField].push(item)
  483. }
  484. } else {
  485. relations[cur.__config__.endRelationField] = [item]
  486. }
  487. }
  488. }
  489. if (config.jnpfKey === 'popupSelect' && cur.templateJson && cur.templateJson.length) {
  490. for (let i = 0; i < cur.templateJson.length; i++) {
  491. const e = cur.templateJson[i];
  492. if (e.relationField) {
  493. let item = {
  494. ...cur,
  495. realVModel: cur.__config__.isSubTable ? cur.__config__.parentVModel +
  496. '-' + cur.__vModel__ : cur.__vModel__,
  497. opType: 'setPopupOptions'
  498. }
  499. if (relations.hasOwnProperty(e.relationField)) {
  500. let boo = relations[e.relationField].some(o => o.realVModel === cur
  501. .realVModel)
  502. if (!boo) {
  503. relations[e.relationField].push(item)
  504. }
  505. } else {
  506. relations[e.relationField] = [item]
  507. }
  508. }
  509. }
  510. }
  511. if (config.children) this.buildRelations(config.children, relations)
  512. })
  513. },
  514. onLoadFunc(formConfCopy) {
  515. if (!formConfCopy || !formConfCopy.funcs || !formConfCopy.funcs.onLoad) return
  516. const onLoadFunc = this.jnpf.getScriptFunc(formConfCopy.funcs.onLoad)
  517. if (!onLoadFunc) return
  518. onLoadFunc(this.parameter)
  519. },
  520. initRelationData() {
  521. const handleRelationFun = (list) => {
  522. list.forEach(cur => {
  523. const config = cur.__config__
  524. this.handleDefaultRelation(cur.__vModel__)
  525. if (config.children) handleRelationFun(config.children)
  526. })
  527. }
  528. handleRelationFun(this.formConfCopy.fields)
  529. },
  530. initCss(formCopy) {
  531. // #ifdef H5
  532. if (!formCopy.classJson) return
  533. if (document.getElementById('styleId')) document.getElementById('styleId').remove()
  534. let head = document.getElementsByTagName('head')[0]
  535. let style = document.createElement('style')
  536. style.type = 'text/css'
  537. style.id = 'styleId'
  538. style.innerText = this.buildCSS(formCopy.classJson)
  539. head.appendChild(style)
  540. //#endif
  541. },
  542. buildCSS(str) {
  543. str = str.trim();
  544. let newStr = '';
  545. let cut = str.split('}');
  546. cut.forEach(item => {
  547. if (item) {
  548. item = '.' + this.formClass + ' ' + item + '}';
  549. newStr += item;
  550. }
  551. });
  552. return newStr;
  553. },
  554. handleRelation(field) {
  555. if (!field) return
  556. const currRelations = this.relations
  557. for (let key in currRelations) {
  558. if (key === field) {
  559. for (let i = 0; i < currRelations[key].length; i++) {
  560. const e = currRelations[key][i];
  561. let vModel = e.realVModel || e.__vModel__
  562. const config = e.__config__
  563. const jnpfKey = config.jnpfKey
  564. let defaultValue = ''
  565. if (['checkbox', 'cascader'].includes(jnpfKey) || (['select', 'treeSelect', 'popupSelect',
  566. 'popupTableSelect', 'userSelect'
  567. ].includes(jnpfKey) && e.multiple)) {
  568. defaultValue = []
  569. }
  570. if (vModel.includes('-')) { // 子表字段
  571. const tableVModel = vModel.split('-')[0]
  572. uni.$emit('handleRelation', e, defaultValue, true)
  573. } else {
  574. this.setFormData(vModel, defaultValue)
  575. if (e.opType === 'setOptions') {
  576. let query = {
  577. paramList: this.jnpf.getParamList(config.templateJson, this.formData)
  578. }
  579. getDataInterfaceRes(config.propsUrl, query).then(res => {
  580. let realData = res.data || []
  581. this.setFieldOptions(vModel, realData)
  582. uni.$emit("initCollapse")
  583. })
  584. }
  585. if (e.opType === 'setUserOptions') {
  586. let value = this.formData[e.relationField] || []
  587. this.comSet('ableRelationIds', vModel, Array.isArray(value) ? value : [value])
  588. }
  589. if (e.opType === 'setDate' || e.opType === 'setTime') {
  590. let startTime = ''
  591. let endTime = ''
  592. if (config.startTimeType == 2) {
  593. startTime = this.formData[config.startRelationField] || 0
  594. if (e.opType === 'setTime') {
  595. startTime = this.formData[config.startRelationField] ||
  596. '00:00:00'
  597. if (startTime && (startTime.split(':').length == 3)) {
  598. startTime = startTime
  599. } else {
  600. startTime = startTime + ':00'
  601. }
  602. }
  603. } else {
  604. startTime = e.startTime
  605. }
  606. if (config.endTimeType == 2) {
  607. endTime = this.formData[config.endRelationField] || 0
  608. if (e.opType === 'setTime') {
  609. endTime = this.formData[config.endRelationField] ||
  610. '00:00:00'
  611. if (endTime && (endTime.split(':').length == 3)) {
  612. endTime = endTime
  613. } else {
  614. endTime = endTime + ':00'
  615. }
  616. }
  617. } else {
  618. endTime = e.endTime
  619. }
  620. this.comSet('startTime', vModel, startTime)
  621. this.comSet('endTime', vModel, endTime)
  622. }
  623. }
  624. }
  625. }
  626. }
  627. },
  628. handleDefaultRelation(field) {
  629. if (!field) return
  630. const currRelations = this.relations
  631. for (let key in currRelations) {
  632. if (key === field) {
  633. for (let i = 0; i < currRelations[key].length; i++) {
  634. const e = currRelations[key][i];
  635. let vModel = e.realVModel || e.__vModel__
  636. const config = e.__config__
  637. let defaultValue = ''
  638. if (vModel.includes('-')) { // 子表字段
  639. const tableVModel = vModel.split('-')[0]
  640. uni.$emit('handleRelation', e, defaultValue)
  641. } else {
  642. if (e.opType === 'setUserOptions') {
  643. let value = this.formData[e.relationField] || []
  644. this.comSet('ableRelationIds', e.__vModel__, Array.isArray(value) ? value : [value])
  645. }
  646. if (e.opType === 'setDate' || e.opType === 'setTime') {
  647. let startTime = ''
  648. let endTime = ''
  649. if (config.startTimeType == 2) {
  650. startTime = this.formData[config.startRelationField] || 0
  651. if (e.opType === 'setTime') {
  652. startTime = this.formData[config.startRelationField] ||
  653. '00:00:00'
  654. if (startTime && (startTime.split(':').length == 3)) {
  655. startTime = startTime
  656. } else {
  657. startTime = startTime + ':00'
  658. }
  659. }
  660. } else {
  661. startTime = e.startTime
  662. }
  663. if (config.endTimeType == 2) {
  664. endTime = this.formData[config.endRelationField] || 0
  665. if (e.opType === 'setTime') {
  666. endTime = this.formData[config.endRelationField] ||
  667. '23:59:59'
  668. if (endTime && (endTime.split(':').length == 3)) {
  669. endTime = endTime
  670. } else {
  671. endTime = endTime + ':00'
  672. }
  673. }
  674. } else {
  675. endTime = e.endTime
  676. }
  677. this.comSet('startTime', e.__vModel__, startTime)
  678. this.comSet('endTime', e.__vModel__, endTime)
  679. }
  680. }
  681. }
  682. }
  683. }
  684. },
  685. onChange(field) {
  686. this.handleRelation(field.__vModel__)
  687. },
  688. setValue(item) {
  689. if (item.__vModel__) {
  690. this.$set(this.formData, item.__vModel__, item.__config__.defaultValue)
  691. }
  692. },
  693. setFormData(prop, value) {
  694. if (!prop || this.formData[prop] === value) return;
  695. const isChildTable = prop.indexOf('.') > -1
  696. if (isChildTable) {
  697. const list = prop.split('.')
  698. for (let i = 0; i < this.refList.length; i++) {
  699. const item = this.refList[i]
  700. if (item[0] == list[0]) {
  701. const tableRef = Array.isArray(item[1]) ? item[1][0] : item[1]
  702. tableRef.setTableFormData(list[1], value)
  703. break
  704. }
  705. }
  706. } else {
  707. this.comSet('defaultValue', prop, value)
  708. this.formData[prop] = value
  709. }
  710. this.handleRelation(prop)
  711. },
  712. setShowOrHide(prop, value) {
  713. const newVal = !!value
  714. const isChildTable = prop.indexOf('.') > -1
  715. if (!isChildTable) {
  716. this.comSet('noShow', prop, !newVal)
  717. }
  718. },
  719. setRequired(prop, value) {
  720. const newVal = !!value
  721. const isChildTable = prop.indexOf('.') > -1
  722. if (!isChildTable) {
  723. this.comSet('required', prop, newVal)
  724. this.rules = {}
  725. this.buildRules(this.$u.deepClone(this.formConfCopy.fields), this.rules)
  726. this.$refs.dataForm.setRules(this.rules);
  727. }
  728. },
  729. setDisabled(prop, value) {
  730. const newVal = !!value
  731. const isChildTable = prop.indexOf('.') > -1
  732. if (!isChildTable) {
  733. this.comSet('disabled', prop, newVal)
  734. }
  735. },
  736. setFieldOptions(prop, value) {
  737. const newVal = Array.isArray(value) ? value : []
  738. const isChildTable = prop.indexOf('.') > -1
  739. if (!isChildTable) {
  740. this.comSet('options', prop, newVal)
  741. }
  742. },
  743. comSet(field, prop, value) {
  744. if (!prop) return
  745. const loop = list => {
  746. for (let i = 0; i < list.length; i++) {
  747. let item = list[i]
  748. const config = item.__config__
  749. if (item.__vModel__ && item.__vModel__ === prop) {
  750. config.defaultValue = this.formData[prop]
  751. switch (field) {
  752. case 'disabled':
  753. item[field] = value
  754. break;
  755. case 'ableRelationIds':
  756. this.$set(item, field, value)
  757. case 'options':
  758. if (dyOptionsList.indexOf(config.jnpfKey) > -1) item.options = value
  759. break;
  760. case 'startTime':
  761. this.$set(item, field, value)
  762. break;
  763. case 'endTime':
  764. this.$set(item, field, value)
  765. break;
  766. default:
  767. config[field] = value
  768. this.setValue(item)
  769. break;
  770. }
  771. config.renderKey = +new Date() + item.__vModel__
  772. break;
  773. }
  774. if (config && config.jnpfKey !== 'table' && config.children && Array.isArray(config
  775. .children)) loop(config.children)
  776. }
  777. }
  778. loop(this.formConfCopy.fields)
  779. },
  780. submitForm() {
  781. try {
  782. this.beforeSubmit().then(() => {
  783. this.submit()
  784. })
  785. } catch (e) {
  786. this.submit()
  787. }
  788. },
  789. submit() {
  790. this.$refs.dataForm.validate(valid => {
  791. if (!valid) return
  792. if (!this.checkTableData()) return
  793. this.$emit('submit', this.formData, this.afterSubmit)
  794. });
  795. },
  796. beforeSubmit() {
  797. if (!this.formConfCopy || !this.formConfCopy.funcs || !this.formConfCopy.funcs.beforeSubmit) return Promise
  798. .resolve()
  799. const func = this.jnpf.getScriptFunc(this.formConfCopy.funcs.beforeSubmit)
  800. if (!func) return Promise.resolve()
  801. return func(this.parameter)
  802. },
  803. afterSubmit() {
  804. if (!this.formConfCopy || !this.formConfCopy.funcs || !this.formConfCopy.funcs.afterSubmit) return
  805. const func = this.jnpf.getScriptFunc(this.formConfCopy.funcs.afterSubmit)
  806. if (!func) return
  807. func(this.parameter)
  808. },
  809. // 子表必填验证,子表赋值
  810. checkTableData() {
  811. this.getRefList()
  812. let valid = true
  813. for (var i = 0; i < Object.keys(this.tableRefs).length; i++) {
  814. const vModel = Object.keys(this.tableRefs)[i]
  815. const config = this.tableRefs[vModel].__config__
  816. if (!config.isVisibility || config.noShow) continue
  817. let tableRef = null
  818. for (let i = 0; i < this.refList.length; i++) {
  819. const item = this.refList[i]
  820. if (item[0] === vModel) {
  821. tableRef = Array.isArray(item[1]) ? item[1][0] : item[1]
  822. break
  823. }
  824. }
  825. if (!tableRef) continue
  826. const res = (tableRef && tableRef.$refs && tableRef.$refs[vModel] ? tableRef.$refs[vModel] : tableRef)
  827. .submit() || false;
  828. res ? (this.formData[vModel] = res) : (valid = false)
  829. }
  830. return valid
  831. },
  832. //获取子表实例ref
  833. getRefList() {
  834. this.refList = []
  835. const refList = this.$refs || []
  836. const loop = (list) => {
  837. for (var i = 0; i < list.length; i++) {
  838. const refs = list[i].$refs
  839. if (Array.isArray(refs)) {
  840. loop(refs)
  841. } else {
  842. if (Object.values(refs).length) {
  843. for (var k in refs) {
  844. if (Array.isArray(refs[k])) {
  845. loop(refs[k])
  846. } else {
  847. this.refList.push([k, refs[k]])
  848. }
  849. }
  850. }
  851. }
  852. }
  853. }
  854. for (var i in refList) {
  855. const item = refList[i]
  856. if (Array.isArray(item)) loop(item)
  857. }
  858. },
  859. clickIcon(e) {
  860. if (!e) return
  861. if (!e.__config__.tipLabel && !e.helpMessage) return
  862. this.tipsContent = e.helpMessage || e.__config__.tipLabel
  863. this.tipsTitle = e.__config__.label
  864. if (e.__config__.jnpfKey === 'card') this.tipsTitle = e.header
  865. if (e.__config__.jnpfKey === 'groupTitle') this.tipsTitle = e.content
  866. this.showTipsModal = true
  867. },
  868. onBlur(item, data) {
  869. this.setValue(item)
  870. this.setScriptFunc(data, item, 'blur')
  871. },
  872. onTabChange(item, data) {
  873. this.setScriptFunc(data, item, 'tabClick')
  874. },
  875. onClick(item, data) {
  876. this.setScriptFunc(data, item, 'click')
  877. },
  878. onCollapseChange(item, data) {
  879. this.setScriptFunc(data, item)
  880. },
  881. setScriptFunc(val, data, type = 'change') {
  882. if (data && data.on && data.on[type]) {
  883. const func = this.jnpf.getScriptFunc(data.on[type]);
  884. if (!func) return
  885. func.call(this, {
  886. data: val,
  887. ...this.parameter
  888. })
  889. }
  890. },
  891. }
  892. }
  893. </script>