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.
 
 
 
 
 
 

443 lines
14 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,tableName,fieldName)
  38. }
  39. }
  40. result[tableName] = '__GIRDTABLE__' in tableItem ? [itemData] : itemData
  41. }
  42. return result
  43. },
  44. // 获取单条表单项的默认值
  45. async getDefaultValue(path, schemeItem,tableName,fieldName) {
  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. let folderIds = {}
  85. let getstData = uni.getStorageSync('folderIds');
  86. if(getstData){
  87. folderIds = JSON.parse(getstData)
  88. }
  89. if(folderIds[tableName]){
  90. folderIds[tableName][fieldName] = ''
  91. }else{
  92. let obj = {}
  93. obj[fieldName] = ''
  94. folderIds[tableName] = obj
  95. }
  96. uni.setStorageSync('folderIds',JSON.stringify(folderIds));
  97. return []
  98. case 'upload_old':
  99. return []
  100. case 'guid':
  101. return this.GUID('-')
  102. default:
  103. return schemeItem.dfvalue || ''
  104. }
  105. },
  106. // 验证表单项输入是否正确,返回一个包含所有错误信息的数组
  107. verifyForm() {
  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. // const uploadUid = []
  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. // if (fileId) {
  197. // uploadUid.push(fileId)
  198. // }
  199. // }
  200. // }
  201. var uploadUid = '';
  202. let folderIds = uni.getStorageSync('folderIds');
  203. if(folderIds){
  204. folderIds = JSON.parse(folderIds)
  205. if(folderIds[tableName]&&folderIds[tableName][fieldName]){
  206. uploadUid = folderIds[tableName][fieldName]
  207. }
  208. }
  209. if(!uploadUid){
  210. uploadUid = this.newguid()
  211. }
  212. for (const item of val) {
  213. if (item.uid) {
  214. uploadUid = item.uid
  215. continue
  216. }
  217. const fileId = await this.HTTP_UPLOAD(item.path || item, undefined, uploadUid)
  218. if (fileId) {
  219. uploadUid = fileId;
  220. }
  221. }
  222. return uploadUid;
  223. case 'upload_old':
  224. const valArray = val.map(item=>{
  225. return {
  226. uid:item.uid,
  227. path:item.path===undefined?item:item.path
  228. }
  229. })
  230. const uploadUid_ = []
  231. for (const { path, uid } of valArray) {
  232. if (uid) {
  233. uploadUid_.push(uid)
  234. continue
  235. }
  236. const fileId = await this.HTTP_UPLOAD(path)
  237. if (fileId) {
  238. uploadUid_.push(fileId)
  239. }
  240. }
  241. return uploadUid_.join(',')
  242. default:
  243. return val || ''
  244. }
  245. },
  246. // 格式化处理表单数据(拉取时使用)
  247. async formatFormData(formData) {
  248. const data = omit(formData, 'keyValue')
  249. for (const [tableName, schemeItem] of Object.entries(this.scheme)) {
  250. if ('__GIRDTABLE__' in schemeItem) {
  251. if (!data[tableName] || data[tableName].length <= 0) { data[tableName] = [{}] }
  252. const tableData = data[tableName]
  253. for (let index = 0; index < tableData.length; ++index) {
  254. const tableValue = tableData[index]
  255. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  256. if (fieldName === '__GIRDTABLE__') { continue }
  257. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  258. tableValue[fieldName] = await this.convertToFormValue(scheme, tableValue[fieldName], dataSource,tableName,fieldName)
  259. }
  260. }
  261. } else {
  262. for (const [fieldName, scheme] of Object.entries(schemeItem)) {
  263. const dataSource = get(this.dataSource, `${tableName}.${fieldName}`)
  264. data[tableName][fieldName] = await this.convertToFormValue(scheme, data[tableName][fieldName], dataSource,tableName,fieldName)
  265. }
  266. }
  267. }
  268. return data
  269. },
  270. // 将单项表单数据格式化(拉取时使用)
  271. async convertToFormValue(scheme, val, dataSource,tableName,fieldName) {
  272. switch (scheme.type) {
  273. case 'upload':
  274. // if (!val) { return [] }
  275. // const uidList = val.split(',')
  276. // const fileList = []
  277. // for (const uid of uidList || []) {
  278. // const fileInfo = await this.FETCH_FILEINFO(uid)
  279. // if (!fileInfo) { continue }
  280. // const fileType = fileInfo.F_FileType
  281. // const fileSize = fileInfo.F_FileSize
  282. // const fileName = fileInfo.F_FileName
  283. // const path = this.API + '/annexes/wxdown?' + this.URL_QUERY(uid, true)
  284. // fileList.push({ path, type: fileType, uid, size: fileSize, name:fileName })
  285. // }
  286. // return fileList
  287. let folderIds = {}
  288. let getstData = uni.getStorageSync('folderIds');
  289. if(getstData){
  290. folderIds = JSON.parse(getstData)
  291. }
  292. if(folderIds[tableName]){
  293. folderIds[tableName][fieldName] = val
  294. }else{
  295. let obj = {}
  296. obj[fieldName] = val
  297. folderIds[tableName] = obj
  298. }
  299. uni.setStorageSync('folderIds',JSON.stringify(folderIds));
  300. if (!val) {
  301. return []
  302. }
  303. const uidList = val;
  304. const fileList = []
  305. const wxlist = await this.FETCH_FILEList(uidList);
  306. for (const wxfile of wxlist) {
  307. const fileInfo = await this.FETCH_FILEINFO(wxfile.F_Id)
  308. if (!fileInfo) {
  309. continue
  310. }
  311. const fileType = fileInfo.F_FileType
  312. const fileSize = fileInfo.F_FileSize
  313. const fileName = fileInfo.F_FileName
  314. const path = this.API + '/learun/adms/annexes/wxdown?' + this.URL_QUERY(wxfile.F_Id, true)
  315. fileList.push({
  316. path,
  317. type: fileType,
  318. uid:wxfile.F_Id,
  319. folderId:wxfile.F_FolderId,
  320. size: fileSize,
  321. name:fileName
  322. })
  323. }
  324. return fileList
  325. case 'upload_old':
  326. if (!val) { return [] }
  327. const uidList_ = val.split(',')
  328. const fileList_ = []
  329. for (const uid of uidList_ || []) {
  330. const fileInfo = await this.FETCH_FILEINFO(uid)
  331. if (!fileInfo) { continue }
  332. const fileType = fileInfo.F_FileType
  333. const fileSize = fileInfo.F_FileSize
  334. const fileName = fileInfo.F_FileName
  335. const path = this.API + 'learun/adms/annexes/wxdown?' + this.URL_QUERY(uid, true)
  336. fileList_.push({ path, type: fileType, uid, size: fileSize, name:fileName })
  337. }
  338. return fileList_
  339. case 'radio':
  340. case 'select':
  341. if (!val || !dataSource.map(t => t.value).includes(String(val))) { return '' }
  342. return String(val)
  343. case 'checkbox':
  344. if (!val) { return [] }
  345. const validValue = dataSource.map(t => t.value)
  346. const checkboxVal = val.split(',') || []
  347. return checkboxVal.filter(t => validValue.includes(t))
  348. case 'datetime':
  349. if (!val) { return '' }
  350. return moment(val).format(
  351. Number(scheme.dateformat) === 0 || scheme.datetime === 'date' ?
  352. 'YYYY-MM-DD' :
  353. 'YYYY-MM-DD HH:mm:ss'
  354. )
  355. default:
  356. return val === null || val === undefined ? '' : val
  357. }
  358. }
  359. },
  360. computed: {
  361. // 验证函数
  362. verify() {
  363. return {
  364. NotNull: t => t.length > 0 || '不能为空',
  365. Num: t => !isNaN(t) || '须输入数值',
  366. NumOrNull: t => t.length <= 0 || !isNaN(t) || '须留空或输入数值',
  367. Email: t => /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) || '须符合Email格式',
  368. EmailOrNull: t => t.length <= 0 || /^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+.[a-zA-Z0-9]+$/.test(t) ||
  369. '须留空或符合Email格式',
  370. EnglishStr: t => /^[a-zA-Z]*$/.test(t) || '须由英文字母组成',
  371. EnglishStrOrNull: t => t.length <= 0 || /^[a-zA-Z]*$/.test(t) || '须留空或由英文字母组成',
  372. Phone: t => /^[+0-9- ]*$/.test(t) || '须符合电话号码格式',
  373. PhoneOrNull: t => t.length <= 0 || /^[+0-9- ]*$/.test(t) || '须留空或符合电话号码格式',
  374. Fax: t => /^[+0-9- ]*$/.test(t) || '须符合传真号码格式',
  375. Mobile: t => /^1[0-9]{10}$/.test(t) || '须符合手机号码格式',
  376. MobileOrPhone: t => /^[+0-9- ]*$/.test(t) || /^1[0-9]{10}$/.test(t) || '须符合电话或手机号码格式',
  377. MobileOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || '须留空或符合手机号码格式',
  378. MobileOrPhoneOrNull: t => t.length <= 0 || /^1[0-9]{10}$/.test(t) || /^[+0-9- ]*$/.test(t) ||
  379. '须留空或符合手机/电话号码格式',
  380. Uri: t => /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须符合网址Url格式',
  381. UriOrNull: t => t.length <= 0 || /^[a-zA-z]+:\/\/[^\s]*$/.test(t) || '须留空或符合网址Url格式'
  382. }
  383. }
  384. }
  385. }