Files
pixelheros/assets/script/game/wx_clound_client_api/WxCloudApi.ts
pan ac2de96bd0 refactor(game): 重构游戏云端数据同步逻辑
1. 新增微信云函数双向同步接口,将时间戳对比逻辑移至云端完成
2. 简化初始化流程,合并云端登录与数据同步步骤
3. 重构GameDataSync类,统一同步流程并改为异步调用
4. 替换旧的getCloudData接口为新的syncFromCloud方法
2026-08-07 10:04:55 +08:00

122 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { get } from "http";
export type CloudReturnType<T = any> = {
code: number,// 200成功
msg?:string,
data?:T
}
export class WxCloudApi{
/**
* @en init the cloud
* @zh 初始化云客户端(只调用一次即可)
* @param {string} env 云环境名称
*/
public static init(env:string){
wx?.cloud.init({
env: env
});
}
/**
* @en Login to the cloud
* @zh 登录云
* @return 返回结果
* 参考:
* result.result = {
* code: number, // 200成功其他都是失败
* msg: string, // 如果失败,这里是失败原因等信息
* data: { // 成功才有
* openid: string, // 用户微信平台openid
* regist_time: number, // 时间戳,用户注册时间
* game_data: object, // 开发者自己保存的数据
* }
* }
* 如果这个泛型令你报错一般是因为你删了wx.api.d.ts导致请使用以下签名
* login():Promise<{result: CloudReturnType<{openid: string, regist_time: number, game_data: object}>}>
* 或者你是个“不拘小节”的老哥,可以用以下简洁版签名(参考上方的数据结构例子使用即可)
* login():Promise<any>
*/
public static async login(): Promise<CloudCallFunctionResult<CloudReturnType<{
openid:string,
regist_time:number,
game_data:any
}>>>{
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
cmd: "login"
}
});
}
/**
* @en Save game data to the cloud
* @zh 把客户端数据写入云,此为全覆盖写入,请自行管理完整数据
* @return 返回结果
* 参考:
* result.result = {
* code: number, // 200成功其他都是失败
* msg: string, // 如果失败,这里是失败原因等信息
* data: {
* errMsg: "document.update:ok", // 数据库返回结果
* stats: {
* updated: number, // 更新了几条数据正常是1
* }
* }
* 如果这个泛型令你报错一般是因为你删了wx.api.d.ts导致请使用以下签名
* save(gameData: any): Promise<{resoult:CloudReturnType}>
* 或者你是个“不拘小节”的老哥,可以用以下简洁版签名(参考上方的数据结构例子使用即可)
* login():Promise<any>
*/
public static async save(gameData: any): Promise<CloudCallFunctionResult<CloudReturnType<{
errMsg: string,
status:{
updated: number
}
}>>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
cmd: 'save',
data: gameData
}
});
}
public static async get(): Promise<CloudCallFunctionResult<CloudReturnType<{}>>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
cmd: 'get',
}
});
}
/**
* @en Sync game data with the cloud (two-way, timestamp-based)
* @zh 与云端双向同步游戏数据:客户端较新则上传覆盖云端,云端较新则返回云端数据
* @param gameData 本地游戏数据(需包含 timestamp 字段)
* @return 返回结果
* result.result = {
* code: number, // 200成功其他都是失败
* msg?: string, // 失败原因
* updated: boolean, // true=云端已被本地数据覆盖false=客户端应使用返回的云端数据
* data: {
* openid: string,
* game_data: object, // 同步后的最终数据
* }
* }
*/
public static async sync(gameData: any): Promise<CloudCallFunctionResult<CloudReturnType<{
openid: string,
game_data: any
}> & { updated: boolean }>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
cmd: 'sync',
data: gameData
}
});
}
}