2025-06-29 15:28:57 +08:00
|
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
|
import { authenticationApi, loginCode, loginPass, weChatLogin } from "@/api/system/login";
|
|
|
|
|
|
import { AUTH_KEY } from "@/config";
|
|
|
|
|
|
import { imagUrl } from "@/utils";
|
2025-07-21 14:34:20 +08:00
|
|
|
|
import { useWebSocket } from '@/utils/webSocket/webSocket'
|
2025-04-30 01:43:23 +08:00
|
|
|
|
|
|
|
|
|
|
interface UserState {
|
2025-06-29 15:28:57 +08:00
|
|
|
|
userdata: any;
|
|
|
|
|
|
curXs: any;
|
|
|
|
|
|
token: string;
|
2025-07-21 14:34:20 +08:00
|
|
|
|
auth: string[],
|
|
|
|
|
|
ws: any,
|
|
|
|
|
|
wsCallback: any
|
2025-04-30 01:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore({
|
2025-06-29 15:28:57 +08:00
|
|
|
|
id: "app-user",
|
|
|
|
|
|
state: (): UserState => ({
|
|
|
|
|
|
//用户数据
|
|
|
|
|
|
userdata: '',
|
|
|
|
|
|
// 当前学生
|
|
|
|
|
|
curXs: {},
|
|
|
|
|
|
// token
|
|
|
|
|
|
token: '',
|
|
|
|
|
|
//用户注册信息
|
2025-07-21 14:34:20 +08:00
|
|
|
|
auth: [],
|
|
|
|
|
|
ws: null,
|
|
|
|
|
|
wsCallback: (type: string, res: any) => {}
|
2025-06-29 15:28:57 +08:00
|
|
|
|
}),
|
|
|
|
|
|
getters: {
|
|
|
|
|
|
getToken(): string {
|
|
|
|
|
|
return this.token;
|
|
|
|
|
|
},
|
|
|
|
|
|
getUser(): any {
|
|
|
|
|
|
return this.userdata;
|
|
|
|
|
|
},
|
|
|
|
|
|
getCurXs(): any {
|
|
|
|
|
|
return this.curXs;
|
|
|
|
|
|
},
|
|
|
|
|
|
getAuth(): string[] {
|
|
|
|
|
|
return this.auth;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
actions: {
|
|
|
|
|
|
setToken(token: string) {
|
|
|
|
|
|
this.token = token
|
|
|
|
|
|
},
|
|
|
|
|
|
setUser(data: any) {
|
|
|
|
|
|
this.userdata = data;
|
|
|
|
|
|
},
|
|
|
|
|
|
setCurXs(data: any) {
|
|
|
|
|
|
Object.assign(this.curXs, data);
|
2025-04-30 01:43:23 +08:00
|
|
|
|
},
|
2025-06-29 15:28:57 +08:00
|
|
|
|
setAuth(data: string[]) {
|
|
|
|
|
|
this.auth = data;
|
|
|
|
|
|
},
|
2025-07-21 14:34:20 +08:00
|
|
|
|
setWsCallback(callback: any) {
|
|
|
|
|
|
this.wsCallback = callback;
|
|
|
|
|
|
},
|
2025-06-29 15:28:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description: 验证码登录
|
|
|
|
|
|
*/
|
|
|
|
|
|
async codeLogin(params: { phone: number | string, code: number | string }) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { result } = await loginCode({ phone: params.phone, code: params.code, openId: '' });
|
|
|
|
|
|
this.afterLoginAction(result)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.log(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description: 密码登录
|
|
|
|
|
|
*/
|
|
|
|
|
|
async passwordLogin(params: { name: string, password: string }) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { result } = await loginPass({ username: params.name, password: params.password, openId: '' });
|
|
|
|
|
|
this.afterLoginAction(result)
|
|
|
|
|
|
} catch (e) {
|
2025-04-30 01:43:23 +08:00
|
|
|
|
|
2025-06-29 15:28:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description: 微信登录
|
|
|
|
|
|
*/
|
|
|
|
|
|
async weChatLogin(params: { code: string }) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { result } = await weChatLogin({ code: params.code })
|
|
|
|
|
|
this.afterLoginAction(result)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.log(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description: 登录成功后的操作
|
|
|
|
|
|
*/
|
|
|
|
|
|
afterLoginAction(value: any) {
|
|
|
|
|
|
this.setUser(value)
|
|
|
|
|
|
if (value.xsList && value.xsList.length > 0) {
|
|
|
|
|
|
for (let i = 0; i < value.xsList.length; i++) {
|
|
|
|
|
|
const xs = value.xsList[i];
|
|
|
|
|
|
xs.xstxUrl = imagUrl(xs.xstx) || "";
|
|
|
|
|
|
}
|
|
|
|
|
|
this.setCurXs(value.xsList[0])
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.setCurXs({})
|
|
|
|
|
|
}
|
|
|
|
|
|
if (value[AUTH_KEY]) {
|
|
|
|
|
|
this.setToken(value[AUTH_KEY])
|
|
|
|
|
|
}
|
2025-07-21 14:34:20 +08:00
|
|
|
|
if (!this.ws) {
|
|
|
|
|
|
this.ws = useWebSocket(`/zhxy/webSocket/${value.userId}`, (type: string, res: any) => {
|
|
|
|
|
|
// 判断this.wsCallback是函数,调用
|
|
|
|
|
|
if (typeof this.wsCallback === "function") {
|
|
|
|
|
|
this.wsCallback(type, res);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
this.ws.reconnect();
|
|
|
|
|
|
}
|
2025-06-29 15:28:57 +08:00
|
|
|
|
authenticationApi({ userId: value.userId }).then(({ result }) => {
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
this.setAuth(result)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-04-30 01:43:23 +08:00
|
|
|
|
},
|
2025-06-29 15:28:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description: 注销
|
|
|
|
|
|
*/
|
|
|
|
|
|
logout() {
|
|
|
|
|
|
this.setToken('')
|
|
|
|
|
|
this.setUser('')
|
|
|
|
|
|
this.setCurXs({})
|
|
|
|
|
|
this.setAuth([])
|
2025-07-21 14:34:20 +08:00
|
|
|
|
if (this.ws) {
|
|
|
|
|
|
this.ws.closeConnect();
|
|
|
|
|
|
this.ws = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.wsCallback((type: string, res: any) => {});
|
2025-04-30 01:43:23 +08:00
|
|
|
|
},
|
2025-06-29 15:28:57 +08:00
|
|
|
|
},
|
|
|
|
|
|
persist: {
|
|
|
|
|
|
enabled: true,
|
|
|
|
|
|
detached: true,
|
|
|
|
|
|
H5Storage: localStorage
|
|
|
|
|
|
},
|
2025-04-30 01:43:23 +08:00
|
|
|
|
});
|