Skip to content

feat: Add Cloud Demo #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/APISO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MonoBehaviour:
- {fileID: 11400000, guid: 587fe4b03da9d44779ac921ffdb216a7, type: 2}
- {fileID: 11400000, guid: 82450cc74b3b4439da53b9f0f2298454, type: 2}
- {fileID: 11400000, guid: b34629c67a7664a428561dd0b3621995, type: 2}
- {fileID: 11400000, guid: 24d9eba5a9b3048b59cf7e5495bb999d, type: 2}
- {fileID: 11400000, guid: 9977a581037b84833a32b508e00eb1a2, type: 2}
- {fileID: 11400000, guid: 56f316e0e10ba419bbf19bd7a68bfc4c, type: 2}
- {fileID: 11400000, guid: 6f0972f5fdc56c543b23c9873d760bf5, type: 2}
Expand Down
8 changes: 8 additions & 0 deletions Demo/API_V2/Assets/API/Cloud.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallContainer.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallContainer/CallContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Collections.Generic;
using LitJson;
using UnityEngine;
using WeChatWASM;

public class CallContainer : Details
{
// data
private class CloudCallContainerData
{
public string action;
}

private void Start()
{
GameManager.Instance.detailsController.BindExtraButtonAction(0, CloudCallContainerByInstance);
}

protected override void TestAPI(string[] args)
{
this.CloudCallContainer();
}

// 使用默认云开发操作实例调用云托管
private void CloudCallContainer()
{
WX.cloud.Init(); // 注意初始化

var data = new CloudCallContainerData()
{
action = "inc"
};

WX.cloud.CallContainer(new CallContainerParam()
{
config = new ICloudConfig()
{
env = "prod-xxx", // 云托管环境ID,通过创建云托管服务获取
},
path = "/api/count", // 服务路径
header = new Dictionary<string, string>() // 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json
{
{ "X-WX-SERVICE", "express-hodt" }
},
method = "POST", // HTTP 请求方法
data = JsonMapper.ToJson(data), // 请求数据
success = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Success: " + JsonMapper.ToJson(res)
});
},
fail = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Fail: " + JsonMapper.ToJson(res)
});
},
complete = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Complete: " + JsonMapper.ToJson(res)
});
}
});
}

// 新建云开发操作实例调用云托管
private void CloudCallContainerByInstance()
{
var clo = WX.cloud.Cloud(new ICloudInstanceOptions()
{
resourceEnv = "cloud1-xxx", // 云开发环境ID,通过创建云开发环境获取
});

clo.Init(); // 注意初始化

var data = new CloudCallContainerData()
{
action = "inc"
};

clo.CallContainer(new CallContainerParam()
{
config = new ICloudConfig()
{
env = "prod-xxx", // 云托管环境ID,通过创建云托管服务获取
},
path = "/api/count", // 服务路径
header = new Dictionary<string, string>() // 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json
{
{ "X-WX-SERVICE", "express-hodt" }
},
method = "POST", // HTTP 请求方法
data = JsonMapper.ToJson(data), // 请求数据
success = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Success: " + JsonMapper.ToJson(res)
});
},
fail = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Fail: " + JsonMapper.ToJson(res)
});
},
complete = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallContainer Complete: " + JsonMapper.ToJson(res)
});
}
});
}
}
15 changes: 15 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallContainer/CallContainer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallContainer/CallContainerSO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
m_Name: CallContainerSO
m_EditorClassIdentifier:
entryScriptTypeName: CallContainer
entryName: "\u4E91\u6258\u7BA1"
entryAPI: WX.cloud.CallContainer
entryDescription: "<color=red>\u8BF7\u5C06\u53C2\u6570\u6309\u5B9E\u9645\u60C5\u51B5\u8FDB\u884C\u66FF\u6362</color>\n\u6709\u5173\u5FEB\u901F\u90E8\u7F72\u4E91\u6258\u7BA1\u8BE6\u89C1https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/quickstart/template/"
optionList: []
initialButtonText: "\u4F7F\u7528\u9ED8\u8BA4\u4E91\u5F00\u53D1\u64CD\u4F5C\u5B9E\u4F8B\u8C03\u7528\u4E91\u6258\u7BA1"
extraButtonList:
- buttonText: "\u65B0\u5EFA\u4E91\u5F00\u53D1\u64CD\u4F5C\u5B9E\u4F8B\u8C03\u7528\u4E91\u6258\u7BA1"
initialResultList: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallFunction.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallFunction/CallFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Collections.Generic;
using LitJson;
using UnityEngine;
using WeChatWASM;

public class CallFunction : Details
{
// data
private class CloudCallFunctionData
{
public int NormalData;
public string WeRunData;
public string BigData1;
public string BigData2;
public CloudCallFunctionDataObj Obj;
}
private class CloudCallFunctionDataObj
{
public string BigData3;
public string ShareInfo;
}

private string CloudID; // 通过其他接口获取的CloudID

private void Start()
{
GameManager.Instance.detailsController.BindExtraButtonAction(0, CloudCallFunctionByInstance);

WX.GetUserInfo(new GetUserInfoOption() // 获取敏感数据对应的CloudID
{
success = (res) =>
{
CloudID = res.cloudID;
}
});
}

protected override void TestAPI(string[] args)
{
this.CloudCallFunction();
}

// 使用默认云开发操作实例调用云函数
private void CloudCallFunction()
{
WX.cloud.Init(); // 注意初始化

var obj = new CloudCallFunctionDataObj()
{
BigData3 = WX.cloud.CDN(new ICDNFilePathSpec()
{
type = "filePath", // 此项必填filePath
filePath = "/7865-xxx-xxx-xxx/xxx.png", // 替换为实际的文件路径
}), // 参数为文件路径定义对象
ShareInfo = WX.cloud.CloudID(CloudID), // 通过其他接口获取的CloudID
};
var data = new CloudCallFunctionData()
{
NormalData = 111,
WeRunData = WX.cloud.CloudID(CloudID), // 通过其他接口获取的CloudID
BigData1 = WX.cloud.CDN("some large string"), // 参数为字符串
BigData2 = WX.cloud.CDN(new byte[] { 1, 2, 3, 4 }), // 参数为byte[]
Obj = obj,
};

WX.cloud.CallFunction(new CallFunctionParam()
{
name = "myFunction", // 须在云函数侧创建对应函数
data = data,
success = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Success: " + JsonMapper.ToJson(res)
});
},
fail = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Fail: " + JsonMapper.ToJson(res)
});
},
complete = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Complete: " + JsonMapper.ToJson(res)
});
}
});
}

// 新建云开发操作实例调用云函数
private void CloudCallFunctionByInstance()
{
var clo = WX.cloud.Cloud(new ICloudInstanceOptions()
{
resourceEnv = "cloud1-xxx", // 云开发环境ID,通过创建云开发环境获取
});

clo.Init(); // 注意初始化

var obj = new CloudCallFunctionDataObj()
{
BigData3 = clo.CDN(new ICDNFilePathSpec()
{
type = "filePath", // 此项必填filePath
filePath = "/7865-xxx-xxx-xxx/xxx.png", // 替换为实际的文件路径
}), // 参数为文件路径定义对象
ShareInfo = clo.CloudID(CloudID), // 通过其他接口获取的CloudID
};
var data = new CloudCallFunctionData()
{
NormalData = 111,
WeRunData = clo.CloudID(CloudID), // 通过其他接口获取的CloudID
BigData1 = clo.CDN("some large string"), // 参数为字符串
BigData2 = clo.CDN(new byte[] { 1, 2, 3, 4 }), // 参数为byte[]
Obj = obj,
};

clo.CallFunction(new CallFunctionParam()
{
name = "myFunction", // 须在云函数侧创建对应函数
data = data,
success = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Success: " + JsonMapper.ToJson(res)
});
},
fail = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Fail: " + JsonMapper.ToJson(res)
});
},
complete = (res) =>
{
WX.ShowModal(new ShowModalOption()
{
content = "Cloud CallFunction Complete: " + JsonMapper.ToJson(res)
});
}
});
}
}
15 changes: 15 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallFunction/CallFunction.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Demo/API_V2/Assets/API/Cloud/CallFunction/CallFunctionSO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
m_Name: CallFunctionSO
m_EditorClassIdentifier:
entryScriptTypeName: CallFunction
entryName: "\u4E91\u51FD\u6570"
entryAPI: WX.cloud.CallFunction
entryDescription: "<color=red>\u8BF7\u5C06\u53C2\u6570\u6309\u5B9E\u9645\u60C5\u51B5\u8FDB\u884C\u66FF\u6362</color>\n\u5982\u679C
data \u4E2D\u5305\u542B\u5927\u6570\u636E\u5B57\u6BB5\uFF08\u5EFA\u8BAE\u4E34\u754C\u503C
100KB\uFF09\uFF0C\u5EFA\u8BAE\u4F7F\u7528 WX.cloud.CDN \u6807\u8BB0\u5927\u6570\u636E\u5B57\u6BB5\u3002\n\u5BF9\u4E8E\u654F\u611F\u6570\u636E\uFF0C\u53EF\u4EE5\u901A\u8FC7\u5728data\u4E2D\u4F7F\u7528WX.cloud.CloudID\u6765\u83B7\u53D6\u3002\u8BE6\u89C1https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html\n\u6709\u5173\u4E91\u51FD\u6570\u7684\u521B\u5EFA\uFF0C\u8BF7\u53C2\u8003https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/getting-started.html"
optionList: []
initialButtonText: "\u4F7F\u7528\u9ED8\u8BA4\u4E91\u5F00\u53D1\u64CD\u4F5C\u5B9E\u4F8B\u8C03\u7528\u4E91\u51FD\u6570"
extraButtonList:
- buttonText: "\u65B0\u5EFA\u4E91\u5F00\u53D1\u64CD\u4F5C\u5B9E\u4F8B\u8C03\u7528\u4E91\u51FD\u6570"
initialResultList: []
Loading
Loading