# WeChatManagement.MiniPrograms

ABP version (opens new window) 小程序模块 (opens new window) 下载量 (opens new window) Discord online (opens new window) GitHub stars (opens new window)

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

# Online Demo

We have launched an online demo for this module: https://wechat.samples.easyabp.io (opens new window)

# Installation

  1. Install the following NuGet packages. (see how (opens new window))

    • EasyAbp.WeChatManagement.MiniPrograms.Application
    • EasyAbp.WeChatManagement.MiniPrograms.Application.Contracts
    • (2选1) EasyAbp.WeChatManagement.MiniPrograms.Domain.OpenIddict
    • (2选1) 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: https://github.com/EasyAbp/WeChatManagement/issues/15#issuecomment-769718739)
  2. Add DependsOn(typeof(WeChatManagementMiniProgramsXxxModule)) attribute to configure the module dependencies. (see how (opens new window))

  3. Add builder.ConfigureWeChatManagementCommon(); and builder.ConfigureWeChatManagementMiniPrograms(); to the OnModelCreating() method in MyProjectMigrationsDbContext.cs.

  4. Add EF Core migrations and update your database. See: ABP document (opens new window).

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

    {
      "WeChatManagement": {
        "MiniPrograms": {
          "AuthServer": {
            "Authority": "https://localhost:44380",
            "ClientId": "MyProjectName_WeChatMiniProgram",
            "ClientSecret": "1q2w3e*"
          }
        }
      }
    }
    
  6. 在 OpenIddictDataSeedContributor 中增加新的客户端 Data Seed (你也可以使用 IDS4 (opens new window)):

    // WeChat MiniProgram
    var weChatMiniProgramClientId =
        configurationSection["MyProjectName_WeChatMiniProgram:ClientId"];
    
    if (!weChatMiniProgramClientId.IsNullOrWhiteSpace())
    {
        await CreateClientAsync(
            weChatMiniProgramClientId,
            commonScopes,
            new[] { "refresh_token", WeChatMiniProgramConsts.GrantType },
            (configurationSection["MyProjectName_WeChatMiniProgram:ClientSecret"] ?? "1q2w3e*").Sha256()
        );
    }
    
  7. 在 DbMigrator 项目的 appsettings.json 中增加:

    {
      "IdentityServer": {
        "Clients": {
          "WeChatManagementSample_WeChatMiniProgram": {
            "ClientId": "MyProjectName_WeChatMiniProgram",
            "ClientSecret": "1q2w3e*"
          }
        }
      }
    }
    
  8. 运行 DbMigrator 项目,以创建新的授权客户端。

# Usage

# 小程序登录

  1. 使用 /api/wechat-management/mini-programs/login/login (POST) 接口进行微信登录,留意 LoginInput (opens new window) 的注释说明。

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

  3. 在有需要时,使用 /api/wechat-management/mini-programs/user-info (PUT) 接口对存储的微信用户信息进行更新。(见 https://github.com/EasyAbp/WeChatManagement/issues/20)

# 小程序授权 Razor 页面登录

  1. 配置用于微信登录的小程序的 Name,默认为Default,参考本模块设置 (opens new window)。

  2. 重写登录页,在页面中插入 WeChatMiniProgramPcLoginWidget (opens new window),重写方法参考 官方文档 (opens new window) 和 本模块示例 (opens new window)。

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

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

MiniProgram MiniProgramUser UserInfo PcLogin

# Roadmap

  • [ ] 微信服务器
  • [ ] 旧账号关联微信登录
  • [x] 微信授权 Razor 页面登录
  • [ ] 对接第三方平台模块
  • [ ] 单元测试
Last Updated: 11/19/2022, 3:18:43 PM