first commit

This commit is contained in:
muzi
2025-06-16 14:42:26 +08:00
commit 6f06721506
149 changed files with 56883 additions and 0 deletions

26
src/utils/auth.ts Normal file
View File

@ -0,0 +1,26 @@
/*
* @Author: 田鑫
* @Date: 2023-02-21 15:09:11
* @LastEditors: 田鑫
* @LastEditTime: 2023-03-05 19:21:56
* @Description:
*/
const TOKEN_KEY = 'satoken';
const isLogin = () => {
return !!localStorage.getItem(TOKEN_KEY);
};
const getToken = () => {
return localStorage.getItem(TOKEN_KEY);
};
const setToken = (token: string) => {
localStorage.setItem(TOKEN_KEY, token);
};
const clearToken = () => {
localStorage.removeItem(TOKEN_KEY);
};
const clearAllLocalStorage = () => {
localStorage.clear();
};
export { isLogin, getToken, setToken, clearToken, clearAllLocalStorage };

25
src/utils/event.ts Normal file
View File

@ -0,0 +1,25 @@
// eslint-disable-next-line max-params
export function addEventListen(
target: Window | HTMLElement,
event: string,
// eslint-disable-next-line no-undef
handler: EventListenerOrEventListenerObject,
capture = false,
) {
if (target.addEventListener && typeof target.addEventListener === 'function') {
target.addEventListener(event, handler, capture);
}
}
// eslint-disable-next-line max-params
export function removeEventListen(
target: Window | HTMLElement,
event: string,
// eslint-disable-next-line no-undef
handler: EventListenerOrEventListenerObject,
capture = false,
) {
if (target.removeEventListener && typeof target.removeEventListener === 'function') {
target.removeEventListener(event, handler, capture);
}
}

20
src/utils/index.ts Normal file
View File

@ -0,0 +1,20 @@
type TargetContext = '_self' | '_parent' | '_blank' | '_top';
export const openWindow = (url: string, opts?: { target?: TargetContext; [key: string]: any }) => {
const { target = '_blank', ...others } = opts || {};
window.open(
url,
target,
Object.entries(others)
.reduce((preValue: string[], curValue) => {
const [key, value] = curValue;
return [...preValue, `${key}=${value}`];
}, [])
.join(','),
);
};
export const regexUrl =
/^(?!mailto:)(?:(?:http|https|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:(\/|\?|#)[^\s]*)?$/i;
export default null;

53
src/utils/is.ts Normal file
View File

@ -0,0 +1,53 @@
const opt = Object.prototype.toString;
export function isArray(obj: any): obj is any[] {
return opt.call(obj) === '[object Array]';
}
export function isObject(obj: any): obj is { [key: string]: any } {
return opt.call(obj) === '[object Object]';
}
export function isString(obj: any): obj is string {
return opt.call(obj) === '[object String]';
}
export function isNumber(obj: any): obj is number {
return opt.call(obj) === '[object Number]' && obj === obj; // eslint-disable-line
}
export function isRegExp(obj: any) {
return opt.call(obj) === '[object RegExp]';
}
export function isFile(obj: any): obj is File {
return opt.call(obj) === '[object File]';
}
export function isBlob(obj: any): obj is Blob {
return opt.call(obj) === '[object Blob]';
}
export function isUndefined(obj: any): obj is undefined {
return obj === undefined;
}
export function isNull(obj: any): obj is null {
return obj === null;
}
export function isFunction(obj: any): obj is (...args: any[]) => any {
return typeof obj === 'function';
}
export function isEmptyObject(obj: any): boolean {
return isObject(obj) && Object.keys(obj).length === 0;
}
export function isExist(obj: any): boolean {
return obj || obj === 0;
}
export function isWindow(el: any): el is Window {
return el === window;
}

View File

@ -0,0 +1,30 @@
/**
* Listening to routes alone would waste rendering performance. Use the publish-subscribe model for distribution management
* 单独监听路由会浪费渲染性能。使用发布订阅模式去进行分发管理。
*/
import type { RouteLocationNormalized } from 'vue-router';
// @ts-ignore
import mitt, { type Handler } from 'mitt';
const emitter = mitt();
const key = Symbol('ROUTE_CHANGE');
let latestRoute: RouteLocationNormalized;
export function setRouteEmitter(to: RouteLocationNormalized) {
emitter.emit(key, to);
latestRoute = to;
}
export function listenerRouteChange(handler: (route: RouteLocationNormalized) => void, immediate = true) {
emitter.on(key, handler as Handler);
if (immediate && latestRoute) {
handler(latestRoute);
}
}
export function removeRouteListener() {
emitter.off(key);
}

48
src/utils/validators.ts Normal file
View File

@ -0,0 +1,48 @@
/*
* @Author: 田鑫
* @Date: 2023-02-21 15:05:52
* @LastEditors: 田鑫
* @LastEditTime: 2023-02-21 15:08:07
* @Description:
*/
/**
* 11位有效电话号码验证规则
*/
const validPhoneNumber = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
/**
* 手机号验证
*/
export function validateMobile(value, callback) {
if (value == '' || value == undefined) {
callback();
} else {
// const reg = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/
const reg = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
if (!reg.test(value)) {
callback(new Error('不是有效的手机号'));
} else {
callback();
}
}
}
/**
* 邮箱校验
*/
export function validateEmail(value, callback) {
if (value == '' || value == undefined) {
callback();
} else {
const reg = /^([0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2})+$/;
if (!reg.test(value)) {
callback(new Error('邮箱格式不正确'));
} else {
callback();
}
}
}
export const uuid = '1234';
export { validPhoneNumber };