SocialNetworks_duan/src/store/authentication/index.js

22 lines
652 B
JavaScript
Raw Normal View History

2025-08-14 13:33:15 +08:00
import { defineStore } from "pinia"
2025-08-14 14:42:11 +08:00
import { login } from "@/service/api/authentication"
2025-08-14 16:05:58 +08:00
import cache from "@/utils/cache"
import router from "@/router"
2025-08-14 14:42:11 +08:00
import { ElMessage } from "element-plus"
2025-08-14 16:05:58 +08:00
const TOKEN_KEY = "token"
2025-08-14 13:33:15 +08:00
export const useLoginStore = defineStore("loginStore", {
state: () => ({
2025-08-14 16:05:58 +08:00
token: cache.getItem(TOKEN_KEY) ?? ""
2025-08-14 13:33:15 +08:00
}),
2025-08-14 14:42:11 +08:00
actions: {
async loginForAccessToken(userInfo) {
const res = await login(userInfo)
2025-08-14 16:05:58 +08:00
if (res.code != 200) return
this.token = res.data.accessToken
cache.setItem(TOKEN_KEY, res.data.accessToken)
ElMessage.success("登录成功!")
router.push("/navigation")
2025-08-14 14:42:11 +08:00
}
}
2025-08-14 13:33:15 +08:00
})