abp对接微信、小程序等

转载链接:git地址
Abp 小程序管理模块,提供小程序登录、用户个人信息记录、小程序微信服务器等功能,自动适应微信开放平台规则,与微信第三方平台模块轻松衔接。

1.Online Demo
We have launched an online demo for this module: 添加链接描述

2.Installation

Install the following NuGet packages. (see how)

EasyAbp.WeChatManagement.MiniPrograms.Application
EasyAbp.WeChatManagement.MiniPrograms.Application.Contracts
(21) EasyAbp.WeChatManagement.MiniPrograms.Domain.OpenIddict
(21) EasyAbp.WeChatManagement.MiniPrograms.Domain.Ids4
EasyAbp.WeChatManagement.MiniPrograms.Domain.Shared
EasyAbp.WeChatManagement.MiniPrograms.EntityFrameworkCore
EasyAbp.WeChatManagement.MiniPrograms.HttpApi
EasyAbp.WeChatManagement.MiniPrograms.HttpApi.Client
(Optional) EasyAbp.WeChatManagement.MiniPrograms.MongoDB
(Optional) EasyAbp.WeChatManagement.MiniPrograms.Web
(Optional) EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis (重要!如果开发/沙盒/线上均使用了相同的微信AppId,请安装此模块,使用中立缓存共享 AccessToken: #15 (comment))

这里的包注意一下要区分不同的项目分别引用。注意包版本号和项目的匹配。

3.Add DependsOn(typeof(WeChatManagementMiniProgramsXxxModule)) attribute to configure the module dependencies.
这里注意,每个引入了nuget包的有module的都要添加:比如application层:typeof(WeChatManagementMiniProgramsApplicationModule)
4.Add builder.ConfigureWeChatManagementCommon();
and builder.ConfigureWeChatManagementMiniPrograms(); to the OnModelCreating() method in MyProjectMigrationsDbContext.cs.
5. Add EF Core migrations and update your database


dotnet ef migrations add wechat

6.在 Web/Host 项目的 appsettings.json 中增加微信登录授权服务器配置:

{
  "WeChatManagement": {
    "MiniPrograms": {
      "AuthServer": {
        "Authority": "https://localhost:44380",
        "ClientId": "MyProjectName_WeChatMiniProgram",
        "ClientSecret": "1q2w3e*"
      }
    }
  }
}
  1. 在 OpenIddictDataSeedContributor 中增加新的客户端 Data Seed (你也可以使用 IDS4):
    在CreateApplicationsAsync()里面添加
// WeChat MiniProgram
var weChatMiniProgramClientId =
    configurationSection["MyProjectName_WeChatMiniProgram:ClientId"];
if (!weChatMiniProgramClientId.IsNullOrWhiteSpace())
{
    await CreateApplicationAsync(
        name: weChatMiniProgramClientId,
        type: OpenIddictConstants.ClientTypes.Confidential,
        consentType: OpenIddictConstants.ConsentTypes.Implicit,
        displayName: "WeChat Mini-program",
        secret: configurationSection["MyProjectName_WeChatMiniProgram:ClientSecret"] ?? "1q2w3e*",
        grantTypes: new List<string>
        {
            OpenIddictConstants.GrantTypes.RefreshToken,
            WeChatMiniProgramConsts.GrantType
        },
        scopes: commonScopes
    );
}

8.在 OpenIddictDataSeedContributor 中找到

if (grantType == OpenIddictConstants.GrantTypes.ClientCredentials)
{
    application.Permissions.Add(OpenIddictConstants.Permissions.GrantTypes.ClientCredentials);
}

在下面增加

if (grantType == WeChatMiniProgramConsts.GrantType)
{
    application.Permissions.Add($"gt:{WeChatMiniProgramConsts.GrantType}");
}

9.在 DbMigrator 项目的 appsettings.json 中增加:

{
  "OpenIddict": {
    "Applications": {
      "MyProjectName_WeChatMiniProgram": {
        "ClientId": "MyProjectName_WeChatMiniProgram",
        "ClientSecret": "1q2w3e*"
      }
    }
  }
}

10.运行 DbMigrator 项目,以创建新的授权客户端。

小程序登录
使用 /api/wechat-management/mini-programs/login/login (POST) 接口进行微信登录,留意 LoginInput 的注释说明。

使用 /api/wechat-management/mini-programs/login/refresh (POST) 接口对 AccessToken 续期。

在有需要时,使用 /api/wechat-management/mini-programs/user-info (PUT) 接口对存储的微信用户信息进行更新。(见 #20)

配置用于微信登录的小程序的 Name,默认为Default,参考本模块设置。

重写登录页,在页面中插入 WeChatMiniProgramPcLoginWidget,重写方法参考 官方文档 和 本模块示例。

微信扫码后(默认配置下,会打开小程序首页),确保小程序本身已完成用户登录,小程序需要将扫码获得的 scene 作为 token 参数传入 /api/wechat-management/mini-programs/login/authorize-pc 接口。

完成上一步后,Razor 登录页将自动完成登录并跳转。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值