feat: 支持登录、个人信息存储

This commit is contained in:
richarjiang
2026-04-05 13:37:58 +08:00
parent e438f6fce4
commit b732e4d8f8
23 changed files with 3572 additions and 144 deletions

View File

@@ -0,0 +1,146 @@
import { HttpUtil } from './HttpUtil';
import { StorageManager } from './StorageManager';
import { WxSDK } from './WxSDK';
import { API_ENDPOINTS, API_TIMEOUT } from '../config/ApiConfig';
import { ApiEnvelope, WxLoginData, GameData } from '../types/ApiTypes';
/**
* 认证管理器
* 单例模式,负责微信登录和 JWT token 管理
*/
export class AuthManager {
private static _instance: AuthManager | null = null;
private _userId: string = '';
private _isLoggedIn: boolean = false;
/** 服务端返回的已完成关卡 ID登录后暂存等 LevelDataManager 就绪后同步) */
private _completedLevelIds: string[] = [];
/** 服务端返回的已完成关卡 ID登录后暂存等 LevelDataManager 就绪后同步) */
private _completedLevelIds: string[] = [];
static get instance(): AuthManager {
if (!this._instance) {
this._instance = new AuthManager();
}
return this._instance;
}
private constructor() {}
get isLoggedIn(): boolean {
return this._isLoggedIn;
}
get userId(): string {
return this._userId;
}
get completedLevelIds(): string[] {
return this._completedLevelIds;
}
/**
* 初始化认证:尝试恢复 token 或执行微信登录
*/
async initialize(): Promise<boolean> {
const savedToken = StorageManager.getToken();
if (savedToken) {
HttpUtil.setAuthToken(savedToken);
try {
const valid = await this.validateToken();
if (valid) {
console.log('[AuthManager] Token 恢复成功');
return true;
}
} catch {
console.warn('[AuthManager] 本地 token 无效,重新登录');
}
}
return this.wxLogin();
}
private async wxLogin(): Promise<boolean> {
try {
let code: string;
if (WxSDK.isWechat()) {
code = await WxSDK.login();
} else {
console.warn('[AuthManager] 非微信环境,使用开发模式 mock code');
code = 'dev_mock_code';
}
const response = await HttpUtil.post<ApiEnvelope<WxLoginData>>(
API_ENDPOINTS.WX_LOGIN,
{ code },
API_TIMEOUT.DEFAULT
);
if (!response.success || !response.data) {
console.error('[AuthManager] 登录失败:', response.message);
return false;
}
const { token, user } = response.data;
HttpUtil.setAuthToken(token);
StorageManager.setToken(token);
this._userId = user.id;
this._isLoggedIn = true;
StorageManager.setPoints(user.points);
// 获取通关进度
await this.fetchCompletedLevels();
console.log(`[AuthManager] 登录成功,用户: ${user.id},积分: ${user.points}`);
return true;
} catch (err) {
console.error('[AuthManager] 登录异常:', err);
return false;
}
}
private async validateToken(): Promise<boolean> {
try {
const response = await HttpUtil.get<ApiEnvelope<GameData>>(
API_ENDPOINTS.USER_GAME_DATA,
API_TIMEOUT.SHORT
);
if (!response.success || !response.data) {
return false;
}
this._userId = response.data.user.id;
this._isLoggedIn = true;
StorageManager.setPoints(response.data.user.points);
this._completedLevelIds = response.data.completedLevelIds;
console.log(`[AuthManager] Token 验证成功,积分: ${response.data.user.points},已完成: ${this._completedLevelIds.length}`);
return true;
} catch {
return false;
}
}
/**
* 登录成功后获取通关进度
*/
private async fetchCompletedLevels(): Promise<void> {
try {
const response = await HttpUtil.get<ApiEnvelope<GameData>>(
API_ENDPOINTS.USER_GAME_DATA,
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
this._completedLevelIds = response.data.completedLevelIds;
// 同步最新积分
StorageManager.setPoints(response.data.user.points);
}
} catch {
console.warn('[AuthManager] 获取通关进度失败');
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "69e4504f-57ab-43b0-b02d-8732cbef7c7f",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,8 +1,27 @@
/**
* HTTP 请求工具类
* 封装 XMLHttpRequest支持 GET/POST 请求
* 封装 XMLHttpRequest支持 GET/POST 请求,支持 JWT 认证
*/
export class HttpUtil {
/** 认证 token */
private static _authToken: string | null = null;
/**
* 设置认证 token
* @param token JWT token
*/
static setAuthToken(token: string | null): void {
HttpUtil._authToken = token;
console.log(`[HttpUtil] Auth token ${token ? '已设置' : '已清除'}`);
}
/**
* 获取认证 token
*/
static getAuthToken(): string | null {
return HttpUtil._authToken;
}
/**
* 发送 GET 请求
* @param url 请求 URL
@@ -17,6 +36,11 @@ export class HttpUtil {
xhr.timeout = timeout;
xhr.responseType = 'json';
// 设置认证头
if (HttpUtil._authToken) {
xhr.setRequestHeader('Authorization', `Bearer ${HttpUtil._authToken}`);
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response as T);
@@ -53,6 +77,11 @@ export class HttpUtil {
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/json');
// 设置认证头
if (HttpUtil._authToken) {
xhr.setRequestHeader('Authorization', `Bearer ${HttpUtil._authToken}`);
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response as T);

View File

@@ -1,6 +1,7 @@
import { SpriteFrame, Texture2D, ImageAsset, assetManager } from 'cc';
import { HttpUtil } from './HttpUtil';
import { ApiLevelData, ApiResponse, RuntimeLevelConfig } from '../types/LevelTypes';
import { API_ENDPOINTS, API_TIMEOUT } from '../config/ApiConfig';
/**
* 进度回调类型
@@ -16,12 +17,6 @@ export type ProgressCallback = (progress: number, message: string) => void;
export class LevelDataManager {
private static _instance: LevelDataManager | null = null;
/** API 地址 */
private readonly API_URL = 'https://ilookai.cn/api/v1/wechat-game/levels';
/** 请求超时时间(毫秒) */
private readonly REQUEST_TIMEOUT = 8000;
/** API 请求重试次数 */
private readonly API_RETRY_COUNT = 2;
@@ -114,6 +109,28 @@ export class LevelDataManager {
return this._apiData.length;
}
/**
* 根据已完成的关卡 ID 列表,计算最高已完成关卡索引
* @param completedLevelIds 服务端返回的已完成关卡 ID
* @returns 最高已完成关卡的索引0-based无匹配返回 -1
*/
getMaxCompletedIndex(completedLevelIds: string[]): number {
if (!this._hasApiData || completedLevelIds.length === 0) {
return -1;
}
const completedSet = new Set(completedLevelIds);
let maxIndex = -1;
for (let i = 0; i < this._apiData.length; i++) {
if (completedSet.has(this._apiData[i].id)) {
maxIndex = i;
}
}
return maxIndex;
}
/**
* 检查是否有 API 数据
*/
@@ -220,7 +237,7 @@ export class LevelDataManager {
try {
onProgress?.(progress, `正在请求服务端数据 (第${attempt}次)...`);
const response = await HttpUtil.get<ApiResponse>(this.API_URL, this.REQUEST_TIMEOUT);
const response = await HttpUtil.get<ApiResponse>(API_ENDPOINTS.LEVELS, API_TIMEOUT.DEFAULT);
if (!response.success) {
console.warn(`[LevelDataManager] API 返回失败, 消息: ${response.message}`);

View File

@@ -15,17 +15,20 @@ interface UserProgress {
* 统一管理用户数据的本地持久化存储
*/
export class StorageManager {
/** 生命值存储键 */
private static readonly KEY_LIVES = 'game_lives';
/** 积分存储键 */
private static readonly KEY_POINTS = 'game_points';
/** 用户进度存储键 */
private static readonly KEY_PROGRESS = 'game_progress';
/** 默认生命值 */
private static readonly DEFAULT_LIVES = 10;
/** 认证 token 存储键 */
private static readonly KEY_TOKEN = 'auth_token';
/** 最小生命值 */
private static readonly MIN_LIVES = 0;
/** 默认积分 */
private static readonly DEFAULT_POINTS = 10;
/** 最小积分 */
private static readonly MIN_POINTS = 0;
/** 默认进度 */
private static readonly DEFAULT_PROGRESS: UserProgress = {
@@ -36,75 +39,101 @@ export class StorageManager {
/** 进度缓存(避免重复读取 localStorage */
private static _progressCache: UserProgress | null = null;
// ==================== 生命值管理 ====================
// ==================== 积分管理 ====================
/**
* 获取当前生命值
* @returns 当前生命值,新用户返回默认值 10
* 获取当前积分
* @returns 当前积分,新用户返回默认值 10
*/
static getLives(): number {
const stored = sys.localStorage.getItem(StorageManager.KEY_LIVES);
static getPoints(): number {
const stored = sys.localStorage.getItem(StorageManager.KEY_POINTS);
if (stored === null || stored === '') {
// 新用户,设置默认值
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
return StorageManager.DEFAULT_LIVES;
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
return StorageManager.DEFAULT_POINTS;
}
const lives = parseInt(stored, 10);
const points = parseInt(stored, 10);
// 防止异常数据
if (isNaN(lives) || lives < 0) {
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
return StorageManager.DEFAULT_LIVES;
if (isNaN(points) || points < 0) {
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
return StorageManager.DEFAULT_POINTS;
}
return lives;
return points;
}
/**
* 设置生命值
* @param lives 生命值
* 设置积分
* @param points 积分
*/
static setLives(lives: number): void {
const validLives = Math.max(StorageManager.MIN_LIVES, lives);
sys.localStorage.setItem(StorageManager.KEY_LIVES, validLives.toString());
console.log(`[StorageManager] 生命值已更新: ${validLives}`);
static setPoints(points: number): void {
const validPoints = Math.max(StorageManager.MIN_POINTS, points);
sys.localStorage.setItem(StorageManager.KEY_POINTS, validPoints.toString());
console.log(`[StorageManager] 积分已更新: ${validPoints}`);
}
/**
* 消耗一颗生命
* @returns 是否消耗成功(生命值不足时返回 false
* 消耗一个积分
* @returns 是否消耗成功(积分不足时返回 false
*/
static consumeLife(): boolean {
const currentLives = StorageManager.getLives();
if (currentLives <= 0) {
console.warn('[StorageManager] 生命值不足,无法消耗');
static consumePoint(): boolean {
const currentPoints = StorageManager.getPoints();
if (currentPoints <= 0) {
console.warn('[StorageManager] 积分不足,无法消耗');
return false;
}
StorageManager.setLives(currentLives - 1);
StorageManager.setPoints(currentPoints - 1);
return true;
}
/**
* 增加一颗生命
* 增加一个积分
*/
static addLife(): void {
const currentLives = StorageManager.getLives();
StorageManager.setLives(currentLives + 1);
console.log(`[StorageManager] 获得一颗生命,当前生命值: ${currentLives + 1}`);
static addPoint(): void {
const currentPoints = StorageManager.getPoints();
StorageManager.setPoints(currentPoints + 1);
console.log(`[StorageManager] 获得一个积分,当前积分: ${currentPoints + 1}`);
}
/**
* 检查是否有足够的生命值
* @returns 是否有生命值
* 检查是否有足够的积分
* @returns 是否有积分
*/
static hasLives(): boolean {
return StorageManager.getLives() > 0;
static hasPoints(): boolean {
return StorageManager.getPoints() > 0;
}
/**
* 重置生命值为默认值
* 重置积分为默认值
*/
static resetLives(): void {
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
console.log('[StorageManager] 生命值已重置为默认值');
static resetPoints(): void {
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
console.log('[StorageManager] 积分已重置为默认值');
}
// ==================== 认证 Token 管理 ====================
/**
* 获取认证 token
*/
static getToken(): string | null {
const token = sys.localStorage.getItem(StorageManager.KEY_TOKEN);
return (token === null || token === '') ? null : token;
}
/**
* 设置认证 token
*/
static setToken(token: string): void {
sys.localStorage.setItem(StorageManager.KEY_TOKEN, token);
console.log('[StorageManager] Token 已保存');
}
/**
* 清除认证 token
*/
static clearToken(): void {
sys.localStorage.removeItem(StorageManager.KEY_TOKEN);
console.log('[StorageManager] Token 已清除');
}
// ==================== 关卡进度管理 ====================
@@ -229,11 +258,12 @@ export class StorageManager {
}
/**
* 重置所有数据(生命值 + 进度)
* 重置所有数据(积分 + 进度)
*/
static resetAll(): void {
StorageManager.resetLives();
StorageManager.resetPoints();
StorageManager.resetProgress();
StorageManager.clearToken();
console.log('[StorageManager] 所有数据已重置');
}
}

View File

@@ -0,0 +1,117 @@
import { HttpUtil } from './HttpUtil';
import { StorageManager } from './StorageManager';
import { AuthManager } from './AuthManager';
import { API_ENDPOINTS, API_TIMEOUT, POINT_REASONS } from '../config/ApiConfig';
import { ApiEnvelope, UserAssetsData } from '../types/ApiTypes';
/**
* 用户资产管理器
* 单例模式,负责积分的服务端同步
* 以服务端为准,本地 StorageManager 作为缓存
*/
export class UserAssetsManager {
private static _instance: UserAssetsManager | null = null;
static get instance(): UserAssetsManager {
if (!this._instance) {
this._instance = new UserAssetsManager();
}
return this._instance;
}
private constructor() {}
/**
* 从服务端获取最新积分
*/
async fetchPoints(): Promise<number> {
if (!AuthManager.instance.isLoggedIn) {
return StorageManager.getPoints();
}
try {
const response = await HttpUtil.get<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS,
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return response.data.points;
}
} catch (err) {
console.error('[UserAssetsManager] 获取积分失败:', err);
}
return StorageManager.getPoints();
}
/**
* 消耗积分(解锁提示)
* @returns 是否消耗成功
*/
async consumePoint(levelId?: string, hintIndex?: number): Promise<boolean> {
if (!StorageManager.hasPoints()) {
return false;
}
if (!AuthManager.instance.isLoggedIn) {
return StorageManager.consumePoint();
}
try {
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS_CONSUME,
{
reason: POINT_REASONS.HINT_UNLOCK,
levelId,
hintIndex,
},
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return true;
} else {
console.warn('[UserAssetsManager] 消耗积分失败:', response.message);
return false;
}
} catch (err) {
console.error('[UserAssetsManager] 消耗积分请求失败,降级本地处理:', err);
return StorageManager.consumePoint();
}
}
/**
* 获得积分(通关奖励)
* @returns 获得后的积分数
*/
async earnPoint(levelId: string): Promise<number> {
if (!AuthManager.instance.isLoggedIn) {
StorageManager.addPoint();
return StorageManager.getPoints();
}
try {
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS_EARN,
{
reason: POINT_REASONS.LEVEL_COMPLETE,
levelId,
},
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return response.data.points;
}
} catch (err) {
console.error('[UserAssetsManager] 获得积分请求失败,降级本地处理:', err);
}
StorageManager.addPoint();
return StorageManager.getPoints();
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "ddba71db-75ed-468d-ac99-b4632c0b2ae4",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -44,6 +44,38 @@ export class WxSDK {
return typeof wx !== 'undefined' ? wx : null;
}
// ==================== 登录相关 ====================
/**
* 微信登录,获取临时 code
* @returns Promise<string> 登录 code
*/
static login(): Promise<string> {
return new Promise((resolve, reject) => {
const wxApi = WxSDK.getWx();
if (!wxApi) {
reject(new Error('非微信环境,无法调用 wx.login'));
return;
}
wxApi.login({
success: (res: any) => {
if (res.code) {
console.log('[WxSDK] wx.login 成功,获取到 code');
resolve(res.code);
} else {
console.error('[WxSDK] wx.login 失败:', res.errMsg);
reject(new Error(res.errMsg || 'wx.login 失败'));
}
},
fail: (err: any) => {
console.error('[WxSDK] wx.login 调用失败:', err);
reject(new Error(err.errMsg || 'wx.login 调用失败'));
}
});
});
}
// ==================== 分享相关 ====================
/**