Bez popisu

PushMessageController.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. using CallCenter.Utility;
  2. using CallCenterApi.Interface.Controllers.Base;
  3. using com.igetui.api.openservice;
  4. using com.igetui.api.openservice.igetui;
  5. using com.igetui.api.openservice.igetui.template;
  6. using com.igetui.api.openservice.payload;
  7. using GetuiServerApiSDK.payload;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  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. private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
  20. //https的域名
  21. //private static String HOST = "https://api.getui.com/apiex.htm";
  22. //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
  23. private static String APPID = Configs.GetValue("APPID");
  24. private static String APPKEY = Configs.GetValue("APPKEY");
  25. private static String MASTERSECRET = Configs.GetValue("MASTERSECRET");
  26. private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
  27. public class AppClientListDto
  28. {
  29. /// <summary>
  30. /// 应用分类(1android 2ios)
  31. /// </summary>
  32. public int apptype { get; set; }
  33. /// <summary>
  34. /// app端ClientId值
  35. /// </summary>
  36. public string clientid { get; set; }
  37. /// <summary>
  38. /// 用户code
  39. /// </summary>
  40. public string usercode { get; set; }
  41. }
  42. [Authority]
  43. public ActionResult PutAppClientIdAsync(string clientid, int apptype = 1)
  44. {
  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. LogFactory.GetLogger("222").Error(list.ToJson());
  87. RedisHelper.StringSet("clientidlist", list.ToJson());
  88. #endregion
  89. return Success("保存ClientId成功");
  90. }
  91. return Success("没有获取到该设备clientid");
  92. }
  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)
  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. pushResult = PushMessageToList(Title, count, clientid);
  147. }
  148. catch(Exception e)
  149. {
  150. LogFactory.GetLogger("3333").Error(e.Message );
  151. }
  152. }
  153. return pushResult; ;
  154. }
  155. public ActionResult Pushc(string Title, string count, string usercode)
  156. {
  157. string pushResult = "";
  158. if (!string.IsNullOrEmpty(usercode))
  159. {
  160. try
  161. {
  162. pushResult = PushMessageToSingle(Title, count, usercode);
  163. }
  164. catch (Exception e)
  165. {
  166. LogFactory.GetLogger("3333").Error(e.Message);
  167. }
  168. }
  169. var obj = new
  170. {
  171. state = "success",
  172. message = "成功",
  173. pushResult = pushResult
  174. };
  175. return Content(obj.ToJson()); ;
  176. // return pushResult; ;
  177. }
  178. public ActionResult Pushios(string Title, string count, string usercode)
  179. {
  180. // int userId = CurrentUser.UserData.F_UserId;
  181. // Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
  182. string pushResult = "";
  183. if (!string.IsNullOrEmpty(usercode))
  184. {
  185. pushResult = pushResult = PushMessageToSingle(Title, count, usercode);
  186. }
  187. var obj = new
  188. {
  189. state = "success",
  190. message = "成功",
  191. pushResult = pushResult
  192. };
  193. return Content(obj.ToJson()); ;
  194. // return pushResult; ;
  195. }
  196. public string Push(string Title, string count, string usercode)
  197. {
  198. // int userId = CurrentUser.UserData.F_UserId;
  199. // Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
  200. var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
  201. if (userModel != null)
  202. {
  203. usercode = userModel.F_UserCode;
  204. }
  205. var clientidStr = RedisHelper.StringGet("clientidlist");
  206. var list = new List<AppClientListDto>();
  207. if (clientidStr != null)
  208. list = clientidStr.ToString().ToList<AppClientListDto>();
  209. var lists = list.Where(x => x.usercode == usercode).ToList();
  210. string clientid = "";
  211. int type = 1;
  212. if (lists != null)
  213. {
  214. foreach (var item in lists)
  215. {
  216. clientid = item.clientid;
  217. type = item.apptype;
  218. }
  219. }
  220. string pushResult = "";
  221. if (!string.IsNullOrEmpty(clientid))
  222. {
  223. if (type == 1)
  224. pushResult = PushMessageToList(Title, count, clientid);
  225. else
  226. pushResult = PushMessageToSingle(Title, count, clientid);
  227. }
  228. return pushResult; ;
  229. }
  230. public ActionResult SmsOverTime()
  231. {
  232. 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) " +
  233. "and DATEDIFF(HH, GETDATE(), F_LimitTime) < 24 and F_LimitTime > GETDATE()");
  234. if (workorder != null)
  235. {
  236. foreach (var it in workorder)
  237. {
  238. System.TimeSpan time = DateTime.Parse(it.F_LimitTime.ToString()) - DateTime.Now;
  239. if (time.TotalHours <= 24)
  240. {
  241. var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
  242. if (user != null)
  243. {
  244. foreach (var iv in user)
  245. {
  246. string count = " 您有工单24小时后超期,请及时处理,工单号" + it.F_WorkOrderId;
  247. PushApp("临期提醒", count, iv.F_UserCode);
  248. //if (!string.IsNullOrEmpty(iv.F_Mobile))
  249. //{
  250. // Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  251. // keyValuePairs.Add("F_Remind", "1");
  252. // bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
  253. //}
  254. }
  255. }
  256. }
  257. }
  258. }
  259. 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) " +
  260. "AND F_LimitTime<getdate()");
  261. if (LimitTime != null)
  262. {
  263. foreach (var it in LimitTime)
  264. {
  265. var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
  266. if (user != null)
  267. {
  268. foreach (var iv in user)
  269. {
  270. string count = " 您有工单已超期,请及时处理,工单号" + it.F_WorkOrderId;
  271. PushApp("超期提醒", count, iv.F_UserCode);
  272. //if (!string.IsNullOrEmpty(iv.F_Mobile))
  273. //{
  274. // Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  275. // keyValuePairs.Add("F_Remind", "2");
  276. // bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
  277. //}
  278. }
  279. }
  280. }
  281. }
  282. return Success("发送成功");
  283. }
  284. private static string PushMessageToList(string Title, string count, string CLIENTID)
  285. {
  286. // 推送主类(方式1,不可与方式2共存)
  287. IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
  288. // 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名
  289. // IGtPush push = new IGtPush("",APPKEY,MASTERSECRET);
  290. ListMessage message = new ListMessage();
  291. string state = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  292. string end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
  293. NotificationTemplate template = NotificationTemplateDemo(Title, count, state, end);
  294. // 用户当前不在线时,是否离线存储,可选
  295. message.IsOffline = true;
  296. // 离线有效时间,单位为毫秒,可选
  297. message.OfflineExpireTime = 1000 * 3600 * 12;
  298. message.Data = template;
  299. message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。
  300. //设置接收者
  301. List<com.igetui.api.openservice.igetui.Target> targetList = new List<com.igetui.api.openservice.igetui.Target>();
  302. com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target();
  303. target1.appId = APPID;
  304. target1.clientId = CLIENTID;
  305. // 如需要,可以设置多个接收者
  306. //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target();
  307. //target2.AppId = APPID;
  308. //target2.ClientId = "ddf730f6cabfa02ebabf06e0c7fc8da0";
  309. targetList.Add(target1);
  310. //targetList.Add(target2);
  311. string contentId = push.getContentId(message);
  312. string pushResult = push.pushMessageToList(contentId, targetList);
  313. return pushResult; ;
  314. }
  315. /// <summary>
  316. /// 透传模板动作内容
  317. /// </summary>
  318. /// <param name="title"></param>
  319. /// <param name="text"></param>
  320. /// <param name="transmissiontype"></param>
  321. /// <param name="transmissioncontent"></param>
  322. /// <returns></returns>
  323. public static TransmissionTemplate TransmissionTemplateDemo(string title, string transmissioncontent)
  324. {
  325. TransmissionTemplate template = new TransmissionTemplate();
  326. template.AppId = APPID;
  327. template.AppKey = APPKEY;
  328. //应用启动类型,1:强制应用启动 2:等待应用启动
  329. template.TransmissionType = 2;
  330. //透传内容
  331. template.TransmissionContent = transmissioncontent; //透传内容
  332. //APN高级推送
  333. APNPayload apnpayload = new APNPayload();
  334. DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
  335. alertMsg.Body = transmissioncontent;
  336. alertMsg.ActionLocKey = "ActionLocKey";
  337. alertMsg.LocKey = "LocKey";
  338. alertMsg.addLocArg("LocArg");
  339. alertMsg.LaunchImage = "LaunchImage";
  340. //iOS8.2支持字段
  341. alertMsg.Title = title;
  342. alertMsg.TitleLocKey = "TitleLocKey";
  343. alertMsg.addTitleLocArg("TitleLocArg");
  344. apnpayload.AlertMsg = alertMsg;
  345. apnpayload.Badge = 0;
  346. apnpayload.ContentAvailable = 1;
  347. apnpayload.addCustomMsg("payload", "payload");
  348. //多媒体
  349. MultiMedia multiMedia = new MultiMedia();
  350. multiMedia.rid = "xxx-1";
  351. multiMedia.url = "";
  352. multiMedia.setIsOnlyWifi(false);
  353. multiMedia.type = MultiMedia.MediaType.pic;
  354. List<MultiMedia> list = new List<MultiMedia>();
  355. list.Add(multiMedia);
  356. apnpayload.MultiMedias = list;
  357. template.setAPNInfo(apnpayload);
  358. return template;
  359. }
  360. private static string PushMessageToSingle(string Title, string count, string CLIENTID)
  361. {
  362. IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
  363. //消息模版:TransmissionTemplate:透传模板
  364. string state = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  365. string end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
  366. TransmissionTemplate template = TransmissionTemplateDemo(Title, count);
  367. // 单推消息模型
  368. SingleMessage message = new SingleMessage();
  369. message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选
  370. message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选
  371. message.Data = template;
  372. //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境
  373. //message.PushNetWorkType = 1;
  374. com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
  375. target.appId = APPID;
  376. target.clientId = CLIENTID;
  377. //target.alias = ALIAS;
  378. string pushResult = "";
  379. try
  380. {
  381. pushResult = push.pushMessageToSingle(message, target);
  382. }
  383. catch (RequestException e)
  384. {
  385. string requestId = e.RequestId;
  386. }
  387. return pushResult;
  388. }
  389. public static NotificationTemplate NotificationTemplateDemo(string Title, string count, string state, string endtime)
  390. {
  391. NotificationTemplate template = new NotificationTemplate();
  392. template.AppId = APPID;
  393. template.AppKey = APPKEY;
  394. //通知栏标题
  395. template.Title = Title;
  396. //通知栏内容
  397. template.Text = count;
  398. //通知栏显示本地图片
  399. template.Logo = "";
  400. //通知栏显示网络图标
  401. template.LogoURL = "";
  402. //应用启动类型,1:强制应用启动 2:等待应用启动
  403. template.TransmissionType = 1;
  404. //透传内容
  405. template.TransmissionContent = "";
  406. //接收到消息是否响铃,true:响铃 false:不响铃
  407. template.IsRing = true;
  408. //接收到消息是否震动,true:震动 false:不震动
  409. template.IsVibrate = true;
  410. //接收到消息是否可清除,true:可清除 false:不可清除
  411. template.IsClearable = true;
  412. //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
  413. String begin = state;
  414. String end = endtime;
  415. template.setDuration(begin, end);
  416. return template;
  417. }
  418. }
  419. }