// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using System.Text;
using System.Text.RegularExpressions;
namespace Admin.NET.Plugin.WorkWeixin;
///
/// 创建成员输出参数
///
public class CreateUserWorkWxOutput : BaseWorkWxOutput
{
///
/// 因填写不存在的部门,新增的部门列表
///
[CustomJsonProperty("created_department_list")]
public CreatedDepartmentListDto CreatedDepartmentList { get; set; }
///
/// 新增部门列表
///
public class CreatedDepartmentListDto
{
///
/// 部门信息列表
///
[CustomJsonProperty("department_info")]
public List DepartmentInfo { get; set; }
}
///
/// 部门信息
///
public class DepartmentInfo
{
///
/// 部门名称
///
[CustomJsonProperty("name")]
public string Name { get; set; }
///
/// 部门ID
///
[CustomJsonProperty("id")]
public long Id { get; set; }
}
}
///
/// 读取成员输出参数
///
///
///
注意:应用只能获取可见范围内的成员信息,且每种应用获取的字段有所不同
///
从2022年6月20号开始,新创建的自建应用与代开发应用不再返回敏感字段
///
public class UserWorkWxOutput : BaseWorkWxOutput
{
///
/// 成员UserID。对应管理端的账号,企业内必须唯一。不区分大小写,长度为1~64个字节;第三方应用返回的值为open_userid
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
///
/// 成员名称;第三方不可获取,调用时返回userid以代替name;代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("name")]
public string Name { get; set; }
///
/// 成员所属部门id列表,仅返回该应用有查看权限的部门id
///
[CustomJsonProperty("department")]
public List Department { get; set; }
///
/// 部门内的排序值,默认为0。数量必须和department一致,数值越大排序越前面
///
[CustomJsonProperty("order")]
public List Order { get; set; }
///
/// 职务信息;代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("position")]
public string Position { get; set; }
///
/// 手机号码,代开发自建应用需要管理员授权且成员oauth2授权获取
///
[CustomJsonProperty("mobile")]
public string Mobile { get; set; }
///
/// 性别。0表示未定义,1表示男性,2表示女性
///
[CustomJsonProperty("gender")]
public int? Gender { get; set; }
///
/// 性别描述
///
public string GenderDesc => Gender switch
{
1 => "男",
2 => "女",
0 => "未定义",
_ => "未知"
};
///
/// 邮箱,代开发自建应用需要管理员授权且成员oauth2授权获取
///
[CustomJsonProperty("email")]
public string Email { get; set; }
///
/// 企业邮箱,代开发自建应用需要管理员授权且成员oauth2授权获取
///
[CustomJsonProperty("biz_mail")]
public string BizMail { get; set; }
///
/// 表示在所在的部门内是否为部门负责人,数量与department一致
///
[CustomJsonProperty("is_leader_in_dept")]
public List IsLeaderInDept { get; set; }
///
/// 直属上级UserID,返回在应用可见范围内的直属上级列表,最多有1个直属上级
///
[CustomJsonProperty("direct_leader")]
public List DirectLeader { get; set; }
///
/// 头像url。代开发自建应用需要管理员授权且成员oauth2授权获取
///
[CustomJsonProperty("avatar")]
public string Avatar { get; set; }
///
/// 头像缩略图url
///
[CustomJsonProperty("thumb_avatar")]
public string ThumbAvatar { get; set; }
///
/// 座机。代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("telephone")]
public string Telephone { get; set; }
///
/// 别名
///
[CustomJsonProperty("alias")]
public string Alias { get; set; }
///
/// 地址。代开发自建应用需要管理员授权且成员oauth2授权获取
///
[CustomJsonProperty("address")]
public string Address { get; set; }
///
/// 全局唯一ID。对于同一个服务商,不同应用获取到企业内同一个成员的open_userid是相同的
///
[CustomJsonProperty("open_userid")]
public string OpenUserId { get; set; }
///
/// 主部门,仅当应用对主部门有查看权限时返回
///
[CustomJsonProperty("main_department")]
public long? MainDepartment { get; set; }
///
/// 扩展属性
///
[CustomJsonProperty("extattr")]
public CreateUserWorkWxInput.ExtAttrDto ExtAttr { get; set; }
///
/// 激活状态: 1=已激活,2=已禁用,4=未激活,5=退出企业
///
[CustomJsonProperty("status")]
public int? Status { get; set; }
///
/// 状态描述
///
public string StatusDesc => Status switch
{
1 => "已激活",
2 => "已禁用",
4 => "未激活",
5 => "退出企业",
_ => "未知状态"
};
///
/// 员工个人二维码URL
///
[CustomJsonProperty("qr_code")]
public string QrCode { get; set; }
///
/// 对外职务
///
[CustomJsonProperty("external_position")]
public string ExternalPosition { get; set; }
///
/// 成员对外属性
///
[CustomJsonProperty("external_profile")]
public CreateUserWorkWxInput.ExternalProfileDto ExternalProfile { get; set; }
///
/// 是否为部门负责人(主部门)
///
public bool IsLeader
{
get
{
if (IsLeaderInDept == null || Department == null || MainDepartment == null)
return false;
var mainDeptIndex = Department.IndexOf(MainDepartment.Value);
return mainDeptIndex >= 0 && mainDeptIndex < IsLeaderInDept.Count && IsLeaderInDept[mainDeptIndex] == 1;
}
}
///
/// 是否已激活
///
public bool IsActive => Status == 1;
///
/// 获取主部门名称(需要外部传入部门映射)
///
public string GetMainDepartmentName(Dictionary departmentMap)
{
if (MainDepartment.HasValue && departmentMap != null && departmentMap.TryGetValue(MainDepartment.Value, out var name))
return name;
return null;
}
///
/// 视频号信息(响应)
///
public class WechatChannelsResponse
{
///
/// 视频号名称
///
[CustomJsonProperty("nickname")]
public string Nickname { get; set; }
///
/// 视频号状态
///
[CustomJsonProperty("status")]
public int? Status { get; set; }
}
}
///
/// 获取部门成员输出参数
///
public class DeptUserSimpleListWorkWxOutput : BaseWorkWxOutput
{
///
/// 成员列表
///
[CustomJsonProperty("userlist")]
public List UserList { get; set; } = new List();
///
/// 成员数量
///
public int Count => UserList?.Count ?? 0;
///
/// 是否包含成员
///
public bool HasUsers => Count > 0;
///
/// 获取用户ID列表
///
public List UserIds => UserList?.Select(u => u.UserId).ToList() ?? new List();
///
/// 获取OpenUserID列表
///
public List OpenUserIds => UserList?.Where(u => !string.IsNullOrEmpty(u.OpenUserId))
.Select(u => u.OpenUserId)
.ToList() ?? new List();
///
/// 部门成员简略信息
///
public class DepartmentUserSimpleInfo
{
///
/// 成员UserID。对应管理端的账号
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
///
/// 成员名称,第三方应用可能返回userid代替name
///
[CustomJsonProperty("name")]
public string Name { get; set; }
///
/// 成员所属部门列表。列表项为部门ID,32位整型
///
[CustomJsonProperty("department")]
public List Department { get; set; }
///
/// 全局唯一ID。对于同一个服务商,不同应用获取到企业内同一个成员的open_userid是相同的
///
[CustomJsonProperty("open_userid")]
public string OpenUserId { get; set; }
///
/// 获取显示名称(优先显示name,如果没有则显示userid)
///
public string DisplayName => !string.IsNullOrEmpty(Name) ? Name : UserId;
///
/// 是否包含指定部门
///
public bool ContainsDepartment(long departmentId)
{
return Department?.Contains(departmentId) ?? false;
}
///
/// 获取主部门(第一个部门)
///
public long? MainDepartment => Department?.FirstOrDefault();
}
}
///
/// 获取部门成员详情输出参数
///
public class DeptUserDetailListWorkWxOutput : BaseWorkWxOutput
{
///
/// 成员列表
///
[CustomJsonProperty("userlist")]
public List UserList { get; set; } = new List();
///
/// 成员数量
///
public int Count => UserList?.Count ?? 0;
///
/// 是否包含成员
///
public bool HasUsers => Count > 0;
///
/// 获取已激活的成员列表
///
public List ActiveUsers => UserList?.Where(u => u.IsActive).ToList() ?? new List();
///
/// 获取部门负责人列表
///
public List LeaderUsers => UserList?.Where(u => u.IsLeader).ToList() ?? new List();
///
/// 获取用户ID列表
///
public List UserIds => UserList?.Select(u => u.UserId).ToList() ?? new List();
///
/// 按状态分组统计
///
public Dictionary GetStatusStatistics()
{
return UserList?
.GroupBy(u => u.StatusDesc)
.ToDictionary(g => g.Key, g => g.Count()) ?? new Dictionary();
}
///
/// 部门成员详细信息
///
public class DepartmentUserDetailInfo
{
///
/// 成员UserID。对应管理端的账号
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
///
/// 成员名称;第三方不可获取,调用时返回userid以代替name
///
[CustomJsonProperty("name")]
public string Name { get; set; }
///
/// 英文名
///
[CustomJsonProperty("english_name")]
public string EnglishName { get; set; }
///
/// 成员所属部门id列表,仅返回该应用有查看权限的部门id
///
[CustomJsonProperty("department")]
public List Department { get; set; }
///
/// 部门内的排序值,默认为0。数量必须和department一致
///
[CustomJsonProperty("order")]
public List Order { get; set; }
///
/// 职务信息;代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("position")]
public string Position { get; set; }
///
/// 手机号码,代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("mobile")]
public string Mobile { get; set; }
///
/// 性别。0表示未定义,1表示男性,2表示女性
///
[CustomJsonProperty("gender")]
public int? Gender { get; set; }
///
/// 性别描述
///
public string GenderDesc => Gender switch
{
1 => "男",
2 => "女",
0 => "未定义",
_ => "未知"
};
///
/// 邮箱,代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("email")]
public string Email { get; set; }
///
/// 企业邮箱,代开发自建应用不返回
///
[CustomJsonProperty("biz_mail")]
public string BizMail { get; set; }
///
/// 表示在所在的部门内是否为部门负责人。0-否;1-是
///
[CustomJsonProperty("is_leader_in_dept")]
public List IsLeaderInDept { get; set; }
///
/// 直属上级UserID,返回在应用可见范围内的直属上级列表
///
[CustomJsonProperty("direct_leader")]
public List DirectLeader { get; set; }
///
/// 头像url。第三方仅通讯录应用可获取
///
[CustomJsonProperty("avatar")]
public string Avatar { get; set; }
///
/// 头像缩略图url。第三方仅通讯录应用可获取
///
[CustomJsonProperty("thumb_avatar")]
public string ThumbAvatar { get; set; }
///
/// 座机。代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("telephone")]
public string Telephone { get; set; }
///
/// 别名;第三方仅通讯录应用可获取
///
[CustomJsonProperty("alias")]
public string Alias { get; set; }
///
/// 扩展属性
///
[CustomJsonProperty("extattr")]
public CreateUserWorkWxInput.ExtAttrDto ExtAttr { get; set; }
///
/// 激活状态: 1=已激活,2=已禁用,4=未激活,5=退出企业
///
[CustomJsonProperty("status")]
public int? Status { get; set; }
///
/// 状态描述
///
public string StatusDesc => Status switch
{
1 => "已激活",
2 => "已禁用",
4 => "未激活",
5 => "退出企业",
_ => "未知状态"
};
///
/// 地址。代开发自建应用需要管理员授权才返回
///
[CustomJsonProperty("address")]
public string Address { get; set; }
///
/// 全局唯一ID。仅第三方应用可获取
///
[CustomJsonProperty("open_userid")]
public string OpenUserId { get; set; }
///
/// 主部门,仅当应用对主部门有查看权限时返回
///
[CustomJsonProperty("main_department")]
public long? MainDepartment { get; set; }
///
/// 员工个人二维码URL
///
[CustomJsonProperty("qr_code")]
public string QrCode { get; set; }
///
/// 对外职务
///
[CustomJsonProperty("external_position")]
public string ExternalPosition { get; set; }
///
/// 成员对外属性
///
[CustomJsonProperty("external_profile")]
public CreateUserWorkWxInput.ExternalProfileDto ExternalProfile { get; set; }
///
/// 获取显示名称(优先显示name,如果没有则显示userid)
///
public string DisplayName => !string.IsNullOrEmpty(Name) ? Name : UserId;
///
/// 是否已激活
///
public bool IsActive => Status == 1;
///
/// 是否为部门负责人(主部门)
///
public bool IsLeader
{
get
{
if (IsLeaderInDept == null || Department == null || MainDepartment == null)
return false;
var mainDeptIndex = Department.IndexOf(MainDepartment.Value);
return mainDeptIndex >= 0 && mainDeptIndex < IsLeaderInDept.Count && IsLeaderInDept[mainDeptIndex] == 1;
}
}
///
/// 是否包含指定部门
///
public bool ContainsDepartment(long departmentId)
{
return Department?.Contains(departmentId) ?? false;
}
///
/// 获取主部门排序值
///
public long? GetMainDepartmentOrder()
{
if (Order == null || Department == null || MainDepartment == null)
return null;
var mainDeptIndex = Department.IndexOf(MainDepartment.Value);
return mainDeptIndex >= 0 && mainDeptIndex < Order.Count ? Order[mainDeptIndex] : (long?)null;
}
}
}
///
/// userid转openid输出参数
///
public class ConvToOpenIdWorkWxOutput : BaseWorkWxOutput
{
///
/// 企业微信成员userid对应的openid
///
[CustomJsonProperty("openid")]
public string OpenId { get; set; }
}
///
/// openid转userid输出参数
///
public class ConvToUserIdWorkWxOutput : BaseWorkWxOutput
{
///
/// 该openid在企业微信对应的成员userid
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
}
///
/// 邀请成员输出参数
///
public class InviteUserWorkWxOutput : BaseWorkWxOutput
{
///
/// 非法成员列表
///
[CustomJsonProperty("invaliduser")]
public List InvalidUser { get; set; } = new List();
///
/// 非法部门列表
///
[CustomJsonProperty("invalidparty")]
public List InvalidParty { get; set; } = new List();
///
/// 非法标签列表
///
[CustomJsonProperty("invalidtag")]
public List InvalidTag { get; set; } = new List();
///
/// 有效成员数量
///
public int ValidUserCount => (InputUserCount - InvalidUserCount);
///
/// 有效部门数量
///
public int ValidPartyCount => (InputPartyCount - InvalidPartyCount);
///
/// 有效标签数量
///
public int ValidTagCount => (InputTagCount - InvalidTagCount);
///
/// 非法成员数量
///
public int InvalidUserCount => InvalidUser?.Count ?? 0;
///
/// 非法部门数量
///
public int InvalidPartyCount => InvalidParty?.Count ?? 0;
///
/// 非法标签数量
///
public int InvalidTagCount => InvalidTag?.Count ?? 0;
///
/// 输入成员数量(需要在调用时设置)
///
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public int InputUserCount { get; set; }
///
/// 输入部门数量(需要在调用时设置)
///
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public int InputPartyCount { get; set; }
///
/// 输入标签数量(需要在调用时设置)
///
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public int InputTagCount { get; set; }
///
/// 是否全部有效
///
public bool IsAllValid => InvalidUserCount == 0 && InvalidPartyCount == 0 && InvalidTagCount == 0;
///
/// 获取邀请结果摘要
///
public string GetInviteSummary()
{
var summary = new StringBuilder();
summary.Append("邀请结果: ");
if (InvalidUserCount > 0)
{
summary.Append($"{InvalidUserCount}个无效成员");
}
if (InvalidPartyCount > 0)
{
if (summary.Length > 12) summary.Append(", ");
summary.Append($"{InvalidPartyCount}个无效部门");
}
if (InvalidTagCount > 0)
{
if (summary.Length > 12) summary.Append(", ");
summary.Append($"{InvalidTagCount}个无效标签");
}
if (IsAllValid)
{
summary.Append("全部有效");
}
return summary.ToString();
}
}
///
/// 获取加入企业二维码输出参数
///
public class JoinQrcodeWorkWxOutput : BaseWorkWxOutput
{
///
/// 二维码链接,有效期7天
///
[CustomJsonProperty("join_qrcode")]
public string JoinQrcode { get; set; }
///
/// 二维码尺寸描述
///
public string QrcodeSizeDesc => SizeType switch
{
1 => "171x171",
2 => "399x399",
3 => "741x741",
4 => "2052x2052",
_ => "未知尺寸"
};
///
/// 尺寸类型(从URL中解析)
///
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public int? SizeType
{
get
{
if (string.IsNullOrEmpty(JoinQrcode)) return null;
var match = Regex.Match(JoinQrcode, @"qr_size=(\d)");
return match.Success ? int.Parse(match.Groups[1].Value) : null;
}
}
}
///
/// 成员Id输出参数
///
public class UserIdWorkWxOutput : BaseWorkWxOutput
{
///
/// 用户userid
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
}
///
/// 获取成员ID列表输出参数
///
public class UserIdListWorkWxOutput : BaseWorkWxOutput
{
///
/// 分页游标,下次请求时填写以获取之后分页的记录
///
[CustomJsonProperty("next_cursor")]
public string NextCursor { get; set; }
///
/// 用户-部门关系列表
///
[CustomJsonProperty("dept_user")]
public List DeptUser { get; set; } = new List();
///
/// 用户数量
///
public int UserCount => DeptUser?.Count ?? 0;
///
/// 是否还有更多数据
///
public bool HasMore => !string.IsNullOrEmpty(NextCursor);
///
/// 获取去重后的用户ID列表
///
public List DistinctUserIds => DeptUser?.Select(d => d.UserId).Distinct().ToList() ?? new List();
///
/// 按部门分组用户
///
public Dictionary> GroupUsersByDepartment()
{
var result = new Dictionary>();
if (DeptUser == null) return result;
foreach (var item in DeptUser)
{
if (!result.ContainsKey(item.Department))
{
result[item.Department] = new List();
}
result[item.Department].Add(item.UserId);
}
return result;
}
///
/// 按用户分组部门
///
public Dictionary> GroupDepartmentsByUser()
{
var result = new Dictionary>();
if (DeptUser == null) return result;
foreach (var item in DeptUser)
{
if (!result.ContainsKey(item.UserId))
{
result[item.UserId] = new List();
}
result[item.UserId].Add(item.Department);
}
return result;
}
///
/// 用户-部门关系信息
///
public class DeptUserInfo
{
///
/// 用户userid,当用户在多个部门下时会有多条记录
///
[CustomJsonProperty("userid")]
public string UserId { get; set; }
///
/// 用户所属部门
///
[CustomJsonProperty("department")]
public long Department { get; set; }
}
}