js.js 6.3 KB

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