using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LYFZ.WanYuKeFu
{
public partial class BackgroundProcess : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string type = "";
if (Request.QueryString["type"] != null)
{
type=Request.QueryString["type"].ToLower().Trim();
}
switch(type)
{
case "getusername":
#region 获取登录用名
if (LYFZ.Command.Command_Session.GetObject("User") != null)
{
LYFZ.WanYuKeFuData.Model.User retUser = (LYFZ.WanYuKeFuData.Model.User)LYFZ.Command.Command_Session.GetObject("User");
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, retUser.UserName)));
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败,"未登录")));
}
#endregion
break;
case "getuserinfo":
#region 获取登录用户信息
if (LYFZ.Command.Command_Session.GetObject("User") != null)
{
LYFZ.WanYuKeFuData.Model.User retUser = (LYFZ.WanYuKeFuData.Model.User)LYFZ.Command.Command_Session.GetObject("User");
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, JsonConvert.SerializeObject(retUser))));
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "未登录")));
}
#endregion
break;
case "updatepwd":
#region 修改当前登录用户密码
if (Request.QueryString["newpwd"] != null)
{
string newpwd = Request.QueryString["newpwd"];
if (LYFZ.Command.Command_Session.GetObject("User") != null)
{
LYFZ.WanYuKeFuData.Model.User retUser = (LYFZ.WanYuKeFuData.Model.User)LYFZ.Command.Command_Session.GetObject("User");
retUser.Password = LYFZ.WinAPI.SDKSecurity.MD5Encrypt(newpwd);
LYFZ.WanYuKeFuData.DAL.User userDal = new LYFZ.WanYuKeFuData.DAL.User(Global.DbConn);
if (userDal.Update(retUser))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "修改成功")));
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "更新数据库时失败")));
}
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "未登录")));
}
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.参数错误, "新密码不能为空")));
}
#endregion
break;
case "pushuserlist":
PushUserList();
break;
case "pushmsglist":
PushMsgList();
break;
case "delpushuser":
DelPushUser();
break;
case "delpusmsg":
DelPusMsg();
break;
case "getpushuser":
GetPushUserList();
break;
case "getpushqiyue":
GetPushQiYueList();
break;
case "sendpushmsg":
SendPushMsg();
break;
case "retransmission":
Retransmission();
break;
case "registerpushuser":
RegisterPushUser();
break;
case "badgeclear":
BadgeClear();
break;
case "read":
SetMsgRead();
break;
case "usermsglist":
GetSpecifiedUserMsgList();
break;
case "unreadcount":
GetUnreadCount();
break;
case "wanyukfurl":
WanYuKFUrl();
break;
default:
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.参数错误, "参数错误")));
break;
}
}
}
LYFZ.WanYuKeFuData.DAL.MsgReadStatus msgReadDal = new LYFZ.WanYuKeFuData.DAL.MsgReadStatus(Global.DbConn);
///
/// 获取网鱼客服系统url
///
void WanYuKFUrl()
{
try
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, LYFZ.WanYuKeFu.ErpGetuiApiSDK.WanYuKFUrl)));
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, String.Format("错误:{0}", ex.Message))));
}
}
///
/// 设置消息的已读状态
///
void SetMsgRead()
{
try
{
long msgID = Convert.ToInt64(GetParameterValue("msgid"));
string userID = GetParameterValue("userid").Trim();
if (!msgReadDal.CheckMsgReadExists(msgID, userID))
{
LYFZ.WanYuKeFuData.Model.MsgReadStatus msgReadModel = new WanYuKeFuData.Model.MsgReadStatus();
msgReadModel.MsgID = msgID;
msgReadModel.UserID = userID.Trim();
msgReadModel.ReadStatus = 1;
msgReadModel.ReadTime = DateTime.Now.ToJavaScriptTimeStamp();
if (msgReadDal.Add(msgReadModel))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "OK")));
}
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "OK")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, String.Format("错误:{0}", ex.Message))));
}
}
///
/// 清除badge
///
void BadgeClear()
{
try
{
string cid = GetParameterValue("cid");
int badge = GetParameterIntValue("badge");
msgPushUserDal.BadgeClear(cid, badge);
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, String.Format("已成功设置为 {0}", badge))));
}
catch (Exception ex){
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.参数错误,String.Format("错误:{0}",ex.Message))));
}
}
string GetParameterValue(string key)
{
if (Request[key] != null)
{
return Request[key];
}
else {
return "";
}
}
int GetParameterIntValue(string key)
{
string intValue = GetParameterValue(key);
try
{
if (!String.IsNullOrEmpty(intValue))
{
return Convert.ToInt32(intValue);
}
else
{
return 0;
}
}
catch {
return 0;
}
}
LYFZ.WanYuKeFuData.DAL.GTMessagePushUser msgPushUserDal = new LYFZ.WanYuKeFuData.DAL.GTMessagePushUser(Global.DbConn);
///
/// 注册推送用户设备
///
void RegisterPushUser()
{
try
{
int userType = GetParameterIntValue("uType");
string userID = "0";
string userName = "0";
int appState = 1;
string tempAppSatae = GetParameterValue("appState");
if (!String.IsNullOrEmpty(tempAppSatae))
{
if (tempAppSatae.Trim() == "0")
{
appState = 0;
}
else
{
appState = 1;
}
}
string GTClientID = GetParameterValue("CID");
string LYFZAPPID = GetParameterValue("LYFZAPPID");
if (String.IsNullOrWhiteSpace(LYFZAPPID))
{
LYFZAPPID = "wangyuapp";
}
string AppVersion = GetParameterValue("AppVersion");
if (String.IsNullOrWhiteSpace(AppVersion))
{
AppVersion = "1.0";
}
if (!String.IsNullOrEmpty(GTClientID))
{
LYFZ.WanYuKeFuData.Model.GTMessagePushUser pushUser = msgPushUserDal.GetModel("GTClientID", GTClientID);
pushUser.GTType = userType;
pushUser.GTState = 1;
pushUser.Badge = 0;
pushUser.APPState = appState;
pushUser.UserID = userID;
pushUser.UserName = userName;
pushUser.GTClientID = GTClientID;
pushUser.UpdateTime = DateTime.Now.ToJavaScriptTimeStamp();
pushUser.AppVersion = AppVersion;
pushUser.LYFZAPPID = LYFZAPPID;
bool registerState = false;
if (pushUser.ID > 0)
{
registerState = msgPushUserDal.Update(pushUser);
}
else
{
pushUser.CreationTime = DateTime.Now.ToJavaScriptTimeStamp();
registerState = msgPushUserDal.Add(pushUser);
}
if (registerState)
{
msgPushDal.DelMsgHistoryRecord(DateTime.Now.AddDays(-90).ToJavaScriptTimeStamp());
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "设备注册成功")));
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "设备注册失败,更新数据库时失败")));
}
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.参数错误, "设备注册失败,参数错误")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "设备注册时出错:" + ex.Message)));
}
}
///
/// 重发
///
void Retransmission()
{
try
{
string msgid = GetParameterValue("msgid");
LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord = msgPushDal.GetModel(Convert.ToInt64(msgid));
if (msgRecord != null && msgRecord.ID > 0)
{
msgRecord.SendTime = DateTime.Now.ToJavaScriptTimeStamp();
string retString = ErpGetuiApiSDK.pushMessageToSingleBatchApp(msgRecord, msgPushUserDal);
if (retString.ToLower().Contains("result\":\"ok\","))
{
msgRecord.State = 1;
}
else
{
msgRecord.State = 0;
}
if (msgRecord.State == 1)
{
if (msgPushDal.Update(msgRecord))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "重发成功", new { msgid = msgRecord.ID.ToString() })));
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "更新数据库时失败")));
}
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "向客户端推送时失败")));
}
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "没有找到指定ID 的数据")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "重推时出错:"+ex.Message)));
}
}
LYFZ.WanYuKeFuData.DAL.GTMsgRecord msgPushDal = new LYFZ.WanYuKeFuData.DAL.GTMsgRecord(Global.DbConn);
///
/// 推送消息
///
void SendPushMsg()
{
try
{
string title = GetParameterValue("title");
string description = GetParameterValue("description");
string msgData = GetParameterValue("msgData");//.Replace("$_$", "&");
//目标用户 0 为全部用户 1 为特定用户
int TargetUser = GetParameterIntValue("TargetUser");
string targetCidList = GetParameterValue("targetList").Trim(',');
string targetList = String.Format(",{0},", targetCidList);
string Platform = GetParameterValue("Platform").Trim(',');
if (String.IsNullOrEmpty(Platform))
{
Platform = "IOS,Android";
}
if (String.IsNullOrEmpty(title))
{
title = "APP消息提醒";
}
if (targetList.Split(',').Length <= 100)
{
if (!String.IsNullOrEmpty(description) && !String.IsNullOrEmpty(msgData) && (TargetUser == 0 || (TargetUser > 0 && !String.IsNullOrEmpty(targetList))))
{
LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord = new WanYuKeFuData.Model.GTMsgRecord();
msgRecord.ID = LYFZ.WinAPI.CustomPublicMethod.GenerateId();
msgRecord.Title = title;
msgRecord.Description = description;
msgRecord.MsgData = msgData;
msgRecord.Platform = Platform;
msgRecord.TargetUser = TargetUser;
msgRecord.TargetList = targetList;
msgRecord.SendTime = DateTime.Now.ToJavaScriptTimeStamp();
string retString = ErpGetuiApiSDK.pushMessageToSingleBatchApp(msgRecord, msgPushUserDal);
if (retString.ToLower().Contains("result\":\"ok\","))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "消息推送成功")));
}
else if (retString.ToLower().Contains("result\":\"-900000\","))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.没有找到目标用户注册的APP设备, "消息推送失败,指定的用户还有没使用过APP")));
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.推送失败, retString)));
}
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.参数错误, "消息推送失败,参数错误")));
}
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.推送失败, "每次推送用户数不能超过 100 个用户")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "消息推送时出错:" + ex.Message)));
}
}
void GetPushUserList()
{
string filedOrder = "CreationTime desc";
if (String.IsNullOrEmpty(filedOrder))
{
filedOrder = "id desc";
}
string whereString = "1=1";
int currentPage = 1;
int pageSize = 50;
int _TotalCount = 0;//总记录数
if (Request.QueryString["page"] != null)
{
currentPage = Convert.ToInt32(Request.QueryString["page"]);
}
if (Request.QueryString["pageSize"] != null)
{
pageSize = Convert.ToInt32(Request.QueryString["pageSize"]);
}
if (Request.QueryString["keywords"] != null && Request.QueryString["keywords"].ToString().Trim().Length > 0)
{
whereString = String.Format("GTClientID like '%{0}%'", Request.QueryString["keywords"].ToString().Trim());
}
//if (Request.QueryString["gttype"] != null && Request.QueryString["gttype"].ToString().Trim().Length>0)
//{
// whereString += " and [GTType]=" + Request.QueryString["gttype"].ToString();
//}
System.Data.DataTable tb = msgPushUserDal.PagingQueryDataTable("tb_GTMessagePushUser", "[GTType],[GTClientID],[GTState],[CreationTime] ,[UpdateTime]", filedOrder, whereString, pageSize, currentPage, out _TotalCount).Tables[0];
dynamic data = new
{
TotalCount = _TotalCount,
ListData = tb//JsonConvert.SerializeObject(tb)
};
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, data)));
//返回参数
}
void GetPushQiYueList()
{
string filedOrder = "EnterpriseID desc";
string whereString = "1=1";
int currentPage = 1;
int pageSize = 50;
int _TotalCount = 0;//总记录数
if (Request.QueryString["page"] != null)
{
currentPage = Convert.ToInt32(Request.QueryString["page"]);
}
if (Request.QueryString["pageSize"] != null)
{
pageSize = Convert.ToInt32(Request.QueryString["pageSize"]);
}
if (Request.QueryString["keywords"] != null && Request.QueryString["keywords"].ToString().Trim().Length > 0)
{
whereString = String.Format("EnterpriseID like '%{0}%' or EnterpriseName like '%{0}%'", Request.QueryString["keywords"].ToString().Trim());
}
System.Data.DataTable tb = msgPushUserDal.PagingQueryDataTable("view_GTEnterpriseIDList", "[EnterpriseID],[EnterpriseName]", filedOrder, whereString, pageSize, currentPage, out _TotalCount).Tables[0];
dynamic data = new
{
TotalCount = _TotalCount,
ListData = tb//JsonConvert.SerializeObject(tb)
};
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, data)));
//返回参数
}
void DelPushUser()
{
try
{
string delIDList = "";
if (Request.QueryString["delID"] != null)
{
delIDList = Request.QueryString["delID"].Trim(',');
}
if (msgPushUserDal.Delete(String.Format("id in(" + delIDList + ")")))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功,"删除成功")));
}
else {
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "删除失败")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "删除时出错:" + ex.Message)));
}
}
void DelPusMsg() {
try
{
string delIDList = "";
if (Request["delID"] != null)
{
delIDList = Request["delID"].Trim(',');
}
if (msgPushDal.Delete(String.Format("id in(" + delIDList + ")")))
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, "删除成功")));
}
else
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "删除失败")));
}
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "删除时出错:" + ex.Message)));
}
}
///
/// 获取推送用户集合 分页
///
void PushUserList()
{
try
{
string filedOrder = "CreationTime desc";
if (String.IsNullOrEmpty(filedOrder))
{
filedOrder = "id desc";
}
string whereString = "1=1";
int currentPage = 1;
int pageSize = 50;
int _TotalCount = 0;//总记录数
if (Request.QueryString["page"] != null)
{
currentPage = Convert.ToInt32(Request.QueryString["page"]);
}
if (Request.QueryString["pageSize"] != null)
{
pageSize = Convert.ToInt32(Request.QueryString["pageSize"]);
}
string keywords = Request.QueryString["keywords"];
if (!String.IsNullOrWhiteSpace(keywords))
{
whereString = String.Format("(GTClientID like '%{0}%' or LYFZAPPID='{0}' or AppVersion='{0}')", keywords);
}
System.Data.DataTable tb = msgPushUserDal.PagingQueryDataTable(msgPushUserDal.TableName, msgPushUserDal.TableFieldNameStr + ", CONVERT(nvarchar(20),[ID]) as sid", filedOrder, whereString, pageSize, currentPage, out _TotalCount).Tables[0];
dynamic data = new {
TotalCount = _TotalCount,
ListData = tb//JsonConvert.SerializeObject(tb)
};
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, data)));
//返回参数
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "查询数据时出错:"+ex.Message)));
}
}
///
/// 获取推送消息记录 分页
///
void PushMsgList()
{
try
{
string filedOrder = "sendTime desc";
if (String.IsNullOrEmpty(filedOrder))
{
filedOrder = "id desc";
}
string whereString = "1=1";
int currentPage = 1;
int pageSize = 50;
int _TotalCount = 0;//总记录数
if (Request.QueryString["page"] != null)
{
currentPage = Convert.ToInt32(Request.QueryString["page"]);
}
if (Request.QueryString["pageSize"] != null)
{
pageSize = Convert.ToInt32(Request.QueryString["pageSize"]);
}
if (Request.QueryString["keywords"] != null && Request.QueryString["keywords"].ToString().Trim().Length > 0)
{
whereString = String.Format("title like '%{0}%' or EnterpriseName like '%{0}%' or EnterpriseID like '%{0}%' or Platform like '%{0}%' ", Request.QueryString["keywords"].ToString().Trim());
}
System.Data.DataTable tb = msgPushDal.PagingQueryDataTable("view_GTMsgRecord", "CONVERT(nvarchar(20),[ID]) as ID,title,[description],msgData,TargetUser,targetList,[state],EnterpriseID,[Platform],sendTime,EnterpriseName ", filedOrder, whereString, pageSize, currentPage, out _TotalCount).Tables[0];
dynamic data = new
{
TotalCount = _TotalCount,
ListData = tb//JsonConvert.SerializeObject(tb)
};
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求成功, data)));
//返回参数
}
catch (Exception ex)
{
Response.Write(JsonConvert.SerializeObject(new AjaxResult(EnumCode.AjaxResultCode.请求失败, "查询数据时出错:" + ex.Message)));
}
}
///
/// 获取指定用户收到的推送消息列表 最新的前 100 条
///
void GetSpecifiedUserMsgList()
{
try
{
string filedOrder = "sendTime desc";
if (String.IsNullOrEmpty(filedOrder))
{
filedOrder = "id desc";
}
string EnterpriseID = "";
string whereString = "1=1";
string userID = "";
int currentPage = 1;
int pageSize = 100;
int _TotalCount = 0;//总记录数
string Platform="IOS";
if (Request["page"] != null)
{
currentPage = Convert.ToInt32(Request["page"]);
}
if (Request["EnterpriseID"] != null)
{
EnterpriseID = Request["EnterpriseID"];
}
if (Request["UID"] != null && Request["UID"].ToString().Trim().Length > 0)
{
userID = Request["UID"].ToString().Trim();
}
if(Request["Platform"] != null)
{
Platform=Request["Platform"].ToString().Trim();
}
if (Platform != "IOS")
{
Platform = "Android";
}
if (!String.IsNullOrEmpty(EnterpriseID) && !String.IsNullOrEmpty(userID))
{
string eidAndUid = String.Format("{0}:{1}", EnterpriseID, userID);
whereString = String.Format("(EnterpriseID='{0}' or EnterpriseID='E1000') and (TargetUser <=1 or [targetList] like '%,{1},%' or [targetList] like '%,{3},%') and [Platform] like '%{2}%'", EnterpriseID.Trim(), userID, Platform, eidAndUid);
System.Data.DataTable tb = msgPushDal.PagingQueryDataTable("tb_GTMsgRecord", "CONVERT(nvarchar(20),[ID]) as ID,[title],[description],[msgData],[TargetUser],[targetList],[state],[EnterpriseID],[Platform],[sendTime] ", filedOrder, whereString, pageSize, currentPage, out _TotalCount).Tables[0];
//{"title":"摄控本-安排数量设置 申请授权","content":"{\"JumpURL\":\"http:\/\/webapi.lyfz.net\/#\/RemoteAuthorization?msgid=4629368513340450532&name=Demo&uid=20170623094515297&account=demo&apieid=E6051806481742&apiuid=demo&apimsgid=5438917636193927209\"}","description":"2017年10月30日 16时44分34秒【管理员】向你发起远程授权请求“摄控本-安排数量设置”申请授权","badge":33}
List ReadMsIDList = msgPushDal.GetReadMsIDList(EnterpriseID, userID, Platform);
List