## 如何使用 `Senparc.Weixin SDK` 进行小程序云开发
[云开发(CloudBase)](https://www.cloudbase.net/) 是基于Serverless架构构建的一站式后端云服务,涵盖函数、数据库、存储、CDN等服务,免后端运维,支持小程序、Web和APP开发。 其中,小程序·云开发是微信和腾讯云联合推出的云端一体化解决方案,基于云开发可以免鉴权调用微信所有开放能力,在微信开发者工具中即可开通使用。
### 第一步:引用并配置 Senparc.Weixin SDK
在任意 Web/桌面/命令行项目中引入 [Senparc.Weixin.WxOpen(小程序包)](https://www.nuget.org/packages/Senparc.Weixin.WxOpen)。
在 appsettings.json 中配置小程序的信息,如:
``` json
//CO2NET 设置
"SenparcSetting": {
"IsDebug": true,
"DefaultCacheNamespace": "DefaultCache",
//分布式缓存
"Cache_Redis_Configuration": "#{Cache_Redis_Configuration}#", //Redis配置
"Cache_Memcached_Configuration": "#{Cache_Memcached_Configuration}#", //Memcached配置
"SenparcUnionAgentKey": "#{SenparcUnionAgentKey}#"
},
//Senparc.Weixin SDK 设置
"SenparcWeixinSetting": {
//微信全局
"IsDebug": true,
//追加小程序配置
"WxOpenAppId": "#{WxOpenAppId}#",
"WxOpenAppSecret": "#{WxOpenAppSecret}#",
"WxOpenToken": "#{WxOpenToken}#",
"WxOpenEncodingAESKey": "#{WxOpenEncodingAESKey}#"
}
```
其中, WxOpenAppId 和 WxOpenAppSecret 的字符串值(包括#{}#占位符)替换为小程序后台的值,如,将`#{WxOpenAppId}#` 替换为:`wx12b4f63276b14d4c`。
### 第二步:程序中注册小程序
在启动代码或 Startup.cs 的 ConfigureServices() 方法中,添加代码,注册 Senparc.Weixin SDK:
``` C#
services.AddSenparcWeixinServices(Configuration); //Senparc.Weixin 注册
```
在 Configure() 方法中添加两个参数,自动引入 appsettings.json 中的配置:IOptionssenparcSetting, IOptionssenparcWeixinSetting,完整代码:
``` C#
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IOptions<SenparcSetting> senparcSetting,
IOptions<SenparcWeixinSetting> senparcWeixinSetting)
```
在方法体末尾追加代码:
``` C#
app.UseSenparcGlobal(env, senparcSetting.Value, null, true)
.UseSenparcWeixin(senparcWeixinSetting.Value,
weixinRegister =>
{
weixinRegister.RegisterWxOpenAccount(senparcWeixinSetting.Value, "【云函数】小程序");
});
```
其中第一行代码是配置启用 CO2NET(Senparc.Weixin 的一个基础库)全局配置,第二行代码开始配置 Senparc.Weixin SDK 及小程序参数。
至此,小程序的所有配置工作已经完成。
### 第三步:调用小程序云开发的云函数
高级接口可以在任意地方触发,例如网页、队列、独立线程、服务,或由 UI 触发的事件。
在小程序开发工具的客户端配置完后,直接在需要触发的代码位置,调用接口:
``` C#
var wxOpenSetting = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenSetting;
var envId= "senparc-robot-5f5128";
var result = Senparc.Weixin.WxOpen.AdvancedAPIs.Tcb
.TcbApi.DatabaseCollectionGet(wxOpenSetting.WxOpenAppId, envId);
```
其中,wxOpenSetting 是通过 startup.cs 中代码自动进行了全局配置的全套小程序配置参数,evnId 是云函数的环境ID。
除此以外,还可以调用所有 Senparc.Weixin.WxOpen.AdvancedAPIs.Tcb.TcbApi 下的所有云开发接口,及其他小程序接口。
### 更多
请参考《[如何在 C# 平台调用云开发?](https://mp.weixin.qq.com/s/6dKkdxoyF4x3mZkBuDZjyg)》。
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
使用 Senparc.Weixin,您可以方便快速地开发微信全平台的应用(包括微信公众号、小程序、小游戏、企业号、开放平台、微信支付、JS-SDK、微信硬件/蓝牙,等等)。本项目的 Demo 同样适合初学者进行 .NET 编程学习。目前 Senparc.Weixin 已经支持几乎所有微信平台模块和接口,并同时支持 .NET 3.5 / 4.0 / 4.5 / .NET Standard 2.x / .NET Core 2.x / .NET Core 3.x / .NET 6.0 多种框架,同时兼容所有 MVC、Razor、WebApi、Console(命令行)、桌面应用(.exe)、Blazor、MAUI、后台服务等环境,和外部框架完全解耦。Senparc.Weixin SDK 是目前使用率最高的微信 .NET SDK,也是国内最受欢迎的 .NET 开源项目之一。
资源推荐
资源详情
资源评论
收起资源包目录
WeiXinMPSDK-master.zip (2617个子文件)
Global.asax 118B
jsapi.aspx 9KB
native.aspx 669B
feedback.aspx 438B
payNotifyUrl.aspx 437B
index.aspx 426B
nativecall.aspx 106B
Web.config 12KB
packages.config 10KB
packages.config 7KB
Web.config 3KB
app.config 2KB
app.config 2KB
NeuCharRoot.config 2KB
app.config 1KB
app.config 1KB
Web.Release.config 1KB
Web.Debug.config 1KB
app.config 659B
app.config 657B
app.config 619B
app.config 422B
app.config 422B
app.config 422B
app.config 422B
app.config 422B
app.config 420B
app.config 420B
app.config 420B
test.bak.config 236B
App.config 182B
ClearDotNetCoreBadFiles.exe.config 182B
App.config 182B
App.config 182B
RemoveBinFiles.exe.config 178B
App.config 178B
CardAPI.cs 226KB
ShakeAroundApi.cs 100KB
ExternalApi.cs 94KB
WxAppApi.cs 90KB
TenPayV3.cs 70KB
Enums.cs 66KB
MailListApi.cs 61KB
MassApi.cs 60KB
GroupMessageApi.cs 60KB
QueryBusifavorStockReturnJson.cs 58KB
InvoiceApi.cs 56KB
TenPayV3Results.cs 56KB
ComponentApi.cs 54KB
QueryBusifavorCouponsReturnJson.cs 53KB
CreateBusifavorStockRequestData.cs 53KB
TenPayV3Controller.cs 53KB
TenPayV3Controller.cs 53KB
TenPayV3Controller.cs 53KB
AnalysisApi.cs 53KB
MediaApi.cs 51KB
TenPayV3Controller.cs 50KB
RedPackApi.cs 50KB
WiFiApi.cs 48KB
QueryBusifavorCouponReturnJson.cs 46KB
QueryServiceOrderReturnJson.cs 45KB
CustomApi.cs 43KB
PayServiceOrderNotifyJson.cs 42KB
MessageHandlerAsync.Event.cs 42KB
RequestMessageFactoryTest.cs 42KB
WorkMessageHandler.cs 41KB
TcbApi.cs 41KB
WorkMessageHandler.Async.cs 40KB
QueryPaygiftActivitiesReturnJson.cs 39KB
CustomServiceApi.cs 38KB
ThirdPartyAuthApi.cs 38KB
Annotations.cs 37KB
QueryPaygiftActivityReturnJson.cs 37KB
AuthorizerContainer.cs 36KB
CodeApi.cs 35KB
MessageHandler.Event.cs 35KB
ComponentContainer.cs 34KB
CustomMessageHandler.cs 34KB
ModifyServiceOrderReturnJson.cs 34KB
SyncPayServiceOrderReturnJson.cs 33KB
Enums.cs 33KB
ChatApi.cs 32KB
MediaApi.cs 31KB
CreateUniqueThresholdActivityRequestData.cs 30KB
TenPayApiV3Controller.cs 29KB
TenPayApiV3Controller.cs 29KB
BasePayApis.cs 29KB
TenpayV3ProfitShareingRequestData.cs 28KB
Startup.cs 28KB
Startup.cs 28KB
TenPayApiV3Controller.cs 28KB
StoreApi.cs 28KB
CustomMessageHandler_Events.cs 27KB
WxOpenController.cs 27KB
WxOpenController.cs 27KB
QueryParkingReturnJson.cs 27KB
CreateServiceOrderReturnJson.cs 27KB
PayNotifyJson.cs 27KB
DataCubeApi.cs 26KB
MessageHandlersTest.cs 26KB
共 2617 条
- 1
- 2
- 3
- 4
- 5
- 6
- 27
资源评论
m0_72731342
- 粉丝: 2
- 资源: 1832
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功