Files
heros/assets/Scripts/wx_clound_client_api/WxCloudApi.ts
2025-08-17 22:31:45 +08:00

84 lines
2.8 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.

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
}
});
}
}