feat: 完善 ele 的请求、路由、百度统计、概览、登录
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import type { Recordable, UserInfo } from '@vben/types';
|
||||
import type { AuthPermissionInfo, Recordable, UserInfo } from '@vben/types';
|
||||
|
||||
import type { AuthApi } from '#/api';
|
||||
|
||||
import { ref } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
@@ -10,7 +12,14 @@ import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api';
|
||||
import {
|
||||
getAuthPermissionInfoApi,
|
||||
loginApi,
|
||||
logoutApi,
|
||||
register,
|
||||
smsLogin,
|
||||
socialLogin,
|
||||
} from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
@@ -23,9 +32,12 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
/**
|
||||
* 异步处理登录操作
|
||||
* Asynchronously handle the login process
|
||||
* @param type 登录类型
|
||||
* @param params 登录表单数据
|
||||
* @param onSuccess 登录成功后的回调函数
|
||||
*/
|
||||
async function authLogin(
|
||||
type: 'mobile' | 'register' | 'social' | 'username',
|
||||
params: Recordable<any>,
|
||||
onSuccess?: () => Promise<void> | void,
|
||||
) {
|
||||
@@ -33,23 +45,30 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
let userInfo: null | UserInfo = null;
|
||||
try {
|
||||
loginLoading.value = true;
|
||||
const { accessToken } = await loginApi(params);
|
||||
const { accessToken, refreshToken } =
|
||||
type === 'mobile'
|
||||
? await smsLogin(params as AuthApi.SmsLoginParams)
|
||||
: type === 'register'
|
||||
? await register(params as AuthApi.RegisterParams)
|
||||
: // eslint-disable-next-line unicorn/no-nested-ternary
|
||||
type === 'social'
|
||||
? await socialLogin(params as AuthApi.SocialLoginParams)
|
||||
: await loginApi(params);
|
||||
|
||||
// 如果成功获取到 accessToken
|
||||
if (accessToken) {
|
||||
// 将 accessToken 存储到 accessStore 中
|
||||
accessStore.setAccessToken(accessToken);
|
||||
accessStore.setRefreshToken(refreshToken);
|
||||
|
||||
// 获取用户信息并存储到 accessStore 中
|
||||
const [fetchUserInfoResult, accessCodes] = await Promise.all([
|
||||
fetchUserInfo(),
|
||||
getAccessCodesApi(),
|
||||
]);
|
||||
// 获取用户信息并存储到 userStore、accessStore 中
|
||||
// TODO @芋艿:清理掉 accessCodes 相关的逻辑
|
||||
// const [fetchUserInfoResult, accessCodes] = await Promise.all([
|
||||
// fetchUserInfo(),
|
||||
// // getAccessCodesApi(),
|
||||
// ]);
|
||||
const fetchUserInfoResult = await fetchUserInfo();
|
||||
|
||||
userInfo = fetchUserInfoResult;
|
||||
|
||||
userStore.setUserInfo(userInfo);
|
||||
accessStore.setAccessCodes(accessCodes);
|
||||
userInfo = fetchUserInfoResult.user;
|
||||
|
||||
if (accessStore.loginExpired) {
|
||||
accessStore.setLoginExpired(false);
|
||||
@@ -61,11 +80,11 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
);
|
||||
}
|
||||
|
||||
if (userInfo?.realName) {
|
||||
ElNotification({
|
||||
message: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
|
||||
if (userInfo?.nickname) {
|
||||
ElNotification.success({
|
||||
message: `${$t('authentication.loginSuccessDesc')}:${userInfo?.nickname}`,
|
||||
duration: 3,
|
||||
title: $t('authentication.loginSuccess'),
|
||||
type: 'success',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -80,7 +99,10 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
|
||||
async function logout(redirect: boolean = true) {
|
||||
try {
|
||||
await logoutApi();
|
||||
const accessToken = accessStore.accessToken as string;
|
||||
if (accessToken) {
|
||||
await logoutApi(accessToken);
|
||||
}
|
||||
} catch {
|
||||
// 不做任何处理
|
||||
}
|
||||
@@ -99,10 +121,16 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
}
|
||||
|
||||
async function fetchUserInfo() {
|
||||
let userInfo: null | UserInfo = null;
|
||||
userInfo = await getUserInfoApi();
|
||||
userStore.setUserInfo(userInfo);
|
||||
return userInfo;
|
||||
// 加载
|
||||
let authPermissionInfo: AuthPermissionInfo | null;
|
||||
authPermissionInfo = await getAuthPermissionInfoApi();
|
||||
// userStore
|
||||
userStore.setUserInfo(authPermissionInfo.user);
|
||||
userStore.setUserRoles(authPermissionInfo.roles);
|
||||
// accessStore
|
||||
accessStore.setAccessMenus(authPermissionInfo.menus);
|
||||
accessStore.setAccessCodes(authPermissionInfo.permissions);
|
||||
return authPermissionInfo;
|
||||
}
|
||||
|
||||
function $reset() {
|
||||
|
||||
74
apps/web-ele/src/store/dict.ts
Normal file
74
apps/web-ele/src/store/dict.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||
|
||||
export interface DictItem {
|
||||
colorType?: string;
|
||||
cssClass?: string;
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export type Dict = Record<string, DictItem[]>;
|
||||
|
||||
interface DictState {
|
||||
dictCache: Dict;
|
||||
}
|
||||
|
||||
// TODO @芋艿:可以共享么?
|
||||
export const useDictStore = defineStore('dict', {
|
||||
actions: {
|
||||
getDictData(dictType: string, value: any) {
|
||||
const dict = this.dictCache[dictType];
|
||||
if (!dict) {
|
||||
return undefined;
|
||||
}
|
||||
return (
|
||||
dict.find((d) => d.value === value || d.value === value.toString()) ??
|
||||
undefined
|
||||
);
|
||||
},
|
||||
getDictOptions(dictType: string) {
|
||||
const dictOptions = this.dictCache[dictType];
|
||||
if (!dictOptions) {
|
||||
return [];
|
||||
}
|
||||
return dictOptions;
|
||||
},
|
||||
setDictCache(dicts: Dict) {
|
||||
this.dictCache = dicts;
|
||||
},
|
||||
setDictCacheByApi(
|
||||
api: (params: Record<string, any>) => Promise<Record<string, any>[]>,
|
||||
params: Record<string, any> = {},
|
||||
labelField: string = 'label',
|
||||
valueField: string = 'value',
|
||||
) {
|
||||
api(params).then((dicts) => {
|
||||
const dictCacheData: Dict = {};
|
||||
dicts.forEach((dict) => {
|
||||
dictCacheData[dict.dictType] = dicts
|
||||
.filter((d) => d.dictType === dict.dictType)
|
||||
.map((d) => ({
|
||||
colorType: d.colorType,
|
||||
cssClass: d.cssClass,
|
||||
label: d[labelField],
|
||||
value: d[valueField],
|
||||
}));
|
||||
});
|
||||
this.setDictCache(dictCacheData);
|
||||
});
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
// 持久化
|
||||
pick: ['dictCache'],
|
||||
},
|
||||
state: (): DictState => ({
|
||||
dictCache: {},
|
||||
}),
|
||||
});
|
||||
|
||||
// 解决热更新问题
|
||||
const hot = import.meta.hot;
|
||||
if (hot) {
|
||||
hot.accept(acceptHMRUpdate(useDictStore, hot));
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from './auth';
|
||||
export * from './dict';
|
||||
|
||||
Reference in New Issue
Block a user