Brak opisu

PushMessageController.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using CallCenter.Utility;
  2. using CallCenterApi.Interface.Controllers.Base;
  3. using CallCenterApi.Model;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace CallCenterApi.Interface.Controllers
  14. {
  15. public class PushMessageController : BaseController
  16. {
  17. //参数设置 <-----参数需要重新设置----->
  18. //http的域名
  19. //https的域名
  20. //private static String HOST = "https://api.getui.com/apiex.htm";
  21. //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
  22. private static String APPID = Configs.GetValue("APPID");
  23. private static String APPKEY = Configs.GetValue("APPKEY");
  24. private static String MASTERSECRET = Configs.GetValue("MASTERSECRET");
  25. private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
  26. public class AppClientListDto
  27. {
  28. /// <summary>
  29. /// 应用分类(1android 2ios)
  30. /// </summary>
  31. public int apptype { get; set; }
  32. /// <summary>
  33. /// app端ClientId值
  34. /// </summary>
  35. public string clientid { get; set; }
  36. /// <summary>
  37. /// 用户code
  38. /// </summary>
  39. public string usercode { get; set; }
  40. }
  41. [Authority]
  42. public ActionResult PutAppClientIdAsync(string clientid, int apptype = 1)
  43. {
  44. LogFactory.GetLogger("clientid").Warn (clientid);
  45. var list = new List<AppClientListDto>();
  46. string usercode = "";
  47. if (User != null)
  48. {
  49. usercode = User.F_UserCode;
  50. }
  51. //获取redis缓存
  52. var clientidStr = RedisHelper.StringGet("clientidlist");
  53. if (clientidStr != null)
  54. list = clientidStr.ToString().ToList<AppClientListDto>();
  55. if (!string.IsNullOrEmpty(clientid))
  56. {
  57. var count = 0;
  58. #region 去重 - 保证只有一个坐席账号,且只往一个登录账号的手机上推送
  59. var lists = list.Where(x => x.usercode == usercode).ToList();
  60. if (lists != null) count = lists.Count();
  61. if (count > 0)
  62. {
  63. foreach (var item in lists)
  64. {
  65. list.Remove(item);
  66. }
  67. }
  68. var cilid = list.Where(x => x.clientid == clientid).ToList();
  69. if (cilid != null)
  70. {
  71. if (cilid.Count > 0)
  72. {
  73. foreach (var item in cilid)
  74. {
  75. list.Remove(item);
  76. }
  77. }
  78. }
  79. //存入redis缓存
  80. list.Add(new AppClientListDto
  81. {
  82. apptype = apptype,
  83. clientid = clientid,
  84. usercode = usercode
  85. });
  86. RedisHelper.StringSet("clientidlist", list.ToJson());
  87. #endregion
  88. return Success("保存ClientId成功");
  89. }
  90. return Success("没有获取到该设备clientid");
  91. }
  92. private static information.InternalMessagesController msg = new information.InternalMessagesController();
  93. public ActionResult Clild(string usercode)
  94. {
  95. var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
  96. if (userModel != null)
  97. {
  98. usercode = userModel.F_UserCode;
  99. }
  100. var clientidStr = RedisHelper.StringGet("clientidlist");
  101. var list = new List<AppClientListDto>();
  102. if (clientidStr != null)
  103. list = clientidStr.ToString().ToList<AppClientListDto>();
  104. var lists = list.Where(x => x.usercode == usercode).ToList();
  105. string clientid = "";
  106. if (lists != null)
  107. {
  108. foreach (var item in lists)
  109. {
  110. clientid = item.clientid;
  111. }
  112. }
  113. var obj = new
  114. {
  115. state = "success",
  116. message = "成功",
  117. pushResult = clientid
  118. };
  119. return Content(obj.ToJson()); ;
  120. }
  121. public static string PushApp(string Title, string count, string usercode,string workOrderId)
  122. {
  123. string pushResult = "";
  124. var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
  125. if (userModel != null)
  126. {
  127. usercode = userModel.F_UserCode;
  128. }
  129. var clientidStr = RedisHelper.StringGet("clientidlist");
  130. var list = new List<AppClientListDto>();
  131. if (clientidStr != null)
  132. list = clientidStr.ToString().ToList<AppClientListDto>();
  133. var lists = list.Where(x => x.usercode == usercode).ToList();
  134. string clientid = "";
  135. if (lists != null)
  136. {
  137. foreach (var item in lists)
  138. {
  139. clientid = item.clientid;
  140. }
  141. }
  142. if (!string.IsNullOrEmpty(clientid))
  143. {
  144. try
  145. {
  146. msg.AddInternalMessagesInfo(Title, count,
  147. usercode, "8000", (int)EnumSmsType.workorder, 0, workOrderId);
  148. var t = PushAppSend(Title, count, clientid);
  149. }
  150. catch(Exception e)
  151. {
  152. LogFactory.GetLogger("3333").Error(e.Message );
  153. }
  154. }
  155. return pushResult; ;
  156. }
  157. public ActionResult SmsOverTime()
  158. {
  159. var workorder = new BLL.T_Bus_WorkOrder().GetModelList("F_IsDelete=0 and F_WorkState in (2,4,8) and (F_Remind!=1 or F_Remind is null) " +
  160. "and DATEDIFF(HH, GETDATE(), F_LimitTime) < 24 and F_LimitTime > GETDATE()");
  161. if (workorder != null)
  162. {
  163. foreach (var it in workorder)
  164. {
  165. System.TimeSpan time = DateTime.Parse(it.F_LimitTime.ToString()) - DateTime.Now;
  166. if (time.TotalHours <= 24)
  167. {
  168. var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
  169. if (user != null)
  170. {
  171. foreach (var iv in user)
  172. {
  173. string count = " 您有工单24小时后超期,请及时处理,工单号" + it.F_WorkOrderId;
  174. PushApp("临期提醒", count, iv.F_UserCode, it.F_WorkOrderId);
  175. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  176. keyValuePairs.Add("F_Remind", "1");
  177. bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. var LimitTime = new BLL.T_Bus_WorkOrder().GetModelList("F_IsDelete=0 and F_WorkState in (2,4,8) and (F_Remind=1 or F_Remind is null) " +
  184. "AND F_LimitTime<getdate()");
  185. if (LimitTime != null)
  186. {
  187. foreach (var it in LimitTime)
  188. {
  189. var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
  190. if (user != null)
  191. {
  192. foreach (var iv in user)
  193. {
  194. string count = " 您有工单已超期,请及时处理,工单号" + it.F_WorkOrderId;
  195. PushApp("超期提醒", count, iv.F_UserCode, it.F_WorkOrderId);
  196. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  197. keyValuePairs.Add("F_Remind", "2");
  198. bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
  199. }
  200. }
  201. }
  202. }
  203. return Success("发送成功");
  204. }
  205. private static string appSendToken = "";
  206. /// <summary>
  207. /// 获取token
  208. /// </summary>
  209. /// <returns></returns>
  210. public static string GetAppSendToken()
  211. {
  212. string token = "";
  213. if (!string.IsNullOrWhiteSpace(appSendToken))
  214. {
  215. return appSendToken;
  216. }
  217. //签名 : appkey+timestamp+mastersecret SHA256加密
  218. string timestamp = GenerateTimeStamp();//时间戳
  219. string sign = APPKEY + timestamp + MASTERSECRET;
  220. sign = sha256(sign); //sha256加密
  221. var postdata = new
  222. {
  223. sign = sign,
  224. timestamp = timestamp,
  225. appkey = APPKEY
  226. };
  227. string data = JsonConvert.SerializeObject(postdata);
  228. WebClient wc = new WebClient();
  229. string url = $@"https://restapi.getui.com/v2/{APPID}/auth";
  230. wc.Encoding = Encoding.UTF8;
  231. wc.Headers.Add("Content-Type", "application/json");
  232. // wc2.Headers.Add("token", "STZN202204020911111");
  233. try
  234. {
  235. string result = wc.UploadString(url, data);
  236. //{"msg":"success","code":0,"data":{"expire_time":"1652341431131","token":"ef9b1b20798956299552a28ef549e859f2856eae17a2021c81f85e5e8244a40b"}}
  237. // string result = "{\"msg\":\"success\",\"code\":0,\"data\":{\"expire_time\":\"1652341431131\",\"token\":\"ef9b1b20798956299552a28ef549e859f2856eae17a2021c81f85e5e8244a40b\"}}";
  238. var detail = result.ToObject<AppResultModel>();
  239. appSendToken = detail.data.token;
  240. token = appSendToken;
  241. }
  242. catch (Exception ex)
  243. {
  244. throw;
  245. }
  246. return token;
  247. }
  248. public ActionResult Push ()
  249. {
  250. string t = "";
  251. try
  252. {
  253. t = PushAppSend("测试", "1111", "5625b2681ce04dd769f60c604c47711c");
  254. }
  255. catch(Exception e )
  256. {
  257. t = e.Message;
  258. }
  259. return Success(t);
  260. }
  261. /// <summary>
  262. /// 推送
  263. /// </summary>
  264. public static string PushAppSend(string title,string content, string cid)
  265. {
  266. string token = GetAppSendToken();
  267. string guid = Guid.NewGuid().ToString();
  268. #region 示例说明
  269. //https://restapi.getui.com/v2/kylXOUvnEd8Al3yT39ELR1/push/single/cid
  270. #endregion
  271. APPSendModel postDate = new APPSendModel();
  272. postDate.request_id = guid;
  273. postDate.settings.ttl = -1;
  274. postDate.audience.cid.Add(cid);//cid 就是手机的唯一ID,我现在要给这个手机推消息
  275. postDate.push_message.notification.title = title;
  276. postDate.push_message.notification.body = content;
  277. postDate.push_message.notification.click_type = "url";
  278. postDate.push_message.notification.url = "https//:xxx";
  279. string data = JsonConvert.SerializeObject(postDate);
  280. WebClient wc = new WebClient();
  281. string url = $@"https://restapi.getui.com/v2/{APPID}/push/single/cid";
  282. wc.Encoding = Encoding.UTF8;
  283. wc.Headers.Add("Content-Type", "application/json");
  284. wc.Headers.Add("token", token);
  285. try
  286. {
  287. string result = wc.UploadString(url, data);
  288. var detail = result.ToObject<AppResultModel>();
  289. return detail.ToJson();
  290. }
  291. catch (Exception ex)
  292. {
  293. return ex.Message;
  294. }
  295. }
  296. /// <summary>
  297. /// app发送model
  298. /// </summary>
  299. public class APPSendModel
  300. {
  301. public APPSendModel()
  302. {
  303. settings = new settings();
  304. audience = new audience();
  305. push_message = new push_message();
  306. }
  307. /// <summary>
  308. /// 请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
  309. /// </summary>
  310. public string request_id { get; set; }
  311. /// <summary>
  312. /// 推送条件设置,详细解释见下方settings说明
  313. /// </summary>
  314. public settings settings { get; set; }
  315. /// <summary>
  316. /// 个推推送消息参数,详细内容见push_message
  317. /// </summary>
  318. public audience audience { get; set; }
  319. /// <summary>
  320. /// 个推推送消息参数
  321. /// </summary>
  322. public push_message push_message { get; set; }
  323. }
  324. /// <summary>
  325. /// 推送条件设置
  326. /// </summary>
  327. public class settings
  328. {
  329. /// <summary>
  330. /// 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
  331. /// </summary>
  332. public int? ttl { get; set; }
  333. }
  334. /// <summary>
  335. /// 推送目标用户
  336. /// </summary>
  337. public class audience
  338. {
  339. /// <summary>
  340. /// 构造
  341. /// </summary>
  342. public audience()
  343. {
  344. cid = new List<string>();
  345. }
  346. /// <summary>
  347. /// 别名数组,只能填一个别名;绑定别名请参考
  348. /// </summary>
  349. public List<string> cid { get; set; }
  350. }
  351. /// <summary>
  352. /// 发送消息
  353. /// </summary>
  354. public class push_message
  355. {
  356. /// <summary>
  357. /// 构造
  358. /// </summary>
  359. public push_message()
  360. {
  361. notification = new notification();
  362. }
  363. /// <summary>
  364. /// 通知
  365. /// </summary>
  366. public notification notification { get; set; }
  367. }
  368. /// <summary>
  369. /// 通知
  370. /// </summary>
  371. public class notification
  372. {
  373. /// <summary>
  374. /// 通知标题
  375. /// </summary>
  376. public string title { get; set; }
  377. /// <summary>
  378. /// 通知内容
  379. /// </summary>
  380. public string body { get; set; }
  381. /// <summary>
  382. /// 写死 url就好
  383. /// </summary>
  384. public string click_type { get; set; }
  385. /// <summary>
  386. /// "https//:xxx
  387. /// </summary>
  388. public string url { get; set; }
  389. }
  390. /// <summary>
  391. /// APP 发送 model
  392. /// </summary>
  393. public class AppResultModel
  394. {
  395. /// <summary>
  396. ///
  397. /// </summary>
  398. public string msg { get; set; }
  399. /// <summary>
  400. /// 10001 就是过期 0 是成功
  401. /// </summary>
  402. public int? code { get; set; }
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. public AppResultModelDetail data { get; set; }
  407. }
  408. /// <summary>
  409. ///
  410. /// </summary>
  411. public class AppResultModelDetail
  412. {
  413. /// <summary>
  414. ///
  415. /// </summary>
  416. public string expire_time { get; set; }
  417. /// <summary>
  418. ///
  419. /// </summary>
  420. public string token { get; set; }
  421. }
  422. #region 私有成员
  423. /// <summary>
  424. /// 获取时间撮
  425. /// </summary>
  426. /// <returns></returns>
  427. private static string GenerateTimeStamp()
  428. {
  429. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  430. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  431. }
  432. /// <summary>
  433. /// 加密算法
  434. /// </summary>
  435. /// <param name="data"></param>
  436. /// <returns></returns>
  437. private static string sha256(string data)
  438. {
  439. byte[] bytes = Encoding.UTF8.GetBytes(data);
  440. byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
  441. StringBuilder builder = new StringBuilder();
  442. for (int i = 0; i < hash.Length; i++)
  443. {
  444. builder.Append(hash[i].ToString("X2"));
  445. }
  446. return builder.ToString();
  447. }
  448. #endregion
  449. }
  450. }