人民医院API

DingTalkHelper.cs 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace RMYY_CallCenter_Api.Utility
  10. {
  11. public class DingTalkHelper
  12. {
  13. /// <summary>
  14. /// 获取AccessToken
  15. /// </summary>
  16. /// <returns></returns>
  17. public static string GetAccessToken()
  18. {
  19. var accesstoken = RedisHelper.StringGet("DingTalkToken")?.ToString() ?? "";
  20. if (string.IsNullOrEmpty(accesstoken))
  21. {
  22. string result = HttpHelper.HttpGet(string.Format("https://oapi.dingtalk.com/gettoken?appkey={0}&appsecret={1}", ConfigHelper.GetValue("DingTalk_AppKey"), ConfigHelper.GetValue("DingTalk_AppSecret")));
  23. var resultobj = result.ToJObject();
  24. if (resultobj["errcode"].ToString() == "0")
  25. {
  26. accesstoken = resultobj["access_token"].ToString();
  27. string cachetime = ConfigHelper.GetValue("DingTalk_TokenCacheTime");
  28. RedisHelper.StringSet("DingTalkToken", accesstoken, new TimeSpan(0, 0, cachetime.ToInt()));
  29. }
  30. }
  31. return accesstoken;
  32. }
  33. /// <summary>
  34. /// 根据手机号获取用户id
  35. /// </summary>
  36. /// <param name="mobile">手机号</param>
  37. /// <returns></returns>
  38. public static string GetUserIdByMobile(string mobile)
  39. {
  40. string userid = string.Empty;
  41. var param = new
  42. {
  43. mobile = mobile
  44. };
  45. var strresult = HttpHelper.HttpPost(string.Format("https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token={0}", GetAccessToken()), param.ToJson(), "application/json");
  46. if (!string.IsNullOrEmpty(strresult))
  47. {
  48. var resultobj = strresult.ToJObject();
  49. if (resultobj["errcode"].ToString() != "0")
  50. {
  51. LogHelper.Error(strresult);
  52. //userid = resultobj["errmsg"].ToString();
  53. }
  54. else
  55. {
  56. var robj = resultobj["result"].ToString().ToJObject();
  57. userid = robj["userid"].ToString();
  58. }
  59. }
  60. return userid;
  61. }
  62. /// <summary>
  63. /// 发送模板消息
  64. /// </summary>
  65. /// <param name="userids">多个用户逗号隔开</param>
  66. /// <param name="deptids">多个部门逗号隔开</param>
  67. /// <param name="msgjson">模板参数json</param>
  68. /// <param name="templateid">模板ID</param>
  69. /// <returns></returns>
  70. public static string SendMessageByTemplate(string userids, string deptids, string msgjson, string templateid)
  71. {
  72. string result = string.Empty;
  73. var param = new
  74. {
  75. agent_id = ConfigHelper.GetValue("DingTalk_AgentId"),
  76. template_id = templateid,
  77. //data = "{\"name\":\"淘宝6\",\"name2\":\"http://www.taobao.com\"}",
  78. data = msgjson,
  79. //userid_list = "213,234,123",
  80. userid_list = userids,
  81. //dept_id_list = "421,897,262"
  82. dept_id_list= deptids
  83. };
  84. var strresult = HttpHelper.HttpPost(string.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/sendbytemplate?access_token={0}", GetAccessToken()), param.ToJson(), "application/json");
  85. if (!string.IsNullOrEmpty(strresult))
  86. {
  87. var resultobj = strresult.ToJObject();
  88. if (resultobj["errcode"].ToString() != "0")
  89. {
  90. LogHelper.Error(strresult);
  91. result = resultobj["errmsg"].ToString();
  92. }
  93. }
  94. return result;
  95. }
  96. /// <summary>
  97. /// 发送工作通知
  98. /// </summary>
  99. /// <param name="userids">多个用户逗号隔开</param>
  100. /// <param name="deptids">多个部门逗号隔开</param>
  101. /// <param name="msgjson">内容json</param>
  102. /// <param name="isall">是否全部群发</param>
  103. /// <returns></returns>
  104. public static string SendMessage(string userids, string deptids, string msgjson, bool isall=false)
  105. {
  106. string result = string.Empty;
  107. var param = new JObject();
  108. param["agent_id"] = ConfigHelper.GetValue("DingTalk_AgentId");
  109. if (!string.IsNullOrEmpty(userids))
  110. {
  111. param["userid_list"] = userids;
  112. }
  113. if (!string.IsNullOrEmpty(deptids))
  114. {
  115. param["dept_id_list"] = deptids;
  116. }
  117. param["to_all_user"] = isall;
  118. param["msg"] = msgjson.ToJObject();
  119. //var param=new
  120. //{
  121. // agent_id = ConfigHelper.GetValue("DingTalk_AgentId"),
  122. // //userid_list = "213,234,123",
  123. // userid_list = userids,
  124. // //dept_id_list = "421,897,262"
  125. // dept_id_list = deptids,
  126. // to_all_user = isall,
  127. // //msg = new {
  128. // // msgtype="text",
  129. // // text = new {content="内容" }
  130. // //},
  131. // //msg = new
  132. // //{
  133. // // msgtype = "markdown",
  134. // // markdown = new { text = "内容",title="标题" }
  135. // //},
  136. // msg
  137. //};
  138. var strresult = HttpHelper.HttpPost(string.Format("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={0}", GetAccessToken()), param.ToJson(), "application/json");
  139. if (!string.IsNullOrEmpty(strresult))
  140. {
  141. var resultobj = strresult.ToJObject();
  142. if (resultobj["errcode"].ToString() != "0")
  143. {
  144. LogHelper.Error(strresult);
  145. result = resultobj["errmsg"].ToString();
  146. }
  147. }
  148. return result;
  149. }
  150. /// <summary>
  151. /// 发送工作通知-文本
  152. /// </summary>
  153. /// <param name="userids">多个用户逗号隔开</param>
  154. /// <param name="deptids">多个部门逗号隔开</param>
  155. /// <param name="content">文本内容</param>
  156. /// <returns></returns>
  157. public static string SendMessageText(string userids, string deptids, string content)
  158. {
  159. var msg = new
  160. {
  161. msgtype = "text",
  162. text = new { content }
  163. };
  164. string result = SendMessage(userids, deptids, msg.ToJson());
  165. return result;
  166. }
  167. /// <summary>
  168. ///
  169. /// </summary>
  170. /// <param name="userid"></param>
  171. /// <returns></returns>
  172. public static string GetUserInfoByUserId(string userid)
  173. {
  174. //WebClient web = new WebClient();
  175. //web.Encoding = Encoding.UTF8;
  176. //string strresult = web.DownloadString(string.Format("https://oapi.dingtalk.com/user/get?access_token={0}&userid={1}", GetAccessToken(), userid));
  177. //JObject jo = (JObject)JsonConvert.DeserializeObject(strresult);
  178. //return jo.ToJson();
  179. string url = string.Format("https://oapi.dingtalk.com/user/get?access_token={0}&userid={1}", GetAccessToken(), userid);
  180. var strresult = HttpHelper.HttpGet(url);
  181. return strresult;
  182. }
  183. //给某个部门的所有的人发消息
  184. /// <summary>
  185. /// 获取部门列表 参数传1 是根节点的子部门
  186. /// https://open.dingtalk.com/document/orgapp-server/obtain-the-department-list-v2
  187. /// </summary>
  188. /// <returns></returns>
  189. public static string GetDeptlist()
  190. {
  191. WebClient web = new WebClient();
  192. web.Encoding = Encoding.UTF8;
  193. string strresult = web.DownloadString(string.Format("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token={0}&dept_id={1}", GetAccessToken(), 1));
  194. JObject jo = (JObject)JsonConvert.DeserializeObject(strresult);
  195. return jo.ToJson();
  196. }
  197. /// <summary>
  198. /// 获取部门所有的userid
  199. /// </summary>
  200. /// <param name="deptid">Get https://oapi.dingtalk.com/user/getDeptMember </param>
  201. /// <returns></returns>
  202. public static string GetUserIdsByDeptId(double deptid)
  203. {
  204. WebClient web = new WebClient();
  205. web.Encoding = Encoding.UTF8;
  206. string strresult = web.DownloadString(string.Format("https://oapi.dingtalk.com/user/getDeptMember?access_token={0}&deptId={1}", GetAccessToken(), deptid));
  207. JObject jo = (JObject)JsonConvert.DeserializeObject(strresult);
  208. if (jo != null && jo["userIds"] != null)
  209. {
  210. return jo["userIds"].ToString();
  211. }
  212. //JsonConvert.SerializeObject();
  213. //TreeModel model= JsonConvert.DeserializeObject<TreeModel>(strresult);
  214. return "";
  215. }
  216. }
  217. }