/// <summary> /// 获取token /// </summary> /// <returns></returns> public static string GetToken(EnumWeChatAppType app) { try { Dictionary <string, object> dic = new Dictionary <string, object> { { "corpid", autoInfoList.FirstOrDefault(a => a.AppId == (int)app)?.CorpId }, { "corpsecret", autoInfoList.FirstOrDefault(a => a.AppId == (int)app)?.CorpSecret } }; string resultJson = HttpHelper.GetData(GetTokenUrl, dic); WeChatTokenReuslt token = resultJson.ToObject <WeChatTokenReuslt>(); if (token.errcode != "0") { LogHelper.WriteLog_LocalTxt(resultJson); logger?.LogWarning(resultJson); return(null); } return(token.access_token); } catch (Exception e) { LogHelper.WriteLog_LocalTxt(e.Message); return(null); } }
/// <summary> /// 发送企业微信消息 /// </summary> /// <param name="token">token</param> /// <param name="userList">指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为”@all”,则向该企业应用的全部成员发送</param> /// <param name="agentid">企业应用的id,整型。企业内部开发,可在应用的设置页面查看</param> /// <param name="msgContent">消息内容,最长不超过2048个字节,超过将截断(支持id转译)</param> /// <returns></returns> public static bool SendMsg(string token, string userList, EnumWeChatAppType app, WeChatSendMsgContext weChatSendMsgContext) { try { WeChatSendMsg weChatSendMsg = new WeChatSendMsg(); weChatSendMsgContext.url = autoInfoList.FirstOrDefault(a => a.AppId == (int)app)?.Url + weChatSendMsgContext.url; Dictionary <string, object> dic = new Dictionary <string, object> { { "touser", userList }, { "msgtype", "textcard" }, { "agentid", autoInfoList.FirstOrDefault(a => a.AppId == (int)app)?.AgentId }, { "textcard", weChatSendMsgContext } }; string url = SendMssage + "?access_token=" + token; string resultJson = HttpHelper.PostData(url, dic, contentType: ContentType.Json); WeChatSendMsgResult weChatSendMsgResult = resultJson.ToObject <WeChatSendMsgResult>(); if (weChatSendMsgResult.errcode != "0") { LogHelper.WriteLog_LocalTxt(resultJson); logger?.LogWarning(resultJson); return(false); } return(true); } catch (Exception e) { LogHelper.WriteLog_LocalTxt(e.Message); return(false); } }
/// <summary> /// 根据Code获取用户信息 /// </summary> /// <returns></returns> public static WeChatUserInfo GetUserInfo(EnumWeChatAppType app, string code) { try { string token = GetToken(app); string userId = GetUserId(code, token); Dictionary <string, object> dic = new Dictionary <string, object> { { "access_token", token }, { "userid", userId } }; string resultJson = HttpHelper.GetData(GetUserInfoUrl, dic); WeChatUserInfo userIdInfo = resultJson.ToObject <WeChatUserInfo>(); if (userIdInfo.errcode != "0") { LogHelper.WriteLog_LocalTxt(resultJson); logger?.LogWarning(resultJson); return(null); } return(userIdInfo); } catch (Exception e) { LogHelper.WriteLog_LocalTxt(e.Message); return(null); } }
/// <summary> /// 获取配置信息 /// </summary> /// <param name="app"></param> /// <returns></returns> public static WeChatAuthInfo GetWeChatAuthInfo(EnumWeChatAppType app) { return(autoInfoList.FirstOrDefault(a => a.AppId == (int)app)); }