using com.igetui.api.openservice;
|
using com.igetui.api.openservice.igetui;
|
using com.igetui.api.openservice.igetui.template;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
|
namespace MesMessage
|
{
|
public static class GeTuiService
|
{
|
// 个推接口地址(使用 HTTPS)
|
private static readonly string HOST = "https://api.getui.com/apiex.htm";
|
private static readonly string APPID = "X3I10VqaROAH8C7ElIc1L5";
|
private static readonly string APPKEY = "V425beWn5K65BcOYKTcXx8";
|
private static readonly string MASTERSECRET = "LOXAgRbXS98NxHKUXmlJQ";
|
|
// 注意:不再声明静态 IGtPush 字段,避免类型初始化异常
|
|
/// <summary>
|
/// 单条消息推送(工作联系单)
|
/// </summary>
|
public static void PubishSingle(int HInterID)
|
{
|
try
|
{
|
DataSet ds = new DataSet();
|
DBHelper oCN = new DBHelper();
|
ds = oCN.RunProcReturn($@"
|
select * from h_v_OA_WorkLinkBillAllList where hmainid = {HInterID};
|
select * from Gy_UserClientIdRelation
|
", "h_v_OA_WorkLinkBillAllList");
|
|
if (ds.Tables[0].Rows.Count == 0) return;
|
|
DataRow billRow = ds.Tables[0].Rows[0];
|
string title = billRow["主题"].ToString();
|
string content = billRow["内容"].ToString();
|
string sendType = billRow["发送类型"].ToString();
|
string receiver = billRow["接收人"].ToString();
|
string ccReceiver = billRow["抄送接收人"].ToString();
|
|
// 创建通知模板
|
var template = GetNotificationTemplate(
|
title,
|
content,
|
"/pages/ZLGL/OA_WorkLink/OA_WorkLinkBillQuery",
|
"0"
|
);
|
|
bool anySuccess = false;
|
|
// 遍历所有客户端ID
|
foreach (DataRow userRow in ds.Tables[1].Rows)
|
{
|
string userName = userRow["HUserName"].ToString();
|
string clientId = userRow["HClientID"].ToString();
|
|
bool needPush = sendType == "公共" ||
|
userName == receiver ||
|
userName == ccReceiver;
|
|
if (needPush)
|
{
|
bool success = PushMessageToSingle(template, clientId);
|
if (success) anySuccess = true;
|
}
|
}
|
|
if (!anySuccess)
|
{
|
// 推送失败,备用方案:使用 RabbitMQ 或 WebSocket
|
DBHelper.CustomWriteLog("个推推送失败(可能内网原因),尝试备用方案", DateTime.Now.ToString("yyyy-MM-dd"));
|
// WebSocketServer.PushOne(HInterID);
|
}
|
}
|
catch (Exception ex)
|
{
|
DBHelper.CustomWriteLog($"PubishSingle 异常: {ex}", DateTime.Now.ToString("yyyy-MM-dd"));
|
}
|
}
|
|
/// <summary>
|
/// 单推消息(通知模板)
|
/// </summary>
|
private static bool PushMessageToSingle(NotificationTemplate template, string clientId)
|
{
|
try
|
{
|
// 每次调用创建新实例,避免共享状态
|
IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
|
|
SingleMessage message = new SingleMessage
|
{
|
IsOffline = true,
|
OfflineExpireTime = 1000 * 3600 * 12, // 12小时
|
Data = template
|
};
|
|
Target target = new Target
|
{
|
appId = APPID,
|
clientId = clientId
|
};
|
|
string pushResult = push.pushMessageToSingle(message, target);
|
DBHelper.CustomWriteLog($"个推推送结果: {pushResult}", DateTime.Now.ToString("yyyy-MM-dd"));
|
return true;
|
}
|
catch (RequestException ex)
|
{
|
// 可尝试重发(使用原请求ID)
|
DBHelper.CustomWriteLog($"推送异常,请求ID: {ex.RequestId}, 错误: {ex}", DateTime.Now.ToString("yyyy-MM-dd"));
|
return false;
|
}
|
catch (Exception ex)
|
{
|
DBHelper.CustomWriteLog($"推送异常: {ex}", DateTime.Now.ToString("yyyy-MM-dd"));
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 创建通知模板(携带透传数据)
|
/// </summary>
|
public static NotificationTemplate GetNotificationTemplate(string title, string content, string pagePath, string itemId)
|
{
|
var template = new NotificationTemplate
|
{
|
AppId = APPID,
|
AppKey = APPKEY,
|
Title = title,
|
Text = content,
|
Logo = "icon", // 应用内图标名称(根据实际修改)
|
LogoURL = "", // 网络图标,可留空
|
IsRing = true,
|
IsVibrate = true,
|
IsClearable = true,
|
TransmissionType = 1 // 1=点击通知触发客户端透传回调
|
};
|
|
// 透传内容:JSON 格式
|
var customData = new Dictionary<string, string>
|
{
|
{ "pagePath", pagePath },
|
{ "itemId", itemId }
|
};
|
template.TransmissionContent = JsonConvert.SerializeObject(customData);
|
|
return template;
|
}
|
}
|
}
|