Нет описания

WxHelper.cs 9.4KB

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