js.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { exportDefault, titleCase } from '@/utils/index'
  2. import { trigger } from './config'
  3. const units = {
  4. KB: '1024',
  5. MB: '1024 / 1024',
  6. GB: '1024 / 1024 / 1024'
  7. }
  8. let confGlobal
  9. const inheritAttrs = {
  10. file: '',
  11. dialog: 'inheritAttrs: false,'
  12. }
  13. export function makeUpJs(conf, type) {
  14. confGlobal = conf = JSON.parse(JSON.stringify(conf))
  15. const dataList = []
  16. const ruleList = []
  17. const optionsList = []
  18. const propsList = []
  19. const methodList = mixinMethod(type)
  20. const uploadVarList = []
  21. conf.fields.forEach(el => {
  22. buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
  23. })
  24. const script = buildexport(
  25. conf,
  26. type,
  27. dataList.join('\n'),
  28. ruleList.join('\n'),
  29. optionsList.join('\n'),
  30. uploadVarList.join('\n'),
  31. propsList.join('\n'),
  32. methodList.join('\n')
  33. )
  34. confGlobal = null
  35. return script
  36. }
  37. function buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList) {
  38. buildData(el, dataList)
  39. buildRules(el, ruleList)
  40. if (el.options && el.options.length) {
  41. buildOptions(el, optionsList)
  42. if (el.dataType === 'dynamic') {
  43. const model = `${el.vModel}Options`
  44. const options = titleCase(model)
  45. buildOptionMethod(`get${options}`, model, methodList)
  46. }
  47. }
  48. if (el.props && el.props.props) {
  49. buildProps(el, propsList)
  50. }
  51. if (el.action && el.tag === 'el-upload') {
  52. uploadVarList.push(
  53. `${el.vModel}Action: '${el.action}',
  54. ${el.vModel}fileList: [],`
  55. )
  56. methodList.push(buildBeforeUpload(el))
  57. if (!el['auto-upload']) {
  58. methodList.push(buildSubmitUpload(el))
  59. }
  60. }
  61. if (el.children) {
  62. el.children.forEach(el2 => {
  63. buildAttributes(el2, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
  64. })
  65. }
  66. }
  67. function mixinMethod(type) {
  68. const list = []; const
  69. minxins = {
  70. file: confGlobal.formBtns ? {
  71. submitForm: `submitForm() {
  72. this.$refs['${confGlobal.formRef}'].validate(valid => {
  73. if(!valid) return
  74. // TODO 提交表单
  75. })
  76. },`,
  77. resetForm: `resetForm() {
  78. this.$refs['${confGlobal.formRef}'].resetFields()
  79. },`
  80. } : null,
  81. dialog: {
  82. onOpen: 'onOpen() {},',
  83. onClose: `onClose() {
  84. this.$refs['${confGlobal.formRef}'].resetFields()
  85. },`,
  86. close: `close() {
  87. this.$emit('update:visible', false)
  88. },`,
  89. handleConfirm: `handleConfirm() {
  90. this.$refs['${confGlobal.formRef}'].validate(valid => {
  91. if(!valid) return
  92. this.close()
  93. })
  94. },`
  95. }
  96. }
  97. const methods = minxins[type]
  98. if (methods) {
  99. Object.keys(methods).forEach(key => {
  100. list.push(methods[key])
  101. })
  102. }
  103. return list
  104. }
  105. function buildData(conf, dataList) {
  106. if (conf.vModel === undefined) return
  107. let defaultValue
  108. if (typeof (conf.defaultValue) === 'string' && !conf.multiple) {
  109. defaultValue = `'${conf.defaultValue}'`
  110. } else {
  111. defaultValue = `${JSON.stringify(conf.defaultValue)}`
  112. }
  113. dataList.push(`${conf.vModel}: ${defaultValue},`)
  114. }
  115. function buildRules(conf, ruleList) {
  116. if (conf.vModel === undefined) return
  117. const rules = []
  118. if (trigger[conf.tag]) {
  119. if (conf.required) {
  120. const type = Array.isArray(conf.defaultValue) ? 'type: \'array\',' : ''
  121. let message = Array.isArray(conf.defaultValue) ? `请至少选择一个${conf.vModel}` : conf.placeholder
  122. if (message === undefined) message = `${conf.label}不能为空`
  123. rules.push(`{ required: true, ${type} message: '${message}', trigger: '${trigger[conf.tag]}' }`)
  124. }
  125. if (conf.regList && Array.isArray(conf.regList)) {
  126. conf.regList.forEach(item => {
  127. if (item.pattern) {
  128. rules.push(`{ pattern: ${eval(item.pattern)}, message: '${item.message}', trigger: '${trigger[conf.tag]}' }`)
  129. }
  130. })
  131. }
  132. ruleList.push(`${conf.vModel}: [${rules.join(',')}],`)
  133. }
  134. }
  135. function buildOptions(conf, optionsList) {
  136. if (conf.vModel === undefined) return
  137. if (conf.dataType === 'dynamic') { conf.options = [] }
  138. const str = `${conf.vModel}Options: ${JSON.stringify(conf.options)},`
  139. optionsList.push(str)
  140. }
  141. function buildProps(conf, propsList) {
  142. if (conf.dataType === 'dynamic') {
  143. conf.valueKey !== 'value' && (conf.props.props.value = conf.valueKey)
  144. conf.labelKey !== 'label' && (conf.props.props.label = conf.labelKey)
  145. conf.childrenKey !== 'children' && (conf.props.props.children = conf.childrenKey)
  146. }
  147. const str = `${conf.vModel}Props: ${JSON.stringify(conf.props.props)},`
  148. propsList.push(str)
  149. }
  150. function buildBeforeUpload(conf) {
  151. const unitNum = units[conf.sizeUnit]; let rightSizeCode = ''; let acceptCode = ''; const
  152. returnList = []
  153. if (conf.fileSize) {
  154. rightSizeCode = `let isRightSize = file.size / ${unitNum} < ${conf.fileSize}
  155. if(!isRightSize){
  156. this.$message.error('文件大小超过 ${conf.fileSize}${conf.sizeUnit}')
  157. }`
  158. returnList.push('isRightSize')
  159. }
  160. if (conf.accept) {
  161. acceptCode = `let isAccept = new RegExp('${conf.accept}').test(file.type)
  162. if(!isAccept){
  163. this.$message.error('应该选择${conf.accept}类型的文件')
  164. }`
  165. returnList.push('isAccept')
  166. }
  167. const str = `${conf.vModel}BeforeUpload(file) {
  168. ${rightSizeCode}
  169. ${acceptCode}
  170. return ${returnList.join('&&')}
  171. },`
  172. return returnList.length ? str : ''
  173. }
  174. function buildSubmitUpload(conf) {
  175. const str = `submitUpload() {
  176. this.$refs['${conf.vModel}'].submit()
  177. },`
  178. return str
  179. }
  180. function buildOptionMethod(methodName, model, methodList) {
  181. const str = `${methodName}() {
  182. // TODO 发起请求获取数据
  183. this.${model}
  184. },`
  185. methodList.push(str)
  186. }
  187. function buildexport(conf, type, data, rules, selectOptions, uploadVar, props, methods) {
  188. const str = `${exportDefault}{
  189. ${inheritAttrs[type]}
  190. components: {},
  191. props: [],
  192. data () {
  193. return {
  194. ${conf.formModel}: {
  195. ${data}
  196. },
  197. ${conf.formRules}: {
  198. ${rules}
  199. },
  200. ${uploadVar}
  201. ${selectOptions}
  202. ${props}
  203. }
  204. },
  205. computed: {},
  206. watch: {},
  207. created () {},
  208. mounted () {},
  209. methods: {
  210. ${methods}
  211. }
  212. }`
  213. return str
  214. }