县级12345后端

WxHelper.cs 9.6KB

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