基於Senparc開發微信公眾號
- 2019 年 12 月 4 日
- 筆記
幫朋友做一套微信公眾號/企業號下的用戶積分,分享追蹤的服務,初次使用Senparc,記錄使用筆記和開發過程。
公眾號資訊確認/伺服器關聯
公眾號
公眾號註冊,開通,驗證,進入開發選項,完成相關配置,此處略過。
伺服器關聯
這裡採用了 Senparc 框架來進行快速開發。
驗證伺服器
Nuget 安裝 Senparc SDK ,這裡採用 .NET Core MVC WEB API 來開發。
namespace WX_H5_ShareCount_Backend.Controllers { [Route("api/[controller]")] [ApiController] public class WeiXInController : ControllerBase { public readonly string Token = "raphaelli"; public ActionResult Get(string signature, string timestamp, string nonce,string echostr) { if (CheckSignature.Check(signature, timestamp, nonce, Token)) { return Content(echostr); //返回隨機字元串則表示驗證通過 } else { return Content("failed:" + signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(timestamp, nonce, Token) + "。如果您在瀏覽器中看到這條資訊,表明此Url可以填入微信後台。"); } } } }
運行,打開瀏覽器,訪問對應API,成功!
部署方面,這裡採用了 Auzre 的直接部署,當然也可以採用其它的部署方式。
Senparc SDK MP
配置
ConfigureServices
在 ConfigureServices() 方法中加入:
public void ConfigureServices(IServiceCollection services) { //... services.AddSenparcGlobalServices(Configuration)//Senparc.CO2NET 全局註冊 .AddSenparcWeixinServices(Configuration);//Senparc.Weixin 註冊 }
Configure
修改 Configure() 方法傳入參數,並添加程式碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting) { //app.UseMvc()等設置... // 啟動 CO2NET 全局註冊,必須! IRegisterService register = RegisterService.Start(env, senparcSetting.Value) .UseSenparcGlobal(false, null); //開始註冊微信資訊,必須! register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value) }
appsetting.json
在 appsetting.json 文件下插入以下設置(可參考 Demo):
//以下資訊會被自動識別,如無特殊說明,不用的參數可以刪除,但修改 key 後將會無法自動識別! //CO2NET 設置 "SenparcSetting": { //以下為 CO2NET 的 SenparcSetting 全局配置,請勿修改 key,勿刪除任何項 "IsDebug": true, "DefaultCacheNamespace": "DefaultCache", //分散式快取 "Cache_Redis_Configuration": "Redis配置", //"Cache_Redis_Configuration": "localhost:6379", "Cache_Memcached_Configuration": "Memcached配置", "SenparcUnionAgentKey": "SenparcUnionAgentKey" }, //Senparc.Weixin SDK 設置 "SenparcWeixinSetting": { //以下為 Senparc.Weixin 的 SenparcWeixinSetting 微信配置 //微信全局 "IsDebug": true, //以下不使用的參數可以刪除,key 修改後將會失效 //公眾號 "Token": "weixin", "EncodingAESKey": "", "WeixinAppId": "WeixinAppId", "WeixinAppSecret": "WeixinAppSecret", //小程式 "WxOpenAppId": "WxOpenAppId", "WxOpenAppSecret": "WxOpenAppSecret", "WxOpenToken": "WxOpenToken", "WxOpenEncodingAESKey": "WxOpenEncodingAESKey", //企業微信 "WeixinCorpId": "WeixinCorpId", "WeixinCorpSecret": "WeixinCorpSecret", //微信支付 //微信支付V2(舊版) "WeixinPay_PartnerId": "WeixinPay_PartnerId", "WeixinPay_Key": "WeixinPay_Key", "WeixinPay_AppId": "WeixinPay_AppId", "WeixinPay_AppKey": "WeixinPay_AppKey", "WeixinPay_TenpayNotify": "WeixinPay_TenpayNotify", //微信支付V3(新版) "TenPayV3_MchId": "TenPayV3_MchId", "TenPayV3_Key": "TenPayV3_Key", "TenPayV3_AppId": "TenPayV3_AppId", "TenPayV3_AppSecret": "TenPayV3_AppId", "TenPayV3_TenpayNotify": "TenPayV3_TenpayNotify", //開放平台 "Component_Appid": "Component_Appid", "Component_Secret": "Component_Secret", "Component_Token": "Component_Token", "Component_EncodingAESKey": "Component_EncodingAESKey", //擴展及代理參數 "AgentUrl": "AgentUrl", "AgentToken": "AgentToken", "SenparcWechatAgentKey": "SenparcWechatAgentKey" }