提交 a79b3e0e 编写于 作者: 查尔斯-BUG万象集's avatar 查尔斯-BUG万象集

新增:新增公共查询枚举字典 API,优化前端获取枚举数据的方式

上级 8200ea82
......@@ -64,6 +64,11 @@ public class ContiNewAdminProperties {
*/
private String url;
/**
* 基本包
*/
private String basePackage;
/**
* 作者信息
*/
......
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import top.charles7c.cnadmin.common.base.BaseEnum;
/**
* 成功/失败状态枚举
*
* @author Charles7c
* @since 2023/2/26 21:35
*/
@Getter
@RequiredArgsConstructor
public enum SuccessFailureStatusEnum implements BaseEnum<Integer, String> {
/** 成功 */
SUCCESS(1, "成功"),
/** 失败 */
FAILURE(2, "失败"),;
private final Integer value;
private final String description;
}
......@@ -10,7 +10,6 @@ declare module '@vue/runtime-core' {
AAffix: typeof import('@arco-design/web-vue')['Affix']
AAlert: typeof import('@arco-design/web-vue')['Alert']
AAvatar: typeof import('@arco-design/web-vue')['Avatar']
AAvatarGroup: typeof import('@arco-design/web-vue')['AvatarGroup']
ABadge: typeof import('@arco-design/web-vue')['Badge']
ABreadcrumb: typeof import('@arco-design/web-vue')['Breadcrumb']
ABreadcrumbItem: typeof import('@arco-design/web-vue')['BreadcrumbItem']
......@@ -20,7 +19,6 @@ declare module '@vue/runtime-core' {
ACardMeta: typeof import('@arco-design/web-vue')['CardMeta']
ACarousel: typeof import('@arco-design/web-vue')['Carousel']
ACarouselItem: typeof import('@arco-design/web-vue')['CarouselItem']
ACascader: typeof import('@arco-design/web-vue')['Cascader']
ACheckbox: typeof import('@arco-design/web-vue')['Checkbox']
ACol: typeof import('@arco-design/web-vue')['Col']
AConfigProvider: typeof import('@arco-design/web-vue')['ConfigProvider']
......@@ -60,7 +58,6 @@ declare module '@vue/runtime-core' {
ASelect: typeof import('@arco-design/web-vue')['Select']
ASkeleton: typeof import('@arco-design/web-vue')['Skeleton']
ASkeletonLine: typeof import('@arco-design/web-vue')['SkeletonLine']
ASkeletonShape: typeof import('@arco-design/web-vue')['SkeletonShape']
ASpace: typeof import('@arco-design/web-vue')['Space']
ASpin: typeof import('@arco-design/web-vue')['Spin']
AStatistic: typeof import('@arco-design/web-vue')['Statistic']
......
......@@ -5,11 +5,7 @@ import { MenuParam } from '@/api/system/menu';
import { RoleParam } from '@/api/system/role';
import { PostParam } from '@/api/system/post';
import { TreeNodeData } from '@arco-design/web-vue';
export interface LabelValueRecord {
label: string;
value: any;
}
import { LabelValueState } from '@/store/modules/dict/types';
export function listDeptTree(params: DeptParam) {
return axios.get<TreeNodeData[]>('/common/tree/dept', {
......@@ -30,7 +26,7 @@ export function listMenuTree(params: MenuParam) {
}
export function listRoleDict(params: RoleParam) {
return axios.get<LabelValueRecord[]>('/common/dict/role', {
return axios.get<LabelValueState[]>('/common/dict/role', {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);
......@@ -39,10 +35,14 @@ export function listRoleDict(params: RoleParam) {
}
export function listPostDict(params: PostParam) {
return axios.get<LabelValueRecord[]>('/common/dict/post', {
return axios.get<LabelValueState[]>('/common/dict/post', {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);
},
});
}
\ No newline at end of file
}
export function listEnumDict(enumTypeName: string) {
return axios.get<LabelValueState[]>(`/common/dict/enum/${enumTypeName}`);
}
......@@ -4,6 +4,7 @@ import ArcoVueIcon from '@arco-design/web-vue/es/icon';
// eslint-disable-next-line import/no-unresolved
import 'virtual:svg-icons-register';
import globalComponents from '@/components';
import useDict from '@/utils/dict';
import router from './router';
import store from './store';
import i18n from './locale';
......@@ -16,6 +17,9 @@ import '@/utils/request';
const app = createApp(App);
// 全局方法挂载
app.config.globalProperties.useDict = useDict;
app.use(ArcoVue, {});
app.use(ArcoVueIcon);
......
import { createPinia } from 'pinia';
import useAppStore from './modules/app';
import useLoginStore from './modules/login';
import useDictStore from './modules/dict';
import useTabBarStore from './modules/tab-bar';
const pinia = createPinia();
export { useAppStore, useLoginStore, useTabBarStore };
export { useAppStore, useLoginStore, useDictStore, useTabBarStore };
export default pinia;
import { defineStore } from 'pinia';
import { DictState, LabelValueState } from '@/store/modules/dict/types';
const useDictStore = defineStore('dict', {
state: () => ({ dict: [] as Array<DictState> }),
actions: {
// 获取字典
getDict(_name: string) {
if (_name === null && _name === '') {
return null;
}
try {
for (let i = 0; i < this.dict.length; i += 1) {
if (this.dict[i].name === _name) {
return this.dict[i].detail;
}
}
} catch (e) {
console.log(e);
}
return null;
},
// 设置字典
setDict(_name: string, detail: Array<LabelValueState>) {
if (_name !== null && _name !== '') {
this.dict.push({
name: _name,
detail,
});
}
},
// 删除字典
deleteDict(_name: string) {
let bln = false;
try {
for (let i = 0; i < this.dict.length; i += 1) {
if (this.dict[i].name === _name) {
this.dict.splice(i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
return bln;
},
// 清空字典
cleanDict() {
this.dict = [];
},
},
});
export default useDictStore;
export interface LabelValueState {
label: string;
value: any;
}
export interface DictState {
name: string;
detail: Array<LabelValueState>;
}
import { ref, toRefs } from 'vue';
import { listEnumDict } from '@/api/common';
import { useDictStore } from '@/store';
/**
* 获取字典数据
*
* @param names 字典名列表
*/
export default function useDict(...names: Array<string>) {
const res = ref<any>({});
return (() => {
names.forEach((name: string) => {
res.value[name] = [];
const dict = useDictStore().getDict(name);
if (dict) {
res.value[name] = dict;
} else {
listEnumDict(name).then((resp) => {
res.value[name] = resp.data;
useDictStore().setDict(name, res.value[name]);
});
}
});
return toRefs(res.value);
})();
}
......@@ -10,10 +10,10 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="SuccessFailureStatusEnum"
placeholder="登录状态搜索"
allow-clear
style="width: 150px;"
style="width: 150px"
/>
</a-form-item>
<a-form-item field="createTime" hide-label>
......@@ -81,7 +81,6 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import { SelectOptionData } from '@arco-design/web-vue';
import {
LoginLogParam,
LoginLogRecord,
......@@ -89,14 +88,11 @@
} from '@/api/monitor/log';
const { proxy } = getCurrentInstance() as any;
const { SuccessFailureStatusEnum } = proxy.useDict('SuccessFailureStatusEnum');
const loginLogList = ref<LoginLogRecord[]>([]);
const total = ref(0);
const loading = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '成功', value: 1 },
{ label: '失败', value: 2 },
]);
const data = reactive({
// 查询参数
......
......@@ -19,7 +19,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="SuccessFailureStatusEnum"
placeholder="操作状态搜索"
allow-clear
style="width: 150px"
......@@ -91,7 +91,6 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import { SelectOptionData } from '@arco-design/web-vue';
import {
OperationLogParam,
OperationLogRecord,
......@@ -99,14 +98,11 @@
} from '@/api/monitor/log';
const { proxy } = getCurrentInstance() as any;
const { SuccessFailureStatusEnum } = proxy.useDict('SuccessFailureStatusEnum');
const operationLogList = ref<OperationLogRecord[]>([]);
const total = ref(0);
const loading = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '成功', value: 1 },
{ label: '失败', value: 2 },
]);
const data = reactive({
// 查询参数
......
......@@ -19,7 +19,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="DisEnableStatusEnum"
placeholder="状态搜索"
allow-clear
style="width: 150px"
......@@ -282,11 +282,7 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import {
SelectOptionData,
TreeNodeData,
TableData,
} from '@arco-design/web-vue';
import { TreeNodeData, TableData } from '@arco-design/web-vue';
import {
DeptRecord,
DeptParam,
......@@ -299,6 +295,7 @@
import { listDeptTree } from '@/api/common';
const { proxy } = getCurrentInstance() as any;
const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum');
const deptList = ref<DeptRecord[]>([]);
const dept = ref<DeptRecord>({
......@@ -322,10 +319,6 @@
const exportLoading = ref(false);
const visible = ref(false);
const detailVisible = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '启用', value: 1 },
{ label: '禁用', value: 2 },
]);
const treeData = ref<TreeNodeData[]>();
const data = reactive({
......
......@@ -19,7 +19,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="DisEnableStatusEnum"
placeholder="状态搜索"
allow-clear
style="width: 150px"
......@@ -335,11 +335,7 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import {
SelectOptionData,
TreeNodeData,
TableData,
} from '@arco-design/web-vue';
import { TreeNodeData, TableData } from '@arco-design/web-vue';
import {
MenuRecord,
MenuParam,
......@@ -352,6 +348,7 @@
import { listMenuTree } from '@/api/common';
const { proxy } = getCurrentInstance() as any;
const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum');
const menuList = ref<MenuRecord[]>([]);
const ids = ref<Array<number>>([]);
......@@ -364,10 +361,6 @@
const expandAll = ref(false);
const visible = ref(false);
const showChooseIcon = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '启用', value: 1 },
{ label: '禁用', value: 2 },
]);
const treeData = ref<TreeNodeData[]>();
const data = reactive({
......
......@@ -19,7 +19,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="DisEnableStatusEnum"
placeholder="状态搜索"
allow-clear
style="width: 150px"
......@@ -267,7 +267,6 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import { SelectOptionData } from '@arco-design/web-vue';
import {
PostRecord,
PostParam,
......@@ -279,6 +278,7 @@
} from '@/api/system/post';
const { proxy } = getCurrentInstance() as any;
const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum');
const postList = ref<PostRecord[]>([]);
const post = ref<PostRecord>({
......@@ -301,10 +301,6 @@
const exportLoading = ref(false);
const visible = ref(false);
const detailVisible = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '启用', value: 1 },
{ label: '禁用', value: 2 },
]);
const data = reactive({
// 查询参数
......
......@@ -19,7 +19,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="DisEnableStatusEnum"
placeholder="状态搜索"
allow-clear
style="width: 150px"
......@@ -244,7 +244,7 @@
<a-form-item label="数据权限" field="dataScope">
<a-select
v-model="form.dataScope"
:options="dataScopeOptions"
:options="DataScopeEnum"
placeholder="请选择数据权限"
/>
</a-form-item>
......@@ -378,7 +378,7 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
import { SelectOptionData, TreeNodeData } from '@arco-design/web-vue';
import { TreeNodeData } from '@arco-design/web-vue';
import {
RoleRecord,
RoleParam,
......@@ -391,6 +391,7 @@
import { listMenuTree, listDeptTree } from '@/api/common';
const { proxy } = getCurrentInstance() as any;
const { DataScopeEnum, DisEnableStatusEnum } = proxy.useDict('DataScopeEnum', 'DisEnableStatusEnum');
const roleList = ref<RoleRecord[]>([]);
const role = ref<RoleRecord>({
......@@ -417,17 +418,6 @@
const exportLoading = ref(false);
const visible = ref(false);
const detailVisible = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '启用', value: 1 },
{ label: '禁用', value: 2 },
]);
const dataScopeOptions = ref<SelectOptionData[]>([
{ label: '全部数据权限', value: 1 },
{ label: '本部门及以下数据权限', value: 2 },
{ label: '本部门数据权限', value: 3 },
{ label: '仅本人数据权限', value: 4 },
{ label: '自定义数据权限', value: 5 },
]);
const menuLoading = ref(false);
const deptLoading = ref(false);
const menuOptions = ref<TreeNodeData[]>([]);
......
......@@ -36,7 +36,7 @@
<a-form-item field="status" hide-label>
<a-select
v-model="queryParams.status"
:options="statusOptions"
:options="DisEnableStatusEnum"
placeholder="状态搜索"
allow-clear
style="width: 150px"
......@@ -484,7 +484,7 @@
<script lang="ts" setup>
import { getCurrentInstance, ref, toRefs, reactive, watch } from 'vue';
import { SelectOptionData, TreeNodeData } from '@arco-design/web-vue';
import { TreeNodeData } from '@arco-design/web-vue';
import {
UserRecord,
UserParam,
......@@ -496,15 +496,12 @@
resetPassword,
updateUserRole,
} from '@/api/system/user';
import {
LabelValueRecord,
listDeptTree,
listPostDict,
listRoleDict,
} from '@/api/common';
import { listDeptTree, listPostDict, listRoleDict } from '@/api/common';
import { LabelValueState } from '@/store/modules/dict/types';
import getAvatar from '@/utils/avatar';
const { proxy } = getCurrentInstance() as any;
const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum');
const userList = ref<UserRecord[]>([]);
const user = ref<UserRecord>({
......@@ -535,16 +532,12 @@
const visible = ref(false);
const userRoleVisible = ref(false);
const detailVisible = ref(false);
const statusOptions = ref<SelectOptionData[]>([
{ label: '启用', value: 1 },
{ label: '禁用', value: 2 },
]);
const deptLoading = ref(false);
const postLoading = ref(false);
const roleLoading = ref(false);
const deptOptions = ref<TreeNodeData[]>([]);
const postOptions = ref<LabelValueRecord[]>([]);
const roleOptions = ref<LabelValueRecord[]>([]);
const postOptions = ref<LabelValueState[]>([]);
const roleOptions = ref<LabelValueState[]>([]);
const deptTree = ref<TreeNodeData[]>([]);
const deptName = ref('');
......
......@@ -16,7 +16,8 @@
package top.charles7c.cnadmin.webapi.controller.common;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
......@@ -24,12 +25,13 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ClassUtil;
import top.charles7c.cnadmin.common.base.BaseEnum;
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
import top.charles7c.cnadmin.common.model.query.SortQuery;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.common.model.vo.R;
......@@ -55,6 +57,7 @@ import top.charles7c.cnadmin.system.service.RoleService;
*/
@Tag(name = "公共 API")
@Log(ignore = true)
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
......@@ -64,6 +67,7 @@ public class CommonController {
private final MenuService menuService;
private final RoleService roleService;
private final PostService postService;
private final ContiNewAdminProperties properties;
@Operation(summary = "查询部门树", description = "查询树结构的部门列表")
@GetMapping("/tree/dept")
......@@ -96,4 +100,24 @@ public class CommonController {
List<LabelValueVO<Long>> dictList = postService.buildDict(list);
return R.ok(dictList);
}
@Operation(summary = "查询枚举字典", description = "查询枚举字典列表")
@GetMapping("/dict/enum/{enumTypeName}")
public R<List<LabelValueVO>> listEnumDict(@PathVariable String enumTypeName) {
// 扫描所有 BaseEnum 枚举基类的子类
Set<Class<?>> classSet = ClassUtil.scanPackageBySuper(properties.getBasePackage(), BaseEnum.class);
Optional<Class<?>> first =
classSet.stream().filter(c -> c.getSimpleName().equalsIgnoreCase(enumTypeName)).findFirst();
if (!first.isPresent()) {
return R.fail("枚举字典不存在");
}
// 转换枚举为字典列表
Class<?> enumClass = first.get();
Object[] enumConstants = enumClass.getEnumConstants();
List<LabelValueVO> dictList = Arrays.stream(enumConstants).map(e -> {
BaseEnum<Integer, String> baseEnum = (BaseEnum<Integer, String>)e;
return new LabelValueVO(baseEnum.getDescription(), baseEnum.getValue());
}).collect(Collectors.toList());
return R.ok(dictList);
}
}
......@@ -10,6 +10,8 @@ continew-admin:
description: ContiNew Admin 中后台管理框架(孵化中),Continue New Admin,持续以最新流行技术栈构建。
# URL
url: https://github.com/Charles7c/continew-admin
# 基本包
basePackage: top.charles7c.cnadmin
## 作者信息配置
author:
name: Charles7c
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册