pref: 优化插件store

This commit is contained in:
xiaojunnuo
2025-04-12 23:59:03 +08:00
parent 965dc2cb47
commit cc0657aaa8
71 changed files with 764 additions and 767 deletions
@@ -0,0 +1,68 @@
import { request } from "/src/api/service";
export interface RegisterReq {
username: string;
password: string;
confirmPassword: string;
}
/**
* @description: Login interface parameters
*/
export interface LoginReq {
username: string;
password: string;
}
export interface SmsLoginReq {
mobile: string;
phoneCode: string;
smsCode: string;
randomStr: string;
}
export interface UserInfoRes {
id: string | number;
username: string;
nickName: string;
avatar?: string;
roleIds: number[];
isWeak?: boolean;
}
export interface LoginRes {
token: string;
expire: number;
}
export async function register(user: RegisterReq): Promise<UserInfoRes> {
return await request({
url: "/register",
method: "post",
data: user,
});
}
export async function login(data: LoginReq): Promise<LoginRes> {
//如果开启了登录与权限模块,则真实登录
return await request({
url: "/login",
method: "post",
data,
});
}
export async function loginBySms(data: SmsLoginReq): Promise<LoginRes> {
//如果开启了登录与权限模块,则真实登录
return await request({
url: "/loginBySms",
method: "post",
data,
});
}
export async function mine(): Promise<UserInfoRes> {
return await request({
url: "/mine/info",
method: "post",
});
}
@@ -0,0 +1,134 @@
import { defineStore } from "pinia";
import router from "../../router";
// @ts-ignore
import { LocalStorage } from "/src/utils/util.storage";
// @ts-ignore
import * as UserApi from "./api.user";
import { RegisterReq, SmsLoginReq } from "./api.user";
// @ts-ignore
import { LoginReq, UserInfoRes } from "/@/api/modules/api.user";
import { message, Modal, notification } from "ant-design-vue";
import { useI18n } from "vue-i18n";
import { mitter } from "/src/utils/util.mitt";
import { resetAllStores, useAccessStore } from "/@/vben/stores";
import { useUserStore as vbenUserStore } from "/@/vben/stores/modules/user";
interface UserState {
userInfo: Nullable<UserInfoRes>;
token?: string;
}
const USER_INFO_KEY = "USER_INFO";
const TOKEN_KEY = "TOKEN";
export const useUserStore = defineStore({
id: "app.user",
state: (): UserState => ({
// user info
userInfo: null,
// token
token: undefined,
}),
getters: {
getUserInfo(): UserInfoRes {
return this.userInfo || LocalStorage.get(USER_INFO_KEY) || {};
},
getToken(): string {
return this.token || LocalStorage.get(TOKEN_KEY);
},
isAdmin(): boolean {
return this.getUserInfo.roleIds?.includes(1) || this.getUserInfo.id === 1;
},
},
actions: {
setToken(token: string, expire: number) {
this.token = token;
const accessStore = useAccessStore();
accessStore.setAccessToken(token);
LocalStorage.set(TOKEN_KEY, this.token, expire);
},
setUserInfo(info: UserInfoRes) {
this.userInfo = info;
const userStore = vbenUserStore();
userStore.setUserInfo(info);
LocalStorage.set(USER_INFO_KEY, info);
},
resetState() {
this.userInfo = null;
this.token = "";
LocalStorage.remove(TOKEN_KEY);
LocalStorage.remove(USER_INFO_KEY);
},
async register(user: RegisterReq) {
await UserApi.register(user);
notification.success({
message: "注册成功,请登录",
});
await router.replace("/login");
},
/**
* @description: login
*/
async login(loginType: string, params: LoginReq | SmsLoginReq): Promise<any> {
try {
let loginRes: any = null;
if (loginType === "sms") {
loginRes = await UserApi.loginBySms(params as SmsLoginReq);
} else {
loginRes = await UserApi.login(params as LoginReq);
}
const { token, expire } = loginRes;
// save token
this.setToken(token, expire);
// get user info
return await this.onLoginSuccess(loginRes);
} catch (error) {
console.error(error);
return null;
}
},
async getUserInfoAction(): Promise<UserInfoRes> {
const userInfo = await UserApi.mine();
this.setUserInfo(userInfo);
return userInfo;
},
async loadUserInfo() {
await this.getUserInfoAction();
},
async onLoginSuccess(loginData: any) {
// await this.getUserInfoAction();
// const userInfo = await this.getUserInfoAction();
mitter.emit("app.login", { token: loginData });
await router.replace("/");
},
/**
* @description: logout
*/
logout(goLogin = true) {
this.resetState();
resetAllStores();
goLogin && router.push("/login");
mitter.emit("app.logout");
},
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { t } = useI18n();
Modal.confirm({
iconType: "warning",
title: t("app.login.logoutTip"),
content: t("app.login.logoutMessage"),
onOk: async () => {
await this.logout(true);
},
});
},
},
});