2.开始开发
微信公众号后台配置
-
公众号类型选择: 能看到本篇文章的应该都是码农,不多废话,如需要开发人员介入,建议选择服务号,如下是服务号与订阅号的区别
-
微信公众号注册过程略...
-
公众号认证,登录公众号后点击右上角下拉菜单中的“认证详情”,认证费用300RMB/年,材料提交成功后一般1个工作日审核通过,请保持电话畅通,以接听腾讯第三方审核公司的电话(备注: 不做认证,很多接口无权调用, 如需要看接口权限,请参考 https://mp.weixin.qq.com/advanced/advanced?action=table&token=1865166120&lang=zh_CN )
-
域名注册、实名、备案与IP解析(具体步骤请咨询相关云供应商客户)
- 云平台上购买域名后需要做实名认证, 该过程一般在3个工作日之内完成,企业实名和个人实名提交材料不同,各个省市相关政策也不同,建议拨打云平台客服电话咨询
- 备案需要在云平台购买至少3个月的云服务器,且需要在实名认证通过3个工作日之后进行备案材料提交; 云平台供应商进行初审,初审通过后提交管局审核,备案通过与否以管局通过短信为准
- 备案通过后进行域名与服务器IP地址的解析
-
开发配置
-
基础配置: 登录公众号后拉倒最下方,找到基础配置
- 记录AppID以后后续使用
- 重置AppSecret, 很重要,一定记下来,因为这玩意没法再次查看,每次只能重置
- 配置IP白名单: 你的服务器IP、本地开发机器的IP都配置进去,不然调用接口时失败
-
服务器配置
-
服务器地址(URL): 用于监听用户在公众号中的操作,例如用户关注了该公众号、亦或用户在公众号中发送消息、点击菜单等
在LazyWeChat中以Middleware的形式处理上述操作,可配置为服务器地址加/LazyWechatListener, 如: http://test.lazywechat.com/LazyWechatListener, http://test.lazywechat.com 指向网站根目录,具体路径请开发人员自行配置
-
记录Token
-
EncodingAESKey用于消息加密与解密, 如加密方式为明文模式,请忽略此项
-
-
引入LazyWeChat
-
LazyWeChat目前仅支持.Netcore 2.0及以上版本
-
如何使用
-
NuGet中下载LazyWeChat
-
打开startup.cs文件导入namespace
using LazyWeChat.OfficialAccount;
- 找到ConfigureServices方法,添加如下代码:
services.AddLazyWeChat();
- 该方法需在appsettings.json中配置微信相关参数,请参考如下
"LazyWeChatSettings": { "AppID": "wxb******************991d", "AppSecret": "2a07***********************dfb12", "Token": "***********" },
-
具体配置信息科参考如下
备注 : 不需要的配置可以不出现在appsettings里面
"LazyWeChatSettings": {
"AppID": "wxbb2xxxxxxxxb991d",
"AppSecret": "2a0783cf50xxxxxxxxx2f085e8ddfb12",
"EncodingAESKey": "4oXVgu7O4EjMWMccxxxxxxxxx1omGlKiFd7hWxHCpPfI",
"Token": "bxpisxmp3yxxxxxxxxfji4phconluzvc",
"APIType": "OfficialAccount",
"MCHID": "15xxxxxxxxx",
"Key": "508b99xxxxxxxx708db83aa635247248",
"MQConnectionString": "Server=serverName;Database=LazyWeChat;User Id=sa;Password=Password!;"
}
/// <summary>
/// 公众号或者小程序 AppID
/// </summary>
public string AppID { get; set; }
/// <summary>
/// 公众号或者小程序 AppSecret
/// </summary>
public string AppSecret { get; set; }
/// <summary>
/// 公众号或者小程序 EncodingAESKey
/// </summary>
public string EncodingAESKey { get; set; }
/// <summary>
/// API类型:OfficialAccount或者MiniProgram
/// </summary>
public string APIType { get; set; }
/// <summary>
/// 公众号或者小程序 Token
/// </summary>
public string Token { get; set; }
/// <summary>
/// 微信支付商户号
/// </summary>
public string MCHID { get; set; }
/// <summary>
/// 支付秘钥
/// </summary>
public string Key { get; set; }
/// <summary>
/// 消息队列的链接字符串, 如使用默认消息队列,则无需配置
/// </summary>
public string MQConnectionString { get; set; }
-
亦可使用如下方法配置
services.AddLazyWeChat(options => { options.AppID = "xxxxxxxx"; options.AppSecret = "xxxxxxxx"; });
-
找到Configure方法,添加如下代码:
app.UseLazyWeChat();
-
至此你已经完成了LazyWeChat的基础配置
LazyWeChat服务说明
公众号的相关服务
调用AddLazyWeChat,会向ServiceCollection中注册了4个接口
其中三个是和微信公众号接口相关的
ILazyWeChatBasic
ILazyMessager
ILazyMaterials
public static IServiceCollection AddLazyWeChat(
this IServiceCollection services, Action<LazyWeChatConfiguration> configure)
{
services.AddHttpClient();
var items = new List<(Type, string, ServiceLifetime)>();
items.Add((typeof(IHttpRepository), Constant.IHTTPREPOSITORYIMPELEMENTATION, ServiceLifetime.Transient));
items.Add((typeof(ILazyWeChatBasic), ILAZYWECHATBASICIMPELEMENTATION, ServiceLifetime.Singleton));
items.Add((typeof(ILazyMessager), ILAZYMESSAGERIMPELEMENTATION, ServiceLifetime.Scoped));
items.Add((typeof(ILazyMaterials), ILAZYMATERIALSIMPELEMENTATION, ServiceLifetime.Transient));
return services.RegisterServices(LAZYWXSECTIONNAME, configure, items);
}
在startup.cs中注册服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddLazyWeChat();
}
小程序的相关服务
调用AddLazyMiniProgram,会向ServiceCollection中注册了4个接口
其中三个是和微信小程序接口相关的
ILazyWeChatBasic
ILazyMaterials
ILazyMiniBasic
public static IServiceCollection AddLazyMiniProgram(this IServiceCollection services, Action<LazyWeChatConfiguration> configure)
{
services.AddHttpClient();
var items = new List<(Type, string, ServiceLifetime)>();
items.Add((typeof(IHttpRepository), Constant.IHTTPREPOSITORYIMPELEMENTATION, ServiceLifetime.Transient));
items.Add((typeof(ILazyWeChatBasic), LazyWeChatServiceCollectionExtension.ILAZYWECHATBASICIMPELEMENTATION, ServiceLifetime.Singleton));
items.Add((typeof(ILazyMaterials), LazyWeChatServiceCollectionExtension.ILAZYMATERIALSIMPELEMENTATION, ServiceLifetime.Transient));
items.Add((typeof(ILazyMiniBasic), ILAZYMINIBASICIMPELEMENTATION, ServiceLifetime.Transient));
return services.RegisterServices(LazyWeChatServiceCollectionExtension.LAZYWXSECTIONNAME, configure, items);
}
在startup.cs中注册服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddLazyMiniProgram();
}
微信支付V2相关服务
调用AddLazyPay,会向ServiceCollection中注册了3个接口
其中一个是和微信小程序接口相关的
ILazyBasicPayV2
public static IServiceCollection AddLazyPay(this IServiceCollection services, Action<LazyWeChatConfiguration> configure)
{
services.AddHttpClient();
var items = new List<(Type, string, ServiceLifetime)>();
items.Add((typeof(IHttpRepository), Constant.IHTTPREPOSITORYIMPELEMENTATION, ServiceLifetime.Singleton));
items.Add((typeof(IQRGenerator), Constant.IQRGENERATORIMPELEMENTATION, ServiceLifetime.Transient));
items.Add((typeof(ILazyBasicPayV2), ILAZYBASICPAYV2IMPELEMENTATION, ServiceLifetime.Transient));
return services.RegisterServices(LAZYWXSECTIONNAME, configure, items);
}
在startup.cs中注册服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddLazyPay();
}