| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- using CallCenter.Utility;
- using CallCenterApi.Interface.Controllers.Base;
- using com.igetui.api.openservice;
- using com.igetui.api.openservice.igetui;
- using com.igetui.api.openservice.igetui.template;
- using com.igetui.api.openservice.payload;
- using GetuiServerApiSDK.payload;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace CallCenterApi.Interface.Controllers
- {
- public class PushMessageController : BaseController
- {
- //参数设置 <-----参数需要重新设置----->
- //http的域名
- private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
- //https的域名
- //private static String HOST = "https://api.getui.com/apiex.htm";
- //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
- private static String APPID = Configs.GetValue("APPID");
- private static String APPKEY = Configs.GetValue("APPKEY");
- private static String MASTERSECRET = Configs.GetValue("MASTERSECRET");
- private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
- public class AppClientListDto
- {
- /// <summary>
- /// 应用分类(1android 2ios)
- /// </summary>
- public int apptype { get; set; }
- /// <summary>
- /// app端ClientId值
- /// </summary>
- public string clientid { get; set; }
- /// <summary>
- /// 用户code
- /// </summary>
- public string usercode { get; set; }
- }
-
-
- [Authority]
- public ActionResult PutAppClientIdAsync(string clientid, int apptype = 1)
- {
-
- var list = new List<AppClientListDto>();
- string usercode = "";
- if (User != null)
- {
- usercode = User.F_UserCode;
- }
- //获取redis缓存
- var clientidStr = RedisHelper.StringGet("clientidlist");
- if (clientidStr != null)
- list = clientidStr.ToString().ToList<AppClientListDto>();
- if (!string.IsNullOrEmpty(clientid))
- {
- var count = 0;
- #region 去重 - 保证只有一个坐席账号,且只往一个登录账号的手机上推送
- var lists = list.Where(x => x.usercode == usercode).ToList();
- if (lists != null) count = lists.Count();
- if (count > 0)
- {
- foreach (var item in lists)
- {
- list.Remove(item);
- }
- }
- var cilid = list.Where(x => x.clientid == clientid).ToList();
- if (cilid != null)
- {
- if (cilid.Count > 0)
- {
- foreach (var item in cilid)
- {
- list.Remove(item);
- }
- }
- }
- //存入redis缓存
- list.Add(new AppClientListDto
- {
- apptype = apptype,
- clientid = clientid,
- usercode = usercode
- });
- LogFactory.GetLogger("222").Error(list.ToJson());
- RedisHelper.StringSet("clientidlist", list.ToJson());
- #endregion
- return Success("保存ClientId成功");
- }
- return Success("没有获取到该设备clientid");
- }
- public ActionResult Clild(string usercode)
- {
- var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
- if (userModel != null)
- {
- usercode = userModel.F_UserCode;
- }
- var clientidStr = RedisHelper.StringGet("clientidlist");
- var list = new List<AppClientListDto>();
- if (clientidStr != null)
- list = clientidStr.ToString().ToList<AppClientListDto>();
- var lists = list.Where(x => x.usercode == usercode).ToList();
- string clientid = "";
- if (lists != null)
- {
- foreach (var item in lists)
- {
- clientid = item.clientid;
- }
- }
- var obj = new
- {
- state = "success",
- message = "成功",
- pushResult = clientid
- };
- return Content(obj.ToJson()); ;
- }
- public static string PushApp(string Title, string count, string usercode)
- {
- string pushResult = "";
- var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
- if (userModel != null)
- {
- usercode = userModel.F_UserCode;
- }
- var clientidStr = RedisHelper.StringGet("clientidlist");
- var list = new List<AppClientListDto>();
- if (clientidStr != null)
- list = clientidStr.ToString().ToList<AppClientListDto>();
- var lists = list.Where(x => x.usercode == usercode).ToList();
- string clientid = "";
- if (lists != null)
- {
- foreach (var item in lists)
- {
- clientid = item.clientid;
- }
- }
- if (!string.IsNullOrEmpty(clientid))
- {
- try
- {
- pushResult = PushMessageToList(Title, count, clientid);
- }
- catch(Exception e)
- {
- LogFactory.GetLogger("3333").Error(e.Message );
- }
- }
-
- return pushResult; ;
- }
- public ActionResult Pushc(string Title, string count, string usercode)
- {
- string pushResult = "";
- if (!string.IsNullOrEmpty(usercode))
- {
- try
- {
- pushResult = PushMessageToSingle(Title, count, usercode);
- }
- catch (Exception e)
- {
- LogFactory.GetLogger("3333").Error(e.Message);
- }
- }
- var obj = new
- {
- state = "success",
- message = "成功",
- pushResult = pushResult
- };
- return Content(obj.ToJson()); ;
- // return pushResult; ;
- }
- public ActionResult Pushios(string Title, string count, string usercode)
- {
- // int userId = CurrentUser.UserData.F_UserId;
- // Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
- string pushResult = "";
- if (!string.IsNullOrEmpty(usercode))
- {
- pushResult = pushResult = PushMessageToSingle(Title, count, usercode);
- }
- var obj = new
- {
- state = "success",
- message = "成功",
- pushResult = pushResult
- };
- return Content(obj.ToJson()); ;
- // return pushResult; ;
- }
- public string Push(string Title, string count, string usercode)
- {
- // int userId = CurrentUser.UserData.F_UserId;
- // Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
- var userModel = new BLL.T_Sys_UserAccount().GetModel(usercode);
- if (userModel != null)
- {
- usercode = userModel.F_UserCode;
- }
- var clientidStr = RedisHelper.StringGet("clientidlist");
- var list = new List<AppClientListDto>();
- if (clientidStr != null)
- list = clientidStr.ToString().ToList<AppClientListDto>();
- var lists = list.Where(x => x.usercode == usercode).ToList();
- string clientid = "";
- int type = 1;
- if (lists != null)
- {
- foreach (var item in lists)
- {
- clientid = item.clientid;
- type = item.apptype;
- }
- }
- string pushResult = "";
- if (!string.IsNullOrEmpty(clientid))
- {
- if (type == 1)
- pushResult = PushMessageToList(Title, count, clientid);
- else
- pushResult = PushMessageToSingle(Title, count, clientid);
- }
- return pushResult; ;
- }
- public ActionResult SmsOverTime()
- {
- 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) " +
- "and DATEDIFF(HH, GETDATE(), F_LimitTime) < 24 and F_LimitTime > GETDATE()");
- if (workorder != null)
- {
- foreach (var it in workorder)
- {
- System.TimeSpan time = DateTime.Parse(it.F_LimitTime.ToString()) - DateTime.Now;
- if (time.TotalHours <= 24)
- {
- var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
- if (user != null)
- {
- foreach (var iv in user)
- {
- string count = " 您有工单24小时后超期,请及时处理,工单号" + it.F_WorkOrderId;
- PushApp("临期提醒", count, iv.F_UserCode);
- //if (!string.IsNullOrEmpty(iv.F_Mobile))
- //{
-
- // Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
- // keyValuePairs.Add("F_Remind", "1");
- // bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
-
- //}
- }
- }
- }
- }
- }
- 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) " +
- "AND F_LimitTime<getdate()");
- if (LimitTime != null)
- {
- foreach (var it in LimitTime)
- {
- var user = new BLL.T_Sys_UserAccount().GetModelList(" F_DeptId=" + it.F_MainDeptId + "and F_DeleteFlag=0");
- if (user != null)
- {
- foreach (var iv in user)
- {
- string count = " 您有工单已超期,请及时处理,工单号" + it.F_WorkOrderId;
- PushApp("超期提醒", count, iv.F_UserCode);
- //if (!string.IsNullOrEmpty(iv.F_Mobile))
- //{
-
- // Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
- // keyValuePairs.Add("F_Remind", "2");
- // bool t = new BLL.T_Bus_WorkOrder().UpdateWorkOrder(it.F_Id, keyValuePairs);
-
- //}
- }
- }
- }
- }
- return Success("发送成功");
- }
- private static string PushMessageToList(string Title, string count, string CLIENTID)
- {
- // 推送主类(方式1,不可与方式2共存)
- IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
- // 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名
- // IGtPush push = new IGtPush("",APPKEY,MASTERSECRET);
- ListMessage message = new ListMessage();
- string state = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- string end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
- NotificationTemplate template = NotificationTemplateDemo(Title, count, state, end);
- // 用户当前不在线时,是否离线存储,可选
- message.IsOffline = true;
- // 离线有效时间,单位为毫秒,可选
- message.OfflineExpireTime = 1000 * 3600 * 12;
- message.Data = template;
- message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。
- //设置接收者
- List<com.igetui.api.openservice.igetui.Target> targetList = new List<com.igetui.api.openservice.igetui.Target>();
- com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target();
- target1.appId = APPID;
- target1.clientId = CLIENTID;
- // 如需要,可以设置多个接收者
- //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target();
- //target2.AppId = APPID;
- //target2.ClientId = "ddf730f6cabfa02ebabf06e0c7fc8da0";
- targetList.Add(target1);
- //targetList.Add(target2);
- string contentId = push.getContentId(message);
- string pushResult = push.pushMessageToList(contentId, targetList);
- return pushResult; ;
- }
- /// <summary>
- /// 透传模板动作内容
- /// </summary>
- /// <param name="title"></param>
- /// <param name="text"></param>
- /// <param name="transmissiontype"></param>
- /// <param name="transmissioncontent"></param>
- /// <returns></returns>
- public static TransmissionTemplate TransmissionTemplateDemo(string title, string transmissioncontent)
- {
- TransmissionTemplate template = new TransmissionTemplate();
- template.AppId = APPID;
- template.AppKey = APPKEY;
- //应用启动类型,1:强制应用启动 2:等待应用启动
- template.TransmissionType = 2;
- //透传内容
- template.TransmissionContent = transmissioncontent; //透传内容
- //APN高级推送
- APNPayload apnpayload = new APNPayload();
- DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
- alertMsg.Body = transmissioncontent;
- alertMsg.ActionLocKey = "ActionLocKey";
- alertMsg.LocKey = "LocKey";
- alertMsg.addLocArg("LocArg");
- alertMsg.LaunchImage = "LaunchImage";
- //iOS8.2支持字段
- alertMsg.Title = title;
- alertMsg.TitleLocKey = "TitleLocKey";
- alertMsg.addTitleLocArg("TitleLocArg");
- apnpayload.AlertMsg = alertMsg;
- apnpayload.Badge = 0;
- apnpayload.ContentAvailable = 1;
- apnpayload.addCustomMsg("payload", "payload");
- //多媒体
- MultiMedia multiMedia = new MultiMedia();
- multiMedia.rid = "xxx-1";
- multiMedia.url = "";
- multiMedia.setIsOnlyWifi(false);
- multiMedia.type = MultiMedia.MediaType.pic;
- List<MultiMedia> list = new List<MultiMedia>();
- list.Add(multiMedia);
- apnpayload.MultiMedias = list;
- template.setAPNInfo(apnpayload);
- return template;
- }
- private static string PushMessageToSingle(string Title, string count, string CLIENTID)
- {
- IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
- //消息模版:TransmissionTemplate:透传模板
- string state = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- string end = DateTime.Now.AddMinutes(7).ToString("yyyy-MM-dd HH:mm:ss");
- TransmissionTemplate template = TransmissionTemplateDemo(Title, count);
- // 单推消息模型
- SingleMessage message = new SingleMessage();
- message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选
- message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选
- message.Data = template;
- //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境
- //message.PushNetWorkType = 1;
- com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target();
- target.appId = APPID;
- target.clientId = CLIENTID;
- //target.alias = ALIAS;
- string pushResult = "";
- try
- {
- pushResult = push.pushMessageToSingle(message, target);
- }
- catch (RequestException e)
- {
- string requestId = e.RequestId;
- }
- return pushResult;
- }
- public static NotificationTemplate NotificationTemplateDemo(string Title, string count, string state, string endtime)
- {
- NotificationTemplate template = new NotificationTemplate();
- template.AppId = APPID;
- template.AppKey = APPKEY;
- //通知栏标题
- template.Title = Title;
- //通知栏内容
- template.Text = count;
- //通知栏显示本地图片
- template.Logo = "";
- //通知栏显示网络图标
- template.LogoURL = "";
- //应用启动类型,1:强制应用启动 2:等待应用启动
- template.TransmissionType = 1;
- //透传内容
- template.TransmissionContent = "";
- //接收到消息是否响铃,true:响铃 false:不响铃
- template.IsRing = true;
- //接收到消息是否震动,true:震动 false:不震动
- template.IsVibrate = true;
- //接收到消息是否可清除,true:可清除 false:不可清除
- template.IsClearable = true;
- //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
- String begin = state;
- String end = endtime;
- template.setDuration(begin, end);
- return template;
- }
-
-
-
- }
- }
|