0099
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { createPinia } from "pinia"
|
||||
import useUserStore from "./modules/user"
|
||||
import useAppStore from "./modules/app"
|
||||
|
||||
const pinia = createPinia()
|
||||
|
||||
export { useUserStore, useAppStore }
|
||||
export default pinia
|
||||
|
||||
73
cdTMP/src/store/modules/app.js
Normal file
73
cdTMP/src/store/modules/app.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { Notification } from "@arco-design/web-vue"
|
||||
import defaultSettings from "@/config/setting.json"
|
||||
|
||||
const useAppStore = defineStore("app", {
|
||||
state: () => ({
|
||||
...defaultSettings
|
||||
}),
|
||||
getters: {
|
||||
appCurrentSetting(state) {
|
||||
return { ...state }
|
||||
},
|
||||
appDevice(state) {
|
||||
return state.device
|
||||
},
|
||||
appAsyncMenus(state) {
|
||||
return state.serverMenu
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
// 更新设置
|
||||
updateSettings(partial) {
|
||||
this.$patch(partial)
|
||||
},
|
||||
// 改变主题
|
||||
toggleTheme(dark) {
|
||||
if (dark) {
|
||||
this.theme = "dark"
|
||||
document.body.setAttribute("arco-theme", "dark")
|
||||
} else {
|
||||
this.theme = "light"
|
||||
document.body.setAttribute("arco-theme", "light")
|
||||
}
|
||||
},
|
||||
// 切换用户设备
|
||||
toggleDevice(device) {
|
||||
this.device = device
|
||||
},
|
||||
// 切换菜单
|
||||
toggleMenu(value) {
|
||||
this.hideMenu = value
|
||||
},
|
||||
async fetchServerMenuConfig() {
|
||||
let notifyInstance = null
|
||||
try {
|
||||
notifyInstance = Notification.info({
|
||||
id: "menuNotice", // Keep the instance id the same
|
||||
content: "loading",
|
||||
closable: true
|
||||
})
|
||||
const { data } = await getMenuList()
|
||||
this.serverMenu = data
|
||||
notifyInstance = Notification.success({
|
||||
id: "menuNotice",
|
||||
content: "success",
|
||||
closable: true
|
||||
})
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
notifyInstance = Notification.error({
|
||||
id: "menuNotice",
|
||||
content: "error",
|
||||
closable: true
|
||||
})
|
||||
}
|
||||
},
|
||||
clearServerMenu() {
|
||||
this.serverMenu = []
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default useAppStore
|
||||
@@ -0,0 +1,101 @@
|
||||
import { defineStore } from "pinia"
|
||||
import tool from "@/utils/tool"
|
||||
import { removeRouteListener } from "@/utils/route-listener"
|
||||
import loginAPI from "@/api/login"
|
||||
import { useAppStore } from "@/store"
|
||||
|
||||
const useUserStore = defineStore("user", {
|
||||
state: () => ({
|
||||
name: undefined,
|
||||
avatar: undefined,
|
||||
job: undefined,
|
||||
organization: undefined,
|
||||
location: undefined,
|
||||
email: undefined,
|
||||
introduction: undefined,
|
||||
personalWebsite: undefined,
|
||||
jobName: undefined,
|
||||
organizationName: undefined,
|
||||
locationName: undefined,
|
||||
phone: undefined,
|
||||
registrationDate: undefined,
|
||||
accountId: undefined,
|
||||
certification: undefined,
|
||||
role: '',
|
||||
}),
|
||||
|
||||
getters: {
|
||||
setUserInfo(state) {
|
||||
return { ...state }
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 切换角色函数
|
||||
switchRoles() {
|
||||
return new Promise((resolve) => {
|
||||
this.role = this.role === "user" ? "admin" : "user"
|
||||
resolve(this.role)
|
||||
})
|
||||
},
|
||||
// token相关操作
|
||||
isLogin() {
|
||||
return !!localStorage.getItem(import.meta.env.VITE_APP_TOKEN_PREFIX)
|
||||
},
|
||||
setToken(token) {
|
||||
tool.local.set(import.meta.env.VITE_APP_TOKEN_PREFIX, token)
|
||||
},
|
||||
getToken() {
|
||||
return tool.local.get(import.meta.env.VITE_APP_TOKEN_PREFIX)
|
||||
},
|
||||
clearToken() {
|
||||
tool.local.remove(import.meta.env.VITE_APP_TOKEN_PREFIX)
|
||||
},
|
||||
// 全局更新userInfo信息
|
||||
setInfo(data) {
|
||||
this.$patch(data)
|
||||
},
|
||||
// 请求用户信息
|
||||
async info() {
|
||||
const res = await loginAPI.getInfo()
|
||||
this.setInfo(res.data)
|
||||
},
|
||||
resetUserInfo() {
|
||||
this.$reset()
|
||||
},
|
||||
|
||||
// login函数,传一个form信息
|
||||
login(form) {
|
||||
return loginAPI
|
||||
.login(form)
|
||||
.then((res) => {
|
||||
if (res.success) {
|
||||
this.setToken(res.data.token)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e)
|
||||
return false
|
||||
})
|
||||
},
|
||||
logoutCallBack() {
|
||||
const appStore = useAppStore()
|
||||
this.resetUserInfo()
|
||||
this.clearToken()
|
||||
removeRouteListener()
|
||||
appStore.clearServerMenu()
|
||||
},
|
||||
async logout() {
|
||||
try {
|
||||
await loginAPI.logout()
|
||||
} finally {
|
||||
this.logoutCallBack()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default useUserStore
|
||||
|
||||
Reference in New Issue
Block a user