Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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