mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-10 17:26:38 +08:00
压缩历史至 95081aff
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import { BaseService } from '../core/base.service';
|
||||
import type {
|
||||
OAuthLoginUrlResponse,
|
||||
OAuthCallbackRequest,
|
||||
User,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* 认证服务
|
||||
* 处理 OAuth 认证、用户信息获取、登出等
|
||||
*/
|
||||
export class AuthService extends BaseService {
|
||||
protected static readonly basePath = '/api/v1/oauth';
|
||||
|
||||
/**
|
||||
* 获取 OAuth 登录 URL
|
||||
* @returns OAuth 授权 URL
|
||||
* @throws {ApiErrorBase} 当获取失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const url = await AuthService.getLoginUrl();
|
||||
* window.location.href = url; // 重定向到 OAuth 授权页面
|
||||
* ```
|
||||
*/
|
||||
static async getLoginUrl(): Promise<OAuthLoginUrlResponse> {
|
||||
return this.get<OAuthLoginUrlResponse>('/login');
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 OAuth 回调
|
||||
* @param request - OAuth 回调参数(state 和 code)
|
||||
* @throws {ApiErrorBase} 当回调处理失败时
|
||||
* @throws {ValidationError} 当 state 无效时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 在回调页面获取 URL 参数
|
||||
* const params = new URLSearchParams(window.location.search);
|
||||
* const state = params.get('state');
|
||||
* const code = params.get('code');
|
||||
*
|
||||
* if (state && code) {
|
||||
* await AuthService.handleCallback({ state, code });
|
||||
* // 登录成功,跳转到首页
|
||||
* router.push('/home');
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
static async handleCallback(request: OAuthCallbackRequest): Promise<void> {
|
||||
return this.post<void>('/callback', request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
* @returns 用户信息
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* try {
|
||||
* const user = await AuthService.getUserInfo();
|
||||
* console.log('当前用户:', user.username);
|
||||
* } catch (error) {
|
||||
* if (error instanceof UnauthorizedError) {
|
||||
* // 跳转到登录页
|
||||
* router.push('/login');
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
static async getUserInfo(): Promise<User> {
|
||||
return this.get<User>('/user-info');
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
* @throws {ApiErrorBase} 当登出失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AuthService.logout();
|
||||
* // 清除本地状态
|
||||
* router.push('/login');
|
||||
* ```
|
||||
*/
|
||||
static async logout(): Promise<void> {
|
||||
await this.get<void>('/logout');
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起登录流程
|
||||
* 直接获取登录 URL 并重定向
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 在登录按钮点击时调用
|
||||
* await AuthService.initiateLogin();
|
||||
* ```
|
||||
*/
|
||||
static async initiateLogin(): Promise<void> {
|
||||
if (typeof window !== 'undefined') {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const callbackUrl = params.get('callbackUrl');
|
||||
|
||||
if (callbackUrl) {
|
||||
sessionStorage.setItem('redirect_after_login', callbackUrl);
|
||||
}
|
||||
}
|
||||
|
||||
const url = await this.getLoginUrl();
|
||||
if (typeof window !== 'undefined' && url) {
|
||||
window.location.href = url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 认证服务模块
|
||||
*
|
||||
* @description
|
||||
* 提供 OAuth 认证相关的功能,包括:
|
||||
* - 获取登录 URL
|
||||
* - 处理 OAuth 回调
|
||||
* - 获取用户信息
|
||||
* - 用户登出
|
||||
* - 发起登录流程
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { AuthService } from '@/lib/services';
|
||||
*
|
||||
* // 发起登录
|
||||
* await AuthService.initiateLogin();
|
||||
*
|
||||
* // 获取用户信息
|
||||
* const user = await AuthService.getUserInfo();
|
||||
* console.log('当前用户:', user.username);
|
||||
*
|
||||
* // 登出
|
||||
* await AuthService.logout();
|
||||
* ```
|
||||
*/
|
||||
|
||||
export { AuthService } from './auth.service';
|
||||
export { TrustLevel, PayLevel } from './types';
|
||||
export type {
|
||||
User,
|
||||
OAuthLoginUrlResponse,
|
||||
OAuthCallbackRequest,
|
||||
} from './types';
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 信任等级
|
||||
*/
|
||||
export enum TrustLevel {
|
||||
/** 新用户 */
|
||||
New = 0,
|
||||
/** 基础用户 */
|
||||
Basic = 1,
|
||||
/** 成员 */
|
||||
Member = 2,
|
||||
/** 常规用户 */
|
||||
Regular = 3,
|
||||
/** 领导者 */
|
||||
Leader = 4,
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付等级
|
||||
*/
|
||||
export enum PayLevel {
|
||||
/** 普通 */
|
||||
Ordinary = 0,
|
||||
/** 黄金 */
|
||||
Gold = 1,
|
||||
/** 白金 */
|
||||
WhiteGold = 2,
|
||||
/** 黑金 */
|
||||
BlackGold = 3,
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户基本信息
|
||||
*/
|
||||
export interface User {
|
||||
/** 用户 ID */
|
||||
id: string;
|
||||
/** 账户 */
|
||||
username: string;
|
||||
/** 昵称 */
|
||||
nickname: string;
|
||||
/** 信任等级 */
|
||||
trust_level: TrustLevel;
|
||||
/** 头像 URL */
|
||||
avatar_url: string;
|
||||
/** 总接收金额 */
|
||||
total_receive: string;
|
||||
/** 总支付金额 */
|
||||
total_payment: string;
|
||||
/** 总社区金额 */
|
||||
total_community: string;
|
||||
/** 社区余额 */
|
||||
community_balance: string;
|
||||
/** 可用余额 */
|
||||
available_balance: string;
|
||||
/** 在途资金(延迟到账中) */
|
||||
pending_balance: string;
|
||||
/** 支付分数 */
|
||||
pay_score: number;
|
||||
/** 是否有支付密钥 */
|
||||
is_pay_key: boolean;
|
||||
/** 是否为管理员 */
|
||||
is_admin: boolean;
|
||||
/** 当日剩余配额 */
|
||||
remain_quota: string;
|
||||
/** 支付等级 */
|
||||
pay_level: PayLevel;
|
||||
/** 每日限额 */
|
||||
daily_limit: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth 登录 URL 响应
|
||||
* 后端直接返回字符串 URL
|
||||
*/
|
||||
export type OAuthLoginUrlResponse = string;
|
||||
|
||||
/**
|
||||
* OAuth 回调请求参数
|
||||
*/
|
||||
export interface OAuthCallbackRequest {
|
||||
/** 状态码 */
|
||||
state: string;
|
||||
/** 授权码 */
|
||||
code: string;
|
||||
}
|
||||
Reference in New Issue
Block a user