| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- using CallCenter.Utility;
- using CallCenterApi.Interface.Controllers.Base;
- using CallCenterApi.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- namespace CallCenterApi.Interface.Controllers
- {
- public class PushMessageController : BaseController
- {
- //参数设置 <-----参数需要重新设置----->
- //http的域名
-
- //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)
- {
- LogFactory.GetLogger("clientid").Warn (clientid);
- 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
- });
-
- RedisHelper.StringSet("clientidlist", list.ToJson());
- #endregion
- return Success("保存ClientId成功");
- }
- return Success("没有获取到该设备clientid");
- }
- private static information.InternalMessagesController msg = new information.InternalMessagesController();
- 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 workOrderId)
- {
- 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
- {
- msg.AddInternalMessagesInfo(Title, count,
- usercode, "8000", (int)EnumSmsType.workorder, 0, workOrderId);
- var t = PushAppSend(Title, count, clientid);
- }
- catch(Exception e)
- {
- LogFactory.GetLogger("3333").Error(e.Message );
- }
- }
-
- 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, it.F_WorkOrderId);
- 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, it.F_WorkOrderId);
- 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 appSendToken = "";
- /// <summary>
- /// 获取token
- /// </summary>
- /// <returns></returns>
- public static string GetAppSendToken()
- {
- string token = "";
- if (!string.IsNullOrWhiteSpace(appSendToken))
- {
- return appSendToken;
- }
- //签名 : appkey+timestamp+mastersecret SHA256加密
- string timestamp = GenerateTimeStamp();//时间戳
- string sign = APPKEY + timestamp + MASTERSECRET;
- sign = sha256(sign); //sha256加密
- var postdata = new
- {
- sign = sign,
- timestamp = timestamp,
- appkey = APPKEY
- };
- string data = JsonConvert.SerializeObject(postdata);
- WebClient wc = new WebClient();
- string url = $@"https://restapi.getui.com/v2/{APPID}/auth";
- wc.Encoding = Encoding.UTF8;
- wc.Headers.Add("Content-Type", "application/json");
- // wc2.Headers.Add("token", "STZN202204020911111");
- try
- {
- string result = wc.UploadString(url, data);
- //{"msg":"success","code":0,"data":{"expire_time":"1652341431131","token":"ef9b1b20798956299552a28ef549e859f2856eae17a2021c81f85e5e8244a40b"}}
- // string result = "{\"msg\":\"success\",\"code\":0,\"data\":{\"expire_time\":\"1652341431131\",\"token\":\"ef9b1b20798956299552a28ef549e859f2856eae17a2021c81f85e5e8244a40b\"}}";
- var detail = result.ToObject<AppResultModel>();
- appSendToken = detail.data.token;
- token = appSendToken;
- }
- catch (Exception ex)
- {
- throw;
- }
- return token;
- }
- public ActionResult Push ()
- {
- string t = "";
- try
- {
- t = PushAppSend("测试", "1111", "5625b2681ce04dd769f60c604c47711c");
- }
- catch(Exception e )
- {
- t = e.Message;
- }
- return Success(t);
- }
- /// <summary>
- /// 推送
- /// </summary>
- public static string PushAppSend(string title,string content, string cid)
- {
- string token = GetAppSendToken();
- string guid = Guid.NewGuid().ToString();
- #region 示例说明
- //https://restapi.getui.com/v2/kylXOUvnEd8Al3yT39ELR1/push/single/cid
- #endregion
- APPSendModel postDate = new APPSendModel();
- postDate.request_id = guid;
- postDate.settings.ttl = -1;
- postDate.audience.cid.Add(cid);//cid 就是手机的唯一ID,我现在要给这个手机推消息
- postDate.push_message.notification.title = title;
- postDate.push_message.notification.body = content;
- postDate.push_message.notification.click_type = "url";
- postDate.push_message.notification.url = "https//:xxx";
- string data = JsonConvert.SerializeObject(postDate);
- WebClient wc = new WebClient();
- string url = $@"https://restapi.getui.com/v2/{APPID}/push/single/cid";
- wc.Encoding = Encoding.UTF8;
- wc.Headers.Add("Content-Type", "application/json");
- wc.Headers.Add("token", token);
- try
- {
- string result = wc.UploadString(url, data);
- var detail = result.ToObject<AppResultModel>();
- return detail.ToJson();
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- /// <summary>
- /// app发送model
- /// </summary>
- public class APPSendModel
- {
- public APPSendModel()
- {
- settings = new settings();
- audience = new audience();
- push_message = new push_message();
- }
- /// <summary>
- /// 请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
- /// </summary>
- public string request_id { get; set; }
- /// <summary>
- /// 推送条件设置,详细解释见下方settings说明
- /// </summary>
- public settings settings { get; set; }
- /// <summary>
- /// 个推推送消息参数,详细内容见push_message
- /// </summary>
- public audience audience { get; set; }
- /// <summary>
- /// 个推推送消息参数
- /// </summary>
- public push_message push_message { get; set; }
- }
- /// <summary>
- /// 推送条件设置
- /// </summary>
- public class settings
- {
- /// <summary>
- /// 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
- /// </summary>
- public int? ttl { get; set; }
- }
- /// <summary>
- /// 推送目标用户
- /// </summary>
- public class audience
- {
- /// <summary>
- /// 构造
- /// </summary>
- public audience()
- {
- cid = new List<string>();
- }
- /// <summary>
- /// 别名数组,只能填一个别名;绑定别名请参考
- /// </summary>
- public List<string> cid { get; set; }
- }
- /// <summary>
- /// 发送消息
- /// </summary>
- public class push_message
- {
- /// <summary>
- /// 构造
- /// </summary>
- public push_message()
- {
- notification = new notification();
- }
- /// <summary>
- /// 通知
- /// </summary>
- public notification notification { get; set; }
- }
- /// <summary>
- /// 通知
- /// </summary>
- public class notification
- {
- /// <summary>
- /// 通知标题
- /// </summary>
- public string title { get; set; }
- /// <summary>
- /// 通知内容
- /// </summary>
- public string body { get; set; }
- /// <summary>
- /// 写死 url就好
- /// </summary>
- public string click_type { get; set; }
- /// <summary>
- /// "https//:xxx
- /// </summary>
- public string url { get; set; }
- }
- /// <summary>
- /// APP 发送 model
- /// </summary>
- public class AppResultModel
- {
- /// <summary>
- ///
- /// </summary>
- public string msg { get; set; }
- /// <summary>
- /// 10001 就是过期 0 是成功
- /// </summary>
- public int? code { get; set; }
- /// <summary>
- ///
- /// </summary>
- public AppResultModelDetail data { get; set; }
- }
- /// <summary>
- ///
- /// </summary>
- public class AppResultModelDetail
- {
- /// <summary>
- ///
- /// </summary>
- public string expire_time { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string token { get; set; }
- }
- #region 私有成员
- /// <summary>
- /// 获取时间撮
- /// </summary>
- /// <returns></returns>
- private static string GenerateTimeStamp()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(ts.TotalMilliseconds).ToString();
- }
- /// <summary>
- /// 加密算法
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- private static string sha256(string data)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(data);
- byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- builder.Append(hash[i].ToString("X2"));
- }
- return builder.ToString();
- }
- #endregion
-
-
- }
- }
|