init
This commit is contained in:
11
src/utils/api-types.ts
Normal file
11
src/utils/api-types.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* 与 `src/utils/request` 拦截器一致:resolve 的是若依风格 JSON `{ code, msg, data }`。
|
||||
* axios 1.13 + TS6 下避免从 `axios` 拉已移除的 AxiosPromise。
|
||||
*/
|
||||
export interface RuoYiAjaxResult<T = unknown> {
|
||||
code?: number;
|
||||
msg?: string;
|
||||
data?: T;
|
||||
}
|
||||
|
||||
export type AxiosPromise<T = unknown> = Promise<RuoYiAjaxResult<T>>;
|
||||
9
src/utils/auth.ts
Normal file
9
src/utils/auth.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
const TokenKey = 'Admin-Token';
|
||||
|
||||
const tokenStorage = useStorage<null | string>(TokenKey, null);
|
||||
|
||||
export const getToken = () => tokenStorage.value;
|
||||
|
||||
export const setToken = (access_token: string) => (tokenStorage.value = access_token);
|
||||
|
||||
export const removeToken = () => (tokenStorage.value = null);
|
||||
42
src/utils/createCustomNameComponent.tsx
Normal file
42
src/utils/createCustomNameComponent.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 后台返回的路由动态生成name 解决缓存问题
|
||||
* 感谢 @fourteendp
|
||||
* 详见 https://github.com/vbenjs/vue-vben-admin/issues/3927
|
||||
*/
|
||||
import { Component, defineComponent, h } from 'vue';
|
||||
|
||||
interface Options {
|
||||
name?: string;
|
||||
}
|
||||
|
||||
export function createCustomNameComponent(loader: () => Promise<any>, options: Options = {}): () => Promise<Component> {
|
||||
const { name } = options;
|
||||
let component: Component | null = null;
|
||||
let wrappedComponent: Component | null = null;
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const { default: loadedComponent } = await loader();
|
||||
component = loadedComponent;
|
||||
} catch (error) {
|
||||
console.error(`Cannot resolve component ${name}, error:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
return async () => {
|
||||
if (!component) {
|
||||
await load();
|
||||
}
|
||||
|
||||
if (!wrappedComponent) {
|
||||
wrappedComponent = defineComponent({
|
||||
name,
|
||||
render() {
|
||||
return h(component as Component);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.resolve(wrappedComponent);
|
||||
};
|
||||
}
|
||||
78
src/utils/crypto.ts
Normal file
78
src/utils/crypto.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { ecb } from '@noble/ciphers/aes.js';
|
||||
|
||||
/**
|
||||
* 随机生成32位的字符串
|
||||
* @returns {string}
|
||||
*/
|
||||
const generateRandomString = (): string => {
|
||||
const array = new Uint8Array(32);
|
||||
crypto.getRandomValues(array);
|
||||
return Array.from(array, b => b.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
.slice(0, 32);
|
||||
};
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
const bytesToBase64 = (bytes: Uint8Array): string => {
|
||||
let binary = '';
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCharCode(byte);
|
||||
}
|
||||
return btoa(binary);
|
||||
};
|
||||
|
||||
const base64ToBytes = (str: string): Uint8Array => {
|
||||
const binary = atob(str);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
bytes[i] = binary.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
|
||||
/**
|
||||
* 随机生成aes 密钥
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
export const generateAesKey = (): Uint8Array => {
|
||||
return encoder.encode(generateRandomString());
|
||||
};
|
||||
|
||||
/**
|
||||
* 加密base64
|
||||
* @returns {string}
|
||||
*/
|
||||
export const encryptBase64 = (bytes: Uint8Array): string => {
|
||||
return bytesToBase64(bytes);
|
||||
};
|
||||
|
||||
/**
|
||||
* 解密base64
|
||||
*/
|
||||
export const decryptBase64 = (str: string): Uint8Array => {
|
||||
return base64ToBytes(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* 使用密钥对数据进行加密 (AES/ECB/PKCS7Padding 与后端保持一致)
|
||||
* @param message
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
export const encryptWithAes = (message: string, aesKey: Uint8Array): string => {
|
||||
const encrypted = ecb(aesKey).encrypt(encoder.encode(message));
|
||||
return bytesToBase64(encrypted);
|
||||
};
|
||||
|
||||
/**
|
||||
* 使用密钥对数据进行解密
|
||||
* @param message
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
export const decryptWithAes = (message: string, aesKey: Uint8Array): string => {
|
||||
const decrypted = ecb(aesKey).decrypt(base64ToBytes(message));
|
||||
return decoder.decode(decrypted);
|
||||
};
|
||||
39
src/utils/dict.ts
Normal file
39
src/utils/dict.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { getDicts } from '@/api/system/dict/data';
|
||||
import { useDictStore } from '@/store/modules/dict';
|
||||
|
||||
const pendingRequests = new Map<string, Promise<DictDataOption[]>>();
|
||||
|
||||
/**
|
||||
* 获取字典数据
|
||||
*/
|
||||
export const useDict = (...args: string[]): { [key: string]: DictDataOption[] } => {
|
||||
const res = reactive<{ [key: string]: DictDataOption[] }>({});
|
||||
|
||||
args.forEach(async dictType => {
|
||||
res[dictType] = [];
|
||||
const dicts = useDictStore().getDict(dictType);
|
||||
if (dicts) {
|
||||
res[dictType] = dicts;
|
||||
} else {
|
||||
if (!pendingRequests.has(dictType)) {
|
||||
const request = getDicts(dictType)
|
||||
.then(resp => {
|
||||
const data = resp.data.map(
|
||||
(p): DictDataOption => ({
|
||||
label: p.dictLabel,
|
||||
value: p.dictValue,
|
||||
elTagType: p.listClass,
|
||||
elTagClass: p.cssClass
|
||||
})
|
||||
);
|
||||
useDictStore().setDict(dictType, data);
|
||||
return data;
|
||||
})
|
||||
.finally(() => pendingRequests.delete(dictType));
|
||||
pendingRequests.set(dictType, request);
|
||||
}
|
||||
res[dictType] = await pendingRequests.get(dictType)!;
|
||||
}
|
||||
});
|
||||
return res;
|
||||
};
|
||||
14
src/utils/dynamicTitle.ts
Normal file
14
src/utils/dynamicTitle.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import defaultSettings from '@/settings';
|
||||
import { useSettingsStore } from '@/store/modules/settings';
|
||||
|
||||
/**
|
||||
* 动态修改标题
|
||||
*/
|
||||
export const useDynamicTitle = () => {
|
||||
const settingsStore = useSettingsStore();
|
||||
if (settingsStore.dynamicTitle) {
|
||||
document.title = settingsStore.title + ' - ' + import.meta.env.VITE_APP_TITLE;
|
||||
} else {
|
||||
document.title = defaultSettings.title as string;
|
||||
}
|
||||
};
|
||||
7
src/utils/errorCode.ts
Normal file
7
src/utils/errorCode.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const errorCode: any = {
|
||||
'401': '认证失败,无法访问系统资源',
|
||||
'403': '当前操作没有权限',
|
||||
'404': '访问资源不存在',
|
||||
default: '系统未知错误,请反馈给管理员'
|
||||
};
|
||||
export default errorCode;
|
||||
125
src/utils/geolocation.ts
Normal file
125
src/utils/geolocation.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
// utils/geolocation.ts
|
||||
|
||||
/**
|
||||
* 定位配置参数
|
||||
* @interface GeoOptions
|
||||
*/
|
||||
interface GeoOptions {
|
||||
/** 是否开启高精度模式,一般会调用设备GPS,耗电更高、速度可能变慢 */
|
||||
enableHighAccuracy?: boolean;
|
||||
/** 定位超时时间,单位毫秒 */
|
||||
timeout?: number;
|
||||
/** 复用缓存位置的最大有效期,单位毫秒;0代表不使用缓存,每次都重新获取 */
|
||||
maximumAge?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定位成功返回的数据结构
|
||||
* @interface GeoResult
|
||||
*/
|
||||
export interface GeoResult {
|
||||
/** 纬度 */
|
||||
lat: number;
|
||||
/** 经度 */
|
||||
lng: number;
|
||||
/** 定位精度,单位米,数值越小越精准 */
|
||||
accuracy: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览器单次当前定位,Promise封装
|
||||
* @param options 定位配置项,可选
|
||||
* @returns Promise<GeoResult> 定位信息
|
||||
* @description 线上环境必须HTTPS,localhost本地调试不受限制;需要用户授权定位权限
|
||||
*/
|
||||
export function getCurrentPosition(
|
||||
options: GeoOptions = {},
|
||||
): Promise<GeoResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 判断当前环境是否支持浏览器原生定位API
|
||||
if (!navigator?.geolocation) {
|
||||
return reject(new Error("当前浏览器不支持定位"));
|
||||
}
|
||||
|
||||
// 合并默认配置与传入配置,PositionOptions是TS内置原生类型
|
||||
const geoOpt: PositionOptions = {
|
||||
timeout: 5000,
|
||||
enableHighAccuracy: true, // ✅ 关键:开启高精度GPS模式
|
||||
maximumAge: 0, // ❗不使用缓存,每次都重新获取最新GPS坐标
|
||||
...options,
|
||||
};
|
||||
|
||||
// 调用原生定位API
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
// 定位成功回调
|
||||
(position: GeolocationPosition) => {
|
||||
console.log(position);
|
||||
const { latitude, longitude, accuracy } = position.coords;
|
||||
resolve({
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
accuracy,
|
||||
});
|
||||
},
|
||||
// 定位失败回调,GeolocationPositionError是TS原生错误类型
|
||||
(err: GeolocationPositionError) => {
|
||||
let msg = "定位失败";
|
||||
console.log(err);
|
||||
switch (err.code) {
|
||||
case err.PERMISSION_DENIED:
|
||||
msg = "用户拒绝定位权限";
|
||||
break;
|
||||
case err.POSITION_UNAVAILABLE:
|
||||
msg = "无法获取位置,信号较差";
|
||||
break;
|
||||
case err.TIMEOUT:
|
||||
msg = "定位超时";
|
||||
break;
|
||||
}
|
||||
reject(new Error(msg));
|
||||
},
|
||||
geoOpt,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 持续监听位置变化(适合轨迹、导航场景)
|
||||
* @param onSuccess 位置更新成功回调,每次位置变动都会触发
|
||||
* @param onError 定位异常回调,可选
|
||||
* @param options 定位配置项,可选
|
||||
* @returns number watchId 监听标识,用于后续停止监听
|
||||
*/
|
||||
export function watchPosition(
|
||||
onSuccess: (res: GeoResult) => void,
|
||||
onError?: (err: GeolocationPositionError) => void,
|
||||
options: GeoOptions = {},
|
||||
): number {
|
||||
const geoOpt: PositionOptions = {
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0,
|
||||
...options,
|
||||
};
|
||||
|
||||
return navigator.geolocation.watchPosition(
|
||||
(pos) => {
|
||||
// 每次位置更新,统一格式化返回结构
|
||||
onSuccess({
|
||||
lat: pos.coords.latitude,
|
||||
lng: pos.coords.longitude,
|
||||
accuracy: pos.coords.accuracy,
|
||||
});
|
||||
},
|
||||
onError,
|
||||
geoOpt,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止持续位置监听
|
||||
* @param watchId 监听ID,watchPosition返回的值
|
||||
*/
|
||||
export function clearWatchPosition(watchId: number): void {
|
||||
navigator.geolocation?.clearWatch(watchId);
|
||||
}
|
||||
16
src/utils/i18n.ts
Normal file
16
src/utils/i18n.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// translate router.meta.title, be used in breadcrumb sidebar tagsview
|
||||
import i18n from '@/lang/index';
|
||||
|
||||
/**
|
||||
* 获取国际化路由,如果不存在则原生返回
|
||||
* @param title 路由名称
|
||||
* @returns {string}
|
||||
*/
|
||||
export const translateRouteTitle = (title: string): string => {
|
||||
const hasKey = i18n.global.te('route.' + title);
|
||||
if (hasKey) {
|
||||
const translatedTitle = i18n.global.t('route.' + title);
|
||||
return translatedTitle;
|
||||
}
|
||||
return title;
|
||||
};
|
||||
319
src/utils/index.ts
Normal file
319
src/utils/index.ts
Normal file
@@ -0,0 +1,319 @@
|
||||
import { parseTime } from '@/utils/ruoyi';
|
||||
|
||||
/**
|
||||
* 表格时间格式化
|
||||
*/
|
||||
export const formatDate = (cellValue: string) => {
|
||||
if (cellValue == null || cellValue == '') return '';
|
||||
const date = new Date(cellValue);
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
|
||||
const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
|
||||
const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
|
||||
const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
|
||||
const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
|
||||
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} time
|
||||
* @param {string} option
|
||||
* @returns {string}
|
||||
*/
|
||||
export const formatTime = (time: string, option: string) => {
|
||||
let t: number;
|
||||
if (('' + time).length === 10) {
|
||||
t = parseInt(time) * 1000;
|
||||
} else {
|
||||
t = +time;
|
||||
}
|
||||
const d: any = new Date(t);
|
||||
const now = Date.now();
|
||||
|
||||
const diff = (now - d) / 1000;
|
||||
|
||||
if (diff < 30) {
|
||||
return '刚刚';
|
||||
} else if (diff < 3600) {
|
||||
// less 1 hour
|
||||
return Math.ceil(diff / 60) + '分钟前';
|
||||
} else if (diff < 3600 * 24) {
|
||||
return Math.ceil(diff / 3600) + '小时前';
|
||||
} else if (diff < 3600 * 24 * 2) {
|
||||
return '1天前';
|
||||
}
|
||||
if (option) {
|
||||
return parseTime(t, option);
|
||||
} else {
|
||||
return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export const getQueryObject = (url: string) => {
|
||||
url = url == null ? window.location.href : url;
|
||||
const search = url.substring(url.lastIndexOf('?') + 1);
|
||||
const obj: { [key: string]: string } = {};
|
||||
const reg = /([^?&=]+)=([^?&=]*)/g;
|
||||
search.replace(reg, (rs, $1, $2) => {
|
||||
const name = decodeURIComponent($1);
|
||||
let val = decodeURIComponent($2);
|
||||
val = String(val);
|
||||
obj[name] = val;
|
||||
return rs;
|
||||
});
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} input value
|
||||
* @returns {number} output value
|
||||
*/
|
||||
export const byteLength = (str: string) => {
|
||||
// returns the byte length of an utf8 string
|
||||
let s = str.length;
|
||||
for (let i = str.length - 1; i >= 0; i--) {
|
||||
const code = str.charCodeAt(i);
|
||||
if (code > 0x7f && code <= 0x7ff) s++;
|
||||
else if (code > 0x7ff && code <= 0xffff) s += 2;
|
||||
if (code >= 0xdc00 && code <= 0xdfff) i--;
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Array} actual
|
||||
* @returns {Array}
|
||||
*/
|
||||
export const cleanArray = (actual: Array<any>) => {
|
||||
const newArray: any[] = [];
|
||||
for (let i = 0; i < actual.length; i++) {
|
||||
if (actual[i]) {
|
||||
newArray.push(actual[i]);
|
||||
}
|
||||
}
|
||||
return newArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} json
|
||||
* @returns {Array}
|
||||
*/
|
||||
export const param = (json: any) => {
|
||||
if (!json) return '';
|
||||
return cleanArray(
|
||||
Object.keys(json).map(key => {
|
||||
if (json[key] === undefined) return '';
|
||||
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]);
|
||||
})
|
||||
).join('&');
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export const param2Obj = (url: string) => {
|
||||
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ');
|
||||
if (!search) {
|
||||
return {};
|
||||
}
|
||||
const obj: any = {};
|
||||
const searchArr = search.split('&');
|
||||
searchArr.forEach(v => {
|
||||
const index = v.indexOf('=');
|
||||
if (index !== -1) {
|
||||
const name = v.substring(0, index);
|
||||
const val = v.substring(index + 1, v.length);
|
||||
obj[name] = val;
|
||||
}
|
||||
});
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} val
|
||||
* @returns {string}
|
||||
*/
|
||||
export const html2Text = (val: string) => {
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = val;
|
||||
return div.textContent || div.innerText;
|
||||
};
|
||||
|
||||
/**
|
||||
* Merges two objects, giving the last one precedence
|
||||
* @param {Object} target
|
||||
* @param {(Object|Array)} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export const objectMerge = (target: any, source: any | any[]) => {
|
||||
if (typeof target !== 'object') {
|
||||
target = {};
|
||||
}
|
||||
if (Array.isArray(source)) {
|
||||
return source.slice();
|
||||
}
|
||||
Object.keys(source).forEach(property => {
|
||||
const sourceProperty = source[property];
|
||||
if (typeof sourceProperty === 'object') {
|
||||
target[property] = objectMerge(target[property], sourceProperty);
|
||||
} else {
|
||||
target[property] = sourceProperty;
|
||||
}
|
||||
});
|
||||
return target;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element
|
||||
* @param {string} className
|
||||
*/
|
||||
export const toggleClass = (element: HTMLElement, className: string) => {
|
||||
if (!element || !className) {
|
||||
return;
|
||||
}
|
||||
let classString = element.className;
|
||||
const nameIndex = classString.indexOf(className);
|
||||
if (nameIndex === -1) {
|
||||
classString += '' + className;
|
||||
} else {
|
||||
classString = classString.substring(0, nameIndex) + classString.substring(nameIndex + className.length);
|
||||
}
|
||||
element.className = classString;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} type
|
||||
* @returns {Date}
|
||||
*/
|
||||
export const getTime = (type: string) => {
|
||||
if (type === 'start') {
|
||||
return new Date().getTime() - 3600 * 1000 * 24 * 90;
|
||||
} else {
|
||||
return new Date(new Date().toDateString());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Function} func
|
||||
* @param {number} wait
|
||||
* @param {boolean} immediate
|
||||
* @return {*}
|
||||
*/
|
||||
export function debounce(func: (...args: any[]) => any, wait: number, immediate: boolean) {
|
||||
let timeout: ReturnType<typeof setTimeout> | null = null;
|
||||
let lastArgs: any[] | null = null;
|
||||
let lastContext: any = null;
|
||||
let timestamp = 0;
|
||||
let result: any;
|
||||
|
||||
function later() {
|
||||
const last = Date.now() - timestamp;
|
||||
|
||||
if (last < wait && last > 0) {
|
||||
timeout = setTimeout(later, wait - last);
|
||||
} else {
|
||||
timeout = null;
|
||||
if (!immediate) {
|
||||
result = func.apply(lastContext, lastArgs!);
|
||||
if (!timeout) lastContext = lastArgs = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return function (this: any, ...args: any[]) {
|
||||
lastContext = this;
|
||||
lastArgs = args;
|
||||
timestamp = Date.now();
|
||||
const callNow = immediate && !timeout;
|
||||
if (!timeout) timeout = setTimeout(later, wait);
|
||||
if (callNow) {
|
||||
result = func.apply(lastContext, lastArgs);
|
||||
lastContext = lastArgs = null;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This is just a simple version of deep copy
|
||||
* Has a lot of edge cases bug
|
||||
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
|
||||
* @param {Object} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export const deepClone = (source: any) => {
|
||||
if (!source && typeof source !== 'object') {
|
||||
throw new Error('error arguments', 'deepClone' as any);
|
||||
}
|
||||
const targetObj: any = source.constructor === Array ? [] : {};
|
||||
Object.keys(source).forEach(keys => {
|
||||
if (source[keys] && typeof source[keys] === 'object') {
|
||||
targetObj[keys] = deepClone(source[keys]);
|
||||
} else {
|
||||
targetObj[keys] = source[keys];
|
||||
}
|
||||
});
|
||||
return targetObj;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Array} arr
|
||||
* @returns {Array}
|
||||
*/
|
||||
export const uniqueArr = (arr: any) => {
|
||||
return Array.from(new Set(arr));
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
export const createUniqueString = (): string => {
|
||||
const timestamp = +new Date() + '';
|
||||
const num = (1 + Math.random()) * 65536;
|
||||
const randomNum = parseInt(num + '');
|
||||
return (+(randomNum + timestamp)).toString(32);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if an element has a class
|
||||
* @param ele
|
||||
* @param {string} cls
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const hasClass = (ele: HTMLElement, cls: string): boolean => {
|
||||
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
|
||||
};
|
||||
|
||||
/**
|
||||
* Add class to element
|
||||
* @param ele
|
||||
* @param {string} cls
|
||||
*/
|
||||
export const addClass = (ele: HTMLElement, cls: string) => {
|
||||
if (!hasClass(ele, cls)) ele.className += ' ' + cls;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove class from element
|
||||
* @param ele
|
||||
* @param {string} cls
|
||||
*/
|
||||
export const removeClass = (ele: HTMLElement, cls: string) => {
|
||||
if (hasClass(ele, cls)) {
|
||||
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
|
||||
ele.className = ele.className.replace(reg, ' ');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const isExternal = (path: string) => {
|
||||
return /^(https?:|http?:|mailto:|tel:)/.test(path);
|
||||
};
|
||||
22
src/utils/jsencrypt.ts
Normal file
22
src/utils/jsencrypt.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { JSEncrypt } from 'jsencrypt';
|
||||
|
||||
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
||||
|
||||
const publicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;
|
||||
|
||||
// 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
|
||||
const privateKey = import.meta.env.VITE_APP_RSA_PRIVATE_KEY;
|
||||
|
||||
// 加密
|
||||
export const encrypt = (txt: string) => {
|
||||
const encryptor = new JSEncrypt();
|
||||
encryptor.setPublicKey(publicKey); // 设置公钥
|
||||
return encryptor.encrypt(txt); // 对数据进行加密
|
||||
};
|
||||
|
||||
// 解密
|
||||
export const decrypt = (txt: string) => {
|
||||
const encryptor = new JSEncrypt();
|
||||
encryptor.setPrivateKey(privateKey); // 设置私钥
|
||||
return encryptor.decrypt(txt); // 对数据进行解密
|
||||
};
|
||||
43
src/utils/message-read.ts
Normal file
43
src/utils/message-read.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import cache from '@/plugins/cache';
|
||||
|
||||
const MESSAGE_READ_KEY = 'message_read_ids';
|
||||
const MAX_READ_IDS = 300;
|
||||
|
||||
const buildCacheKey = (userId: string | number) => `${MESSAGE_READ_KEY}:${userId}`;
|
||||
|
||||
const normalizeIds = (ids: Array<string | number>) => {
|
||||
const values = ids.map(item => String(item)).filter(item => !!item);
|
||||
return Array.from(new Set(values)).slice(0, MAX_READ_IDS);
|
||||
};
|
||||
|
||||
export const getMessageReadIds = (userId: string | number) => {
|
||||
if (!userId) {
|
||||
return [] as string[];
|
||||
}
|
||||
const ids = cache.local.getJSON(buildCacheKey(userId));
|
||||
return Array.isArray(ids) ? normalizeIds(ids) : [];
|
||||
};
|
||||
|
||||
export const isMessageRead = (userId: string | number, messageId?: string | number) => {
|
||||
if (!userId || messageId === undefined || messageId === null) {
|
||||
return false;
|
||||
}
|
||||
return getMessageReadIds(userId).includes(String(messageId));
|
||||
};
|
||||
|
||||
export const markMessageRead = (userId: string | number, messageId?: string | number) => {
|
||||
if (!userId || messageId === undefined || messageId === null) {
|
||||
return [];
|
||||
}
|
||||
return markMessageReadBatch(userId, [messageId]);
|
||||
};
|
||||
|
||||
export const markMessageReadBatch = (userId: string | number, messageIds: Array<string | number>) => {
|
||||
if (!userId) {
|
||||
return [] as string[];
|
||||
}
|
||||
const current = getMessageReadIds(userId);
|
||||
const next = normalizeIds([...messageIds, ...current]);
|
||||
cache.local.setJSON(buildCacheKey(userId), next);
|
||||
return next;
|
||||
};
|
||||
31
src/utils/ossContent.ts
Normal file
31
src/utils/ossContent.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { listByIds } from '@/api/system/oss';
|
||||
|
||||
const OSS_MARKER_RE = /oss:\/\/([\w-]+)/g;
|
||||
|
||||
/**
|
||||
* 将 HTML 中的 oss://{ossId} 标记批量解析为真实的 OSS 授权 URL
|
||||
*
|
||||
* 适用于富文本内容展示场景(Editor 组件 / 详情页只读渲染等)
|
||||
*
|
||||
* @example
|
||||
* const html = await resolveOssContent('<p><img src="oss://12345"/></p>');
|
||||
*/
|
||||
export async function resolveOssContent(html: string): Promise<string> {
|
||||
if (!html) return html;
|
||||
|
||||
const matches = [...html.matchAll(OSS_MARKER_RE)];
|
||||
if (matches.length === 0) return html;
|
||||
|
||||
const ossIds = [...new Set(matches.map(m => m[1]))];
|
||||
|
||||
try {
|
||||
const res = await listByIds(ossIds.join(','));
|
||||
let result = html;
|
||||
for (const oss of res.data) {
|
||||
result = result.replaceAll(`oss://${oss.ossId}`, oss.url);
|
||||
}
|
||||
return result;
|
||||
} catch {
|
||||
return html;
|
||||
}
|
||||
}
|
||||
51
src/utils/permission.ts
Normal file
51
src/utils/permission.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
|
||||
/**
|
||||
* 字符权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const checkPermi = (value: any) => {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissions = useUserStore().permissions;
|
||||
const permissionDatas = value;
|
||||
const all_permission = '*:*:*';
|
||||
|
||||
const hasPermission = permissions.some(permission => {
|
||||
return all_permission === permission || permissionDatas.includes(permission);
|
||||
});
|
||||
|
||||
if (!hasPermission) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 角色权限校验
|
||||
* @param {Array} value 校验值
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const checkRole = (value: any): boolean => {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const roles = useUserStore().roles;
|
||||
const permissionRoles = value;
|
||||
const super_admin = 'admin';
|
||||
|
||||
const hasRole = roles.some(role => {
|
||||
return super_admin === role || permissionRoles.includes(role);
|
||||
});
|
||||
|
||||
if (!hasRole) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
console.error(`need roles! Like checkRole="['admin','editor']"`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
26
src/utils/propTypes.ts
Normal file
26
src/utils/propTypes.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { CSSProperties } from 'vue';
|
||||
import VueTypes, { createTypes, toValidableType, type VueTypesInterface, type VueTypeValidableDef } from 'vue-types';
|
||||
|
||||
type PropTypes = VueTypesInterface & {
|
||||
readonly style: VueTypeValidableDef<CSSProperties>;
|
||||
readonly fieldOption: VueTypeValidableDef<Array<FieldOption>>;
|
||||
};
|
||||
|
||||
const propTypes = createTypes({
|
||||
func: undefined,
|
||||
bool: undefined,
|
||||
string: undefined,
|
||||
number: undefined,
|
||||
object: undefined,
|
||||
integer: undefined
|
||||
}) as PropTypes;
|
||||
|
||||
export default class ProjectTypes extends VueTypes {
|
||||
static get style() {
|
||||
return toValidableType('style', {
|
||||
type: [String, Object],
|
||||
default: undefined
|
||||
});
|
||||
}
|
||||
}
|
||||
export { propTypes };
|
||||
82
src/utils/push-message.ts
Normal file
82
src/utils/push-message.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
export const PUSH_MESSAGE_TYPE = {
|
||||
MESSAGE: 'message',
|
||||
NOTICE: 'notice',
|
||||
LLM: 'llm',
|
||||
CUSTOM: 'custom'
|
||||
} as const;
|
||||
|
||||
export const PUSH_MESSAGE_SOURCE = {
|
||||
BACKEND: 'backend',
|
||||
NOTICE: 'notice',
|
||||
WORKFLOW: 'workflow',
|
||||
LLM: 'llm',
|
||||
CLIENT: 'client'
|
||||
} as const;
|
||||
|
||||
export const NOTICE_GROUP = {
|
||||
SYSTEM: 'system',
|
||||
NOTICE: 'notice',
|
||||
WORKFLOW: 'workflow'
|
||||
} as const;
|
||||
|
||||
export interface PushMessagePayload {
|
||||
messageId?: string | number;
|
||||
type?: string;
|
||||
source?: string;
|
||||
message?: string;
|
||||
data?: Record<string, any> | null;
|
||||
path?: string;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
const MESSAGE_CENTER_TYPES = new Set<string>([PUSH_MESSAGE_TYPE.MESSAGE, PUSH_MESSAGE_TYPE.NOTICE]);
|
||||
|
||||
export const parsePushMessage = (raw: string): PushMessagePayload => {
|
||||
try {
|
||||
const payload = JSON.parse(raw) as PushMessagePayload;
|
||||
return {
|
||||
type: payload.type ?? PUSH_MESSAGE_TYPE.MESSAGE,
|
||||
source: payload.source ?? 'backend',
|
||||
messageId: payload.messageId,
|
||||
message: payload.message ?? '',
|
||||
data: payload.data ?? null,
|
||||
path: payload.path,
|
||||
timestamp: payload.timestamp ?? Date.now()
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
type: PUSH_MESSAGE_TYPE.MESSAGE,
|
||||
source: 'backend',
|
||||
messageId: undefined,
|
||||
message: raw,
|
||||
data: null,
|
||||
path: undefined,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const shouldAppendNotice = (payload: PushMessagePayload) => {
|
||||
return MESSAGE_CENTER_TYPES.has(payload.type ?? PUSH_MESSAGE_TYPE.MESSAGE);
|
||||
};
|
||||
|
||||
export const resolveNoticeGroup = (payload: PushMessagePayload) => {
|
||||
if (payload.type === PUSH_MESSAGE_TYPE.NOTICE || payload.source === PUSH_MESSAGE_SOURCE.NOTICE) {
|
||||
return NOTICE_GROUP.NOTICE;
|
||||
}
|
||||
if (payload.source === PUSH_MESSAGE_SOURCE.WORKFLOW) {
|
||||
return NOTICE_GROUP.WORKFLOW;
|
||||
}
|
||||
return NOTICE_GROUP.SYSTEM;
|
||||
};
|
||||
|
||||
export const resolveNoticeTitle = (payload: PushMessagePayload) => {
|
||||
const group = resolveNoticeGroup(payload);
|
||||
if (group === NOTICE_GROUP.NOTICE) {
|
||||
return '通知公告消息';
|
||||
}
|
||||
if (group === NOTICE_GROUP.WORKFLOW) {
|
||||
return '工作流消息';
|
||||
}
|
||||
return '系统消息';
|
||||
};
|
||||
192
src/utils/push.ts
Normal file
192
src/utils/push.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import { ElNotification } from 'element-plus';
|
||||
import type { MessageVO } from '@/api/system/message/types';
|
||||
import { getMessageBox } from '@/api/system/message';
|
||||
import { useNoticeStore } from '@/store/modules/notice';
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { isMessageRead } from '@/utils/message-read';
|
||||
import { parsePushMessage, resolveNoticeGroup, resolveNoticeTitle, shouldAppendNotice } from '@/utils/push-message';
|
||||
|
||||
let closePushConnection: (() => void) | undefined;
|
||||
let stopPushWatchers: Array<() => void> = [];
|
||||
const KICKED_MESSAGE = 'kicked';
|
||||
let pushKicked = false;
|
||||
let resumePushTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const formatNoticeTime = (timestamp?: number | string) => {
|
||||
const time = timestamp ? new Date(timestamp) : new Date();
|
||||
return time.toLocaleString();
|
||||
};
|
||||
|
||||
const appendNotice = (raw: string) => {
|
||||
const payload = parsePushMessage(raw);
|
||||
if (!shouldAppendNotice(payload)) {
|
||||
return;
|
||||
}
|
||||
const userId = useUserStore().userId;
|
||||
const title = resolveNoticeTitle(payload);
|
||||
useNoticeStore().addNotice({
|
||||
messageId: payload.messageId,
|
||||
title,
|
||||
category: resolveNoticeGroup(payload),
|
||||
type: payload.type,
|
||||
source: payload.source,
|
||||
message: payload.message ?? '',
|
||||
content: payload.data?.noticeContent,
|
||||
data: payload.data,
|
||||
path: payload.path,
|
||||
read: isMessageRead(userId, payload.messageId),
|
||||
timestamp: payload.timestamp ?? Date.now(),
|
||||
time: formatNoticeTime(payload.timestamp)
|
||||
});
|
||||
ElNotification({
|
||||
title,
|
||||
message: payload.message ?? '',
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
};
|
||||
|
||||
const handlePushMessage = (raw: string) => {
|
||||
if (raw === KICKED_MESSAGE) {
|
||||
pushKicked = true;
|
||||
closePush();
|
||||
return;
|
||||
}
|
||||
appendNotice(raw);
|
||||
};
|
||||
|
||||
const toNoticeItem = (item: MessageVO) => {
|
||||
const userId = useUserStore().userId;
|
||||
const timestamp = item.createTime ? new Date(item.createTime).getTime() : Date.now();
|
||||
return {
|
||||
messageId: item.messageId,
|
||||
title: item.title,
|
||||
category: resolveNoticeGroup(item),
|
||||
type: item.type,
|
||||
source: item.source,
|
||||
message: item.message ?? '',
|
||||
content: item.content,
|
||||
data: item.data ?? null,
|
||||
path: item.path,
|
||||
read: isMessageRead(userId, item.messageId),
|
||||
timestamp,
|
||||
time: formatNoticeTime(timestamp)
|
||||
};
|
||||
};
|
||||
|
||||
const buildSseUrl = (path: string) => {
|
||||
return `${import.meta.env.VITE_APP_BASE_API}${path}?Authorization=Bearer ${getToken()}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`;
|
||||
};
|
||||
|
||||
const buildWsUrl = (path: string) => {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
|
||||
return `${protocol}${window.location.host}${buildSseUrl(path)}`;
|
||||
};
|
||||
|
||||
const initSsePush = (url: string) => {
|
||||
const { data, error, close } = useEventSource(url, [], {
|
||||
autoReconnect: {
|
||||
retries: 5,
|
||||
delay: 5000,
|
||||
onFailed() {
|
||||
console.warn('SSE connection failed after 5 retries');
|
||||
}
|
||||
}
|
||||
});
|
||||
closePushConnection = close;
|
||||
|
||||
const stopErrorWatch = watch(error, () => {
|
||||
console.warn('SSE connection error:', error.value);
|
||||
error.value = null;
|
||||
});
|
||||
|
||||
const stopDataWatch = watch(data, () => {
|
||||
if (!data.value) return;
|
||||
handlePushMessage(data.value);
|
||||
data.value = null;
|
||||
});
|
||||
stopPushWatchers.push(stopErrorWatch, stopDataWatch);
|
||||
};
|
||||
|
||||
const initWsPush = (url: string) => {
|
||||
const { close } = useWebSocket(url, {
|
||||
autoReconnect: {
|
||||
retries: 3,
|
||||
delay: 1000,
|
||||
onFailed() {
|
||||
console.warn('websocket重连失败');
|
||||
}
|
||||
},
|
||||
heartbeat: {
|
||||
message: 'ping',
|
||||
interval: 10000,
|
||||
pongTimeout: 2000
|
||||
},
|
||||
onMessage: (_, e) => {
|
||||
if (String(e.data) === 'pong') {
|
||||
return;
|
||||
}
|
||||
handlePushMessage(String(e.data));
|
||||
}
|
||||
});
|
||||
closePushConnection = close;
|
||||
};
|
||||
|
||||
export const initPush = () => {
|
||||
closePush();
|
||||
if (import.meta.env.VITE_APP_MESSAGE_ENABLED === 'false') {
|
||||
return;
|
||||
}
|
||||
pushKicked = false;
|
||||
const path = import.meta.env.VITE_APP_MESSAGE_PATH || '/resource/message';
|
||||
const transport = import.meta.env.VITE_APP_MESSAGE_TRANSPORT || 'sse';
|
||||
if (transport.toLowerCase() === 'websocket') {
|
||||
initWsPush(buildWsUrl(path));
|
||||
return;
|
||||
}
|
||||
initSsePush(buildSseUrl(path));
|
||||
};
|
||||
|
||||
export const initMessageBox = async () => {
|
||||
if (import.meta.env.VITE_APP_MESSAGE_ENABLED === 'false') {
|
||||
useNoticeStore().clearNotice();
|
||||
return;
|
||||
}
|
||||
const { data } = await getMessageBox();
|
||||
const notices = [...(data?.systemList ?? []), ...(data?.noticeList ?? []), ...(data?.workflowList ?? [])].map(
|
||||
toNoticeItem
|
||||
);
|
||||
useNoticeStore().setNotices(notices);
|
||||
};
|
||||
|
||||
export const closePush = () => {
|
||||
closePushConnection?.();
|
||||
closePushConnection = undefined;
|
||||
stopPushWatchers.forEach(stop => stop());
|
||||
stopPushWatchers = [];
|
||||
};
|
||||
|
||||
const resumePushIfNeeded = () => {
|
||||
if (!pushKicked || !getToken() || document.visibilityState !== 'visible') {
|
||||
return;
|
||||
}
|
||||
if (resumePushTimer) {
|
||||
clearTimeout(resumePushTimer);
|
||||
}
|
||||
resumePushTimer = setTimeout(async () => {
|
||||
resumePushTimer = undefined;
|
||||
if (!pushKicked || !getToken() || document.visibilityState !== 'visible') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await initMessageBox();
|
||||
} finally {
|
||||
initPush();
|
||||
}
|
||||
}, 300);
|
||||
};
|
||||
|
||||
window.addEventListener('focus', resumePushIfNeeded);
|
||||
document.addEventListener('visibilitychange', resumePushIfNeeded);
|
||||
window.addEventListener('online', resumePushIfNeeded);
|
||||
296
src/utils/request.ts
Normal file
296
src/utils/request.ts
Normal file
@@ -0,0 +1,296 @@
|
||||
import type { LoadingInstance } from 'element-plus';
|
||||
import axiosModule from 'axios';
|
||||
import { HttpStatus } from '@/enums/RespEnum';
|
||||
import { getLanguage } from '@/lang';
|
||||
import cache from '@/plugins/cache';
|
||||
import router from '@/router';
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { decryptBase64, decryptWithAes, encryptBase64, encryptWithAes, generateAesKey } from '@/utils/crypto';
|
||||
import { errorCode } from '@/utils/errorCode';
|
||||
import { decrypt, encrypt } from '@/utils/jsencrypt';
|
||||
import { blobValidate, tansParams } from '@/utils/ruoyi';
|
||||
import { saveBlob } from '@/utils/save';
|
||||
|
||||
/** axios 1.13 + TS6:默认导出在类型上会被解析为不可调用的 export= 形态 */
|
||||
const axios = axiosModule as any;
|
||||
|
||||
const encryptHeader = 'encrypt-key';
|
||||
let downloadLoadingInstance: LoadingInstance | undefined;
|
||||
// 是否显示重新登录
|
||||
export const isRelogin = { show: false };
|
||||
|
||||
function createHandledError(message: string) {
|
||||
const error = new Error(message) as Error & { isHandled?: boolean };
|
||||
error.isHandled = true;
|
||||
return error;
|
||||
}
|
||||
|
||||
export function isHandledRequestError(error: unknown) {
|
||||
return Boolean((error as { isHandled?: boolean } | undefined)?.isHandled);
|
||||
}
|
||||
|
||||
function normalizeErrorMessage(message?: string) {
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
if (message === 'Network Error') {
|
||||
return '后端接口连接异常';
|
||||
}
|
||||
if (message.includes('timeout')) {
|
||||
return '系统接口请求超时';
|
||||
}
|
||||
if (message.includes('Request failed with status code')) {
|
||||
return '系统接口' + message.slice(-3) + '异常';
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
async function parseResponseErrorData(data: unknown): Promise<string | undefined> {
|
||||
if (!data) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (data instanceof Blob) {
|
||||
return parseResponseErrorData(await data.text());
|
||||
}
|
||||
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return parseResponseErrorData(new TextDecoder().decode(data));
|
||||
}
|
||||
|
||||
if (typeof data === 'string') {
|
||||
const text = data.trim();
|
||||
if (!text) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
return parseResponseErrorData(JSON.parse(text));
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof data === 'object') {
|
||||
const payload = data as Record<string, any>;
|
||||
return payload.msg || payload.message || errorCode[payload.code] || errorCode['default'];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function extractErrorMessage(error: any): Promise<string | undefined> {
|
||||
const responseMessage = await parseResponseErrorData(error?.response?.data);
|
||||
if (responseMessage) {
|
||||
return responseMessage;
|
||||
}
|
||||
return normalizeErrorMessage(error?.message);
|
||||
}
|
||||
|
||||
export const globalHeaders = () => {
|
||||
return {
|
||||
Authorization: 'Bearer ' + getToken(),
|
||||
clientid: import.meta.env.VITE_APP_CLIENT_ID
|
||||
};
|
||||
};
|
||||
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
|
||||
axios.defaults.headers['clientid'] = import.meta.env.VITE_APP_CLIENT_ID;
|
||||
// 创建 axios 实例
|
||||
const service = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_BASE_API,
|
||||
timeout: 50000,
|
||||
transitional: {
|
||||
// 超时错误更明确
|
||||
clarifyTimeoutError: true
|
||||
}
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
(config: any) => {
|
||||
// 对应国际化资源文件后缀
|
||||
config.headers['Content-Language'] = getLanguage();
|
||||
|
||||
const isToken = config.headers?.isToken === false;
|
||||
// 是否需要防止数据重复提交
|
||||
const isRepeatSubmit = config.headers?.repeatSubmit === false;
|
||||
// 是否需要加密
|
||||
const isEncrypt = config.headers?.isEncrypt === 'true';
|
||||
|
||||
if (getToken() && !isToken) {
|
||||
config.headers['Authorization'] = 'Bearer ' + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
}
|
||||
// get请求映射params参数
|
||||
if (config.method === 'get' && config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params);
|
||||
url = url.slice(0, -1);
|
||||
config.params = {};
|
||||
config.url = url;
|
||||
}
|
||||
|
||||
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
|
||||
const requestObj = {
|
||||
url: config.url,
|
||||
data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
|
||||
time: new Date().getTime()
|
||||
};
|
||||
const sessionObj = cache.session.getJSON('sessionObj');
|
||||
if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
|
||||
cache.session.setJSON('sessionObj', requestObj);
|
||||
} else {
|
||||
const s_url = sessionObj.url; // 请求地址
|
||||
const s_data = sessionObj.data; // 请求数据
|
||||
const s_time = sessionObj.time; // 请求时间
|
||||
const interval = 500; // 间隔时间(ms),小于此时间视为重复提交
|
||||
if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
|
||||
const message = '数据正在处理,请勿重复提交';
|
||||
console.warn(`[${s_url}]: ` + message);
|
||||
return Promise.reject(new Error(message));
|
||||
} else {
|
||||
cache.session.setJSON('sessionObj', requestObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (import.meta.env.VITE_APP_ENCRYPT === 'true') {
|
||||
// 当开启参数加密
|
||||
if (isEncrypt && (config.method === 'post' || config.method === 'put')) {
|
||||
// 生成一个 AES 密钥
|
||||
const aesKey = generateAesKey();
|
||||
config.headers[encryptHeader] = encrypt(encryptBase64(aesKey));
|
||||
config.data =
|
||||
typeof config.data === 'object'
|
||||
? encryptWithAes(JSON.stringify(config.data), aesKey)
|
||||
: encryptWithAes(config.data, aesKey);
|
||||
}
|
||||
}
|
||||
// FormData数据去请求头Content-Type
|
||||
if (config.data instanceof FormData) {
|
||||
delete config.headers['Content-Type'];
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error: any) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(res: any) => {
|
||||
if (import.meta.env.VITE_APP_ENCRYPT === 'true') {
|
||||
// 加密后的 AES 秘钥
|
||||
const keyStr = res.headers[encryptHeader];
|
||||
// 加密
|
||||
if (keyStr != null && keyStr != '') {
|
||||
const data = res.data;
|
||||
// 请求体 AES 解密
|
||||
const base64Str = decrypt(keyStr);
|
||||
// base64 解码 得到请求头的 AES 秘钥
|
||||
const aesKey = decryptBase64(base64Str.toString());
|
||||
// aesKey 解码 data
|
||||
const decryptData = decryptWithAes(data, aesKey);
|
||||
// 将结果 (得到的是 JSON 字符串) 转为 JSON
|
||||
res.data = JSON.parse(decryptData);
|
||||
}
|
||||
}
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || HttpStatus.SUCCESS;
|
||||
// 获取错误信息
|
||||
const msg = res.data.msg || errorCode[code] || errorCode['default'];
|
||||
// 二进制数据则直接返回
|
||||
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
|
||||
return res.data;
|
||||
}
|
||||
if (code === 401) {
|
||||
// prettier-ignore
|
||||
if (!isRelogin.show) {
|
||||
isRelogin.show = true;
|
||||
ElMessageBox.confirm(
|
||||
"登录状态已过期,您可以继续留在该页面,或者重新登录",
|
||||
"系统提示",
|
||||
{
|
||||
confirmButtonText: "重新登录",
|
||||
cancelButtonText: "取消",
|
||||
type: "warning",
|
||||
},
|
||||
)
|
||||
.then(() => {
|
||||
isRelogin.show = false;
|
||||
useUserStore()
|
||||
.logout()
|
||||
.then(() => {
|
||||
router.replace({
|
||||
path: "/login",
|
||||
query: {
|
||||
redirect: encodeURIComponent(
|
||||
router.currentRoute.value.fullPath || "/",
|
||||
),
|
||||
},
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
isRelogin.show = false;
|
||||
});
|
||||
}
|
||||
return Promise.reject('无效的会话,或者会话已过期,请重新登录。');
|
||||
} else if (code === HttpStatus.SERVER_ERROR) {
|
||||
ElMessage({ message: msg, type: 'error' });
|
||||
return Promise.reject(createHandledError(msg));
|
||||
} else if (code === HttpStatus.WARN) {
|
||||
ElMessage({ message: msg, type: 'warning' });
|
||||
return Promise.reject(createHandledError(msg));
|
||||
} else if (code !== HttpStatus.SUCCESS) {
|
||||
ElNotification.error({ title: msg });
|
||||
return Promise.reject(createHandledError(msg));
|
||||
} else {
|
||||
return Promise.resolve(res.data);
|
||||
}
|
||||
},
|
||||
async (error: any) => {
|
||||
const message = (await extractErrorMessage(error)) || errorCode['default'];
|
||||
ElMessage({ message: message, type: 'error', duration: 5 * 1000 });
|
||||
error.isHandled = true;
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
// 通用下载方法
|
||||
export function download(url: string, params: any, fileName: string) {
|
||||
downloadLoadingInstance = ElLoading.service({
|
||||
text: '正在下载数据,请稍候',
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
});
|
||||
// prettier-ignore
|
||||
return service
|
||||
.post(url, params, {
|
||||
transformRequest: [
|
||||
(params: any) => {
|
||||
return tansParams(params);
|
||||
},
|
||||
],
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
responseType: "blob",
|
||||
})
|
||||
.then(async (resp: any) => {
|
||||
const isLogin = blobValidate(resp);
|
||||
if (isLogin) {
|
||||
const blob = new Blob([resp]);
|
||||
saveBlob(blob, fileName);
|
||||
} else {
|
||||
const blob = new Blob([resp]);
|
||||
const resText = await blob.text();
|
||||
const rspObj = JSON.parse(resText);
|
||||
const errMsg =
|
||||
errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
|
||||
ElMessage.error(errMsg);
|
||||
}
|
||||
downloadLoadingInstance?.close();
|
||||
})
|
||||
.catch((r: any) => {
|
||||
console.error(r);
|
||||
downloadLoadingInstance?.close();
|
||||
});
|
||||
}
|
||||
// 导出 axios 实例
|
||||
export default service;
|
||||
237
src/utils/ruoyi.ts
Normal file
237
src/utils/ruoyi.ts
Normal file
@@ -0,0 +1,237 @@
|
||||
// 日期格式化
|
||||
export function parseTime(time: any, pattern?: string) {
|
||||
if (arguments.length === 0 || !time) {
|
||||
return null;
|
||||
}
|
||||
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
|
||||
let date;
|
||||
if (typeof time === 'object') {
|
||||
date = time;
|
||||
} else {
|
||||
if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
|
||||
time = parseInt(time);
|
||||
} else if (typeof time === 'string') {
|
||||
time = time
|
||||
.replace(new RegExp(/-/gm), '/')
|
||||
.replace('T', ' ')
|
||||
.replace(new RegExp(/\.[\d]{3}/gm), '');
|
||||
}
|
||||
if (typeof time === 'number' && time.toString().length === 10) {
|
||||
time = time * 1000;
|
||||
}
|
||||
date = new Date(time);
|
||||
}
|
||||
const formatObj: { [key: string]: any } = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay()
|
||||
};
|
||||
return format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: string, key: string) => {
|
||||
let value = formatObj[key];
|
||||
// Note: getDay() returns 0 on Sunday
|
||||
if (key === 'a') {
|
||||
return ['日', '一', '二', '三', '四', '五', '六'][value];
|
||||
}
|
||||
if (result.length > 0 && value < 10) {
|
||||
value = '0' + value;
|
||||
}
|
||||
return value || 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加日期范围
|
||||
* @param params
|
||||
* @param dateRange
|
||||
* @param propName
|
||||
*/
|
||||
export const addDateRange = (params: any, dateRange: any[], propName?: string) => {
|
||||
const search = params;
|
||||
search.params =
|
||||
typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
|
||||
dateRange = Array.isArray(dateRange) ? dateRange : [];
|
||||
if (typeof propName === 'undefined') {
|
||||
search.params['beginTime'] = dateRange[0];
|
||||
search.params['endTime'] = dateRange[1];
|
||||
} else {
|
||||
search.params['begin' + propName] = dateRange[0];
|
||||
search.params['end' + propName] = dateRange[1];
|
||||
}
|
||||
return search;
|
||||
};
|
||||
|
||||
// 回显数据字典
|
||||
export const selectDictLabel = (datas: any, value: number | string) => {
|
||||
if (value === undefined) {
|
||||
return '';
|
||||
}
|
||||
const actions: Array<string | number> = [];
|
||||
Object.keys(datas).some(key => {
|
||||
if (datas[key].value == '' + value) {
|
||||
actions.push(datas[key].label);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (actions.length === 0) {
|
||||
actions.push(value);
|
||||
}
|
||||
return actions.join('');
|
||||
};
|
||||
|
||||
// 回显数据字典(字符串数组)
|
||||
export const selectDictLabels = (datas: any, value: any, separator: any) => {
|
||||
if (value === undefined || value.length === 0) {
|
||||
return '';
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value = value.join(',');
|
||||
}
|
||||
const actions: any[] = [];
|
||||
const currentSeparator = undefined === separator ? ',' : separator;
|
||||
const temp = value.split(currentSeparator);
|
||||
Object.keys(value.split(currentSeparator)).some(val => {
|
||||
let match = false;
|
||||
Object.keys(datas).some(key => {
|
||||
if (datas[key].value == '' + temp[val]) {
|
||||
actions.push(datas[key].label + currentSeparator);
|
||||
match = true;
|
||||
}
|
||||
});
|
||||
if (!match) {
|
||||
actions.push(temp[val] + currentSeparator);
|
||||
}
|
||||
});
|
||||
return actions.join('').substring(0, actions.join('').length - 1);
|
||||
};
|
||||
|
||||
// 字符串格式化(%s )
|
||||
export function sprintf(str: string) {
|
||||
if (arguments.length !== 0) {
|
||||
let flag = true,
|
||||
i = 1;
|
||||
str = str.replace(/%s/g, function () {
|
||||
const arg = arguments[i++];
|
||||
if (typeof arg === 'undefined') {
|
||||
flag = false;
|
||||
return '';
|
||||
}
|
||||
return arg;
|
||||
});
|
||||
return flag ? str : '';
|
||||
}
|
||||
}
|
||||
|
||||
// 转换字符串,undefined,null等转化为""
|
||||
export const parseStrEmpty = (str: any) => {
|
||||
if (!str || str == 'undefined' || str == 'null') {
|
||||
return '';
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
// 数据合并
|
||||
export const mergeRecursive = (source: any, target: any) => {
|
||||
for (const p in target) {
|
||||
try {
|
||||
if (target[p].constructor == Object) {
|
||||
source[p] = mergeRecursive(source[p], target[p]);
|
||||
} else {
|
||||
source[p] = target[p];
|
||||
}
|
||||
} catch (e) {
|
||||
source[p] = target[p];
|
||||
}
|
||||
}
|
||||
return source;
|
||||
};
|
||||
|
||||
/**
|
||||
* 构造树型结构数据
|
||||
* @param {*} data 数据源
|
||||
* @param {*} id id字段 默认 'id'
|
||||
* @param {*} parentId 父节点字段 默认 'parentId'
|
||||
* @param {*} children 孩子节点字段 默认 'children'
|
||||
*/
|
||||
export const handleTree = <T>(data: any[], id?: string, parentId?: string, children?: string): T[] => {
|
||||
const config: {
|
||||
id: string;
|
||||
parentId: string;
|
||||
childrenList: string;
|
||||
} = {
|
||||
id: id || 'id',
|
||||
parentId: parentId || 'parentId',
|
||||
childrenList: children || 'children'
|
||||
};
|
||||
|
||||
const childrenListMap: any = {};
|
||||
const tree: T[] = [];
|
||||
for (const d of data) {
|
||||
const id = d[config.id];
|
||||
childrenListMap[id] = d;
|
||||
if (!d[config.childrenList]) {
|
||||
d[config.childrenList] = [];
|
||||
}
|
||||
}
|
||||
|
||||
for (const d of data) {
|
||||
const parentId = d[config.parentId];
|
||||
const parentObj = childrenListMap[parentId];
|
||||
if (!parentObj) {
|
||||
tree.push(d);
|
||||
} else {
|
||||
parentObj[config.childrenList].push(d);
|
||||
}
|
||||
}
|
||||
return tree;
|
||||
};
|
||||
|
||||
/**
|
||||
* 参数处理
|
||||
* @param {*} params 参数
|
||||
*/
|
||||
export const tansParams = (params: any) => {
|
||||
let result = '';
|
||||
for (const propName of Object.keys(params)) {
|
||||
const value = params[propName];
|
||||
const part = encodeURIComponent(propName) + '=';
|
||||
if (value !== null && value !== '' && typeof value !== 'undefined') {
|
||||
if (typeof value === 'object') {
|
||||
for (const key of Object.keys(value)) {
|
||||
if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
|
||||
const params = propName + '[' + key + ']';
|
||||
const subPart = encodeURIComponent(params) + '=';
|
||||
result += subPart + encodeURIComponent(value[key]) + '&';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result += part + encodeURIComponent(value) + '&';
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// 返回项目路径
|
||||
export const getNormalPath = (p: string): string => {
|
||||
if (p.length === 0 || !p || p === 'undefined') {
|
||||
return p;
|
||||
}
|
||||
const res = p.replace('//', '/');
|
||||
if (res[res.length - 1] === '/') {
|
||||
return res.slice(0, res.length - 1);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 验证是否为blob格式
|
||||
export const blobValidate = (data: any) => {
|
||||
return data.type !== 'application/json';
|
||||
};
|
||||
|
||||
export default {
|
||||
handleTree
|
||||
};
|
||||
214
src/utils/sanitize.ts
Normal file
214
src/utils/sanitize.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
const DEFAULT_ALLOWED_TAGS = new Set([
|
||||
'a',
|
||||
'article',
|
||||
'b',
|
||||
'blockquote',
|
||||
'br',
|
||||
'caption',
|
||||
'code',
|
||||
'col',
|
||||
'colgroup',
|
||||
'del',
|
||||
'div',
|
||||
'em',
|
||||
'figcaption',
|
||||
'figure',
|
||||
'h1',
|
||||
'h2',
|
||||
'h3',
|
||||
'h4',
|
||||
'h5',
|
||||
'h6',
|
||||
'hr',
|
||||
'i',
|
||||
'img',
|
||||
'li',
|
||||
'ol',
|
||||
'p',
|
||||
'pre',
|
||||
's',
|
||||
'section',
|
||||
'span',
|
||||
'strong',
|
||||
'sub',
|
||||
'sup',
|
||||
'table',
|
||||
'tbody',
|
||||
'td',
|
||||
'th',
|
||||
'thead',
|
||||
'tr',
|
||||
'u',
|
||||
'ul',
|
||||
'video',
|
||||
'source'
|
||||
]);
|
||||
|
||||
const URI_ATTRS = new Set(['href', 'src']);
|
||||
const GLOBAL_ALLOWED_ATTRS = new Set(['class', 'title', 'alt', 'style']);
|
||||
const TAG_ALLOWED_ATTRS: Record<string, Set<string>> = {
|
||||
a: new Set(['href', 'target', 'rel']),
|
||||
img: new Set(['src', 'alt', 'title']),
|
||||
video: new Set(['src', 'poster', 'controls', 'autoplay', 'muted', 'loop']),
|
||||
source: new Set(['src', 'type']),
|
||||
td: new Set(['colspan', 'rowspan']),
|
||||
th: new Set(['colspan', 'rowspan']),
|
||||
col: new Set(['span']),
|
||||
colgroup: new Set(['span'])
|
||||
};
|
||||
|
||||
const DANGEROUS_TAGS = new Set([
|
||||
'script',
|
||||
'style',
|
||||
'iframe',
|
||||
'object',
|
||||
'embed',
|
||||
'link',
|
||||
'meta',
|
||||
'base',
|
||||
'form',
|
||||
'input',
|
||||
'button',
|
||||
'textarea',
|
||||
'select',
|
||||
'option',
|
||||
'svg',
|
||||
'math'
|
||||
]);
|
||||
const SAFE_URL_PATTERN = /^(https?:|mailto:|tel:|\/|#|data:image\/(?:png|jpeg|jpg|gif|webp);base64,)/i;
|
||||
const ALLOWED_STYLE_PROPS = new Set([
|
||||
'background-color',
|
||||
'color',
|
||||
'font-family',
|
||||
'font-size',
|
||||
'font-style',
|
||||
'font-weight',
|
||||
'height',
|
||||
'line-height',
|
||||
'list-style-type',
|
||||
'margin-left',
|
||||
'max-width',
|
||||
'min-width',
|
||||
'padding-left',
|
||||
'text-align',
|
||||
'text-decoration',
|
||||
'text-indent',
|
||||
'vertical-align',
|
||||
'white-space',
|
||||
'width'
|
||||
]);
|
||||
const UNSAFE_STYLE_VALUE_PATTERN = /(?:expression\s*\(|url\s*\(|javascript:|vbscript:|data:|@import|behavior:)/i;
|
||||
|
||||
const sanitizeUrl = (value: string) => {
|
||||
const normalized = value.trim();
|
||||
if (!normalized) {
|
||||
return '';
|
||||
}
|
||||
return SAFE_URL_PATTERN.test(normalized) ? normalized : '';
|
||||
};
|
||||
|
||||
const sanitizeStyle = (value: string) => {
|
||||
const declarations = value
|
||||
.split(';')
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
const safeDeclarations = declarations.flatMap(declaration => {
|
||||
const separatorIndex = declaration.indexOf(':');
|
||||
if (separatorIndex <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const property = declaration.slice(0, separatorIndex).trim().toLowerCase();
|
||||
const propertyValue = declaration.slice(separatorIndex + 1).trim();
|
||||
|
||||
if (!ALLOWED_STYLE_PROPS.has(property) || !propertyValue || UNSAFE_STYLE_VALUE_PATTERN.test(propertyValue)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [`${property}: ${propertyValue}`];
|
||||
});
|
||||
|
||||
return safeDeclarations.join('; ');
|
||||
};
|
||||
|
||||
const shouldKeepAttr = (tagName: string, attrName: string) => {
|
||||
if (attrName.startsWith('on')) {
|
||||
return false;
|
||||
}
|
||||
if (GLOBAL_ALLOWED_ATTRS.has(attrName)) {
|
||||
return true;
|
||||
}
|
||||
return TAG_ALLOWED_ATTRS[tagName]?.has(attrName) ?? false;
|
||||
};
|
||||
|
||||
const sanitizeElement = (element: Element) => {
|
||||
const tagName = element.tagName.toLowerCase();
|
||||
|
||||
if (DANGEROUS_TAGS.has(tagName)) {
|
||||
element.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!DEFAULT_ALLOWED_TAGS.has(tagName)) {
|
||||
const parent = element.parentNode;
|
||||
if (!parent) {
|
||||
element.remove();
|
||||
return;
|
||||
}
|
||||
while (element.firstChild) {
|
||||
parent.insertBefore(element.firstChild, element);
|
||||
}
|
||||
parent.removeChild(element);
|
||||
return;
|
||||
}
|
||||
|
||||
Array.from(element.attributes).forEach(attr => {
|
||||
const attrName = attr.name.toLowerCase();
|
||||
if (!shouldKeepAttr(tagName, attrName)) {
|
||||
element.removeAttribute(attr.name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (attrName === 'style') {
|
||||
const sanitizedStyle = sanitizeStyle(attr.value);
|
||||
if (!sanitizedStyle) {
|
||||
element.removeAttribute(attr.name);
|
||||
return;
|
||||
}
|
||||
element.setAttribute(attr.name, sanitizedStyle);
|
||||
return;
|
||||
}
|
||||
|
||||
if (URI_ATTRS.has(attrName)) {
|
||||
const sanitized = sanitizeUrl(attr.value);
|
||||
if (!sanitized) {
|
||||
element.removeAttribute(attr.name);
|
||||
return;
|
||||
}
|
||||
element.setAttribute(attr.name, sanitized);
|
||||
}
|
||||
});
|
||||
|
||||
if (tagName === 'a' && element.hasAttribute('target')) {
|
||||
element.setAttribute('rel', 'noopener noreferrer');
|
||||
}
|
||||
};
|
||||
|
||||
export function sanitizeHtml(html?: string) {
|
||||
if (!html) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const template = document.createElement('template');
|
||||
template.innerHTML = html;
|
||||
const walker = document.createTreeWalker(template.content, NodeFilter.SHOW_ELEMENT);
|
||||
const elements: Element[] = [];
|
||||
|
||||
while (walker.nextNode()) {
|
||||
elements.push(walker.currentNode as Element);
|
||||
}
|
||||
|
||||
elements.forEach(sanitizeElement);
|
||||
return template.innerHTML;
|
||||
}
|
||||
13
src/utils/save.ts
Normal file
13
src/utils/save.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export function saveBlob(blob: Blob, fileName: string) {
|
||||
const downloadLink = document.createElement('a');
|
||||
const objectUrl = URL.createObjectURL(blob);
|
||||
|
||||
downloadLink.href = objectUrl;
|
||||
downloadLink.download = fileName;
|
||||
downloadLink.rel = 'noopener';
|
||||
downloadLink.click();
|
||||
|
||||
setTimeout(() => {
|
||||
URL.revokeObjectURL(objectUrl);
|
||||
}, 40_000);
|
||||
}
|
||||
64
src/utils/scroll-to.ts
Normal file
64
src/utils/scroll-to.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
const easeInOutQuad = (t: number, b: number, c: number, d: number) => {
|
||||
t /= d / 2;
|
||||
if (t < 1) {
|
||||
return (c / 2) * t * t + b;
|
||||
}
|
||||
t--;
|
||||
return (-c / 2) * (t * (t - 2) - 1) + b;
|
||||
};
|
||||
|
||||
// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
|
||||
const requestAnimFrame = (() =>
|
||||
window.requestAnimationFrame ||
|
||||
(window as any).webkitRequestAnimationFrame ||
|
||||
(window as any).mozRequestAnimationFrame ||
|
||||
(callback => {
|
||||
window.setTimeout(callback, 1000 / 60);
|
||||
}))();
|
||||
|
||||
/**
|
||||
* Because it's so fucking difficult to detect the scrolling element, just move them all
|
||||
* @param {number} amount
|
||||
*/
|
||||
const move = (amount: number) => {
|
||||
document.documentElement.scrollTop = amount;
|
||||
(document.body.parentNode as HTMLElement).scrollTop = amount;
|
||||
document.body.scrollTop = amount;
|
||||
};
|
||||
|
||||
const position = () => {
|
||||
return (
|
||||
document.documentElement.scrollTop || (document.body.parentNode as HTMLElement).scrollTop || document.body.scrollTop
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} to
|
||||
* @param {number} duration
|
||||
* @param {Function} callback
|
||||
*/
|
||||
export const scrollTo = (to: number, duration: number, callback?: any) => {
|
||||
const start = position();
|
||||
const change = to - start;
|
||||
const increment = 20;
|
||||
let currentTime = 0;
|
||||
duration = typeof duration === 'undefined' ? 500 : duration;
|
||||
const animateScroll = () => {
|
||||
// increment the time
|
||||
currentTime += increment;
|
||||
// find the value with the quadratic in-out easing function
|
||||
const val = easeInOutQuad(currentTime, start, change, duration);
|
||||
// move the document.body
|
||||
move(val);
|
||||
// do the animation unless its over
|
||||
if (currentTime < duration) {
|
||||
requestAnimFrame(animateScroll);
|
||||
} else {
|
||||
if (callback && typeof callback === 'function') {
|
||||
// the animation is done so lets callback
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
animateScroll();
|
||||
};
|
||||
52
src/utils/theme.ts
Normal file
52
src/utils/theme.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
// 处理主题样式
|
||||
export const handleThemeStyle = (theme: string) => {
|
||||
document.documentElement.style.setProperty('--el-color-primary', theme);
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(theme, i / 10)}`);
|
||||
}
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(theme, i / 10)}`);
|
||||
}
|
||||
};
|
||||
|
||||
// hex颜色转rgb颜色
|
||||
export const hexToRgb = (str: string): string[] => {
|
||||
str = str.replace('#', '');
|
||||
const hexs = str.match(/../g);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (hexs) {
|
||||
hexs[i] = String(parseInt(hexs[i], 16));
|
||||
}
|
||||
}
|
||||
return hexs ? hexs : [];
|
||||
};
|
||||
|
||||
// rgb颜色转Hex颜色
|
||||
export const rgbToHex = (r: string, g: string, b: string) => {
|
||||
const hexs = [Number(r).toString(16), Number(g).toString(16), Number(b).toString(16)];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (hexs[i].length == 1) {
|
||||
hexs[i] = `0${hexs[i]}`;
|
||||
}
|
||||
}
|
||||
return `#${hexs.join('')}`;
|
||||
};
|
||||
|
||||
// 变浅颜色值
|
||||
export const getLightColor = (color: string, level: number) => {
|
||||
const rgb = hexToRgb(color);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const s = (255 - Number(rgb[i])) * level + Number(rgb[i]);
|
||||
rgb[i] = String(Math.floor(s));
|
||||
}
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2]);
|
||||
};
|
||||
|
||||
// 变深颜色值
|
||||
export const getDarkColor = (color: string, level: number) => {
|
||||
const rgb = hexToRgb(color);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
rgb[i] = String(Math.floor(Number(rgb[i]) * (1 - level)));
|
||||
}
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2]);
|
||||
};
|
||||
109
src/utils/validate.ts
Normal file
109
src/utils/validate.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* 路径匹配器
|
||||
* @param {string} pattern
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function isPathMatch(pattern: string, path: string) {
|
||||
const regexPattern = pattern
|
||||
.replace(/([.+^${}()|[\]\\])/g, '\\$1')
|
||||
.replace(/\*\*/g, '__DOUBLE_STAR__')
|
||||
.replace(/\*/g, '[^/]*')
|
||||
.replace(/\?/g, '[^/]')
|
||||
.replace(/__DOUBLE_STAR__/g, '.*');
|
||||
const regex = new RegExp(`^${regexPattern}$`);
|
||||
return regex.test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断url是否是http或https
|
||||
* @returns {Boolean}
|
||||
* @param url
|
||||
*/
|
||||
export const isHttp = (url: string): boolean => {
|
||||
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
|
||||
};
|
||||
|
||||
/**
|
||||
* 判断path是否为外链
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const isExternal = (path: string) => {
|
||||
return /^(https?:|mailto:|tel:)/.test(path);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validUsername = (str: string) => {
|
||||
const valid_map = ['admin', 'editor'];
|
||||
return valid_map.indexOf(str.trim()) >= 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validURL = (url: string) => {
|
||||
const reg =
|
||||
/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
|
||||
return reg.test(url);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validLowerCase = (str: string) => {
|
||||
const reg = /^[a-z]+$/;
|
||||
return reg.test(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validUpperCase = (str: string) => {
|
||||
const reg = /^[A-Z]+$/;
|
||||
return reg.test(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validAlphabets = (str: string) => {
|
||||
const reg = /^[A-Za-z]+$/;
|
||||
return reg.test(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} email
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const validEmail = (email: string) => {
|
||||
const reg =
|
||||
/^(([^<>()\]\\.,;:\s@"]+(\.[^<>()\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
return reg.test(email);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const isString = (str: any) => {
|
||||
return typeof str === 'string' || str instanceof String;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Array} arg
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export const isArray = (arg: string | string[]) => {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||
}
|
||||
return Array.isArray(arg);
|
||||
};
|
||||
Reference in New Issue
Block a user