25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

321 satır
11 KiB

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