2026-01-30 12:31:24 +08:00
|
|
|
|
import { login } from "@/api";
|
|
|
|
|
|
import { Session } from "@/utils/storage";
|
|
|
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
|
import { computed, ref } from "vue";
|
|
|
|
|
|
|
|
|
|
|
|
type IUserInfos = {
|
|
|
|
|
|
id?: string;
|
|
|
|
|
|
phone: string;
|
|
|
|
|
|
name?: string;
|
|
|
|
|
|
auth: string[];
|
|
|
|
|
|
role: string[];
|
2026-02-06 17:10:10 +08:00
|
|
|
|
accountType: number;
|
2026-01-30 12:31:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const storeSetup = () => {
|
|
|
|
|
|
const token = ref(Session.get("token"));
|
|
|
|
|
|
const userInfos = ref<IUserInfos>({
|
|
|
|
|
|
phone: "",
|
|
|
|
|
|
name: "",
|
2026-02-06 17:10:10 +08:00
|
|
|
|
auth: [], // 首页导航菜单权限存在, 暂不使用
|
|
|
|
|
|
role: [], // 角色,暂不使用
|
|
|
|
|
|
accountType: 0, // 账号类型,0:测试 ,1:正式
|
2026-01-30 12:31:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const setUserInfos = (payload: Partial<IUserInfos>) => {
|
|
|
|
|
|
userInfos.value = { ...userInfos.value, ...payload };
|
|
|
|
|
|
Session.set("userInfos", userInfos.value);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getUserInfos = () => {
|
|
|
|
|
|
if (Session.get("userInfos")) {
|
|
|
|
|
|
userInfos.value = Session.get("userInfos");
|
|
|
|
|
|
}
|
|
|
|
|
|
return userInfos.value;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 token
|
|
|
|
|
|
const setToken = (payload: string) => {
|
|
|
|
|
|
token.value = payload;
|
|
|
|
|
|
Session.set("token", payload);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取 token
|
|
|
|
|
|
const getToken = () => {
|
|
|
|
|
|
return token.value;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 登录状态
|
|
|
|
|
|
const isLogin = computed(() => {
|
|
|
|
|
|
return !!token.value;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 登录
|
2026-02-06 11:59:14 +08:00
|
|
|
|
const onLogin = async (data: any): Promise<any> => {
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
|
title: "登录中",
|
|
|
|
|
|
mask: true,
|
2026-01-30 12:31:24 +08:00
|
|
|
|
});
|
2026-02-06 11:59:14 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const params = {
|
|
|
|
|
|
phone: data.phone,
|
|
|
|
|
|
smsCode: data.code,
|
|
|
|
|
|
};
|
|
|
|
|
|
const result = await login(params);
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
if (result.code === 200) {
|
|
|
|
|
|
window.aplus_queue.push({
|
|
|
|
|
|
action: "aplus.record",
|
|
|
|
|
|
arguments: [
|
|
|
|
|
|
"login",
|
|
|
|
|
|
"CLK",
|
|
|
|
|
|
{
|
|
|
|
|
|
phone: data.phone,
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
setToken(result.data.token);
|
|
|
|
|
|
setUserInfos({ ...result.data });
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: "登录成功",
|
|
|
|
|
|
icon: "success",
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: result.msg,
|
|
|
|
|
|
icon: "error",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
resolve(result);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
console.log(error, "<=== error");
|
|
|
|
|
|
reject(error);
|
2026-01-30 12:31:24 +08:00
|
|
|
|
}
|
2026-02-06 11:59:14 +08:00
|
|
|
|
});
|
2026-01-30 12:31:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return { isLogin, userInfos, getToken, onLogin, getUserInfos };
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore("user", storeSetup);
|