Skip to content

Commit

Permalink
refactor: 公告类型适配字典数据
Browse files Browse the repository at this point in the history
1.新增 <dict-tag> 自定义组件,用于回显字典标签
2.重构 useDict 方法,支持查询字典数据
3.优化部分字典相关数据类型
  • Loading branch information
Charles7c committed Sep 17, 2023
1 parent d5c5bcf commit 3a3a5d6
Show file tree
Hide file tree
Showing 30 changed files with 224 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import io.swagger.v3.oas.annotations.media.Schema;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
* 键值对信息
*
Expand All @@ -49,8 +51,21 @@ public class LabelValueVO<V> implements Serializable {
@Schema(description = "值", example = "1")
private V value;

/**
* 颜色
*/
@Schema(description = "颜色", example = "#165DFF")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String color;

public LabelValueVO(String label, V value) {
this.label = label;
this.value = value;
}

public LabelValueVO(String label, V value, String color) {
this.label = label;
this.value = value;
this.color = color;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package top.charles7c.cnadmin.system.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.system.model.entity.DictItemDO;

/**
Expand All @@ -25,4 +30,14 @@
* @author Charles7c
* @since 2023/9/11 21:29
*/
public interface DictItemMapper extends BaseMapper<DictItemDO> {}
public interface DictItemMapper extends BaseMapper<DictItemDO> {

/**
* 根据字典编码查询
*
* @param dictCode
* 字典编码
* @return 字典项列表
*/
List<LabelValueVO> listByDictCode(@Param("dictCode") String dictCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.baomidou.mybatisplus.annotation.TableName;

import top.charles7c.cnadmin.common.base.BaseDO;
import top.charles7c.cnadmin.system.enums.AnnouncementTypeEnum;

/**
* 公告实体
Expand All @@ -50,7 +49,7 @@ public class AnnouncementDO extends BaseDO {
/**
* 类型
*/
private AnnouncementTypeEnum type;
private String type;

/**
* 生效时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.hibernate.validator.constraints.Length;

import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.system.enums.AnnouncementTypeEnum;

/**
* 创建或修改公告信息
Expand Down Expand Up @@ -57,11 +56,11 @@ public class AnnouncementRequest extends BaseRequest {
private String content;

/**
* 类型
* 类型(取值于字典 announcement_type)
*/
@Schema(description = "类型", type = "Integer", allowableValues = {"1", "2", "3"}, example = "1")
@NotNull(message = "类型非法")
private AnnouncementTypeEnum type;
@Schema(description = "类型(取值于字典 announcement_type)", example = "1")
@NotBlank(message = "类型不能为空")
private String type;

/**
* 生效时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.alibaba.excel.annotation.ExcelProperty;

import top.charles7c.cnadmin.common.base.BaseDetailVO;
import top.charles7c.cnadmin.common.config.easyexcel.ExcelBaseEnumConverter;
import top.charles7c.cnadmin.system.enums.AnnouncementTypeEnum;

/**
* 公告详情信息
Expand Down Expand Up @@ -57,11 +55,11 @@ public class AnnouncementDetailVO extends BaseDetailVO {
private String content;

/**
* 类型
* 类型(取值于字典 announcement_type)
*/
@Schema(description = "类型", type = "Integer", allowableValues = {"1", "2", "3"}, example = "1")
@ExcelProperty(value = "类型", converter = ExcelBaseEnumConverter.class)
private AnnouncementTypeEnum type;
@Schema(description = "类型(取值于字典 announcement_type)", example = "1")
@ExcelProperty(value = "类型")
private String type;

/**
* 生效时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.swagger.v3.oas.annotations.media.Schema;

import top.charles7c.cnadmin.common.base.BaseVO;
import top.charles7c.cnadmin.system.enums.AnnouncementTypeEnum;

/**
* 公告信息
Expand All @@ -44,10 +43,10 @@ public class AnnouncementVO extends BaseVO {
private String title;

/**
* 类型
* 类型(取值于字典 announcement_type)
*/
@Schema(description = "类型", type = "Integer", allowableValues = {"1", "2", "3"}, example = "1")
private AnnouncementTypeEnum type;
@Schema(description = "类型(取值于字典 announcement_type)", example = "1")
private String type;

/**
* 生效时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;

import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.system.model.query.DictItemQuery;
import top.charles7c.cnadmin.system.model.request.DictItemRequest;
import top.charles7c.cnadmin.system.model.vo.DictItemDetailVO;
Expand All @@ -41,6 +42,15 @@ public interface DictItemService extends BaseService<DictItemVO, DictItemDetailV
*/
List<DictItemDetailVO> listByDictId(Long dictId);

/**
* 根据字典编码查询
*
* @param dictCode
* 字典编码
* @return 字典项列表
*/
List<LabelValueVO> listByDictCode(String dictCode);

/**
* 根据字典 ID 列表删除
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import top.charles7c.cnadmin.common.base.BaseServiceImpl;
import top.charles7c.cnadmin.common.model.query.SortQuery;
import top.charles7c.cnadmin.common.model.vo.LabelValueVO;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.DictItemMapper;
import top.charles7c.cnadmin.system.model.entity.DictItemDO;
Expand Down Expand Up @@ -73,6 +74,11 @@ public List<DictItemDetailVO> listByDictId(Long dictId) {
return detailList;
}

@Override
public List<LabelValueVO> listByDictCode(String dictCode) {
return baseMapper.listByDictCode(dictCode);
}

@Override
public void deleteByDictIds(List<Long> dictIds) {
baseMapper.lambdaUpdate().in(DictItemDO::getDictId, dictIds).remove();
Expand Down
11 changes: 11 additions & 0 deletions continew-admin-system/src/main/resources/mapper/DictItemMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http:https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.charles7c.cnadmin.system.mapper.DictItemMapper">
<select id="listByDictCode" resultType="top.charles7c.cnadmin.common.model.vo.LabelValueVO">
SELECT t1.`label`, t1.`value`, t1.`color`
FROM `sys_dict_item` AS t1
LEFT JOIN `sys_dict` AS t2 ON t1.`dict_id` = t2.`id`
WHERE t2.`code` = #{dictCode}
ORDER BY t1.`sort` ASC
</select>
</mapper>
1 change: 1 addition & 0 deletions continew-admin-ui/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ module.exports = {
'no-param-reassign': 0,
'prefer-regex-literals': 0,
'import/no-extraneous-dependencies': 0,
'camelcase': 'off',
},
};
14 changes: 10 additions & 4 deletions continew-admin-ui/src/api/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { ListParam as RoleParam } from '@/api/system/role';
import { TreeNodeData } from '@arco-design/web-vue';
import { LabelValueState } from '@/store/modules/dict/types';

const BASE_URL = '/common';

export function listDeptTree(params: DeptParam) {
return axios.get<TreeNodeData[]>('/common/tree/dept', {
return axios.get<TreeNodeData[]>(`${BASE_URL}/tree/dept`, {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);
Expand All @@ -16,7 +18,7 @@ export function listDeptTree(params: DeptParam) {
}

export function listMenuTree(params: MenuParam) {
return axios.get<TreeNodeData[]>('/common/tree/menu', {
return axios.get<TreeNodeData[]>(`${BASE_URL}/tree/menu`, {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);
Expand All @@ -25,7 +27,7 @@ export function listMenuTree(params: MenuParam) {
}

export function listRoleDict(params: RoleParam) {
return axios.get<LabelValueState[]>('/common/dict/role', {
return axios.get<LabelValueState[]>(`${BASE_URL}/dict/role`, {
params,
paramsSerializer: (obj) => {
return qs.stringify(obj);
Expand All @@ -34,5 +36,9 @@ export function listRoleDict(params: RoleParam) {
}

export function listEnumDict(enumTypeName: string) {
return axios.get<LabelValueState[]>(`/common/dict/enum/${enumTypeName}`);
return axios.get<LabelValueState[]>(`${BASE_URL}/dict/enum/${enumTypeName}`);
}

export function listDict(code: string) {
return axios.get<LabelValueState[]>(`${BASE_URL}/dict/${code}`);
}
4 changes: 2 additions & 2 deletions continew-admin-ui/src/api/system/announcement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface DataRecord {
id?: string;
title?: string;
content?: string;
status?: string;
status?: number;
type?: string;
effectiveTime?: string;
terminateTime?: string;
Expand All @@ -21,7 +21,7 @@ export interface DataRecord {

export interface ListParam {
title?: string;
status?: string;
status?: number;
type?: string;
page?: number;
size?: number;
Expand Down
50 changes: 50 additions & 0 deletions continew-admin-ui/src/components/dict-tag/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<template>
<span v-if="!dictItem"></span>
<span v-else-if="!dictItem.color">{{ dictItem.label }}</span>
<a-tag v-else-if="dictItem.color === 'primary'" color="arcoblue">{{
dictItem.label
}}</a-tag>
<a-tag v-else-if="dictItem.color === 'success'" color="green">{{
dictItem.label
}}</a-tag>
<a-tag v-else-if="dictItem.color === 'warning'" color="orangered">{{
dictItem.label
}}</a-tag>
<a-tag v-else-if="dictItem.color === 'error'" color="red">{{
dictItem.label
}}</a-tag>
<a-tag v-else-if="dictItem.color === 'default'" color="gray">{{
dictItem.label
}}</a-tag>
<a-tag v-else :color="dictItem.color">{{ dictItem.label }}</a-tag>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import { LabelValueState } from '@/store/modules/dict/types';
const props = defineProps({
dict: {
type: Array<LabelValueState>,
required: true,
},
value: {
type: [Number, String],
required: true,
},
});
const dictItem = computed(() =>
props.dict.find(
(d) => d.value === String(props.value) || d.value === Number(props.value)
)
);
</script>

<script lang="ts">
export default {
name: 'DictTag',
};
</script>

<style scoped lang="less"></style>
2 changes: 2 additions & 0 deletions continew-admin-ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import Chart from './chart/index.vue';
import Breadcrumb from './breadcrumb/index.vue';
import DateRangePicker from './date-range-picker/index.vue';
import DictTag from './dict-tag/index.vue';
import RightToolbar from './right-toolbar/index.vue';
import SvgIcon from './svg-icon/index.vue';
import IconSelect from './icon-select/index.vue';
Expand Down Expand Up @@ -41,6 +42,7 @@ export default {
Vue.component('Chart', Chart);
Vue.component('Breadcrumb', Breadcrumb);
Vue.component('DateRangePicker', DateRangePicker);
Vue.component('DictTag', DictTag);
Vue.component('RightToolbar', RightToolbar);
Vue.component('SvgIcon', SvgIcon);
Vue.component('IconSelect', IconSelect);
Expand Down
1 change: 1 addition & 0 deletions continew-admin-ui/src/store/modules/dict/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface LabelValueState {
label: string;
value: any;
color?: string;
}

export interface DictState {
Expand Down
18 changes: 13 additions & 5 deletions continew-admin-ui/src/utils/dict.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { ref, toRefs } from 'vue';
import { listEnumDict } from '@/api/common';
import { listEnumDict, listDict } from '@/api/common';
import { useDictStore } from '@/store';

/**
* 获取字典数据
*
* @param names 字典名列表
* @param dicts 字典列表
*/
export default function useDict(...names: Array<string>) {
export default function useDict(
...dicts: Array<{ name: string; isEnum: boolean }>
) {
const res = ref<any>({});
return (() => {
names.forEach((name: string) => {
dicts.forEach((d) => {
const { name } = d;
res.value[name] = [];
const dict = useDictStore().getDict(name);
if (dict) {
res.value[name] = dict;
} else {
} else if (d.isEnum) {
listEnumDict(name).then((resp) => {
res.value[name] = resp.data;
useDictStore().setDict(name, res.value[name]);
});
} else {
listDict(name).then((resp) => {
res.value[name] = resp.data;
useDictStore().setDict(name, res.value[name]);
});
}
});
return toRefs(res.value);
Expand Down
Loading

0 comments on commit 3a3a5d6

Please sign in to comment.