Bitzsoft.Integrations.TeamWork.WeCom 1.0.0-alpha.10

This is a prerelease version of Bitzsoft.Integrations.TeamWork.WeCom.
dotnet add package Bitzsoft.Integrations.TeamWork.WeCom --version 1.0.0-alpha.10
                    
NuGet\Install-Package Bitzsoft.Integrations.TeamWork.WeCom -Version 1.0.0-alpha.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.TeamWork.WeCom" Version="1.0.0-alpha.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.TeamWork.WeCom" Version="1.0.0-alpha.10" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.TeamWork.WeCom" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.TeamWork.WeCom --version 1.0.0-alpha.10
                    
#r "nuget: Bitzsoft.Integrations.TeamWork.WeCom, 1.0.0-alpha.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.TeamWork.WeCom@1.0.0-alpha.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.TeamWork.WeCom&version=1.0.0-alpha.10&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.TeamWork.WeCom&version=1.0.0-alpha.10&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.TeamWork.WeCom

企业微信协同办公服务实现 — 对接企业微信服务端 API。

功能特性

基于 Bitzsoft.Integrations.TeamWork 抽象层实现:

  • 组织架构:部门列表(全量/子树过滤)、成员增删改查(department/*user/*
  • 消息推送:文本应用消息、文本卡片消息(message/send
  • 审批流:发起审批(oa/applyevent XML)、状态查询(oa/getapprovaldetail
  • SSO 登录:OAuth2 扫码授权、code 换用户身份(auth/getuserinfo)、用户敏感信息(auth/getuserdetail
  • 健康检查:通讯录调用探活
  • 客户联系(企业微信特有):外部联系人列表、客户群列表
  • 第三方请求日志:内置 RequestLogging DelegatingHandler,记录所有出站 HTTP 请求与响应

认证机制(多 secret)

企业微信采用应用级权限隔离,不同业务线使用不同 secret 换取的 access_token,互不通用:

用途 secret 覆盖接口
通讯录 通讯录同步 Secret 部门、成员、SSO 用户身份
自建应用 应用 Secret 应用消息、审批
客户联系 客户联系 Secret 外部联系人、客户群

WeComTokenManager 按用途(WeComTokenScope)独立缓存三类 access_token,双重检查锁 + SemaphoreSlim 防并发刷新,提前 300 秒过期。各 API 调用按需取对应用途令牌。当服务端返回 token 失效类错误码(42001/40014/42009)时,自动失效对应用途的缓存令牌,下次调用强制刷新。

平台限制(抛 NotSupportedException

企业微信暂未提供以下能力,对应接口抛 NotSupportedException

  • 统一待办ITeamWorkTodoProvider 全部):平台无统一待办 API,建议审批回调 + 自建待办中台。
  • 消息撤回RecallMessageAsync):应用消息不支持撤回。
  • 审批撤回RecallApprovalAsync):不支持 API 撤回审批。
  • 令牌刷新RefreshAccessTokenAsync):OAuth2 无刷新令牌,需重新授权。
  • 添加外部联系人AddExternalContactAsync):外部联系人通过成员分享添加。

安装

.NET CLI

dotnet add package Bitzsoft.Integrations.TeamWork.WeCom

PackageReference

<PackageReference Include="Bitzsoft.Integrations.TeamWork.WeCom" Version="1.0.0" />

配置

appsettings.json

{
  "TeamWork": {
    "WeCom": {
      "CorpId": "企业 CorpId",
      "AgentId": 1000002,
      "Secrets": {
        "Contacts": "通讯录同步 Secret",
        "Message": "应用消息 Secret",
        "ExternalContact": "客户联系 Secret"
      },
      "BaseUrl": "https://qyapi.weixin.qq.com",
      "OpenBaseUrl": "https://open.work.weixin.qq.com"
    }
  }
}
参数 必填 默认值 说明
CorpId 企业 CorpId
AgentId 应用 AgentId
Secrets:Contacts 通讯录同步 Secret
Secrets:Message 应用消息 Secret
Secrets:ExternalContact 客户联系 Secret(用客户联系能力时必填)
BaseUrl https://qyapi.weixin.qq.com 服务端 API 基地址
OpenBaseUrl https://open.work.weixin.qq.com 开放平台基地址(SSO)
CallbackToken 事件回调 Token
CallbackEncodingAesKey 事件回调加解密 EncodingAESKey

配置在 DI 注册时经 WeComOptionsValidator 校验,必填项缺失或 URL 非 HTTPS 将启动即失败。

注册服务

从 IConfiguration 绑定(推荐)

using Bitzsoft.Integrations.TeamWork.WeCom;

builder.Services.AddBitzsoftWeComTeamWork(
    builder.Configuration, "TeamWork:WeCom");

委托配置

builder.Services.AddBitzsoftWeComTeamWork(opts =>
{
    opts.CorpId = "your-corp-id";
    opts.AgentId = 1000002;
    opts.Secrets = new WeComSecrets
    {
        Contacts = "your-contacts-secret",
        Message = "your-message-secret"
    };
});

第三方请求日志

内置 Bitzsoft.Integrations.RequestLogging 出站请求记录管道,默认使用 NullRequestLogStore 不持久化。

// ① 默认:启用记录管道但不持久化(日志丢弃)
services.AddBitzsoftWeComTeamWork(configuration, "TeamWork:WeCom");

// ② 持久化:宿主注册 IRequestLogStore 实现后,所有出站请求自动落库
services.AddRequestLogging<MyRequestLogStore>(opts =>
{
    opts.MaxInMemoryBodyBytes = 64 * 1024; // 仅控制内存/加密临时文件切换,不截断正文
    opts.SensitiveFields.Add("mySecret");
});
services.AddBitzsoftWeComTeamWork(configuration, "TeamWork:WeCom");

使用示例

核心接口

using Bitzsoft.Integrations.TeamWork.Interfaces;
using Bitzsoft.Integrations.TeamWork.Models.Message;

public class OrgService(ITeamWorkOrgProvider org)
{
    public Task<IReadOnlyList<EmployeeInfo>> GetMembers() => org.GetEmployeesAsync("1");
}

public class NoticeService(ITeamWorkMessageProvider message)
{
    public Task<MessageSendResult> NotifyAsync(string userId) =>
        message.SendWorkNotificationAsync(new WorkNotificationRequest
        {
            ToUserIds = new[] { userId },
            Content = "企业微信应用消息"
        });
}

扩展接口(客户联系)

企业微信实现了 ITeamWorkContactProvider 客户联系接口,使用前用 is 判断供应商是否实现:

if (provider is ITeamWorkContactProvider contacts)
{
    var external = await contacts.GetExternalContactsAsync("staff1");
    var groups = await contacts.GetCustomerGroupsAsync();
}

SSO 单点登录

var sso = (ITeamWorkSsoProvider)provider;

var authUrl = sso.GetAuthorizationUrl("https://app/callback", state: "xyz");
var token = await sso.GetAccessTokenAsync(code, "https://app/callback");
var user = await sso.GetUserInfoAsync(token.AccessToken);

核心类型一览

类型 说明
WeComTeamWorkProvider 企业微信实现(同时实现 6 核心 + 客户联系接口,注册为 Singleton)
WeComOptions 配置(CorpId / AgentId / Secrets / BaseUrl 等)
WeComSecrets 多业务线 Secret 集合(Contacts / Message / ExternalContact)
ITeamWorkOrgProvider 组织架构接口(部门、成员增删改查)
ITeamWorkMessageProvider 消息推送接口(文本 / 卡片消息)
ITeamWorkTodoProvider 统一待办接口(平台无此 API,全部抛 NotSupportedException
ITeamWorkApprovalProvider 审批流接口(发起、状态查询;撤回不支持)
ITeamWorkSsoProvider SSO 登录接口(OAuth2 扫码授权、用户身份/详情)
ITeamWorkHealthProvider 健康检查接口(通讯录调用探活)
ITeamWorkContactProvider 客户联系接口(企业微信特有,外部联系人、客户群)

依赖

说明
Bitzsoft.Integrations.TeamWork 协同办公抽象层
Bitzsoft.Integrations.Compatibility 基础工具库
Bitzsoft.Integrations.RequestLogging 出站请求记录管道
Microsoft.Extensions.Configuration.Abstractions 配置抽象
Microsoft.Extensions.Http IHttpClientFactory
Microsoft.Extensions.Options.ConfigurationExtensions Options 配置节点绑定

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.10 35 7/26/2026
1.0.0-alpha.9 60 7/12/2026
1.0.0-alpha.8 308 7/1/2026
1.0.0-alpha.7 74 6/16/2026
1.0.0-alpha.6 75 6/16/2026
1.0.0-alpha.5 65 6/14/2026