mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-02-22 18:44:42 +08:00
add BAM
This commit is contained in:
28
client/src/utils/format.ts
Normal file
28
client/src/utils/format.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
|
||||
const fmt = {
|
||||
dateFormat( dateStamp:number, format:string='YYYY-mm-dd HH:MM:SS') {
|
||||
// 根据时间戳生成当前时间, 单位 毫秒
|
||||
let date = new Date(dateStamp*1000)
|
||||
const opt = {
|
||||
"Y+": date.getFullYear().toString(), // 年
|
||||
"m+": (date.getMonth() + 1).toString(), // 月
|
||||
"d+": date.getDate().toString(), // 日
|
||||
"H+": date.getHours().toString(), // 时
|
||||
"M+": date.getMinutes().toString(), // 分
|
||||
"S+": date.getSeconds().toString() // 秒
|
||||
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
||||
};
|
||||
for (let k in opt) {
|
||||
// 正则匹配对应的 opt key
|
||||
let r = new RegExp("(" + k + ")").exec(format);
|
||||
// 如果有匹配成功项
|
||||
if (r) {
|
||||
format = format.replace(r[1], (r[1].length == 1) ? (opt[k as keyof typeof opt]) : (opt[k as keyof typeof opt].padStart(r[1].length, "0")))
|
||||
}
|
||||
}
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
export {fmt}
|
||||
@@ -3,9 +3,12 @@ import {ElMessage} from "element-plus";
|
||||
|
||||
// 自定义loading加载动画
|
||||
import {load} from "../components/Loading";
|
||||
import {router} from "../router/router";
|
||||
import {getToken, setToken} from "./token";
|
||||
|
||||
let loadingCount: number = 0;
|
||||
|
||||
|
||||
const http = (options: any) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -21,13 +24,13 @@ const http = (options: any) => {
|
||||
// 开启loading动画
|
||||
loadingCount++
|
||||
load.start('')
|
||||
// let token: string = ""; //此处换成自己获取回来的token,通常存在在cookie或者store里面
|
||||
// if (token) {
|
||||
// // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
|
||||
// config.headers["X-Token"] = token;
|
||||
//
|
||||
// config.headers.Authorization = +token;
|
||||
// }
|
||||
let token = getToken();
|
||||
//此处换成自己获取回来的token,通常存在在cookie或者store里面
|
||||
if (token &&token.value.length > 0) {
|
||||
// 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
|
||||
config.headers[token.key] = token.value;
|
||||
// config.headers.Authorization = +token;
|
||||
}
|
||||
return config;
|
||||
}, (error) => {
|
||||
// Do something with request error
|
||||
@@ -40,13 +43,25 @@ const http = (options: any) => {
|
||||
// 关闭loading动画
|
||||
loadingCount--
|
||||
loadingCount == 0 && load.close()
|
||||
// 如果
|
||||
let token = response.headers['new-token']
|
||||
if (token && token.length > 0) {
|
||||
setToken(token)
|
||||
}
|
||||
return response.data;
|
||||
}, (error) => {
|
||||
if (error.response.status == 403) {
|
||||
ElMessage.error("请求异常: ", error)
|
||||
loadingCount--
|
||||
loadingCount == 0 && load.close()
|
||||
if (error.response.status == 401) {
|
||||
router.replace('/login')
|
||||
ElMessage.error(error.response.data.message)
|
||||
// ElMessage.error(`未获取授权信息, 请先登录!!!`)
|
||||
} else if (error.response.status == 403) {
|
||||
ElMessage.error(`无访问权限!!!`)
|
||||
} else {
|
||||
ElMessage.error("服务器繁忙,请稍后再试");
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
18
client/src/utils/token.ts
Normal file
18
client/src/utils/token.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
// 将 token 存储到 localStorage 中
|
||||
const setToken = (token:string)=>{
|
||||
const auth = {key: "auth-token", value: token}
|
||||
localStorage.setItem("auth", JSON.stringify(auth))
|
||||
}
|
||||
|
||||
// 获取 localStorage 中的 token 信息
|
||||
const getToken = ()=>{
|
||||
return JSON.parse(localStorage.getItem("auth") as string)
|
||||
}
|
||||
|
||||
// 删除 localStorage 中的 token 信息
|
||||
const clearAuthToken = ()=>{
|
||||
localStorage.removeItem("auth")
|
||||
}
|
||||
export {setToken, getToken,clearAuthToken}
|
||||
Reference in New Issue
Block a user