This commit is contained in:
Putoo
2026-04-26 18:53:56 +08:00
parent 2c6c62e88c
commit 4463f9e810
20 changed files with 461 additions and 1035 deletions

View File

@@ -1,7 +1,4 @@
import { navigateTo } from "#app";
import { RequestExtend } from "@/extends/RequestExtend";
import { BaseConfig } from "@/config/BaseConfig";
import type { IResultData } from "@/model/common/ResultData";
type HttpMethod = "get" | "post" | "put" | "delete" | "patch";
type RequestParams = Record<string, unknown>;
@@ -35,7 +32,7 @@ export class ApiService {
}
if (typeof window !== "undefined") {
void navigateTo("/home", { replace: true });
PageExtend.Redirect("/home");
}
}
@@ -95,7 +92,7 @@ export class ApiService {
this.initialized = true;
}
public static async ApiRequest<T = unknown>(
public static async Request<T = unknown>(
method: HttpMethod,
url: string,
params: RequestParams = {}

View File

@@ -1,164 +0,0 @@
/**
* 权限校验组合式函数
* 提供登录检查、权限验证等功能
*/
import { useUserStore } from '~/stores/user'
import { useAppStore } from '~/stores/app'
/**
* 权限校验hook
* 用于页面/组件中的权限校验
*/
export const useAuth = () => {
const userStore = useUserStore()
const appStore = useAppStore()
/**
* 检查是否已登录
* @returns boolean 是否已登录
*/
const isAuthenticated = (): boolean => {
return userStore.isLogin
}
/**
* 检查是否未登录
* @returns boolean 是否未登录
*/
const isGuest = (): boolean => {
return !userStore.isLogin
}
/**
* 检查是否拥有指定角色
* @param roles 角色数组
* @returns boolean 是否有权限
*/
const hasRole = (roles: string | string[]): boolean => {
if (!userStore.isLogin) return false
const userRole = userStore.userRole
const roleList = Array.isArray(roles) ? roles : [roles]
return roleList.includes(userRole)
}
/**
* 检查是否为管理员
* @returns boolean 是否为管理员
*/
const isAdmin = (): boolean => {
return hasRole('admin')
}
/**
* 获取当前用户ID
* @returns number 用户ID
*/
const getUserId = (): number => {
return userStore.userId
}
/**
* 获取当前用户信息
* @returns IUserInfo | null 用户信息
*/
const getUserInfo = () => {
return userStore.userInfo
}
/**
* 获取当前用户昵称
* @returns string 用户昵称
*/
const getNickname = (): string => {
return userStore.userNickname
}
/**
* 跳转到登录页(如果未登录)
* @param redirectUrl 登录后重定向的URL
*/
const requireAuth = (redirectUrl?: string) => {
if (!userStore.isLogin) {
if (typeof window !== 'undefined') {
const url = redirectUrl || window.location.href
window.location.href = `/login?redirect=${encodeURIComponent(url)}`
}
return false
}
return true
}
/**
* 跳转到登录页(如果已登录)
* @param redirectUrl 登录后重定向的URL
*/
const requireGuest = (redirectUrl: string = '/') => {
if (userStore.isLogin) {
if (typeof window !== 'undefined') {
window.location.href = redirectUrl
}
return false
}
return true
}
/**
* 检查功能权限(基于角色)
* @param permission 权限标识
* @returns boolean 是否有权限
*/
const hasPermission = (permission: string): boolean => {
// 简化实现,实际项目中可以结合后端返回的权限列表
if (!userStore.isLogin) return false
// 管理员拥有所有权限
if (userStore.userRole === 'admin') return true
// TODO: 可以从用户信息中获取权限列表进行匹配
return false
}
/**
* 登出
* @param redirectUrl 退出后重定向的URL
*/
const logout = async (redirectUrl: string = '/login') => {
try {
// 调用退出登录API如果需要
// const userService = new UserSERVICE()
// await userService.logout()
} finally {
// 清除用户状态
userStore.clearUserInfo()
// 跳转到登录页
if (typeof window !== 'undefined') {
window.location.href = redirectUrl
}
}
}
return {
// 状态
isLogin: computed(() => userStore.isLogin),
userInfo: computed(() => userStore.userInfo),
userId: computed(() => userStore.userId),
nickname: computed(() => userStore.userNickname),
userRole: computed(() => userStore.userRole),
// 方法
isAuthenticated,
isGuest,
hasRole,
isAdmin,
getUserId,
getUserInfo,
getNickname,
requireAuth,
requireGuest,
hasPermission,
logout
}
}

View File

@@ -0,0 +1,34 @@
import type { EventBusHandler } from '~/extends/EventBusExtend'
/**
* 事件总线组合式封装
* 在组件作用域内订阅时会自动解绑
*/
export const useEventBus = () => {
const registerDispose = (unsubscribe: () => void) => {
if (getCurrentScope()) {
onScopeDispose(unsubscribe)
}
return unsubscribe
}
const on = <T = unknown>(event: string, handler: EventBusHandler<T>) => {
return registerDispose(EventBusExtend.on(event, handler))
}
const off = <T = unknown>(event: string, handler?: EventBusHandler<T>) => {
EventBusExtend.off(event, handler)
}
const emit = <T = unknown>(event: string, payload?: T) => {
EventBusExtend.emit(event, payload)
}
return {
on,
off,
emit,
clear: EventBusExtend.clear
}
}