尚未崩坏 发表于 2022-8-26 21:32:02

.net6 使用Senparc开发小程序配置

1.添加引用
https://img2022.cnblogs.com/blog/2126575/202207/2126575-20220714093342306-1937232722.png
 
 
 
2.添加配置文件
/// <summary>
    /// 微信
    /// </summary>
    public class WeChat
    {
      // 小程序
      public string WxOpenAppId { get; set; }
      public string WxOpenAppSecret { get; set; }
      //// 微信支付
      //public string TenPayV3_AppId { get; set; }
      //public string TenPayV3_AppSecret { get; set; }
      //public string TenPayV3_MchId { get; set; }
      //public string TenPayV3_Key { get; set; }
      //public string TenPayV3_CertPath { get; set; }
      //public string TenPayV3_CertSecret { get; set; }
      //public string TenPayV3_TenpayNotify { get; set; }
      //public string TenPayV3_RechargeTenpayNotify { get; set; }
      //public string TenPayV3_RefundNotify { get; set; }
      //public string TenPayV3_RechargeRefundNotify { get; set; }
      //public string TenPayV3_WxOpenTenpayNotify { get; set; }
      /// <summary>
      /// 是否是开发环境
      /// </summary>
      public string IsDevelopment { get; set; }
    } 
3.在appsetting.jso里面配置参数
"WeChat": {
    // 小程序
    "WxOpenAppId": "",
    "WxOpenAppSecret": ""
    ////微信支付
    //"TenPayV3_AppId": "", //商户平台绑定了微信小程序就可以直接用这个
    //"TenPayV3_AppSecret": "", //商户平台绑定了微信小程序就可以直接用这个
    //"TenPayV3_MchId": "", //
    //"TenPayV3_Key": "", //
    //"TenPayV3_CertPath": "", //(新)支付证书物理路径,如:D:\\cert\\apiclient_cert.p12
    //"TenPayV3_CertSecret": "", //(新)支付证书密码(原始密码和 MchId 相同)
    //"TenPayV3_TenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrl",
    ////http://YourDomainName/TenpayV3/PayNotifyUrl
    ////如果不设置TenPayV3_WxOpenTenpayNotify,默认在 TenPayV3_TenpayNotify 的值最后加上 "WxOpen"
    //"TenPayV3_WxOpenTenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrlWxOpen" //http://YourDomainName/TenpayV3/PayNotifyUrlWxOpen

4.配置program

builder.Services.AddMemoryCache();
<br>//微信

builder.Services.AddSenparcGlobalServices(builder.Configuration)//Senparc.CO2NET 全局注册
                   .AddSenparcWeixinServices(builder.Configuration);//Senparc.Weixin 注册
                                                                  //添加 微信支付api证书
                                                                  //var mac_id = builder.Configuration["WeChat:TenPayV3_MchId"];
                                                                  //builder.Services.AddCertHttpClient(mac_id + "_", mac_id, AppDomain.CurrentDomain.BaseDirectory + "/wwwroot/" + builder.Configuration["WeChat:TenPayV3_CertPath"]);


var senparcSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcSetting>>();

IRegisterService register = RegisterService.Start(senparcSetting.Value).UseSenparcGlobal();// 启动 CO2NET 全局注册,必须!

var senparcWeixinSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcWeixinSetting>>();
//register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value);//微信全局注册,必须!
register.UseSenparcWeixin(senparcWeixinSetting.Value, weixinRegister =>
{
    #region 微信相关配置

    /* 微信配置开始
    *
    * 建议按照以下顺序进行注册,尤其须将缓存放在第一位!
    */

    #region 微信缓存(按需,必须放在配置开头,以确保其他可能依赖到缓存的注册过程使用正确的配置)
    //注意:如果使用非本地缓存,而不执行本块注册代码,将会收到“当前扩展缓存策略没有进行注册”的异常
    // DPBMARK_END

    // 微信的 Memcached 缓存,如果不使用则注释掉(开启前必须保证配置有效,否则会抛错)    -- DPBMARK Memcached
    //if (UseMemcached(senparcSetting.Value, out _))
    //{
    //    app.UseEnyimMemcached();
    //    weixinRegister.UseSenparcWeixinCacheMemcached();
    //}                                                                                     // DPBMARK_END

    #endregion


    /* 微信配置结束 */

    #endregion
});//微信全局注册,必须!
//注册 Token容器 应用凭证
var con = AccessTokenContainer.RegisterAsync(builder.Configuration["WeChat:WxOpenAppId"], builder.Configuration["WeChat:WxOpenAppSecret"]);
con.Wait();<br> 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: .net6 使用Senparc开发小程序配置