重构:重构查询角色树 API 为查询角色字典列表

1.新增 LabelValueVO 通用字典数据结构
This commit is contained in:
Charles7c 2023-02-24 22:24:25 +08:00
parent 7cf56202d8
commit 91165e63e5
6 changed files with 74 additions and 26 deletions

View File

@ -0,0 +1,54 @@
/*
* 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.model.vo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 键值对信息
*
* @param <V>
* @author Charles7c
* @since 2023/2/24 22:02
*/
@Data
@NoArgsConstructor
@Accessors(chain = true)
@Schema(description = "键值对信息")
public class LabelValueVO<V> {
/**
* 标签
*/
@Schema(description = "标签")
private String label;
/**
*
*/
@Schema(description = "")
private V value;
public LabelValueVO(String label, V value) {
this.label = label;
this.value = value;
}
}

View File

@ -18,9 +18,8 @@ package top.charles7c.cnadmin.system.service;
import java.util.List;
import cn.hutool.core.lang.tree.Tree;
import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.system.model.query.RoleQuery;
import top.charles7c.cnadmin.system.model.request.RoleRequest;
import top.charles7c.cnadmin.system.model.vo.RoleDetailVO;
@ -35,13 +34,13 @@ import top.charles7c.cnadmin.system.model.vo.RoleVO;
public interface RoleService extends BaseService<RoleVO, RoleDetailVO, RoleQuery, RoleRequest> {
/**
* 构建
* 构建字典
*
* @param list
* 原始列表数据
* @return 列表
* @return 字典列表
*/
List<Tree<Long>> buildTree(List<RoleVO> list);
List<LabelValueVO<Long>> buildDict(List<RoleVO> list);
/**
* 根据角色 ID 列表查询

View File

@ -28,12 +28,11 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
import top.charles7c.cnadmin.common.consts.Constants;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.util.TreeUtils;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.RoleMapper;
import top.charles7c.cnadmin.system.model.entity.RoleDO;
@ -132,12 +131,11 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
}
@Override
public List<Tree<Long>> buildTree(List<RoleVO> list) {
return TreeUtils.build(list, (r, tree) -> {
tree.setId(r.getRoleId());
tree.setName(r.getRoleName());
tree.setWeight(r.getRoleSort());
});
public List<LabelValueVO<Long>> buildDict(List<RoleVO> list) {
if (CollUtil.isEmpty(list)) {
return Collections.emptyList();
}
return list.stream().map(r -> new LabelValueVO<>(r.getRoleName(), r.getRoleId())).collect(Collectors.toList());
}
@Override

View File

@ -23,8 +23,8 @@ export function listMenuTree(params: MenuParam) {
});
}
export function listRoleTree(params: RoleParam) {
return axios.get<TreeNodeData[]>('/common/tree/role', {
export function listRoleDict(params: RoleParam) {
return axios.get<TreeNodeData[]>('/common/dict/role', {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);

View File

@ -274,10 +274,6 @@
<a-select
v-model="form.roleIds"
:options="roleOptions"
:field-names="{
label: 'title',
value: 'key',
}"
placeholder="请选择所属角色"
:loading="roleLoading"
multiple
@ -425,7 +421,7 @@
updateUser,
deleteUser,
} from '@/api/system/user';
import { listRoleTree, listDeptTree } from '@/api/common';
import { listRoleDict, listDeptTree } from '@/api/common';
import getAvatar from '@/utils/avatar';
const { proxy } = getCurrentInstance() as any;
@ -559,7 +555,7 @@
*/
const getRoleOptions = () => {
roleLoading.value = true;
listRoleTree({})
listRoleDict({})
.then((res) => {
roleOptions.value = res.data;
})

View File

@ -31,6 +31,7 @@ import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.lang.tree.Tree;
import top.charles7c.cnadmin.common.model.query.SortQuery;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.common.model.vo.R;
import top.charles7c.cnadmin.monitor.annotation.Log;
import top.charles7c.cnadmin.system.model.query.DeptQuery;
@ -78,11 +79,11 @@ public class CommonController {
}
@Log(ignore = true)
@Operation(summary = "查询角色", description = "查询树结构的角色列表")
@GetMapping("/tree/role")
public R<List<Tree<Long>>> listRoleTree(@Validated RoleQuery query, @Validated SortQuery sortQuery) {
@Operation(summary = "查询角色字典", description = "查询角色字典列表")
@GetMapping("/dict/role")
public R<List<LabelValueVO<Long>>> listRoleDict(@Validated RoleQuery query, @Validated SortQuery sortQuery) {
List<RoleVO> list = roleService.list(query, sortQuery);
List<Tree<Long>> treeList = roleService.buildTree(list);
return R.ok(treeList);
List<LabelValueVO<Long>> dictList = roleService.buildDict(list);
return R.ok(dictList);
}
}