mirror of
https://gitee.com/h_mo/uniapp-vue3-vite-ts-template
synced 2025-04-05 06:12:44 +08:00
- 更新多个依赖到最新版本,包括 uni-app、alova、pinia、tailwindcss 等 - 移除 postcss.config.ts 配置文件 - 调整 vite 配置和 tailwindcss 引入方式 - 更新 App.vue 和 main.ts 中的样式引入 - 优化 stores 中的 defineStore 使用方式 - 新增 src/styles/main.css 文件
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import type { UserInfoModel } from '@/services/model/userModel';
|
|
import { TOKEN_KEY } from '@/enums/cacheEnum';
|
|
import { login as loginApi } from '@/services/api/auth';
|
|
import { getUserInfoApi } from '@/services/api/user';
|
|
import { getToken, isLogin, setToken } from '@/utils/auth';
|
|
import { removeCache } from '@/utils/cache';
|
|
import { defineStore } from 'pinia';
|
|
|
|
export const useUserStore = defineStore('UserStore', () => {
|
|
const token = ref<string | null>(null);
|
|
const userInfo = ref<UserInfoModel | null>(null);
|
|
|
|
// 初始化
|
|
async function initUserInfo() {
|
|
if (isLogin()) {
|
|
token.value = getToken();
|
|
await getUserInfo();
|
|
}
|
|
}
|
|
|
|
// 是否登录
|
|
const loggedIn = computed(() => !!token.value);
|
|
|
|
// 登录
|
|
const { send: sendLogin } = useRequest(loginApi, { immediate: false });
|
|
async function login(params: LoginParams) {
|
|
try {
|
|
const res = await sendLogin(params);
|
|
token.value = res.token;
|
|
setToken(res.token);
|
|
await getUserInfo();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// 获取用户信息
|
|
const { send: _getUserInfo } = useRequest(getUserInfoApi, { immediate: false });
|
|
async function getUserInfo() {
|
|
try {
|
|
userInfo.value = await _getUserInfo();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// 登出
|
|
// const { send: sendLogout } = useRequest(logoutApi, { immediate: false });
|
|
async function logout() {
|
|
try {
|
|
// await sendLogout();
|
|
removeCache(TOKEN_KEY);
|
|
userInfo.value = null;
|
|
token.value = null;
|
|
} catch (err: any) {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
return {
|
|
userInfo,
|
|
loggedIn,
|
|
login,
|
|
logout,
|
|
getUserInfo,
|
|
initUserInfo,
|
|
};
|
|
});
|