admin 初始化
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// 日期格式化
|
||||
export function parseTime(time, pattern) {
|
||||
if (arguments.length === 0 || !time) {
|
||||
return null
|
||||
}
|
||||
if (time.indexOf('01-01-01') > -1) {
|
||||
return '-'
|
||||
}
|
||||
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)
|
||||
}
|
||||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||||
time = time * 1000
|
||||
}
|
||||
date = new Date(time)
|
||||
}
|
||||
const formatObj = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay()
|
||||
}
|
||||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
||||
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
|
||||
})
|
||||
return time_str
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import axios from 'axios';
|
||||
import { Message } from '@arco-design/web-vue';
|
||||
import { useUserStore } from '../store/userInfo'
|
||||
|
||||
// create an axios instance
|
||||
console.log("________"+import.meta.env.VITE_BASE_URL)
|
||||
const service = axios.create({
|
||||
baseURL:import.meta.env.VITE_BASE_URL,
|
||||
timeout: 8000,
|
||||
});
|
||||
|
||||
// request interceptor
|
||||
service.interceptors.request.use(
|
||||
(config) => {
|
||||
// Store 必须在拦截器内部导入,在外部导入会显示 Pinia 未初始化
|
||||
const store = useUserStore();
|
||||
// 设置请求头部 Authorization
|
||||
if (store.token) {
|
||||
config.headers['Authorization'] = 'Bearer ' + store.token;
|
||||
config.headers['Content-Type'] = 'application/json'
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
console.error(error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// response interceptor
|
||||
service.interceptors.response.use(
|
||||
(response) => {
|
||||
return response.data;
|
||||
},
|
||||
(error) => {
|
||||
const store = useUserStore();
|
||||
const { code, msg } = error.response.data;
|
||||
// 如果过期则退出登录
|
||||
if (code === 401) {
|
||||
Message.error({
|
||||
content: 'Token 已过期, 请重新登陆',
|
||||
duration: 3000
|
||||
});
|
||||
// 重定向路由到登陆页面
|
||||
store.userLogout();
|
||||
// Akiraka 20230410 重定向到登录页面
|
||||
return router.push('/login');
|
||||
} else {
|
||||
Message.error({
|
||||
content: error.message,
|
||||
duration: 3000
|
||||
})
|
||||
return Promise.reject(msg);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export default service;
|
||||
@@ -0,0 +1,41 @@
|
||||
// LocalStorage
|
||||
|
||||
/**
|
||||
* 存储LocalStorage
|
||||
* @param {string, Object} name
|
||||
* @param {*} value
|
||||
*/
|
||||
export const setLocalStorage = (name, value) => {
|
||||
if (!name) throw new Error('name must be specified');
|
||||
if (typeof value !== 'string') {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
window.localStorage.setItem(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取LocalStorage
|
||||
* @param {string, Object} name
|
||||
* @returns
|
||||
*/
|
||||
export const getLocalStorage = (name) => {
|
||||
if(!name) throw new Error('name must be specified');
|
||||
const value = window.localStorage.getItem(name);
|
||||
return JSON.parse(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除指定name
|
||||
* @param {*} name
|
||||
*/
|
||||
export const removeLocalStorage = (name) => {
|
||||
if (!name) throw new Error('name must be specified');
|
||||
window.localStorage.removeItem(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有LocalStorage
|
||||
*/
|
||||
export const clearLocalStorage = () => {
|
||||
window.localStorage.clear();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
let watermark = {}
|
||||
|
||||
let setWatermark = (str) => {
|
||||
let id = '1.23452384164.123412415'
|
||||
|
||||
if (document.getElementById(id) !== null) {
|
||||
document.body.removeChild(document.getElementById(id))
|
||||
}
|
||||
|
||||
let can = document.createElement('canvas')
|
||||
can.width = 200
|
||||
can.height = 120
|
||||
|
||||
let cans = can.getContext('2d')
|
||||
cans.rotate(-15 * Math.PI / 150)
|
||||
cans.font = '18px Vedana'
|
||||
cans.fillStyle = 'rgba(200, 200, 200, 0.20)'
|
||||
cans.textAlign = 'left'
|
||||
cans.textBaseline = 'Middle'
|
||||
cans.fillText(str, can.width / 8, can.height / 2)
|
||||
|
||||
let div = document.createElement('div')
|
||||
div.id = id
|
||||
div.style.pointerEvents = 'none'
|
||||
div.style.top = '35px'
|
||||
div.style.left = '200px'
|
||||
div.style.position = 'fixed'
|
||||
div.style.zIndex = '100000'
|
||||
div.style.width = document.documentElement.clientWidth + 'px'
|
||||
div.style.height = document.documentElement.clientHeight + 'px'
|
||||
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
|
||||
document.body.appendChild(div)
|
||||
return id
|
||||
}
|
||||
|
||||
// 该方法只允许调用一次
|
||||
watermark.set = (str) => {
|
||||
let id = setWatermark(str)
|
||||
setInterval(() => {
|
||||
if (document.getElementById(id) === null) {
|
||||
id = setWatermark(str)
|
||||
}
|
||||
}, 500)
|
||||
window.onresize = () => {
|
||||
setWatermark(str)
|
||||
}
|
||||
}
|
||||
|
||||
const outWatermark = (id) => {
|
||||
if (document.getElementById(id) !== null) {
|
||||
const div = document.getElementById(id)
|
||||
div.style.display = 'none'
|
||||
}
|
||||
}
|
||||
watermark.out = () => {
|
||||
const str = '1.23452384164.123412415'
|
||||
outWatermark(str)
|
||||
}
|
||||
|
||||
export default watermark
|
||||
Reference in New Issue
Block a user