You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

316 line
11 KiB

  1. import get from 'lodash/get'
  2. import omit from 'lodash/omit'
  3. import moment from 'moment'
  4. /**
  5. * 用于代码生成器页面,加载表单数据使用
  6. *
  7. * 提供以下工具方法:
  8. *
  9. * 【新建表单时使用】:
  10. * async getDefaultForm()
  11. * async getDefaultValue(path, scheme)
  12. * 根据表单 scheme 获取初始化的空表单数据 / 获取单条表单项的初始数据
  13. *
  14. * 【提交表单时使用】:
  15. * async getPostData()
  16. * async convertToPostData()
  17. * 生成 POST 提交用的表单数据 / 获取单条表单项的提交数据
  18. *
  19. * verifyForm()
  20. * 验证表单数据,返回一个输入错误信息的数组
  21. *
  22. * 【打开一个表单时使用】:
  23. * async formatFormData(formData)
  24. * async convertToFormValue(scheme, val, dataSource)
  25. * 将拉取的表单值转化为表单数据 / 将单条表单项的表单值转化为表单数据
  26. *
  27. */
  28. export default {
  29. methods: {
  30. // 获取表单默认值
  31. async getDefaultForm() {
  32. const result = {}
  33. for (const [tableName, tableItem] of Object.entries(this.scheme)) {
  34. const itemData = {}
  35. for (const [fieldName, scheme] of Object.entries(tableItem)) {
  36. if (fieldName !== '__GIRDTABLE__') {
  37. itemData[fieldName] = await this.getDefaultValue(`${tableName}.${fieldName}`, scheme)
  38. }
  39. }
  40. result[tableName] = '__GIRDTABLE__' in tableItem ? [itemData] : itemData
  41. }
  42. return result
  43. },
  44. // 获取单条表单项的默认值
  45. async getDefaultValue(path, schemeItem) {
  46. switch (schemeItem.type) {
  47. case 'keyValue':
  48. return this.processId
  49. case 'currentInfo':
  50. switch (schemeItem.dataType) {
  51. case 'user':
  52. return this.GET_GLOBAL('loginUser').userId
  53. case 'department':
  54. return this.GET_GLOBAL('loginUser').departmentId
  55. case 'company':
  56. return this.GET_GLOBAL('loginUser').companyId
  57. case 'time':
  58. return moment().format('YYYY-MM-DD HH:mm:ss')
  59. default:
  60. return ''
  61. }
  62. case 'datetime':
  63. const datetimeFormat = (Number(schemeItem.dateformat) === 0 ? 'YYYY-MM-DD' : 'YYYY-MM-DD HH:mm:ss')
  64. const today = moment()
  65. const dfDatetime = [
  66. today.subtract(1, 'day'),
  67. today,
  68. today.add(1, 'day')
  69. ][Number(schemeItem.dfvalue)] || today
  70. return dfDatetime.format(datetimeFormat) || ''
  71. case 'radio':
  72. case 'select':
  73. const radioItem = get(this.dataSource, path).find(t => t.value === schemeItem.dfvalue) ||
  74. get(this.dataSource, path)[0]
  75. return schemeItem.type === 'radio' ? radioItem.value : ''
  76. case 'checkbox':
  77. if (!schemeItem.dfvalue) { return [] }
  78. return schemeItem.dfvalue.split(',').filter(t => get(this.dataSource, path, []).find(s => s.value === t))
  79. case 'encode':
  80. if (!schemeItem.rulecode) { return '' }
  81. const result = await this.FETCH_ENCODE(schemeItem.rulecode)
  82. return result || ''
  83. case 'upload':
  84. return []
  85. case 'guid':
  86. return this.GUID('-')
  87. default:
  88. return schemeItem.dfvalue || ''
  89. }
  90. },
  91. // 验证表单项输入是否正确,返回一个包含所有错误信息的数组
  92. verifyForm() {
  93. const result = []
  94. Object.entries(this.scheme).forEach(([tableName, tableItem]) => {
  95. if ('__GIRDTABLE__' in tableItem) {
  96. this.getValue(tableName).forEach((tableValue, index) => {
  97. Object.entries(tableItem).forEach(([fieldName, scheme]) => {
  98. if (fieldName === '__GIRDTABLE__' || !scheme.verify) { return }
  99. const val = tableValue[fieldName]
  100. const verifyResult = this.verify[scheme.verify](val)
  101. if (verifyResult !== true) {
  102. result.push(`[表格${tableItem.__GIRDTABLE__}第${index}行${scheme.title}列]: ${verifyResult}`)
  103. }
  104. })
  105. })
  106. } else {
  107. Object.entries(tableItem).forEach(([fieldName, scheme]) => {
  108. if (!scheme.verify) { return }
  109. const val = this.getValue(`${tableName}.${fieldName}`)
  110. const verifyResult = this.verify[scheme.verify](val)
  111. if (verifyResult !== true) {
  112. result.push(`[${scheme.title}]: ${verifyResult}`)
  113. }
  114. })
  115. }
  116. })
  117. return result
  118. },
  119. // 获取要提交的表单数据(提交时使用)
  120. async getPostData(keyValue) {
  121. const result = {}
  122. for (const [tableName, tableItem] of Object.entries(this.scheme)) {
  123. if ('__GIRDTABLE__' in tableItem) {
  124. // 从表
  125. const tableArray = []
  126. const tableData = this.current[tableName]
  127. for (let index = 0; index < tableData.length; ++index) {
  128. const tableValue = tableData[index]
  129. const tableObj = {}
  130. for (const [fieldName, scheme] of Object.entries(tableItem)) {
  131. if (fieldName === '__GIRDTABLE__') { continue }
  132. tableObj[fieldName] = await this.convertToPostData(scheme, tableValue[fieldName])
  133. }
  134. tableArray.push(tableObj)
  135. }
  136. result[`str${tableName}Entity`] = JSON.stringify(tableArray)
  137. } else {
  138. // 主表
  139. const strEntity = {}
  140. for (const [fieldName, scheme] of Object.entries(tableItem)) {
  141. strEntity[fieldName] = await this.convertToPostData(scheme, this.current[tableName][fieldName])
  142. }
  143. result['strEntity'] = JSON.stringify(strEntity)
  144. }
  145. }
  146. if (keyValue) {
  147. result.keyValue = keyValue
  148. }
  149. return result
  150. },
  151. // 将单项表单数据转为 post 数据(提交时使用)
  152. async convertToPostData(scheme, val) {
  153. switch (scheme.type) {
  154. case 'checkbox':
  155. return val ? val.join(',') : ''
  156. case 'datetimerange':
  157. const startTime = this.getValue(scheme.startTime)
  158. const endTime = this.getValue(scheme.endTime)
  159. if (!startTime || !endTime || moment(endTime).isBefore(startTime)) {
  160. return ''
  161. } else {
  162. return moment.duration(moment(endTime).diff(moment(startTime))).asDays().toFixed(0)
  163. }
  164. case 'datetime':
  165. return val ? moment(val).format('YYYY-MM-DD HH:mm') : ''
  166. case 'upload':
  167. const uploadUid = []
  168. for (const entity of val) {
  169. if (entity.uid) {
  170. uploadUid.push(entity.uid)
  171. continue
  172. } else {
  173. const fileId = await this.HTTP_UPLOAD(entity)
  174. if (fileId) {
  175. uploadUid.push(fileId)
  176. }
  177. }
  178. }
  179. return uploadUid.join(',')
  180. default:
  181. return val || ''
  182. }
  183. },
  184. // 格式化处理表单数据(拉取时使用)
  185. async formatFormData(formData) {
  186. const data = omit(formData, 'keyValue')
  187. for (const [tableName, schemeItem] of Object.entries(this.scheme)) {
  188. if ('__GIRDTABLE__' in schemeItem) {
  189. if (!data[tableName] || data[tableName].length <= 0) { data[tableName] = [{}] }
  190. const tableData = data[tableName]
  191. for (let index = 0; index < tableData.length; ++index) {
  192. const tableValue = tableData[index]
  193. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  194. if (fieldName === '__GIRDTABLE__') { continue }
  195. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  196. tableValue[fieldName] = await this.convertToFormValue(scheme, tableValue[fieldName], dataSource)
  197. }
  198. }
  199. } else {
  200. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  201. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  202. data[tableName][fieldName] = await this.convertToFormValue(scheme, data[tableName][fieldName], dataSource)
  203. }
  204. }
  205. }
  206. return data
  207. },
  208. // 将单项表单数据格式化(拉取时使用)
  209. async convertToFormValue(scheme, val, dataSource) {
  210. switch (scheme.type) {
  211. case 'upload':
  212. if (!val) { return [] }
  213. const uidList = val.split(',')
  214. const fileList = []
  215. for (const uid of uidList || []) {
  216. const fileInfo = await this.FETCH_FILEINFO(uid)
  217. if (!fileInfo) { continue }
  218. const fileType = fileInfo.F_FileType
  219. const fileSize = fileInfo.F_FileSize
  220. const path = this.API + '/annexes/wxdown?' + this.URL_QUERY(uid, true)
  221. fileList.push({ path, type: fileType, uid, size: fileSize })
  222. }
  223. return fileList
  224. case 'radio':
  225. case 'select':
  226. if (!val || !dataSource.map(t => t.value).includes(String(val))) { return '' }
  227. return String(val)
  228. case 'checkbox':
  229. if (!val) { return [] }
  230. const validValue = dataSource.map(t => t.value)
  231. const checkboxVal = val.split(',') || []
  232. return checkboxVal.filter(t => validValue.includes(t))
  233. case 'datetime':
  234. if (!val) { return '' }
  235. return moment(val).format(
  236. Number(scheme.dateformat) === 0 || scheme.datetime === 'date' ?
  237. 'YYYY-MM-DD' :
  238. 'YYYY-MM-DD HH:mm:ss'
  239. )
  240. default:
  241. return val === null || val === undefined ? '' : val
  242. }
  243. }
  244. },
  245. computed: {
  246. // 验证函数
  247. verify() {
  248. return {
  249. NotNull: t => t.length > 0 || '不能为空',
  250. Num: t => !isNaN(t) || '须输入数值',
  251. NumOrNull: t => t.length <= 0 || !isNaN(t) || '须留空或输入数值',
  252. Email: t => /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) || '须符合Email格式',
  253. EmailOrNull: t => t.length <= 0 || /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) ||
  254. '须留空或符合Email格式',
  255. EnglishStr: t => /^[a-zA-Z]*$/.test(t) || '须由英文字母组成',
  256. EnglishStrOrNull: t => t.length <= 0 || /^[a-zA-Z]*$/.test(t) || '须留空或由英文字母组成',
  257. Phone: t => /^[+0-9- ]*$/.test(t) || '须符合电话号码格式',
  258. PhoneOrNull: t => t.length <= 0 || /^[+0-9- ]*$/.test(t) || '须留空或符合电话号码格式',
  259. Fax: t => /^[+0-9- ]*$/.test(t) || '须符合传真号码格式',
  260. Mobile: t => /^1[0-9]{10}$/.test(t) || '须符合手机号码格式',
  261. MobileOrPhone: t => /^[+0-9- ]*$/.test(t) || /^1[0-9]{10}$/.test(t) || '须符合电话或手机号码格式',
  262. MobileOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || '须留空或符合手机号码格式',
  263. MobileOrPhoneOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || /^[+0-9- ]*$/.test(t) ||
  264. '须留空或符合手机/电话号码格式',
  265. Uri: t => /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须符合网址Url格式',
  266. UriOrNull: t => t.length <= 0 || /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须留空或符合网址Url格式'
  267. }
  268. }
  269. }
  270. }