Skip to content

Commit b16cd15

Browse files
committed
feat: 级联组件动态获取数据
1 parent 120d272 commit b16cd15

File tree

12 files changed

+2084
-1985
lines changed

12 files changed

+2084
-1985
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
},
1414
"dependencies": {
1515
"@babel/parser": "^7.7.4",
16+
"axios": "^0.19.2",
1617
"clipboard": "^2.0.4",
1718
"core-js": "^3.6.5",
18-
"vue": "^2.6.11",
1919
"file-saver": "^2.0.2",
2020
"throttle-debounce": "^2.1.0",
21+
"vue": "^2.6.11",
2122
"vuedraggable": "^2.23.2"
2223
},
2324
"devDependencies": {

public/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
<link href="https://lib.baomitu.com/monaco-editor/0.19.3/min/vs/editor/editor.main.css" rel="stylesheet">
8181
<script src="https://lib.baomitu.com/vue/2.6.11/vue<%= process.env.NODE_ENV === 'production' ? '.min' : ''%>.js"></script>
8282
<script src="https://lib.baomitu.com/vue-router/3.1.3/vue-router.min.js"></script>
83-
<!-- <script src="https://lib.baomitu.com/axios/0.19.2/axios.min.js"></script> -->
8483
<script src="https://lib.baomitu.com/element-ui/2.13.2/index.js"></script>
8584
</head>
8685

public/preview.html

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<link href="https://lib.baomitu.com/element-ui/2.13.2/theme-chalk/index.css" rel="stylesheet">
1010
<script src="https://lib.baomitu.com/vue/2.6.11/vue.min.js"></script>
1111
<script src="https://lib.baomitu.com/vue-router/3.1.3/vue-router.min.js"></script>
12-
<!-- <script src="https://lib.baomitu.com/axios/0.19.2/axios.min.js"></script> -->
1312
<script src="https://lib.baomitu.com/element-ui/2.13.2/index.js"></script>
1413
<style>
1514
body{

src/components/generator/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export const selectComponents = [
184184
{
185185
__config__: {
186186
label: '级联选择',
187+
url: 'https://www.fastmock.site/mock/f8d7a54fb1e60561e2f720d5a810009d/fg/cascaderList',
188+
method: 'get',
189+
dataKey: 'list',
187190
showLabel: true,
188191
labelWidth: null,
189192
tag: 'el-cascader',

src/components/generator/js.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export function makeUpJs(formConfig, type) {
2626
const propsList = []
2727
const methodList = mixinMethod(type)
2828
const uploadVarList = []
29+
const created = []
2930

3031
formConfig.fields.forEach(el => {
31-
buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
32+
buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList, created)
3233
})
3334

3435
const script = buildexport(
@@ -39,14 +40,15 @@ export function makeUpJs(formConfig, type) {
3940
optionsList.join('\n'),
4041
uploadVarList.join('\n'),
4142
propsList.join('\n'),
42-
methodList.join('\n')
43+
methodList.join('\n'),
44+
created.join('\n')
4345
)
4446
confGlobal = null
4547
return script
4648
}
4749

4850
// 构建组件属性
49-
function buildAttributes(scheme, dataList, ruleList, optionsList, methodList, propsList, uploadVarList) {
51+
function buildAttributes(scheme, dataList, ruleList, optionsList, methodList, propsList, uploadVarList, created) {
5052
const config = scheme.__config__
5153
const slot = scheme.__slot__
5254
buildData(scheme, dataList)
@@ -58,7 +60,9 @@ function buildAttributes(scheme, dataList, ruleList, optionsList, methodList, pr
5860
if (config.dataType === 'dynamic') {
5961
const model = `${scheme.__vModel__}Options`
6062
const options = titleCase(model)
61-
buildOptionMethod(`get${options}`, model, methodList)
63+
const methodName = `get${options}`
64+
buildOptionMethod(methodName, model, methodList, scheme)
65+
callInCreated(methodName, created)
6266
}
6367
}
6468

@@ -83,11 +87,16 @@ function buildAttributes(scheme, dataList, ruleList, optionsList, methodList, pr
8387
// 构建子级组件属性
8488
if (config.children) {
8589
config.children.forEach(item => {
86-
buildAttributes(item, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
90+
buildAttributes(item, dataList, ruleList, optionsList, methodList, propsList, uploadVarList, created)
8791
})
8892
}
8993
}
9094

95+
// 在Created调用函数
96+
function callInCreated(methodName, created) {
97+
created.push(`this.${methodName}()`)
98+
}
99+
91100
// 混入处理函数
92101
function mixinMethod(type) {
93102
const list = []; const
@@ -214,16 +223,23 @@ function buildSubmitUpload(scheme) {
214223
return str
215224
}
216225

217-
function buildOptionMethod(methodName, model, methodList) {
226+
function buildOptionMethod(methodName, model, methodList, scheme) {
227+
const config = scheme.__config__
218228
const str = `${methodName}() {
219-
// TODO 发起请求获取数据
220-
this.${model}
229+
// 注意:this.$axios是通过Vue.prototype.$axios = axios挂载产生的
230+
this.$axios({
231+
method: '${config.method}',
232+
url: '${config.url}'
233+
}).then(resp => {
234+
var { data } = resp
235+
this.${model} = data.${config.dataKey}
236+
})
221237
},`
222238
methodList.push(str)
223239
}
224240

225241
// js整体拼接
226-
function buildexport(conf, type, data, rules, selectOptions, uploadVar, props, methods) {
242+
function buildexport(conf, type, data, rules, selectOptions, uploadVar, props, methods, created) {
227243
const str = `${exportDefault}{
228244
${inheritAttrs[type]}
229245
components: {},
@@ -243,7 +259,9 @@ function buildexport(conf, type, data, rules, selectOptions, uploadVar, props, m
243259
},
244260
computed: {},
245261
watch: {},
246-
created () {},
262+
created () {
263+
${created}
264+
},
247265
mounted () {},
248266
methods: {
249267
${methods}

src/utils/db.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const DRAWING_ITEMS = 'drawingItems'
2-
const DRAWING_ITEMS_VERSION = '1.1'
2+
const DRAWING_ITEMS_VERSION = '1.2'
33
const DRAWING_ITEMS_VERSION_KEY = 'DRAWING_ITEMS_VERSION'
44
const DRAWING_ID = 'idGlobal'
55
const TREE_NODE_ID = 'treeNodeId'

src/views/index/Home.vue

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<template>
32
<div class="container">
43
<div class="left-board">

src/views/index/RightPanel.vue

+22
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,28 @@
353353
</el-form-item>
354354

355355
<template v-if="activeData.__config__.dataType === 'dynamic'">
356+
<el-form-item label="接口地址">
357+
<el-input
358+
v-model="activeData.__config__.url"
359+
:title="activeData.__config__.url"
360+
placeholder="请输入接口地址"
361+
clearable
362+
>
363+
<el-select
364+
slot="prepend"
365+
v-model="activeData.__config__.method"
366+
:style="{width: '85px'}"
367+
>
368+
<el-option label="get" value="get" />
369+
<el-option label="post" value="post" />
370+
<el-option label="put" value="put" />
371+
<el-option label="delete" value="delete" />
372+
</el-select>
373+
</el-input>
374+
</el-form-item>
375+
<el-form-item label="数据位置">
376+
<el-input v-model="activeData.__config__.dataKey" placeholder="请输入标签键名" />
377+
</el-form-item>
356378
<el-form-item label="标签键名">
357379
<el-input v-model="activeData.props.props.label" placeholder="请输入标签键名" />
358380
</el-form-item>

src/views/index/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import App from './App.vue'
33
import router from '@/router'
44
import '@/styles/index.scss'
55
import '@/icons'
6+
import axios from 'axios'
67
import Tinymce from '@/components/tinymce/index.vue'
78

89
Vue.component('tinymce', Tinymce)
910

1011
Vue.config.productionTip = false
12+
Vue.prototype.$axios = axios
1113

1214
new Vue({
1315
router,

src/views/preview/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Vue from 'vue'
22
import { loadScriptQueue } from '@/utils/loadScript'
3+
import axios from 'axios'
34
import Tinymce from '@/components/tinymce/index.vue'
45

56
Vue.component('tinymce', Tinymce)
7+
Vue.prototype.$axios = axios
68

79
const $previewApp = document.getElementById('previewApp')
810
const childAttrs = {

vue.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ module.exports = {
4343
externals: {
4444
vue: 'Vue',
4545
'vue-router': 'VueRouter',
46-
axios: 'axios',
4746
'element-ui': 'ELEMENT'
4847
}
4948
},

0 commit comments

Comments
 (0)