Files
certd/packages/ui/certd-client/src/store/user/index.ts
T

179 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-01-29 13:44:19 +08:00
import { defineStore } from "pinia";
import router from "../../router";
// @ts-ignore
import { LocalStorage } from "/src/utils/util.storage";
// @ts-ignore
2025-04-12 23:59:03 +08:00
import * as UserApi from "./api.user";
2025-07-24 16:56:22 +08:00
import { ForgotPasswordReq, RegisterReq, SmsLoginReq } from "./api.user";
2023-01-29 13:44:19 +08:00
// @ts-ignore
2025-04-13 00:45:01 +08:00
import { LoginReq, UserInfoRes } from "/@/store/user/api.user";
2024-09-05 14:33:45 +08:00
import { message, Modal, notification } from "ant-design-vue";
2023-01-29 13:44:19 +08:00
import { useI18n } from "vue-i18n";
import { mitter } from "/src/utils/util.mitt";
import { resetAllStores, useAccessStore } from "/@/vben/stores";
2023-01-29 13:44:19 +08:00
2025-03-07 18:01:51 +08:00
import { useUserStore as vbenUserStore } from "/@/vben/stores/modules/user";
2025-12-01 00:40:46 +08:00
import { request } from "/@/api/service";
2025-03-07 18:01:51 +08:00
2023-01-29 13:44:19 +08:00
interface UserState {
userInfo: Nullable<UserInfoRes>;
token?: string;
2024-08-14 21:24:12 +08:00
}
2023-01-29 13:44:19 +08:00
const USER_INFO_KEY = "USER_INFO";
const TOKEN_KEY = "TOKEN";
export const useUserStore = defineStore({
id: "app.user",
state: (): UserState => ({
// user info
userInfo: null,
// token
2025-04-12 23:59:03 +08:00
token: undefined,
2023-01-29 13:44:19 +08:00
}),
getters: {
getUserInfo(): UserInfoRes {
return this.userInfo || LocalStorage.get(USER_INFO_KEY) || {};
},
getToken(): string {
return this.token || LocalStorage.get(TOKEN_KEY);
},
isAdmin(): boolean {
2024-10-15 12:01:38 +08:00
return this.getUserInfo.roleIds?.includes(1) || this.getUserInfo.id === 1;
2025-04-12 23:59:03 +08:00
},
2023-01-29 13:44:19 +08:00
},
actions: {
setToken(token: string, expire: number) {
this.token = token;
const accessStore = useAccessStore();
accessStore.setAccessToken(token);
2023-01-29 13:44:19 +08:00
LocalStorage.set(TOKEN_KEY, this.token, expire);
},
setUserInfo(info: UserInfoRes) {
this.userInfo = info;
2025-03-07 18:01:51 +08:00
const userStore = vbenUserStore();
2025-04-17 22:34:21 +08:00
userStore.setUserInfo(info as any);
2023-01-29 13:44:19 +08:00
LocalStorage.set(USER_INFO_KEY, info);
},
resetState() {
this.userInfo = null;
this.token = "";
LocalStorage.remove(TOKEN_KEY);
LocalStorage.remove(USER_INFO_KEY);
},
2023-06-27 09:29:43 +08:00
async register(user: RegisterReq) {
await UserApi.register(user);
notification.success({
2025-04-12 23:59:03 +08:00
message: "注册成功,请登录",
2023-06-27 09:29:43 +08:00
});
await router.replace("/login");
},
2025-07-24 16:56:22 +08:00
async forgotPassword(params: ForgotPasswordReq): Promise<any> {
await UserApi.forgotPassword(params);
notification.success({
message: "密码已重置,请登录",
});
await router.replace("/login");
},
2023-01-29 13:44:19 +08:00
/**
* @description: login
*/
2024-11-28 17:36:45 +08:00
async login(loginType: string, params: LoginReq | SmsLoginReq): Promise<any> {
2025-04-17 22:34:21 +08:00
let loginRes: any = null;
if (loginType === "sms") {
loginRes = await UserApi.loginBySms(params as SmsLoginReq);
} else {
loginRes = await UserApi.login(params as LoginReq);
2023-01-29 13:44:19 +08:00
}
2025-04-17 22:34:21 +08:00
return await this.onLoginSuccess(loginRes);
},
async loginByTwoFactor(form: any) {
const loginRes = await UserApi.loginByTwoFactor(form);
return await this.onLoginSuccess(loginRes);
2023-01-29 13:44:19 +08:00
},
2026-03-12 18:11:02 +08:00
async loginByPasskey(form: any) {
const loginRes = await UserApi.loginByPasskey(form);
return await this.onLoginSuccess(loginRes);
},
async registerPasskey(form: any) {
return await UserApi.registerPasskey(form);
},
2023-01-29 13:44:19 +08:00
async getUserInfoAction(): Promise<UserInfoRes> {
const userInfo = await UserApi.mine();
this.setUserInfo(userInfo);
return userInfo;
},
2024-08-14 21:24:12 +08:00
2024-11-01 10:22:40 +08:00
async loadUserInfo() {
await this.getUserInfoAction();
},
2024-08-14 21:24:12 +08:00
async onLoginSuccess(loginData: any) {
2025-04-17 22:34:21 +08:00
const { token, expire } = loginData;
// save token
this.setToken(token, expire);
// get user info
2025-03-06 21:11:07 +08:00
// await this.getUserInfoAction();
// const userInfo = await this.getUserInfoAction();
2025-04-17 22:34:21 +08:00
mitter.emit("app.login", { ...loginData });
2024-08-14 21:24:12 +08:00
await router.replace("/");
},
2023-01-29 13:44:19 +08:00
/**
* @description: logout
*/
async logout(goLogin = true, from401 = false) {
2025-12-01 00:40:46 +08:00
if (!from401 && this.getToken) {
try {
await UserApi.logout(); //主要是清空cookie
} catch (e) {
console.error("注销登录请求失败:", e);
}
}
2023-01-29 13:44:19 +08:00
this.resetState();
resetAllStores();
2025-12-01 00:40:46 +08:00
// 第三方登录注销
await this.oauthLogout();
2023-01-29 13:44:19 +08:00
goLogin && router.push("/login");
mitter.emit("app.logout");
},
2025-12-01 00:40:46 +08:00
async oauthLogout() {
const providers = await UserApi.OauthProviders();
for (const provider of providers) {
if (provider.logoutUrl) {
try {
await request({
url: provider.logoutUrl,
method: "get",
withCredentials: true,
2025-12-03 00:35:17 +08:00
showErrorNotify: false,
2025-12-01 00:40:46 +08:00
});
} catch (e) {
console.error("注销第三方登录失败:", e);
}
}
}
},
2023-01-29 13:44:19 +08:00
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { t } = useI18n();
2024-10-01 23:52:44 +08:00
Modal.confirm({
2023-01-29 13:44:19 +08:00
iconType: "warning",
title: t("app.login.logoutTip"),
content: t("app.login.logoutMessage"),
onOk: async () => {
await this.logout(true);
2025-04-12 23:59:03 +08:00
},
2023-01-29 13:44:19 +08:00
});
2025-04-12 23:59:03 +08:00
},
},
2023-01-29 13:44:19 +08:00
});