基於.net5 wtm框架、uni-app微信公眾號開發二、搭建后台接口


搭建后台接口

創建基於.net5的wtm框架項目

0、框架介紹:WTM全稱WalkingTec MVVM(不是“我特么”的拼音首字母),WTM是一個快速開發框架,有多快?至少目前dotnetcore的開源項目中,我還沒有見到更接地氣,開發速度更快的框架。WTM的設計理念就是最大程度的加快開發速度,降低開發成本。(作者原話,快確實是這個項目使用它的最大原因,感謝作者劉亮大大)。 點擊官網了解更多

1、點我創建后台接口項目,輸入項目名稱,並點擊下一步
在這里插入圖片描述
2、這里選擇.netcore5版本,數據我使用mysql,連接字符串可以后面再改也可以在這里修改好,文件上傳到硬盤目錄,因為是后台項目再前端UI上我不選擇前后分離,項目結構單一就好(其他項目可以根據自己需要修改),填寫完成后點擊“開始配置”即可下載項目文件,下載完成直接用vs打開即可
在這里插入圖片描述
項目文件結構

3、為項目引入微信sdk,我使用的是Senparc.Weixin.MP,直接在vs包管理工具中搜索Senparc.Weixin.MP並安裝到項目中(更多使用方法官網了解)
在這里插入圖片描述
4、配置Senparc.Weixin.MP,在appsettings.json中添加配置項

// An highlighted block
 //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": "Token",
    "EncodingAESKey": "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"
  }

5、在Startup.cs文件ConfigureServices() 方法中進行注冊

 services.AddSenparcGlobalServices(ConfigRoot)//Senparc.CO2NET 全局注冊
         .AddSenparcWeixinServices(ConfigRoot);//Senparc.Weixin 注冊

6、在Startup.cs文件Configure() 方法中進行注冊

  // 啟動 CO2NET 全局注冊,必須!
IRegisterService register = RegisterService.Start(senparcSetting.Value)           .UseSenparcGlobal(false, null);
//開始注冊微信信息,必須!
register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value);

微信公眾平台服務器配置

1、在項目中創建控制器WxController
在這里插入圖片描述

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NICE_Server.Models.wx;
using WalkingTec.Mvvm.Admin.Api;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkUserVms;
using WalkingTec.Mvvm.Mvc.Auth;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP;
using Senparc.Weixin;

namespace WalkingTec.Mvvm.Wx.Api
{
    [AuthorizeJwtWithCookie]
    [ApiController]
    [Route("api/_[controller]")]
    [ActionDescription("_Wx.Api")]
    [AllRights]
    public class WxController : BaseApiController
    {
        private readonly ILogger _logger;
        private readonly ITokenService _authService;

        public static readonly string Token = Config.SenparcWeixinSetting.Token;//與微信公眾賬號后台的Token設置保持一致,區分大小寫。
        public static readonly string EncodingAESKey = Config.SenparcWeixinSetting.EncodingAESKey;//與微信公眾賬號后台的EncodingAESKey設置保持一致,區分大小寫。
        public static readonly string AppId = Config.SenparcWeixinSetting.WeixinAppId;//與微信公眾賬號后台的AppId設置保持一致,區分大小寫。

        public WxController(
          ILogger<WxController> logger,
          ITokenService authService)
        {
            _logger = logger;
            _authService = authService;
        }

        #region 微信服務器驗證
        /// <summary>
        /// 微信服務器驗證
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("[action]")]
        public ActionResult ReturnWxTonken(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,請注意保持Token一致。");
            }
        }
        #endregion
    }
}

2、依然使用utools工具把接口url內網穿透出去,然后進入公眾平台設置-開發-基本配置-服務器配置,填寫相應的信息,點擊提交即可

在這里插入圖片描述

到此搭建后台接口項目並引入微信sdk完成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM