1
wangyi
2026-02-28 122f94430924e090600daaac28183bd1daeedc14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.igetui.api.openservice;
using System.Threading.Tasks;
using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.igetui;
using System.Collections.Concurrent;
 
namespace WebAPI.Service
{
    public static class GeTuiService
    {
        //http的域名
        // private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
 
        //https的域名
        private static String HOST = "https://api.getui.com/apiex.htm";
 
        // 配置为DCLOUD 中的 unipush 个推 中对应的数据
        private static String APPID = "X3I10VqaROAH8C7ElIc1L5";
        private static String APPKEY = "V425beWn5K65BcOYKTcXx8";
        private static String MASTERSECRET = "LOXAgRbXS98NxHKUXmlJQ";
 
        // 推送服务实例
        private static IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
 
        private static Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
 
 
        // 单条消息推送
        public static void PubishSingle(int HInterID)
        {
            SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
            DataSet ds = new DataSet();
 
            try
            {
                JObject NameMsgKVP = new JObject();
                ds = oCN.RunProcReturn($@"
                    select * from h_v_OA_WorkLinkBillAllList where hmainid = {HInterID}
                    select * from Gy_UserClientIdRelation
                ", "h_v_OA_WorkLinkBillAllList");
                NotificationTemplate template = null;
                ConcurrentDictionary<string, List<string>> userClientIDDictionary = new ConcurrentDictionary<string, List<string>>();
                // 将用户设备关联表 收集为 Map
                if (ds.Tables[1].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[1].Rows)
                    {
                        if (userClientIDDictionary.ContainsKey(row["HUserName"].ToString())){
                            userClientIDDictionary[row["HUserName"].ToString()].Add(row["HClientID"].ToString());
                        }
                        else
                        {
                            userClientIDDictionary.TryAdd(row["HUserName"].ToString(), new List<string>() { row["HClientID"].ToString() });
                        }
                    }
                }
                if(ds.Tables[0].Rows.Count > 0)
                {
                    string title = ds.Tables[0].Rows[0]["主题"].ToString();
                    string content = ds.Tables[0].Rows[0]["内容"].ToString();
                    template = GetNotificationTemplate(title, content, "/pages/ZLGL/OA_WorkLink/OA_WorkLinkBillQuery", "0"); // 跳转到工作联系单查询页面
 
                    bool pushSuccess = false;
                    // 广播
                    if(ds.Tables[0].Rows[0]["发送类型"].ToString() == "公共")
                    {
                        string sReturn = "";
                        if (oSystemParameter.ShowBill(ref sReturn) == true)
                        {
                             if (oSystemParameter.omodel.WMS_CampanyName == "华舟")
                             {
                                 // 推送给接收人 和 抄送人
                                 foreach (DataRow row in ds.Tables[0].Rows)
                                 {
                                    List<string> clientIDList = new List<string>();
                                    if (userClientIDDictionary.ContainsKey(row["接收人"].ToString()))
                                    {
                                        clientIDList = userClientIDDictionary[row["接收人"].ToString()];
                                    }
                                    else if (userClientIDDictionary.ContainsKey(row["抄送接收人"].ToString()))
                                    {
                                        clientIDList = userClientIDDictionary[row["抄送接收人"].ToString()];
                                    }
 
 
                                    foreach(string clientID in clientIDList)
                                    {
                                        pushSuccess = PushMessageToSingle(template, clientID);
 
                                    }
                                 }
                             }
                             else
                             {
                                // 遍历 全推送
                                foreach (DataRow row in ds.Tables[1].Rows)
                                {
                                    pushSuccess = PushMessageToSingle(template, row["HClientID"].ToString());
                                }
                            }
                        }
                    }else // 单独推送
                    {
                        // 推送给接收人 和 抄送人
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            List<string> clientIDList = new List<string>();
                            if (userClientIDDictionary.ContainsKey(row["接收人"].ToString()))
                            {
                                clientIDList = userClientIDDictionary[row["接收人"].ToString()];
                            }
                            else if (userClientIDDictionary.ContainsKey(row["抄送接收人"].ToString()))
 
                            {
                                clientIDList = userClientIDDictionary[row["抄送接收人"].ToString()];
                            }
 
 
                            foreach (string clientID in clientIDList)
                            {
                                pushSuccess = PushMessageToSingle(template, clientID);
 
                            }
                        }
                    }
 
                    if(!pushSuccess)
                    {
                        // 个推 推送不成功(内网环境) 使用webSocket进行推送
                        // TODO: 使用 rabbitMQ 实现消息队列
                        // WebSocketServer.PushOne(HInterID);
                    }
                }
            }
            catch (Exception ex)
            {
                LogService.Write(ex.ToString());
                return;
            }
        }
 
        // 单推 使用通知消息模型
        private static bool PushMessageToSingle(NotificationTemplate template, string ClientID)
        {
 
            IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
 
            // 单推消息模型
            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;
            try
            {
                String pushResult = push.pushMessageToSingle(message, target);
 
                LogService.Write("-----------------------------------------------");
                LogService.Write("-----------------------------------------------");
                LogService.Write("----------------服务端返回结果:" + pushResult);
                return true;
            }
            catch (RequestException e)
            {
                String requestId = e.RequestId;
                //发送失败后的重发
                String pushResult = push.pushMessageToSingle(message, target, requestId);
                LogService.Write("-----------------------------------------------");
                LogService.Write("-----------------------------------------------");
                LogService.Write("----------------服务端返回结果:" + pushResult);
                return false;  
            }
        }
 
        //透传模板动作内容
        public static TransmissionTemplate TransmissionTemplateDemo()
        {
            TransmissionTemplate template = new TransmissionTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //应用启动类型,1:强制应用启动 2:等待应用启动
            template.TransmissionType = 1;
            //透传内容  
            template.TransmissionContent = "透传内容";
            //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
            // String begin = "2015-03-06 14:36:10";
            // String end = "2015-03-06 14:46:20";
            // template.setDuration(begin, end);
 
            return template;
        }
 
        //通知透传模板动作内容
        public static NotificationTemplate GetNotificationTemplate(string title, string content, string pagePath, string HItemID)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = title;
            //通知栏内容     
            template.Text = content;
            //通知栏显示本地图片
            template.Logo = "@/static/logo.png";
            //通知栏显示网络图标
            template.LogoURL = "";
            //应用启动类型,1:强制应用启动  2:等待应用启动
            template.TransmissionType = 1;
            //透传内容  
            template.TransmissionContent = "";
            //接收到消息是否响铃,true:响铃 false:不响铃   
            template.IsRing = true;
            //接收到消息是否震动,true:震动 false:不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除,true:可清除 false:不可清除    
            template.IsClearable = true;
            var customData = new Dictionary<string, string>
            {
                {"pagePath", pagePath},
                {"itemId", HItemID}     
            };
            template.TransmissionContent = JsonConvert.SerializeObject(customData); // 序列化为 JSON 字符串
            template.TransmissionType = 1; // 1=点击通知后触发客户端回调(透传内容给客户端)
            //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟)
            // String begin = "2015-03-06 14:36:10";
            // String end = "2015-03-06 14:46:20";
            // template.setDuration(begin, end);
 
            return template;
        }
    }
}