登录完成
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import axios from "axios";
|
||||
import {ElMessage} from "element-plus";
|
||||
import {getToken} from "./tools";
|
||||
import {mainStore} from "./store";
|
||||
|
||||
|
||||
let ins = axios.create({
|
||||
baseURL: process.env.NODE_ENV == "production" ? "/" : "/dev", timeout: 600000, withCredentials: true,
|
||||
})
|
||||
const ins = axios.create({baseURL: "/", timeout: 600000, withCredentials: true})
|
||||
|
||||
ins.interceptors.request.use(config => {
|
||||
if (!config.withoutToken && !!getToken()) {
|
||||
@@ -23,13 +21,20 @@ ins.interceptors.request.use(config => {
|
||||
ins.interceptors.response.use(res => {
|
||||
if (res?.data?.code == 0) {
|
||||
return res.data
|
||||
} else if (res?.data?.code == 1) {
|
||||
!res.config?.customReturn && res.data?.msg && ElMessage.error(res.data.msg)
|
||||
return res.data
|
||||
} else if (res?.data?.code == 401) {
|
||||
console.error("安全令牌验证无法通过!")
|
||||
mainStore().logout()
|
||||
location.replace("/")
|
||||
} else if (res?.data) {
|
||||
return res.data
|
||||
} else {
|
||||
ElMessage.error("服务器异常,请联系管理员!")
|
||||
console.error("服务器异常,请联系管理员!")
|
||||
}
|
||||
}, ({response}) => {
|
||||
if (response?.data?.error) ElMessage.error(response.data.error)
|
||||
})
|
||||
export default ins
|
||||
35
web/src/utils/dayjs.js
Normal file
35
web/src/utils/dayjs.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import moment from 'dayjs'
|
||||
import duration from 'dayjs/plugin/duration'
|
||||
import updateLocale from 'dayjs/plugin/updateLocale'
|
||||
import customParseFormat from 'dayjs/plugin/customParseFormat'
|
||||
import 'dayjs/locale/zh-cn'
|
||||
|
||||
moment.locale('zh-cn')
|
||||
moment.extend(updateLocale)
|
||||
moment.extend(customParseFormat)
|
||||
moment.extend(duration)
|
||||
moment.updateLocale('zh-cn', {
|
||||
weekdays: "星期日|星期一|星期二|星期三|星期四|星期五|星期六".split("|"),
|
||||
meridiem(hour) {
|
||||
let word = ""
|
||||
if (hour < 6) {
|
||||
word = "凌晨"
|
||||
} else if (hour < 9) {
|
||||
word = "早上"
|
||||
} else if (hour < 12) {
|
||||
word = "上午"
|
||||
} else if (hour < 14) {
|
||||
word = "中午"
|
||||
} else if (hour < 17) {
|
||||
word = "下午"
|
||||
} else if (hour < 19) {
|
||||
word = "傍晚"
|
||||
} else if (hour < 22) {
|
||||
word = "晚上"
|
||||
} else {
|
||||
word = "夜里"
|
||||
}
|
||||
return word;
|
||||
}
|
||||
})
|
||||
export default moment
|
||||
@@ -1,7 +1,37 @@
|
||||
import {defineStore} from "pinia/dist/pinia";
|
||||
import http from "./axios";
|
||||
|
||||
export const mainStore = defineStore('main', {
|
||||
state: () => ({
|
||||
user: {}
|
||||
})
|
||||
user: {},
|
||||
token: ""
|
||||
}),
|
||||
actions: {
|
||||
getToken(params) {
|
||||
return http.post("/api/auth/token", null, {
|
||||
params: {...params}
|
||||
}).then(res => {
|
||||
if (res?.data) {
|
||||
return this.token = res.data
|
||||
}
|
||||
})
|
||||
},
|
||||
getUserInfo() {
|
||||
http.post("/api/user/info").then(res => {
|
||||
if (res?.data) {
|
||||
this.user = res.data
|
||||
}
|
||||
})
|
||||
},
|
||||
logout() {
|
||||
this.user = {}
|
||||
this.token = ""
|
||||
}
|
||||
},
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{storage: localStorage}
|
||||
]
|
||||
}
|
||||
})
|
||||
@@ -1,7 +1,9 @@
|
||||
export const getToken = () => localStorage.getItem("token")
|
||||
export const $confirm = ()=>{
|
||||
import {mainStore} from "./store";
|
||||
|
||||
export const getToken = () => mainStore()?.token
|
||||
export const $confirm = () => {
|
||||
|
||||
}
|
||||
export default {
|
||||
getToken,$confirm
|
||||
getToken, $confirm
|
||||
}
|
||||
Reference in New Issue
Block a user