鄂尔多斯-招源科技

WxHelper.cs 8.5KB

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