三元财务API

WxHelper.cs 8.5KB

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