基於.net5 wtm框架、uni-app微信公衆號開發2、搭建後臺接口

搭建後臺接口

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

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

一、點我建立後臺接口項目,輸入項目名稱,並點擊下一步
在這裏插入圖片描述
二、這裏選擇.netcore5版本,數據我使用mysql,鏈接字符串能夠後面再改也能夠在這裏修改好,文件上傳到硬盤目錄,由於是後臺項目再前端UI上我不選擇先後分離,項目結構單一就好(其餘項目能夠根據本身須要修改),填寫完成後點擊「開始配置」便可下載項目文件,下載完成直接用vs打開便可
在這裏插入圖片描述
項目文件結構前端

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

// 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"
  }

五、在Startup.cs文件ConfigureServices() 方法中進行註冊sql

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

六、在Startup.cs文件Configure() 方法中進行註冊json

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

微信公衆平臺服務器配置

一、在項目中建立控制器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
    }
}

二、依然使用utools工具把接口url內網穿透出去,而後進入公衆平臺設置-開發-基本配置-服務器配置,填寫相應的信息,點擊提交便可api

在這裏插入圖片描述

到此搭建後臺接口項目並引入微信sdk完成瀏覽器

相關文章
相關標籤/搜索