mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-02-07 19:15:23 +08:00
前端和后端源码,合并到一个git仓库中,方便用户下载,避免前后端不匹配的问题
This commit is contained in:
65
jeecgboot-vue3/src/utils/dict/DictColors.js
Normal file
65
jeecgboot-vue3/src/utils/dict/DictColors.js
Normal file
@ -0,0 +1,65 @@
|
||||
const whiteColor = '#ffffff'
|
||||
const blackColor = '#666666'
|
||||
|
||||
export const Colors = [
|
||||
// 背景颜色,文字颜色
|
||||
['#2196F3', whiteColor],
|
||||
['#08C9C9', whiteColor],
|
||||
['#00C345', whiteColor],
|
||||
['#FAD714', whiteColor],
|
||||
['#FF9300', whiteColor],
|
||||
['#F52222', whiteColor],
|
||||
['#EB2F96', whiteColor],
|
||||
['#7500EA', whiteColor],
|
||||
['#2D46C4', whiteColor],
|
||||
['#484848', whiteColor],
|
||||
// --------------------
|
||||
['#C9E6FC', blackColor],
|
||||
['#C3F2F2', blackColor],
|
||||
['#C2F1D2', blackColor],
|
||||
['#FEF6C6', blackColor],
|
||||
['#FFE5C2', blackColor],
|
||||
['#FDCACA', blackColor],
|
||||
['#FACDE6', blackColor],
|
||||
['#DEC2FA', blackColor],
|
||||
['#CCD2F1', blackColor],
|
||||
['#D3D3D3', blackColor],
|
||||
]
|
||||
|
||||
export const NONE_COLOR = ['#e9e9e9', blackColor]
|
||||
|
||||
/**
|
||||
* 返回一个颜色迭代器,每次调用返回一个颜色,当颜色用完后,再从头开始
|
||||
* @param {number} initIndex 初始颜色索引
|
||||
* @returns {{getIndex: function, next: function}}
|
||||
*/
|
||||
export function getColorIterator(initIndex = 0) {
|
||||
let index = initIndex;
|
||||
if (index < 0 || index >= Colors.length) {
|
||||
index = 0;
|
||||
}
|
||||
return {
|
||||
getIndex: () => index,
|
||||
next() {
|
||||
const color = Colors[index];
|
||||
index = (index + 1) % Colors.length;
|
||||
return color;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据颜色获取当前坐标和颜色
|
||||
*/
|
||||
export function getItemColor(color) {
|
||||
if(!color){
|
||||
return NONE_COLOR[1];
|
||||
}
|
||||
let colorIndex = Colors.findIndex((value)=>{
|
||||
return value[0] === color;
|
||||
})
|
||||
if(colorIndex === -1){
|
||||
return NONE_COLOR[1];
|
||||
}
|
||||
return Colors[colorIndex][1];
|
||||
}
|
||||
161
jeecgboot-vue3/src/utils/dict/JDictSelectUtil.js
Normal file
161
jeecgboot-vue3/src/utils/dict/JDictSelectUtil.js
Normal file
@ -0,0 +1,161 @@
|
||||
/**
|
||||
* 字典 util
|
||||
* author: scott
|
||||
* date: 20190109
|
||||
*/
|
||||
|
||||
import { ajaxGetDictItems, getDictItemsByCode } from './index';
|
||||
|
||||
/**
|
||||
* 获取字典数组
|
||||
* 【目前仅表单设计器页面使用该方法】
|
||||
* @param dictCode 字典Code
|
||||
* @param isTransformResponse 是否转换返回结果
|
||||
* @return List<Map>
|
||||
*/
|
||||
export async function initDictOptions(dictCode, isTransformResponse = true) {
|
||||
if (!dictCode) {
|
||||
return '字典Code不能为空!';
|
||||
}
|
||||
//优先从缓存中读取字典配置
|
||||
if (getDictItemsByCode(dictCode)) {
|
||||
let res = {};
|
||||
res.result = getDictItemsByCode(dictCode);
|
||||
res.success = true;
|
||||
if (isTransformResponse) {
|
||||
return res.result;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
//获取字典数组
|
||||
return await ajaxGetDictItems(dictCode, {}, { isTransformResponse });
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典值替换文本通用方法
|
||||
* @param dictOptions 字典数组
|
||||
* @param text 字典值
|
||||
* @return String
|
||||
*/
|
||||
export function filterDictText(dictOptions, text) {
|
||||
// --update-begin----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
|
||||
if (text != null && Array.isArray(dictOptions)) {
|
||||
let result = [];
|
||||
// 允许多个逗号分隔,允许传数组对象
|
||||
let splitText;
|
||||
if (Array.isArray(text)) {
|
||||
splitText = text;
|
||||
} else {
|
||||
splitText = text.toString().trim().split(',');
|
||||
}
|
||||
for (let txt of splitText) {
|
||||
let dictText = txt;
|
||||
for (let dictItem of dictOptions) {
|
||||
// update-begin--author:liaozhiyang---date:20240524---for:【TV360X-469】兼容数据null值防止报错
|
||||
if (dictItem == null) break;
|
||||
// update-end--author:liaozhiyang---date:20240524---for:【TV360X-469】兼容数据null值防止报错
|
||||
if (txt.toString() === dictItem.value.toString()) {
|
||||
dictText = dictItem.text || dictItem.title || dictItem.label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
result.push(dictText);
|
||||
}
|
||||
return result.join(',');
|
||||
}
|
||||
return text;
|
||||
// --update-end----author:sunjianlei---date:20200323------for: 字典翻译 text 允许逗号分隔 ---
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典值替换文本通用方法(多选)
|
||||
* @param dictOptions 字典数组
|
||||
* @param text 字典值
|
||||
* @return String
|
||||
*/
|
||||
export function filterMultiDictText(dictOptions, text) {
|
||||
//js “!text” 认为0为空,所以做提前处理
|
||||
if (text === 0 || text === '0') {
|
||||
if (dictOptions) {
|
||||
for (let dictItem of dictOptions) {
|
||||
if (text == dictItem.value) {
|
||||
return dictItem.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!text || text == 'undefined' || text == 'null' || !dictOptions || dictOptions.length == 0) {
|
||||
return '';
|
||||
}
|
||||
let re = '';
|
||||
text = text.toString();
|
||||
let arr = text.split(',');
|
||||
dictOptions.forEach(function (option) {
|
||||
if (option) {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] === option.value) {
|
||||
re += option.text + ',';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (re == '') {
|
||||
return text;
|
||||
}
|
||||
return re.substring(0, re.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 翻译字段值对应的文本
|
||||
* @param children
|
||||
* @returns string
|
||||
*/
|
||||
export function filterDictTextByCache(dictCode, key) {
|
||||
if (key == null || key.length == 0) {
|
||||
return;
|
||||
}
|
||||
if (!dictCode) {
|
||||
return '字典Code不能为空!';
|
||||
}
|
||||
//优先从缓存中读取字典配置
|
||||
if (getDictItemsByCode(dictCode)) {
|
||||
let item = getDictItemsByCode(dictCode).filter((t) => t['value'] == key);
|
||||
if (item && item.length > 0) {
|
||||
return item[0]['text'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 通过code获取字典数组 */
|
||||
export async function getDictItems(dictCode, params) {
|
||||
// update-begin--author:liaozhiyang---date:20230809---for:【issues/668】JDictSelectUtil数据字典工具类中的getDictItems方法出错
|
||||
//优先从缓存中读取字典配置
|
||||
if (getDictItemsByCode(dictCode)) {
|
||||
let desformDictItems = getDictItemsByCode(dictCode).map((item) => ({
|
||||
...item,
|
||||
label: item.text,
|
||||
}));
|
||||
return Promise.resolve(desformDictItems);
|
||||
}
|
||||
|
||||
//缓存中没有,就请求后台
|
||||
return await ajaxGetDictItems(dictCode, params)
|
||||
.then((result) => {
|
||||
if (result.length) {
|
||||
let res = result.map((item) => ({ ...item, label: item.text }));
|
||||
console.log('------- 从DB中获取到了字典-------dictCode : ', dictCode, res);
|
||||
return Promise.resolve(res);
|
||||
} else {
|
||||
console.error('getDictItems error: : ', res);
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
})
|
||||
.catch((res) => {
|
||||
console.error('getDictItems error: ', res);
|
||||
return Promise.resolve([]);
|
||||
});
|
||||
// update-end--author:liaozhiyang---date:20230809---for:【issues/668】JDictSelectUtil数据字典工具类中的getDictItems方法出错
|
||||
}
|
||||
55
jeecgboot-vue3/src/utils/dict/index.ts
Normal file
55
jeecgboot-vue3/src/utils/dict/index.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { defHttp } from '/@/utils/http/axios';
|
||||
import { useUserStore } from '/@/store/modules/user';
|
||||
import { getAuthCache } from '/@/utils/auth';
|
||||
import { DB_DICT_DATA_KEY } from '/@/enums/cacheEnum';
|
||||
|
||||
/**
|
||||
* 从缓存中获取字典配置
|
||||
* @param code
|
||||
*/
|
||||
export const getDictItemsByCode = (code) => {
|
||||
// update-begin--author:liaozhiyang---date:20230908---for:【QQYUN-6417】生产环境字典慢的问题
|
||||
const userStore = useUserStore();
|
||||
const dictItems = userStore.getAllDictItems;
|
||||
if (null != dictItems && typeof dictItems === 'object' && dictItems[code]) {
|
||||
return dictItems[code];
|
||||
}
|
||||
//update-begin-author:liusq---date:2023-10-13--for: 【issues/777】列表 分类字典不显示
|
||||
//兼容以前的旧写法
|
||||
if (getAuthCache(DB_DICT_DATA_KEY) && getAuthCache(DB_DICT_DATA_KEY)[code]) {
|
||||
return getAuthCache(DB_DICT_DATA_KEY)[code];
|
||||
}
|
||||
//update-end-author:liusq---date:2023-10-13--for:【issues/777】列表 分类字典不显示
|
||||
|
||||
// update-end--author:liaozhiyang---date:20230908---for:【QQYUN-6417】生产环境字典慢的问题
|
||||
|
||||
};
|
||||
/**
|
||||
* 获取字典数组
|
||||
* @param dictCode 字典Code
|
||||
* @return List<Map>
|
||||
*/
|
||||
export const initDictOptions = (code) => {
|
||||
//1.优先从缓存中读取字典配置
|
||||
if (getDictItemsByCode(code)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(getDictItemsByCode(code));
|
||||
});
|
||||
}
|
||||
//2.获取字典数组
|
||||
//update-begin-author:taoyan date:2022-6-21 for: 字典数据请求前将参数编码处理,但是不能直接编码,因为可能之前已经编码过了
|
||||
if (code.indexOf(',') > 0 && code.indexOf(' ') > 0) {
|
||||
// 编码后类似sys_user%20where%20username%20like%20xxx' 是不包含空格的,这里判断如果有空格和逗号说明需要编码处理
|
||||
code = encodeURI(code);
|
||||
}
|
||||
//update-end-author:taoyan date:2022-6-21 for: 字典数据请求前将参数编码处理,但是不能直接编码,因为可能之前已经编码过了
|
||||
return defHttp.get({ url: `/sys/dict/getDictItems/${code}` });
|
||||
};
|
||||
/**
|
||||
* 获取字典数组
|
||||
* @param code 字典Code
|
||||
* @param params 查询参数
|
||||
* @param options 查询配置
|
||||
* @return List<Map>
|
||||
*/
|
||||
export const ajaxGetDictItems = (code, params, options?) => defHttp.get({ url: `/sys/dict/getDictItems/${code}`, params }, options);
|
||||
Reference in New Issue
Block a user