Нет описания

WxHelper.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using CallCenter.Utility;
  2. using CallCenterAPI.WechatSDK.Models;
  3. using Senparc.Weixin;
  4. using Senparc.Weixin.MP;
  5. using Senparc.Weixin.MP.AdvancedAPIs;
  6. using Senparc.Weixin.MP.AdvancedAPIs.CustomService;
  7. using Senparc.Weixin.MP.CommonAPIs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace CallCenterAPI.WechatSDK
  14. {
  15. public static class WxHelper
  16. {
  17. public static string WxId;
  18. public static string AppId;
  19. public static string AppSecret;
  20. public static string RedirectUrl;
  21. public static string CommonToken;//Common的Token
  22. public static DateTime GetCommonTokenDate;//获取CommonToken的时间
  23. public static int CommonTokenExpiresIn;//有效时间(s)
  24. static WxHelper()
  25. {
  26. AppId = Configs.GetValue("WechatAppid");
  27. AppSecret = Configs.GetValue("WechatAppsecret");
  28. }
  29. /// <summary>
  30. /// 获取openid
  31. /// </summary>
  32. /// <param name="wxLoginDto"></param>
  33. /// <returns></returns>
  34. public static WxLoginDto GetOpenId(WxLoginDto wxLoginDto)
  35. {
  36. if (string.IsNullOrWhiteSpace(wxLoginDto.Code))
  37. {
  38. var url = OAuthApi.GetAuthorizeUrl(AppId, RedirectUrl, "qwertyuidfghjkl", OAuthScope.snsapi_base);
  39. return wxLoginDto;
  40. }
  41. var access = OAuthApi.GetAccessToken(AppId, AppSecret, wxLoginDto.Code);
  42. wxLoginDto.OpenId = access.openid;
  43. return wxLoginDto;
  44. }
  45. /// <summary>
  46. /// 获取用户信息
  47. /// </summary>
  48. /// <param name="wxLoginDto"></param>
  49. /// <returns></returns>
  50. public static WxLoginDto GetUserInfo(WxLoginDto wxLoginDto)
  51. {
  52. if (string.IsNullOrWhiteSpace(wxLoginDto.Code))
  53. {
  54. var url = OAuthApi.GetAuthorizeUrl(AppId, wxLoginDto.RedirectUrl, "qwertyuidfghjkl", OAuthScope.snsapi_userinfo);
  55. wxLoginDto.RedirectUrl = url;
  56. return wxLoginDto;
  57. }
  58. var access = OAuthApi.GetAccessToken(AppId, AppSecret, wxLoginDto.Code);
  59. wxLoginDto.OpenId = access.openid;
  60. wxLoginDto.UserInfo = OAuthApi.GetUserInfo(access.access_token, access.openid);
  61. return wxLoginDto;
  62. }
  63. /// <summary>
  64. /// 获取common的token
  65. /// </summary>
  66. /// <returns></returns>
  67. public static string GetCommonToken()
  68. {
  69. double ss = DateTime.Now.Subtract(GetCommonTokenDate).TotalSeconds;
  70. if (ss > CommonTokenExpiresIn)
  71. {
  72. var obj = CommonApi.GetToken(AppId, AppSecret);
  73. if (obj != null && obj.errcode == ReturnCode.请求成功)
  74. {
  75. GetCommonTokenDate = DateTime.Now.AddSeconds(-1);
  76. CommonTokenExpiresIn = obj.expires_in;
  77. CommonToken = obj.access_token;
  78. }
  79. else
  80. {
  81. CommonToken = "";
  82. }
  83. }
  84. return CommonToken;
  85. }
  86. public static bool CheckSignature(string signature, string timestamp, string nonce, string token)
  87. {
  88. bool bl = false;
  89. string accesstoken = GetCommonToken();
  90. if (accesstoken != "")
  91. {
  92. bl = Senparc.Weixin.MP.CheckSignature.Check(signature, timestamp, nonce, token);
  93. }
  94. return bl;
  95. }
  96. #region 多客服
  97. /// <summary>
  98. /// 创建客服会话
  99. /// </summary>
  100. /// <param name="openid"></param>
  101. /// <returns></returns>
  102. public static bool CreateSession(string openid)
  103. {
  104. bool bl = false;
  105. string accesstoken = GetCommonToken();
  106. if (accesstoken != "")
  107. {
  108. CustomApi.SendText(accesstoken, openid, "您好,正在为您接通人工客服,请稍候......");
  109. var zxlist = CustomServiceApi.GetCustomOnlineInfo(accesstoken).kf_online_list;//在线客服列表
  110. if (zxlist.Count > 0)
  111. {
  112. var kf = zxlist.OrderBy(p => p.accepted_case).FirstOrDefault();//获取当前已经接入用户最少的客服
  113. if (kf != null)
  114. {
  115. var obj = CustomServiceApi.CreateSession(accesstoken, openid, kf.kf_account);//创建客服和用户之间的会话
  116. if (obj.errcode == ReturnCode.请求成功)
  117. {
  118. bl = true;
  119. }
  120. else
  121. {
  122. CustomApi.SendText(accesstoken, openid, "接通人工客服异常,请重试");
  123. }
  124. }
  125. else
  126. {
  127. CustomApi.SendText(accesstoken, openid, "人工客服正在忙碌中,请稍候联系");
  128. }
  129. }
  130. }
  131. return bl;
  132. }
  133. /// <summary>
  134. /// 关闭客服会话
  135. /// </summary>
  136. /// <param name="openid"></param>
  137. /// <returns></returns>
  138. public static bool CloseSession(string openid, string kf_account)
  139. {
  140. bool bl = false;
  141. string accesstoken = GetCommonToken();
  142. if (accesstoken != "")
  143. {
  144. var obj = CustomServiceApi.CloseSession(accesstoken, openid, kf_account);
  145. if (obj.errcode == ReturnCode.请求成功)
  146. {
  147. bl = true;
  148. CustomApi.SendText(accesstoken, openid, "已经关闭与人工客服的会话");
  149. }
  150. else
  151. {
  152. CustomApi.SendText(accesstoken, openid, "关闭会话异常,请重试");
  153. }
  154. }
  155. return bl;
  156. }
  157. /// <summary>
  158. /// 获取用户聊天记录
  159. /// </summary>
  160. /// <param name="stime"></param>
  161. /// <param name="etime"></param>
  162. /// <param name="pageSize"></param>
  163. /// <param name="pageIndex"></param>
  164. /// <returns></returns>
  165. public static List<object> GetRecord(DateTime stime, DateTime etime, int pageSize, int pageIndex)
  166. {
  167. List<object> list = new List<object>();
  168. string accesstoken = GetCommonToken();
  169. if (accesstoken != "")
  170. {
  171. var result = CustomServiceApi.GetRecord(accesstoken, stime, etime, pageSize, pageIndex);
  172. if (result.errcode == ReturnCode.请求成功)
  173. {
  174. list = result.recordlist.Select(p => new
  175. {
  176. p.openid,
  177. p.text,
  178. time = GetTime(p.time).ToString("yyyy-MM-dd HH:mm:ss"),
  179. receiver = p.opercode == Opercode.客服收到消息 ? CustomServiceApi.GetCustomBasicInfo(accesstoken).kf_list.Where(d => d.kf_account == p.worker).FirstOrDefault().kf_nick + "(客服)" : UserApi.Info(accesstoken, p.openid).nickname,
  180. sender = p.opercode == Opercode.客服发送消息 ? CustomServiceApi.GetCustomBasicInfo(accesstoken).kf_list.Where(d => d.kf_account == p.worker).FirstOrDefault().kf_nick + "(客服)" : UserApi.Info(accesstoken, p.openid).nickname
  181. }).ToList<object>();
  182. }
  183. }
  184. return list;
  185. }
  186. /// <summary>
  187. /// 获取聊天记录
  188. /// </summary>
  189. /// <param name="stime"></param>
  190. /// <param name="etime"></param>
  191. /// <param name="pageSize"></param>
  192. /// <param name="pageIndex"></param>
  193. /// <returns></returns>
  194. public static List<object> GetMsgList(DateTime stime, DateTime etime, long msgid, int number)
  195. {
  196. List<object> list = new List<object>();
  197. string accesstoken = GetCommonToken();
  198. if (accesstoken != "")
  199. {
  200. var result = CustomServiceApi.GetMsgList(accesstoken, stime, etime, msgid, number);
  201. if (result.errcode == ReturnCode.请求成功)
  202. {
  203. list = result.recordList.Select(p => new
  204. {
  205. p.openid,
  206. p.opercode,
  207. p.text,
  208. p.worker,
  209. time = p.time.ToString("yyyy-MM-dd HH:mm:ss"),
  210. name = UserApi.Info(accesstoken, p.openid).nickname,
  211. kfname = CustomServiceApi.GetCustomBasicInfo(accesstoken).kf_list.Where(d => d.kf_account == p.worker).FirstOrDefault().kf_nick
  212. }).ToList<object>();
  213. }
  214. }
  215. return list;
  216. }
  217. /// <summary>
  218. /// 获取时间
  219. /// </summary>
  220. /// <param name="dt"></param>
  221. /// <returns></returns>
  222. public static DateTime GetTime(double dt)
  223. {
  224. DateTime time = DateTime.MinValue;
  225. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  226. time = startTime.AddSeconds(dt);
  227. return time;
  228. }
  229. #endregion
  230. }
  231. }