Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

413 linhas
14 KiB

  1. import { conforms, reject } 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,tableName,fieldName)
  39. }
  40. }
  41. result[tableName] = '__GIRDTABLE__' in tableItem ? [itemData] : itemData
  42. }
  43. return result
  44. },
  45. // 获取单条表单项的默认值
  46. async getDefaultValue(path, schemeItem,tableName,fieldName) {
  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. let folderIds = {}
  86. let getstData = uni.getStorageSync('folderIds');
  87. if(getstData){
  88. folderIds = JSON.parse(getstData)
  89. }
  90. if(folderIds[tableName]){
  91. folderIds[tableName][fieldName] = ''
  92. }else{
  93. let obj = {}
  94. obj[fieldName] = ''
  95. folderIds[tableName] = obj
  96. }
  97. uni.setStorageSync('folderIds',JSON.stringify(folderIds));
  98. return []
  99. case 'guid':
  100. return this.GUID('-')
  101. default:
  102. return schemeItem.dfvalue || ''
  103. }
  104. },
  105. // 验证表单项输入是否正确,返回一个包含所有错误信息的数组
  106. verifyForm() {
  107. const result = []
  108. Object.entries(this.scheme).forEach(([tableName, tableItem]) => {
  109. if ('__GIRDTABLE__' in tableItem) {
  110. this.getValue(tableName).forEach((tableValue, index) => {
  111. Object.entries(tableItem).forEach(([fieldName, scheme]) => {
  112. if (fieldName === '__GIRDTABLE__' || !scheme.verify) { return }
  113. const val = tableValue[fieldName]
  114. const verifyResult = this.verify[scheme.verify](val)
  115. if (verifyResult !== true) {
  116. result.push(`[表格${tableItem.__GIRDTABLE__}第${index}行${scheme.title}列]: ${verifyResult}`)
  117. }
  118. })
  119. })
  120. } else {
  121. Object.entries(tableItem).forEach(([fieldName, scheme]) => {
  122. if (!scheme.verify) { return }
  123. const val = this.getValue(`${tableName}.${fieldName}`)
  124. const verifyResult = this.verify[scheme.verify](val)
  125. if (verifyResult !== true) {
  126. result.push(`[${scheme.title}]: ${verifyResult}`)
  127. }
  128. })
  129. }
  130. })
  131. return result
  132. },
  133. // 获取要提交的表单数据(提交时使用)
  134. async getPostData(keyValue) {
  135. const result = {}
  136. for (const [tableName, tableItem] of Object.entries(this.scheme)) {
  137. if ('__GIRDTABLE__' in tableItem) {
  138. // 从表
  139. const tableArray = []
  140. const tableData = this.current[tableName]
  141. for (let index = 0; index < tableData.length; ++index) {
  142. const tableValue = tableData[index]
  143. const tableObj = {}
  144. for (const [fieldName, scheme] of Object.entries(tableItem)) {
  145. if (fieldName === '__GIRDTABLE__') { continue }
  146. tableObj[fieldName] = await this.convertToPostData(scheme, tableValue[fieldName],tableName,fieldName)
  147. }
  148. tableArray.push(tableObj)
  149. }
  150. result[`str${tableName}Entity`] = JSON.stringify(tableArray)
  151. } else {
  152. // 主表
  153. const strEntity = {}
  154. for (const [fieldName, scheme] of Object.entries(tableItem)) {
  155. strEntity[fieldName] = await this.convertToPostData(scheme, this.current[tableName][fieldName],tableName,fieldName)
  156. }
  157. result['strEntity'] = JSON.stringify(strEntity)
  158. }
  159. }
  160. if (keyValue) {
  161. result.keyValue = keyValue
  162. }
  163. return result
  164. },
  165. newguid() {
  166. return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  167. var r = Math.random() * 16 | 0,
  168. v = c == 'x' ? r : (r & 0x3 | 0x8);
  169. return v.toString(16);
  170. });
  171. },
  172. // 将单项表单数据转为 post 数据(提交时使用)
  173. async convertToPostData(scheme, val,tableName,fieldName) {
  174. switch (scheme.type) {
  175. case 'checkbox':
  176. return val ? val.join(',') : ''
  177. case 'datetimerange':
  178. const startTime = this.getValue(scheme.startTime)
  179. const endTime = this.getValue(scheme.endTime)
  180. if (!startTime || !endTime || moment(endTime).isBefore(startTime)) {
  181. return ''
  182. } else {
  183. return moment.duration(moment(endTime).diff(moment(startTime))).asDays().toFixed(0)
  184. }
  185. case 'datetime':
  186. return val ? moment(val).format('YYYY-MM-DD HH:mm') : ''
  187. case 'upload':
  188. // let uploadUid = []
  189. // console.log(val)
  190. // for (const entity of val) {
  191. // if (entity.uid) {
  192. // uploadUid.push(entity.uid)
  193. // continue
  194. // } else {
  195. // const fileId = await this.HTTP_UPLOAD(entity)
  196. // console.log(fileId)
  197. // if (fileId) {
  198. // uploadUid.push(fileId)
  199. // }
  200. // }
  201. // }
  202. // console.log(uploadUid.join(','))
  203. // reject()
  204. // return uploadUid.join(',')
  205. var uploadUid = '';
  206. let folderIds = uni.getStorageSync('folderIds');
  207. if(folderIds){
  208. folderIds = JSON.parse(folderIds)
  209. if(folderIds[tableName]&&folderIds[tableName][fieldName]){
  210. uploadUid = folderIds[tableName][fieldName]
  211. }
  212. }
  213. if(!uploadUid){
  214. uploadUid = this.newguid()
  215. }
  216. for (const item of val) {
  217. if (item.uid) {
  218. // uploadUid = item.uid
  219. continue
  220. }
  221. const fileId = await this.HTTP_UPLOAD(item.path || item, undefined, uploadUid)
  222. if (fileId) {
  223. uploadUid = fileId;
  224. }
  225. }
  226. return uploadUid;
  227. default:
  228. return val || ''
  229. }
  230. },
  231. // 格式化处理表单数据(拉取时使用)
  232. async formatFormData(formData) {
  233. const data = omit(formData, 'keyValue')
  234. for (const [tableName, schemeItem] of Object.entries(this.scheme)) {
  235. if ('__GIRDTABLE__' in schemeItem) {
  236. if (!data[tableName] || data[tableName].length <= 0) { data[tableName] = [{}] }
  237. const tableData = data[tableName]
  238. for (let index = 0; index < tableData.length; ++index) {
  239. const tableValue = tableData[index]
  240. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  241. if (fieldName === '__GIRDTABLE__') { continue }
  242. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  243. tableValue[fieldName] = await this.convertToFormValue(scheme, tableValue[fieldName], dataSource,tableName,fieldName)
  244. }
  245. }
  246. } else {
  247. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  248. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  249. data[tableName][fieldName] = await this.convertToFormValue(scheme, data[tableName][fieldName], dataSource,tableName,fieldName)
  250. }
  251. }
  252. }
  253. return data
  254. },
  255. // 将单项表单数据格式化(拉取时使用)
  256. async convertToFormValue(scheme, val, dataSource,tableName,fieldName) {
  257. switch (scheme.type) {
  258. case 'upload':
  259. // if (!val) { return [] }
  260. // const uidList = val.split(',')
  261. // const fileList = []
  262. // for (const uid of uidList || []) {
  263. // const fileInfo = await this.FETCH_FILEINFO(uid)
  264. // if (!fileInfo) { continue }
  265. // const fileType = fileInfo.F_FileType
  266. // const fileSize = fileInfo.F_FileSize
  267. // const fileName = fileInfo.F_FileName
  268. // const path = this.API + '/learun/adms/annexes/wxdown?' + this.URL_QUERY(uid, true)
  269. // fileList.push({ path, type: fileType, uid, size: fileSize, name:fileName })
  270. // }
  271. // return fileList
  272. let folderIds = {}
  273. let getstData = uni.getStorageSync('folderIds');
  274. if(getstData){
  275. folderIds = JSON.parse(getstData)
  276. }
  277. if(folderIds[tableName]){
  278. folderIds[tableName][fieldName] = val
  279. }else{
  280. let obj = {}
  281. obj[fieldName] = val
  282. folderIds[tableName] = obj
  283. }
  284. uni.setStorageSync('folderIds',JSON.stringify(folderIds));
  285. if (!val) {
  286. return []
  287. }
  288. const uidList = val;
  289. const fileList = []
  290. const wxlist = await this.FETCH_FILEList(uidList);
  291. for (const wxfile of wxlist) {
  292. const fileInfo = await this.FETCH_FILEINFO(wxfile.F_Id)
  293. if (!fileInfo) {
  294. continue
  295. }
  296. const fileType = fileInfo.F_FileType
  297. const fileSize = fileInfo.F_FileSize
  298. const fileName = fileInfo.F_FileName
  299. const path = this.API + '/learun/adms/annexes/wxdown?' + this.URL_QUERY(wxfile.F_Id, true)
  300. fileList.push({
  301. path,
  302. type: fileType,
  303. uid:wxfile.F_Id,
  304. folderId:wxfile.F_FolderId,
  305. size: fileSize,
  306. name:fileName
  307. })
  308. }
  309. return fileList
  310. case 'radio':
  311. case 'select':
  312. if (!val || !dataSource.map(t => t.value).includes(String(val))) { return '' }
  313. return String(val)
  314. case 'selectNoMap':
  315. if (!val) { return '' }
  316. return String(val)
  317. case 'checkbox':
  318. if (!val) { return [] }
  319. const validValue = dataSource.map(t => t.value)
  320. const checkboxVal = val.split(',') || []
  321. return checkboxVal.filter(t => validValue.includes(t))
  322. case 'datetime':
  323. if (!val) { return '' }
  324. return moment(val).format(
  325. Number(scheme.dateformat) === 0 || scheme.datetime === 'date' ?
  326. 'YYYY-MM-DD' :
  327. 'YYYY-MM-DD HH:mm:ss'
  328. )
  329. default:
  330. return val === null || val === undefined ? '' : val
  331. }
  332. }
  333. },
  334. computed: {
  335. // 验证函数
  336. verify() {
  337. return {
  338. NotNull: t => t.length > 0 || '不能为空',
  339. Num: t => !isNaN(t) || '须输入数值',
  340. NumOrNull: t => t.length <= 0 || !isNaN(t) || '须留空或输入数值',
  341. Email: t => /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) || '须符合Email格式',
  342. EmailOrNull: t => t.length <= 0 || /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) ||
  343. '须留空或符合Email格式',
  344. EnglishStr: t => /^[a-zA-Z]*$/.test(t) || '须由英文字母组成',
  345. EnglishStrOrNull: t => t.length <= 0 || /^[a-zA-Z]*$/.test(t) || '须留空或由英文字母组成',
  346. Phone: t => /^[+0-9- ]*$/.test(t) || '须符合电话号码格式',
  347. PhoneOrNull: t => t.length <= 0 || /^[+0-9- ]*$/.test(t) || '须留空或符合电话号码格式',
  348. Fax: t => /^[+0-9- ]*$/.test(t) || '须符合传真号码格式',
  349. Mobile: t => /^1[0-9]{10}$/.test(t) || '须符合手机号码格式',
  350. MobileOrPhone: t => /^[+0-9- ]*$/.test(t) || /^1[0-9]{10}$/.test(t) || '须符合电话或手机号码格式',
  351. MobileOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || '须留空或符合手机号码格式',
  352. MobileOrPhoneOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || /^[+0-9- ]*$/.test(t) ||
  353. '须留空或符合手机/电话号码格式',
  354. Uri: t => /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须符合网址Url格式',
  355. UriOrNull: t => t.length <= 0 || /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须留空或符合网址Url格式'
  356. }
  357. }
  358. }
  359. }