feat: 完善分享模式
This commit is contained in:
@@ -19,7 +19,6 @@ export const API_ENDPOINTS = {
|
||||
/** 分享相关 */
|
||||
SHARE_CREATE: `${API_BASE}/share`,
|
||||
SHARE_CREATED: `${API_BASE}/share/created`,
|
||||
SHARE_PROGRESS: `${API_BASE}/share/progress`,
|
||||
/** 用户信息 */
|
||||
USER_INFO: `${API_BASE}/user/info`,
|
||||
/** 用户所有已通关的关卡(成就墙 / 关卡回看) */
|
||||
@@ -38,6 +37,10 @@ export function getShareJoinUrl(code: string): string {
|
||||
return `${API_BASE}/share/${code}/join`;
|
||||
}
|
||||
|
||||
export function getShareSubmitUrl(code: string): string {
|
||||
return `${API_BASE}/share/${code}/submit`;
|
||||
}
|
||||
|
||||
export function getGameConfigUrl(key: string): string {
|
||||
return `${API_BASE}/game-configs/${key}`;
|
||||
}
|
||||
|
||||
@@ -134,13 +134,34 @@ export interface JoinShareData {
|
||||
levels: ShareLevelData[];
|
||||
}
|
||||
|
||||
/** 上报关卡进度响应 */
|
||||
export interface ReportProgressData {
|
||||
passed: boolean;
|
||||
/** 分享挑战单关提交 */
|
||||
export interface SubmitShareLevel {
|
||||
levelId: string;
|
||||
answer: string;
|
||||
timeSpent: number;
|
||||
}
|
||||
|
||||
/** 分享挑战提交后的单关结果 */
|
||||
export interface SubmittedShareLevelData extends ShareLevelData {
|
||||
submittedAnswer: string;
|
||||
timeSpent: number;
|
||||
isCorrect: boolean;
|
||||
timeLimit: number | null;
|
||||
withinTimeLimit: boolean;
|
||||
}
|
||||
|
||||
/** 分享挑战整场提交响应 */
|
||||
export interface SubmitShareData {
|
||||
shareCode: string;
|
||||
title: string;
|
||||
rank: number;
|
||||
correctCount: number;
|
||||
levelCount: number;
|
||||
participantCount: number;
|
||||
totalTimeSpent: number;
|
||||
levels: SubmittedShareLevelData[];
|
||||
}
|
||||
|
||||
/** 我创建的分享挑战条目 */
|
||||
export interface CreatedShareItem {
|
||||
id: string;
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import { SpriteFrame, Texture2D, ImageAsset, assetManager } from 'cc';
|
||||
import { HttpUtil } from './HttpUtil';
|
||||
import { WxSDK } from './WxSDK';
|
||||
import { API_ENDPOINTS, getShareJoinUrl, API_TIMEOUT } from '../config/ApiConfig';
|
||||
import { API_ENDPOINTS, getShareJoinUrl, getShareSubmitUrl, API_TIMEOUT } from '../config/ApiConfig';
|
||||
import {
|
||||
ApiEnvelope,
|
||||
CreateShareData,
|
||||
JoinShareData,
|
||||
ReportProgressData,
|
||||
ShareLevelData,
|
||||
CreatedShareItem,
|
||||
CreatedShareListData,
|
||||
SubmitShareData,
|
||||
SubmitShareLevel,
|
||||
} from '../types/ApiTypes';
|
||||
import { RuntimeLevelConfig } from '../types/LevelTypes';
|
||||
|
||||
@@ -50,6 +51,14 @@ export class ShareManager {
|
||||
return [...this._createdShares];
|
||||
}
|
||||
|
||||
get shareCode(): string | null {
|
||||
return this._shareCode;
|
||||
}
|
||||
|
||||
get shareTitle(): string {
|
||||
return this._shareTitle;
|
||||
}
|
||||
|
||||
async createShare(title: string, levelIds: string[]): Promise<string | null> {
|
||||
try {
|
||||
const response = await HttpUtil.post<ApiEnvelope<CreateShareData>>(
|
||||
@@ -101,6 +110,7 @@ export class ShareManager {
|
||||
clue3: level.hint3,
|
||||
answer: level.answer,
|
||||
completed: false,
|
||||
timeLimit: null,
|
||||
}));
|
||||
|
||||
// 预加载首关图片(两张并行加载)
|
||||
@@ -178,40 +188,34 @@ export class ShareManager {
|
||||
return this._shareLevels?.length ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报单关通关进度
|
||||
* @param levelId 关卡 ID
|
||||
* @param passed 是否通过
|
||||
* @param timeSpent 用时(秒)
|
||||
*/
|
||||
async reportLevelProgress(
|
||||
levelId: string,
|
||||
passed: boolean,
|
||||
timeSpent: number,
|
||||
): Promise<ReportProgressData | null> {
|
||||
getShareLevelIds(): string[] {
|
||||
return this._shareApiLevels.map(level => level.id);
|
||||
}
|
||||
|
||||
async submitShareChallenge(levels: SubmitShareLevel[]): Promise<SubmitShareData | null> {
|
||||
if (!this._shareCode) {
|
||||
console.warn('[ShareManager] reportLevelProgress: 无分享码,跳过上报');
|
||||
console.warn('[ShareManager] submitShareChallenge: 无分享码,跳过提交');
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await HttpUtil.post<ApiEnvelope<ReportProgressData>>(
|
||||
API_ENDPOINTS.SHARE_PROGRESS,
|
||||
{ shareCode: this._shareCode, levelId, passed, timeSpent },
|
||||
const response = await HttpUtil.post<ApiEnvelope<SubmitShareData>>(
|
||||
getShareSubmitUrl(this._shareCode),
|
||||
{ levels },
|
||||
API_TIMEOUT.DEFAULT,
|
||||
);
|
||||
|
||||
if (!response.success || !response.data) {
|
||||
console.error('[ShareManager] 上报进度失败:', response.message);
|
||||
console.error('[ShareManager] 提交挑战结果失败:', response.message);
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[ShareManager] 上报成功: passed=${response.data.passed}, withinTimeLimit=${response.data.withinTimeLimit}`,
|
||||
`[ShareManager] 提交挑战结果成功: rank=${response.data.rank}, correct=${response.data.correctCount}/${response.data.levelCount}`,
|
||||
);
|
||||
return response.data;
|
||||
} catch (err) {
|
||||
console.error('[ShareManager] 上报进度异常:', err);
|
||||
console.error('[ShareManager] 提交挑战结果异常:', err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user