inleft
2022-08-23 124ca536917b5eebe059bb98745f6743c52a6836
commit | author | age
9bcb19 1 <template>
I 2   <a-modal
3     title="新增代码生成配置"
4     :width="900"
5     :visible="visible"
6     :confirmLoading="confirmLoading"
7     @ok="handleSubmit"
8     @cancel="handleCancel"
9   >
10     <a-spin :spinning="confirmLoading">
11       <a-form :form="form">
12         <a-row :gutter="24">
13           <a-col :md="12" :sm="24">
14             <a-form-item
15               :labelCol="labelCol"
16               :wrapperCol="wrapperCol"
17               label="生成表"
18               has-feedback
19             >
20               <a-select style="width: 100%" placeholder="请选择数据库表" v-decorator="['tableName', {rules: [{ required: true, message: '请选择数据库表!' }]}]" >
21                 <a-select-option v-for="(item,index) in tableNameData" :key="index" :value="item.tableName" @click="tableNameSele(item)">{{ item.tableName }}</a-select-option>
22               </a-select>
23             </a-form-item>
24           </a-col>
25           <a-col :md="12" :sm="24">
26             <a-form-item
27               :labelCol="labelCol"
28               :wrapperCol="wrapperCol"
29               label="移除前缀"
30             >
31               <a-radio-group v-decorator="['tablePrefix',{rules: [{ required: true, message: '请选择是否移除前缀!' }]}]" >
32                 <a-radio v-for="(item,index) in tablePrefixData" :key="index" :value="item.code" @click="tablePrefixRadio(item.code)">{{ item.name }}</a-radio>
33               </a-radio-group>
34             </a-form-item>
35           </a-col>
36         </a-row>
37         <a-row :gutter="24">
38           <a-col :md="12" :sm="24">
39             <a-form-item
40               label="功能名"
41               :labelCol="labelCol"
42               :wrapperCol="wrapperCol"
43               has-feedback
44             >
45               <a-input placeholder="请输入功能名" v-decorator="['tableComment', {rules: [{required: true, message: '请输入功能名!'}]}]" />
46             </a-form-item>
47           </a-col>
48           <a-col :md="12" :sm="24">
49             <a-form-item
50               label="类名"
51               :labelCol="labelCol"
52               :wrapperCol="wrapperCol"
53               has-feedback
54             >
55               <a-input placeholder="请输入类名" v-decorator="['className', {rules: [{required: true, message: '请输入类名!'}]}]" />
56             </a-form-item>
57           </a-col>
58         </a-row>
59         <a-row :gutter="24">
60           <a-col :md="12" :sm="24">
61             <a-form-item
62               label="业务名"
63               :labelCol="labelCol"
64               :wrapperCol="wrapperCol"
65               has-feedback
66             >
67               <a-input placeholder="请输入业务名" v-decorator="['busName', {rules: [{required: true, message: '请输入业务名!'}]}]" />
68             </a-form-item>
69           </a-col>
70           <a-col :md="12" :sm="24">
71             <a-form-item
72               :labelCol="labelCol"
73               :wrapperCol="wrapperCol"
74               label="生成方式"
75             >
76               <a-radio-group v-decorator="['generateType',{rules: [{ required: true, message: '请选择生成方式!' }]}]" >
77                 <a-radio v-for="(item,index) in generateTypeData" :key="index" :value="item.code" @click="generateTypeRadio(item.code)">{{ item.name }}</a-radio>
78               </a-radio-group>
79             </a-form-item>
80           </a-col>
81         </a-row>
82         <a-row :gutter="24">
83           <a-col :md="12" :sm="24">
84             <a-form-item
85               :labelCol="labelCol"
86               :wrapperCol="wrapperCol"
87               label="所属应用"
88               has-feedback
89             >
90               <a-select style="width: 100%" placeholder="请选择应用分类" v-decorator="['appCode', {rules: [{ required: true, message: '请选择应用分类!' }]}]" >
91                 <a-select-option v-for="(item,index) in appData" :key="index" :value="item.code" @click="changeApplication(item.code)">{{ item.name }}</a-select-option>
92               </a-select>
93             </a-form-item>
94           </a-col>
95           <a-col :md="12" :sm="24">
96             <a-form-item
97               :labelCol="labelCol"
98               :wrapperCol="wrapperCol"
99               label="父级菜单"
100               has-feedback
101             >
102               <a-tree-select
103                 v-decorator="['menuPid', {rules: [{ required: true, message: '请选择父级菜单!' }]}]"
104                 style="width: 100%"
105                 :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
106                 :treeData="menuTreeData"
107                 placeholder="请选择父级菜单"
108                 treeDefaultExpandAll
109               >
110                 <span slot="title" slot-scope="{ id }">{{ id }}
111                 </span>
112               </a-tree-select>
113             </a-form-item>
114           </a-col>
115         </a-row>
116         <a-row :gutter="24">
117           <a-col :md="12" :sm="24">
118             <a-form-item
119               label="作者姓名"
120               :labelCol="labelCol"
121               :wrapperCol="wrapperCol"
122               has-feedback
123             >
124               <a-input placeholder="请输入作者姓名" v-decorator="['authorName', {rules: [{required: true, message: '请输入作者姓名!'}]}]" />
125             </a-form-item>
126           </a-col>
127           <a-col :md="12" :sm="24" v-show="packageNameShow">
128             <a-form-item
129               label="代码包名"
130               :labelCol="labelCol"
131               :wrapperCol="wrapperCol"
132               has-feedback
133             >
134               <a-input placeholder="请输入代码包名" v-decorator="['packageName', {rules: [{required: true, message: '请输入代码包名!'}]}]" />
135             </a-form-item>
136           </a-col>
137         </a-row>
138       </a-form>
139     </a-spin>
140   </a-modal>
141 </template>
142
143 <script>
144   import { codeGenerateInformationList, codeGenerateAdd } from '@/api/modular/gen/codeGenerateManage'
145   import { getAppList } from '@/api/modular/system/appManage'
146   import { getMenuTree } from '@/api/modular/system/menuManage'
147   export default {
148     data () {
149       return {
150         labelCol: {
151           xs: { span: 24 },
152           sm: { span: 5 }
153         },
154         wrapperCol: {
155           xs: { span: 24 },
156           sm: { span: 15 }
157         },
158         visible: false,
159         tableNameData: [],
160         tablePrefixData: [],
161         generateTypeData: [],
162         confirmLoading: false,
163         tablePrefixValue: 'N',
164         tableNameValue: '',
165         packageNameShow: true,
166         appData: [],
167         menuTreeData: [],
168         form: this.$form.createForm(this)
169       }
170     },
171     methods: {
172       // 初始化方法
173       add () {
174         this.visible = true
175         this.codeGenerateInformationList()
176         this.dataTypeItem()
177         this.selectedByDefault()
178         this.getSysApplist()
179       },
180       /**
181        * 默认选中项
182        */
183       selectedByDefault () {
184         this.form.getFieldDecorator('packageName', { initialValue: 'vip.xiaonuo' })
185         this.form.getFieldDecorator('tablePrefix', { valuePropName: 'checked', initialValue: 'N' })
186         this.form.getFieldDecorator('generateType', { valuePropName: 'checked', initialValue: '1' })
187         this.tablePrefixValue = 'N'
188       },
189       /**
190        * 获得所有数据库的表
191        */
192       codeGenerateInformationList () {
193         codeGenerateInformationList().then((res) => {
194           this.tableNameData = res.data
195         })
196       },
197       /**
198        * 获取应用列表
199        */
200       getSysApplist () {
201         return getAppList().then((res) => {
202           if (res.success) {
203             this.appData = res.data
204           } else {
205             this.$message.warning(res.message)
206           }
207         })
208       },
209       /**
210        * 通过应用获取菜单
211        */
212       changeApplication (value) {
213         this.form.resetFields(`menuPid`, [])
214         getMenuTree({ 'application': value }).then((res) => {
215           if (res.success) {
216             this.menuTreeData = [{
217               'id': '-1',
218               'parentId': '0',
219               'title': '顶级',
220               'value': '0',
221               'pid': '0',
222               'children': res.data
223             }]
224           } else {
225             this.$message.warning(res.message)
226           }
227         })
228       },
229       /**
230        * 获取字典数据
231        */
232       dataTypeItem () {
233         this.tablePrefixData = this.$options.filters['dictData']('yes_or_no')
234         this.generateTypeData = this.$options.filters['dictData']('code_gen_create_type')
235       },
236       /**
237        * 提交表单
238        */
239       handleSubmit () {
240         const { form: { validateFields } } = this
241         validateFields((errors, values) => {
242           if (!errors) {
243             this.confirmLoading = true
244             codeGenerateAdd(values).then((res) => {
245               if (res.success) {
246                 this.$message.success('新增成功')
247                 this.$emit('ok', values)
248                 this.handleCancel()
249               } else {
250                 this.$message.error('新增失败:' + res.message)
251               }
252             }).finally((res) => {
253               this.confirmLoading = false
254             })
255           }
256         })
257       },
258       handleCancel () {
259         this.form.resetFields()
260         this.visible = false
261         // 清空他们三个
262         this.form.getFieldDecorator('className', { initialValue: '' })
263         this.form.getFieldDecorator('busName', { initialValue: '' })
264         this.form.getFieldDecorator('tableComment', { initialValue: '' })
265       },
266       /**
267        * 选择数据库列表
268        */
269       tableNameSele (item) {
270         this.tableNameValue = item.tableName
271         this.form.getFieldDecorator('tableComment', { initialValue: item.tableComment })
272         this.settingDefaultValue()
273       },
274       /**
275        * 选择是否移除前缀触发
276        */
277       tablePrefixRadio (tablePrefixType) {
278         this.tablePrefixValue = tablePrefixType
279         this.settingDefaultValue()
280       },
281       /**
282        * 设置默认值
283        */
284       settingDefaultValue () {
285         const tableName = this.classNameToHump()
286         this.form.getFieldDecorator('className', { initialValue: tableName })
287         this.form.getFieldDecorator('busName', { initialValue: tableName.toLowerCase() })
288       },
289       /**
290        * 设置类名为数据库表的驼峰命名
291        */
292       classNameToHump () {
293         const arr = this.tableNameValue.toLowerCase().split('_')
294         if (this.tablePrefixValue === 'Y') {
295           arr.splice(0, 1)
296         }
297         for (let i = 0; i < arr.length; i++) {
298           // charAt()方法得到第一个字母,slice()得到第二个字母以后的字符串
299           arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1)
300         }
301         return arr.join('')
302       },
303       /**
304        * 选择生成方式
305        */
306       generateTypeRadio (generateType) {
307         if (generateType === '1') {
308           this.packageNameShow = true
309         } else {
310           this.packageNameShow = false
311           this.form.setFieldsValue({ packageName: 'vip.xiaonuo' })
312         }
313       }
314     }
315   }
316 </script>