mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 00:57:13 +08:00 
			
		
		
		
	refactor: 优化前端代码生成逻辑及部分 freemarker 模板
This commit is contained in:
		| @@ -259,11 +259,11 @@ public class GeneratorServiceImpl implements GeneratorService { | |||||||
|             } |             } | ||||||
|             // 1、生成 api 代码 |             // 1、生成 api 代码 | ||||||
|             // 例如:D:/continew-admin/continew-admin-ui |             // 例如:D:/continew-admin/continew-admin-ui | ||||||
|  |             genConfigMap.put("fieldConfigs", fieldConfigList); | ||||||
|             List<String> frontendSubPathList = StrUtil.split(frontendPath, "src"); |             List<String> frontendSubPathList = StrUtil.split(frontendPath, "src"); | ||||||
|             String frontendModulePath = frontendSubPathList.get(0); |             String frontendModulePath = frontendSubPathList.get(0); | ||||||
|             // 例如:D:/continew-admin/continew-admin-ui/src/api/tool/xxx.ts |             // 例如:D:/continew-admin/continew-admin-ui/src/api/tool/xxx.ts | ||||||
|             String moduleSimpleName = new File(frontendPath).getName(); |             File apiParentFile = FileUtil.file(frontendModulePath, "src", "api", apiModuleName); | ||||||
|             File apiParentFile = FileUtil.file(frontendModulePath, "src", "api", moduleSimpleName); |  | ||||||
|             String apiFileName = classNamePrefix.toLowerCase() + ".ts"; |             String apiFileName = classNamePrefix.toLowerCase() + ".ts"; | ||||||
|             File apiFile = new File(apiParentFile, apiFileName); |             File apiFile = new File(apiParentFile, apiFileName); | ||||||
|             if (apiFile.exists() && !isOverride) { |             if (apiFile.exists() && !isOverride) { | ||||||
| @@ -273,7 +273,8 @@ public class GeneratorServiceImpl implements GeneratorService { | |||||||
|             FileUtil.writeString(apiContent, apiFile, StandardCharsets.UTF_8); |             FileUtil.writeString(apiContent, apiFile, StandardCharsets.UTF_8); | ||||||
|             // 2、生成 view 代码 |             // 2、生成 view 代码 | ||||||
|             // 例如:D:/continew-admin/continew-admin-ui/src/views/tool/xxx/index.vue |             // 例如:D:/continew-admin/continew-admin-ui/src/views/tool/xxx/index.vue | ||||||
|             File indexFile = FileUtil.file(frontendPath, classNamePrefix, "index.vue"); |             File indexFile = | ||||||
|  |                 FileUtil.file(frontendPath, apiModuleName, StrUtil.lowerFirst(classNamePrefix), "index.vue"); | ||||||
|             if (indexFile.exists() && !isOverride) { |             if (indexFile.exists() && !isOverride) { | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|   | |||||||
| @@ -54,7 +54,7 @@ public class ${className} implements Serializable { | |||||||
|      */ |      */ | ||||||
|     @Schema(description = "${fieldConfig.comment}") |     @Schema(description = "${fieldConfig.comment}") | ||||||
|     @Query(type = QueryTypeEnum.${fieldConfig.queryType}) |     @Query(type = QueryTypeEnum.${fieldConfig.queryType}) | ||||||
|     <#if fieldConfig.queryType == 'IN' || fieldConfig.queryType == 'NOT_IN' || fieldConfig.queryType == 'BETWEEN'> |     <#if fieldConfig.queryType = 'IN' || fieldConfig.queryType = 'NOT_IN' || fieldConfig.queryType = 'BETWEEN'> | ||||||
|     private List<${fieldConfig.fieldType}> ${fieldConfig.fieldName}; |     private List<${fieldConfig.fieldType}> ${fieldConfig.fieldName}; | ||||||
|     <#else> |     <#else> | ||||||
|     private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; |     private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; | ||||||
|   | |||||||
| @@ -3,17 +3,17 @@ import qs from 'query-string'; | |||||||
|  |  | ||||||
| const BASE_URL = '/${apiModuleName}/${apiName}'; | const BASE_URL = '/${apiModuleName}/${apiName}'; | ||||||
|  |  | ||||||
| export interface ${classNamePrefix}Record { | export interface DataRecord { | ||||||
| <#if fieldConfigs??> | <#if fieldConfigs??> | ||||||
| <#list fieldConfigs as fieldConfig> | <#list fieldConfigs as fieldConfig> | ||||||
|   <#if fieldConfig.showInList> |   ${fieldConfig.fieldName}?: string; | ||||||
|   ${fieldConfig.fieldName}: string; |  | ||||||
|   </#if> |  | ||||||
| </#list> | </#list> | ||||||
|  |   createUserString?: string; | ||||||
|  |   updateUserString?: string; | ||||||
| </#if> | </#if> | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface ${classNamePrefix}Param { | export interface ListParam { | ||||||
| <#if fieldConfigs??> | <#if fieldConfigs??> | ||||||
| <#list fieldConfigs as fieldConfig> | <#list fieldConfigs as fieldConfig> | ||||||
|   <#if fieldConfig.showInQuery> |   <#if fieldConfig.showInQuery> | ||||||
| @@ -26,13 +26,13 @@ export interface ${classNamePrefix}Param { | |||||||
|   sort?: Array<string>; |   sort?: Array<string>; | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface ${classNamePrefix}ListRes { | export interface ListRes { | ||||||
|   list: ${classNamePrefix}Record[]; |   list: DataRecord[]; | ||||||
|   total: number; |   total: number; | ||||||
| } | } | ||||||
|  |  | ||||||
| export function list${classNamePrefix}(params: ${classNamePrefix}Param) { | export function list(params: ListParam) { | ||||||
|   return axios.get<${classNamePrefix}ListRes>(`${BASE_URL}`, { |   return axios.get<ListRes>(`${'$'}{BASE_URL}`, { | ||||||
|     params, |     params, | ||||||
|     paramsSerializer: (obj) => { |     paramsSerializer: (obj) => { | ||||||
|       return qs.stringify(obj); |       return qs.stringify(obj); | ||||||
| @@ -40,18 +40,18 @@ export function list${classNamePrefix}(params: ${classNamePrefix}Param) { | |||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| export function get${classNamePrefix}(id: string) { | export function get(id: string) { | ||||||
|   return axios.get<${classNamePrefix}Record>(`${BASE_URL}/${id}`); |   return axios.get<DataRecord>(`${'$'}{BASE_URL}/${'$'}{id}`); | ||||||
| } | } | ||||||
|  |  | ||||||
| export function add${classNamePrefix}(req: ${classNamePrefix}Record) { | export function add(req: DataRecord) { | ||||||
|   return axios.post(BASE_URL, req); |   return axios.post(BASE_URL, req); | ||||||
| } | } | ||||||
|  |  | ||||||
| export function update${classNamePrefix}(req: ${classNamePrefix}Record, id: string) { | export function update(req: DataRecord, id: string) { | ||||||
|   return axios.put(`${BASE_URL}/${id}`, req); |   return axios.put(`${'$'}{BASE_URL}/${'$'}{id}`, req); | ||||||
| } | } | ||||||
|  |  | ||||||
| export function delete${classNamePrefix}(ids: string | Array<string>) { | export function del(ids: string | Array<string>) { | ||||||
|   return axios.delete(`${BASE_URL}/${ids}`); |   return axios.delete(`${'$'}{BASE_URL}/${'$'}{ids}`); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -89,7 +89,7 @@ | |||||||
|       <a-table |       <a-table | ||||||
|         ref="tableRef" |         ref="tableRef" | ||||||
|         row-key="id" |         row-key="id" | ||||||
|         :data="${apiName}List" |         :data="dataList" | ||||||
|         :loading="loading" |         :loading="loading" | ||||||
|         :row-selection="{ |         :row-selection="{ | ||||||
|           type: 'checkbox', |           type: 'checkbox', | ||||||
| @@ -112,9 +112,17 @@ | |||||||
|       > |       > | ||||||
|         <template #columns> |         <template #columns> | ||||||
|           <#list fieldConfigs as fieldConfig> |           <#list fieldConfigs as fieldConfig> | ||||||
|             <#if fieldConfig.showInList> |           <#if fieldConfig_index = 0> | ||||||
|             <a-table-column title="${fieldConfig.comment}" data-index="${fieldConfig.fieldName}" /> |           <a-table-column title="${fieldConfig.comment}" data-index="${fieldConfig.fieldName}"> | ||||||
|             </#if> |             <template #cell="{ record }"> | ||||||
|  |               <a-link @click="toDetail(record.id)">{{ record.${fieldConfig.fieldName} }}</a-link> | ||||||
|  |             </template> | ||||||
|  |           </a-table-column> | ||||||
|  |           <#else> | ||||||
|  |           <#if fieldConfig.showInList> | ||||||
|  |           <a-table-column title="${fieldConfig.comment}" data-index="${fieldConfig.fieldName}" /> | ||||||
|  |           </#if> | ||||||
|  |           </#if> | ||||||
|           </#list> |           </#list> | ||||||
|           <a-table-column |           <a-table-column | ||||||
|             v-if="checkPermission(['${apiModuleName}:${apiName}:update', '${apiModuleName}:${apiName}:delete'])" |             v-if="checkPermission(['${apiModuleName}:${apiName}:update', '${apiModuleName}:${apiName}:delete'])" | ||||||
| @@ -166,9 +174,9 @@ | |||||||
|           <#list fieldConfigs as fieldConfig> |           <#list fieldConfigs as fieldConfig> | ||||||
|           <#if fieldConfig.showInForm> |           <#if fieldConfig.showInForm> | ||||||
|           <a-form-item label="${fieldConfig.comment}" field="${fieldConfig.fieldName}"> |           <a-form-item label="${fieldConfig.comment}" field="${fieldConfig.fieldName}"> | ||||||
|             <#if fieldConfig.formType == 'TEXT'> |             <#if fieldConfig.formType = 'TEXT'> | ||||||
|             <a-input v-model="form.${fieldConfig.fieldName}" placeholder="请输入${fieldConfig.comment}" /> |             <a-input v-model="form.${fieldConfig.fieldName}" placeholder="请输入${fieldConfig.comment}" /> | ||||||
|             <#elseif fieldConfig.formType == 'TEXT_AREA'> |             <#elseif fieldConfig.formType = 'TEXT_AREA'> | ||||||
|             <a-textarea |             <a-textarea | ||||||
|               v-model="form.${fieldConfig.fieldName}" |               v-model="form.${fieldConfig.fieldName}" | ||||||
|               :max-length="200" |               :max-length="200" | ||||||
| @@ -178,7 +186,7 @@ | |||||||
|               }" |               }" | ||||||
|               show-word-limit |               show-word-limit | ||||||
|             /> |             /> | ||||||
|             <#elseif fieldConfig.formType == 'SELECT'> |             <#elseif fieldConfig.formType = 'SELECT'> | ||||||
|               <#--<a-select |               <#--<a-select | ||||||
|                 v-model="form.${fieldConfig.fieldName}" |                 v-model="form.${fieldConfig.fieldName}" | ||||||
|                 :options="${apiName}Options" |                 :options="${apiName}Options" | ||||||
| @@ -189,12 +197,12 @@ | |||||||
|                 :allow-search="{ retainInputValue: true }" |                 :allow-search="{ retainInputValue: true }" | ||||||
|                 style="width: 416px" |                 style="width: 416px" | ||||||
|               />--> |               />--> | ||||||
|             <#elseif fieldConfig.formType == 'RADIO'> |             <#elseif fieldConfig.formType = 'RADIO'> | ||||||
|             <#--<a-radio-group v-model="form.${fieldConfig.fieldName}" type="button"> |             <#--<a-radio-group v-model="form.${fieldConfig.fieldName}" type="button"> | ||||||
|             </a-radio-group>--> |             </a-radio-group>--> | ||||||
|             <#elseif fieldConfig.formType == 'DATE'> |             <#elseif fieldConfig.formType = 'DATE'> | ||||||
|             <a-date-picker v-model="form.${fieldConfig.fieldName}" placeholder="请选择${fieldConfig.comment}"/> |             <a-date-picker v-model="form.${fieldConfig.fieldName}" placeholder="请选择${fieldConfig.comment}"/> | ||||||
|             <#elseif fieldConfig.formType == 'DATE_TIME'> |             <#elseif fieldConfig.formType = 'DATE_TIME'> | ||||||
|             <a-date-picker |             <a-date-picker | ||||||
|               v-model="form.${fieldConfig.fieldName}" |               v-model="form.${fieldConfig.fieldName}" | ||||||
|               placeholder="请选择${fieldConfig.comment}" |               placeholder="请选择${fieldConfig.comment}" | ||||||
| @@ -225,7 +233,13 @@ | |||||||
|             <a-skeleton v-if="detailLoading" :animation="true"> |             <a-skeleton v-if="detailLoading" :animation="true"> | ||||||
|               <a-skeleton-line :rows="1" /> |               <a-skeleton-line :rows="1" /> | ||||||
|             </a-skeleton> |             </a-skeleton> | ||||||
|             <span v-else>{{ ${apiName}.${fieldConfig.fieldName} }}</span> |             <#if fieldConfig.fieldName = 'createUser'> | ||||||
|  |             <span v-else>{{ detail.createUserString }}</span> | ||||||
|  |             <#elseif fieldConfig.fieldName = 'updateUser'> | ||||||
|  |             <span v-else>{{ detail.updateUserString }}</span> | ||||||
|  |             <#else> | ||||||
|  |             <span v-else>{{ detail.${fieldConfig.fieldName} }}</span> | ||||||
|  |             </#if> | ||||||
|           </a-descriptions-item> |           </a-descriptions-item> | ||||||
|           </#list> |           </#list> | ||||||
|         </a-descriptions> |         </a-descriptions> | ||||||
| @@ -237,22 +251,24 @@ | |||||||
| <script lang="ts" setup> | <script lang="ts" setup> | ||||||
|   import { getCurrentInstance, ref, toRefs, reactive } from 'vue'; |   import { getCurrentInstance, ref, toRefs, reactive } from 'vue'; | ||||||
|   import { |   import { | ||||||
|     ${classNamePrefix}Record, |     DataRecord, | ||||||
|     ${classNamePrefix}Param, |     ListParam, | ||||||
|     list${classNamePrefix}, |     list, | ||||||
|     get${classNamePrefix}, |     get, | ||||||
|     add${classNamePrefix}, |     add, | ||||||
|     update${classNamePrefix}, |     update, | ||||||
|     delete${classNamePrefix}, |     del, | ||||||
|   } from '@/api/${apiModuleName}/${apiName}'; |   } from '@/api/${apiModuleName}/${apiName}'; | ||||||
|   import checkPermission from '@/utils/permission'; |   import checkPermission from '@/utils/permission'; | ||||||
|  |  | ||||||
|   const { proxy } = getCurrentInstance() as any; |   const { proxy } = getCurrentInstance() as any; | ||||||
|   // const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum'); |   // const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum'); | ||||||
|  |  | ||||||
|   const ${apiName}List = ref<${classNamePrefix}Record[]>([]); |   const dataList = ref<DataRecord[]>([]); | ||||||
|   const ${apiName} = ref<${classNamePrefix}Record>({ |   const detail = ref<DataRecord>({ | ||||||
|  |     // TODO 待补充详情字段默认值 | ||||||
|   }); |   }); | ||||||
|  |   const total = ref(0); | ||||||
|   const ids = ref<Array<string>>([]); |   const ids = ref<Array<string>>([]); | ||||||
|   const title = ref(''); |   const title = ref(''); | ||||||
|   const single = ref(true); |   const single = ref(true); | ||||||
| @@ -272,10 +288,12 @@ | |||||||
|       ${fieldConfig.fieldName}: undefined, |       ${fieldConfig.fieldName}: undefined, | ||||||
|       </#if> |       </#if> | ||||||
|       </#list> |       </#list> | ||||||
|  |       page: 1, | ||||||
|  |       size: 10, | ||||||
|       sort: ['createTime,desc'], |       sort: ['createTime,desc'], | ||||||
|     }, |     }, | ||||||
|     // 表单数据 |     // 表单数据 | ||||||
|     form: {} as ${classNamePrefix}Record, |     form: {} as DataRecord, | ||||||
|     // 表单验证规则 |     // 表单验证规则 | ||||||
|     rules: { |     rules: { | ||||||
|       <#list fieldConfigs as fieldConfig> |       <#list fieldConfigs as fieldConfig> | ||||||
| @@ -292,11 +310,11 @@ | |||||||
|    * |    * | ||||||
|    * @param params 查询参数 |    * @param params 查询参数 | ||||||
|    */ |    */ | ||||||
|   const getList = (params: ${classNamePrefix}Param = { ...queryParams.value }) => { |   const getList = (params: ListParam = { ...queryParams.value }) => { | ||||||
|     loading.value = true; |     loading.value = true; | ||||||
|     list${classNamePrefix}(params) |     list(params) | ||||||
|       .then((res) => { |       .then((res) => { | ||||||
|         ${apiName}List.value = res.data.list; |         dataList.value = res.data.list; | ||||||
|         total.value = res.data.total; |         total.value = res.data.total; | ||||||
|       }) |       }) | ||||||
|       .finally(() => { |       .finally(() => { | ||||||
| @@ -321,7 +339,7 @@ | |||||||
|    */ |    */ | ||||||
|   const toUpdate = (id: string) => { |   const toUpdate = (id: string) => { | ||||||
|     reset(); |     reset(); | ||||||
|     get${classNamePrefix}(id).then((res) => { |     get(id).then((res) => { | ||||||
|       form.value = res.data; |       form.value = res.data; | ||||||
|       title.value = '修改${businessName}'; |       title.value = '修改${businessName}'; | ||||||
|       visible.value = true; |       visible.value = true; | ||||||
| @@ -333,6 +351,7 @@ | |||||||
|    */ |    */ | ||||||
|   const reset = () => { |   const reset = () => { | ||||||
|     form.value = { |     form.value = { | ||||||
|  |       // TODO 待补充需要重置的字段默认值,详情请参考其他模块 index.vue | ||||||
|     }; |     }; | ||||||
|     proxy.$refs.formRef?.resetFields(); |     proxy.$refs.formRef?.resetFields(); | ||||||
|   }; |   }; | ||||||
| @@ -352,13 +371,13 @@ | |||||||
|     proxy.$refs.formRef.validate((valid: any) => { |     proxy.$refs.formRef.validate((valid: any) => { | ||||||
|       if (!valid) { |       if (!valid) { | ||||||
|         if (form.value.id !== undefined) { |         if (form.value.id !== undefined) { | ||||||
|           update${classNamePrefix}(form.value, form.value.id).then((res) => { |           update(form.value, form.value.id).then((res) => { | ||||||
|             handleCancel(); |             handleCancel(); | ||||||
|             getList(); |             getList(); | ||||||
|             proxy.$message.success(res.msg); |             proxy.$message.success(res.msg); | ||||||
|           }); |           }); | ||||||
|         } else { |         } else { | ||||||
|           add${classNamePrefix}(form.value).then((res) => { |           add(form.value).then((res) => { | ||||||
|             handleCancel(); |             handleCancel(); | ||||||
|             getList(); |             getList(); | ||||||
|             proxy.$message.success(res.msg); |             proxy.$message.success(res.msg); | ||||||
| @@ -377,9 +396,9 @@ | |||||||
|     if (detailLoading.value) return; |     if (detailLoading.value) return; | ||||||
|     detailLoading.value = true; |     detailLoading.value = true; | ||||||
|     detailVisible.value = true; |     detailVisible.value = true; | ||||||
|     get${classNamePrefix}(id) |     get(id) | ||||||
|       .then((res) => { |       .then((res) => { | ||||||
|         ${apiName}.value = res.data; |         detail.value = res.data; | ||||||
|       }) |       }) | ||||||
|       .finally(() => { |       .finally(() => { | ||||||
|         detailLoading.value = false; |         detailLoading.value = false; | ||||||
| @@ -418,7 +437,7 @@ | |||||||
|    * @param ids ID 列表 |    * @param ids ID 列表 | ||||||
|    */ |    */ | ||||||
|   const handleDelete = (ids: Array<string>) => { |   const handleDelete = (ids: Array<string>) => { | ||||||
|     delete${classNamePrefix}(ids).then((res) => { |     del(ids).then((res) => { | ||||||
|       proxy.$message.success(res.msg); |       proxy.$message.success(res.msg); | ||||||
|       getList(); |       getList(); | ||||||
|     }); |     }); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user