-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdp2CmdService2.cs
More file actions
3233 lines (2851 loc) · 122 KB
/
dp2CmdService2.cs
File metadata and controls
3233 lines (2851 loc) · 122 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using DigitalPlatform.Interfaces;
using DigitalPlatform.IO;
using DigitalPlatform.Message;
using DigitalPlatform.MessageClient;
using DigitalPlatform.Text;
using DigitalPlatform.Xml;
using Senparc.Weixin;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace dp2Command.Service
{
public class dp2CmdService2 : dp2BaseCommandService
{
public static string EncryptKey = "dp2weixinPassword";
#region 模板消息
//微信绑定通知
public const string C_Template_Bind = "hFmNH7on2FqSOAiYPZVJN-FcXBv4xpVLBvHsfpLLQKU";
// 微信解绑通知 overdues
public const string C_Template_UnBind = "1riAKkt2W0AOtkx5rx-Lwa0RKRydDTHaMjSoUBGuHog";
//预约到书通知
public const string C_Template_Arrived = "Wm-7-0HJay4yloWEgGG9HXq9eOF5cL8Qm2aAUy-isoM";
//图书超期提醒
public const string C_Template_CaoQi = "QcS3LoLHk37Jh0rgKJId2o93IZjulr5XxgshzlW5VkY";
//图书到期提醒
public const string C_Template_DaoQi = "Q6O3UFPxPnq0rSz82r9P9be41tqEPaJVPD3U0PU8XOU";
//借阅成功通知
public const string C_Template_Borrow = "_F9kVyDWhunqM5ijvcwm6HwzVCnwbkeZl6GV6awB_fc";
//图书归还通知
public const string C_Template_Return = "86Ee0NevuLIVGZE4Xu0uzDdmg0T3xnRMOJ5tREIEG_w";
//缴费成功通知
public const string C_Template_Pay = "4HNhEfLcroEMdX0Pr6aFo_n7_aHuvAzD8_6lzABHkiM";
//退款通知
public const string C_Template_ReturnPay = "sIzSJJ-VRbFUFrDHszxCqwiIYjr9IyyqEqLr95iJVTs";
//个人消息通知
public const string C_Template_Message = "rtAx0BoUAwZ3npbNIO8Y9eIbdWO-weLGE2iOacGqN_s";
#endregion
MessageConnectionCollection _channels = new MessageConnectionCollection();
public MessageConnectionCollection Channels
{
get
{
return this._channels;
}
}
// 配置文件
public string _cfgFile = "";
// dp2服务器地址与代理账号
public string dp2MServerUrl = "";
public string userName = "";
public string password = "";
// 微信信息
public string weiXinAppId { get; set; }
public string weiXinSecret { get; set; }
public bool bTrace = false;
// 背景图管理器
public string TodayUrl = "";
// dp2消息处理类
MsgRouter _msgRouter = new MsgRouter();
//=================
// 设为单一实例
static dp2CmdService2 _instance;
private dp2CmdService2()
{
}
private static object _lock = new object();
static public dp2CmdService2 Instance
{
get
{
if (null == _instance)
{
lock (_lock) //线程安全的
{
_instance = new dp2CmdService2();
}
}
return _instance;
}
}
//===========
public void Init(string dataDir)
{
this.weiXinDataDir = dataDir;
this._cfgFile = this.weiXinDataDir + "\\" + "weixin.xml";
if (File.Exists(this._cfgFile) == false)
{
throw new Exception("配置文件" + this._cfgFile + "不存在。");
}
// 日志目录
this.weiXinLogDir = this.weiXinDataDir + "/log";
if (!Directory.Exists(weiXinLogDir))
{
Directory.CreateDirectory(weiXinLogDir);
}
XmlDocument dom = new XmlDocument();
dom.Load(this._cfgFile);
XmlNode root = dom.DocumentElement;
// 取出mserver服务器配置信息
XmlNode nodeDp2mserver = root.SelectSingleNode("dp2mserver");
this.dp2MServerUrl = DomUtil.GetAttr(nodeDp2mserver, "url");// WebConfigurationManager.AppSettings["dp2MServerUrl"];
this.userName = DomUtil.GetAttr(nodeDp2mserver, "username");//WebConfigurationManager.AppSettings["userName"];
this.password = DomUtil.GetAttr(nodeDp2mserver, "password");//WebConfigurationManager.AppSettings["password"];
if (string.IsNullOrEmpty(this.password) == false)// 解密
this.password = Cryptography.Decrypt(this.password, dp2CmdService2.EncryptKey);
// 取出微信配置信息
XmlNode nodeDp2weixin = root.SelectSingleNode("dp2weixin");
this.weiXinUrl = DomUtil.GetAttr(nodeDp2weixin, "url"); //WebConfigurationManager.AppSettings["weiXinUrl"];
this.weiXinAppId = DomUtil.GetAttr(nodeDp2weixin, "AppId"); //WebConfigurationManager.AppSettings["weiXinAppId"];
this.weiXinSecret = DomUtil.GetAttr(nodeDp2weixin, "Secret"); //WebConfigurationManager.AppSettings["weiXinSecret"];
string trace = DomUtil.GetAttr(nodeDp2weixin, "trace");
if (trace.ToLower() == "true")
this.bTrace = true;
// mongo配置
XmlNode nodeMongoDB = root.SelectSingleNode("mongoDB");
string connectionString = DomUtil.GetAttr(nodeMongoDB, "connectionString");
if (String.IsNullOrEmpty(connectionString) == true)
{
throw new Exception("尚未配置mongoDB节点的connectionString属性");
}
string instancePrefix = DomUtil.GetAttr(nodeMongoDB, "instancePrefix");
// 打开图书馆账号库与用户库
WxUserDatabase.Current.Open(connectionString, instancePrefix);
LibDatabase.Current.Open(connectionString, instancePrefix);
// 初始化接口类
string strError = "";
int nRet = this.InitialExternalMessageInterfaces(dom, out strError);
if (nRet == -1)
throw new Exception("初始化接口配置信息出错:" + strError);
//全局只需注册一次
AccessTokenContainer.Register(this.weiXinAppId, this.weiXinSecret);
_channels.Login -= _channels_Login;
_channels.Login += _channels_Login;
if (bTrace == true)
{
if (this.Channels.TraceWriter != null)
this.Channels.TraceWriter.Close();
StreamWriter sw = new StreamWriter(Path.Combine(this.weiXinDataDir, "trace.txt"));
sw.AutoFlush = true;
_channels.TraceWriter = sw;
}
// 消息处理类
this._msgRouter.SendMessageEvent -= _msgRouter_SendMessageEvent;
this._msgRouter.SendMessageEvent += _msgRouter_SendMessageEvent;
this._msgRouter.Start(this._channels,
this.dp2MServerUrl,
"_patronNotify");
}
public void Close()
{
if (this._msgRouter != null)
{
this._msgRouter.SendMessageEvent -= _msgRouter_SendMessageEvent;
this._msgRouter.Stop();
}
if (this.Channels != null)
{
this.Channels.Login -= _channels_Login;
if (this.Channels.TraceWriter != null)
this.Channels.TraceWriter.Close();
}
this.WriteLog("走到close()");
}
public void SetDp2mserverInfo(string dp2mserverUrl,
string userName,
string password)
{
XmlDocument dom = new XmlDocument();
dom.Load(this._cfgFile);
XmlNode root = dom.DocumentElement;
// 设置mserver服务器配置信息
XmlNode nodeDp2mserver = root.SelectSingleNode("dp2mserver");
if (nodeDp2mserver == null)
{
nodeDp2mserver = dom.CreateElement("dp2mserver");
root.AppendChild(nodeDp2mserver);
}
DomUtil.SetAttr(nodeDp2mserver, "url", dp2mserverUrl);
DomUtil.SetAttr(nodeDp2mserver, "username", userName);
string encryptPassword = Cryptography.Encrypt(password, dp2CmdService2.EncryptKey);
DomUtil.SetAttr(nodeDp2mserver, "password", encryptPassword);
dom.Save(this._cfgFile);
// 更新内存的信息
this.dp2MServerUrl = dp2mserverUrl;
this.userName = userName;
this.password = password;
}
public void GetDp2mserverInfo(out string dp2mserverUrl,
out string userName,
out string password)
{
dp2mserverUrl = "";
userName = "";
password = "";
XmlDocument dom = new XmlDocument();
dom.Load(this._cfgFile);
XmlNode root = dom.DocumentElement;
// 设置mserver服务器配置信息
XmlNode nodeDp2mserver = root.SelectSingleNode("dp2mserver");
if (nodeDp2mserver != null)
{
dp2mserverUrl = DomUtil.GetAttr(nodeDp2mserver, "url");
userName = DomUtil.GetAttr(nodeDp2mserver, "username");
password = DomUtil.GetAttr(nodeDp2mserver, "password");
if (string.IsNullOrEmpty(password) == false)// 解密
password = Cryptography.Decrypt(this.password, dp2CmdService2.EncryptKey);
}
}
#region 消息处理
// 处理收到的消息
void _msgRouter_SendMessageEvent(object sender, SendMessageEventArgs e)
{
MessageRecord record = e.Message;
if (record == null)
{
this.WriteErrorLog("传过来的e.Message为null");
return;
}
//this.WriteErrorLog("走进_msgRouter_SendMessageEvent");
try
{
string strError = "";
/// <returns>
/// -1 不符合条件,不处理
/// 0 未绑定微信id,未处理
/// 1 成功
/// </returns>
int nRet = this.InternalDoMessage(record, out strError);
if (nRet == -1)
{
this.WriteErrorLog("[" + record.id + "]未发送成功:" + strError);
}
else if (nRet == 0)
{
this.WriteErrorLog("[" + record.id + "]未发送成功:未绑定微信id。");
}
else
{
this.WriteErrorLog("[" + record.id + "]发送成功。");
}
}
catch (Exception ex)
{
this.WriteErrorLog("[" + record.id + "]异常:"+ex.Message);
}
}
/// <summary>
/// 内部处理消息
/// </summary>
/// <param name="record"></param>
/// <param name="strError"></param>
/// <returns>
/// -1 不符合条件,不处理
/// 0 未绑定微信id,未处理
/// 1 成功
/// </returns>
public int InternalDoMessage(MessageRecord record, out string strError)
{
strError = "";
string id = record.id;
string data = record.data;
string[] group = record.groups;
string create = record.creator;
//<root>
// <type>patronNotify</type>
// <recipient>R0000001@LUID:62637a12-1965-4876-af3a-fc1d3009af8a</recipient>
// <mime>xml</mime>
// <body>...</body>
//</root>
XmlDocument dataDom = new XmlDocument();
try
{
dataDom.LoadXml(data);
}
catch (Exception ex)
{
strError = "加载消息返回的data到xml出错:" + ex.Message;
return -1;
}
XmlNode nodeType = dataDom.DocumentElement.SelectSingleNode("type");
if (nodeType == null)
{
strError = "尚未定义<type>节点";
return -1;
}
string type = DomUtil.GetNodeText(nodeType);
if (type != "patronNotify") //只处理通知消息
{
strError = "<type>节点值不是patronNotify。";
return -1;
}
XmlNode nodeBody = dataDom.DocumentElement.SelectSingleNode("body");
if (nodeBody == null)
{
strError = "data中不存在body节点";
return -1;
}
/*
body元素里面是预约到书通知记录(注意这是一个字符串,需要另行装入一个XmlDocument解析),其格式如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
<type>预约到书通知</type>
<itemBarcode>0000001</itemBarcode>
<refID> </refID>
<opacURL>/book.aspx?barcode=0000001</opacURL>
<reserveTime>2天</reserveTime>
<today>2016/5/17 10:10:59</today>
<summary>船舶柴油机 / 聂云超主编. -- ISBN 7-...</summary>
<patronName>张三</patronName>
<patronRecord>
<barcode>R0000001</barcode>
<readerType>本科生</readerType>
<name>张三</name>
<refID>be13ecc5-6a9c-4400-9453-a072c50cede1</refID>
<department>数学系</department>
<address>address</address>
<cardNumber>C12345</cardNumber>
<refid>8aa41a9a-fb42-48c0-b9b9-9d6656dbeb76</refid>
<email>email:xietao@dp2003.com,weixinid:testwx2</email>
<tel>13641016400</tel>
<idCardNumber>1234567890123</idCardNumber>
</patronRecord>
</root
*/
XmlDocument bodyDom = new XmlDocument();
try
{
bodyDom.LoadXml(nodeBody.InnerText);//.InnerXml);
}
catch (Exception ex)
{
strError = "加载消息data中的body到xml出错:" + ex.Message;
return -1;
}
XmlNode root = bodyDom.DocumentElement;
XmlNode typeNode = root.SelectSingleNode("type");
if (typeNode == null)
{
strError = "消息data的body中未定义type节点。";
return -1;
}
string strType = DomUtil.GetNodeText(typeNode);
int nRet = 0;
// 根据类型发送不同的模板消息
if (strType == "预约到书通知")
{
nRet = this.SendArrived(bodyDom, out strError);
}
else if (strType == "超期通知")
{
nRet = this.SendCaoQi(bodyDom, out strError);
}
else if (strType == "借书成功")
{
nRet = this.SendBorrowMsg(bodyDom, out strError);
}
else if (strType == "还书成功")
{
nRet = this.SendReturnMsg(bodyDom, out strError);
}
else if (strType == "交费")
{
nRet = this.SendPayMsg(bodyDom, out strError);
}
else if (strType == "撤销交费")
{
nRet = this.SendReturnPayMsg(bodyDom, out strError);
}
else if (strType == "以停代金到期")
{
nRet = this.SendMessageMsg(bodyDom, out strError);
}
else
{
strError = "不支持的消息类型["+strType+"]";
return -1;
}
return nRet;
}
private string _msgFirstLeft = "尊敬的读者:您好,";
private string _msgRemark = "\n如有疑问,请联系系统管理员。";
/// <returns>
/// -1 出错,格式出错或者发送模板消息出错
/// 0 未绑定微信id
/// 1 成功
/// </returns>
private int SendMessageMsg(XmlDocument bodyDom, out string strError)
{
strError = "";
/*
<root>
<type>以停代金到期</type>
…
根元素下的items元素下,有一个或者多个overdue元素,记载了刚到期的以停代金事项信息。
在patronRecord的下级元素overdues下,可以看到若干overdue元素,这是当前还未到期或者交费的事项。只要还有这样的事项,读者就不被允许借书,只能还书。所以通知消息文字组织的时候,可以考虑提醒尚余多少违约事项,这样可以避免读者空高兴一场以为马上可以借书了
*/
string patronName = "";
List<string> weiXinIdList = this.GetWeiXinIds(bodyDom, out patronName);
if (weiXinIdList.Count == 0)
{
strError = "未绑定微信id";
return 0;
}
XmlNode root = bodyDom.DocumentElement;
XmlNode nodeOperTime = root.SelectSingleNode("operTime");
if (nodeOperTime == null)
{
strError = "尚未定义<borrowDate>节点";
return -1;
}
string operTime = DomUtil.GetNodeText(nodeOperTime);
operTime = DateTimeUtil.ToLocalTime(operTime, "yyyy/MM/dd");
XmlNodeList listOverdue = root.SelectNodes("items/overdue");
string barcodes = "";
double totalPrice = 0;
foreach (XmlNode node in listOverdue)
{
string oneBarcode = DomUtil.GetAttr(node, "barcode");
if (barcodes != "")
barcodes += ",";
barcodes += oneBarcode;
string price = DomUtil.GetAttr(node, "price");
if (String.IsNullOrEmpty(price) == false && price.Length > 3)
{
double dPrice = Convert.ToDouble(price.Substring(3));
totalPrice += dPrice;
}
}
string strText = "您有["+barcodes + "]项违约以停代金到期了,";
XmlNodeList listOverdue1 = root.SelectNodes("patronRecord/overdues/overdue");
if (listOverdue1.Count > 0)
{
strText += "您还有" + listOverdue1.Count.ToString() + "项违约未到期,还不能借书。";
}
else
{
strText += "您可以继续借书了。";
}
foreach (string weiXinId in weiXinIdList)
{
try
{
var accessToken = AccessTokenContainer.GetAccessToken(this.weiXinAppId);
//{{first.DATA}}
//标题:{{keyword1.DATA}}
//时间:{{keyword2.DATA}}
//内容:{{keyword3.DATA}}
//{{remark.DATA}}
var msgData = new BorrowTemplateData()
{
first = new TemplateDataItem("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓", "#9400D3"),// dark violet //this._msgFirstLeft + "您的停借期限到期了。" //$$$$$$$$$$$$$$$$
keyword1 = new TemplateDataItem("以停代金到期", "#000000"),//text.ToString()),// "请让我慢慢长大"),
keyword2 = new TemplateDataItem(operTime, "#000000"),
keyword3 = new TemplateDataItem(strText, "#000000"),
remark = new TemplateDataItem(this._msgRemark, "#CCCCCC")
};
// 发送模板消息
var result1 = TemplateApi.SendTemplateMessage(accessToken,
weiXinId,
dp2CmdService2.C_Template_Message,
"#FF0000",
"",//不出现详细了
msgData);
if (result1.errcode != 0)
{
strError = result1.errmsg;
return -1;
}
}
catch (Exception ex)
{
this.WriteErrorLog("给读者" + patronName + "发送'以停代金到期'通知异常:" + ex.Message);
}
}
return 1;
}
/// <returns>
/// -1 出错,格式出错或者发送模板消息出错
/// 0 未绑定微信id
/// 1 成功
/// </returns>
private int SendReturnPayMsg(XmlDocument bodyDom, out string strError)
{
strError = "";
/*
<?xml version="1.0" encoding="utf-8"?>
<root>
<type>撤销交费</type>
<libraryCode></libraryCode>
<operation>amerce</operation>
<action>undo</action>
<readerBarcode>R0000001</readerBarcode>
<operator>supervisor</operator>
<operTime>Sun, 22 May 2016 19:15:54 +0800</operTime>
<clientAddress>::1</clientAddress>
<version>1.02</version>
<patronRecord>
<barcode>R0000001</barcode>
<readerType>本科生</readerType>
<name>张三</name>
<refID>be13ecc5-6a9c-4400-9453-a072c50cede1</refID>
<department>/</department>
<address>address</address>
<cardNumber>C12345</cardNumber>
<overdues>
<overdue barcode="0000001" reason="超期。超 335天; 违约金因子: CNY1.0/day" overduePeriod="335day" price="CNY335" borrowDate="Tue, 01 Dec 2015 14:09:33 +0800" borrowPeriod="31day" returnDate="Thu, 01 Dec 2016 14:09:52 +0800" borrowOperator="supervisor" operator="supervisor" id="635845758236835562-1" />
</overdues>
<refid>8aa41a9a-fb42-48c0-b9b9-9d6656dbeb76</refid>
<email>email:xietao@dp2003.com,weixinid:testwx2,testid:123456</email>
<idCardNumber>1234567890123</idCardNumber>
<tel>13641016400</tel>
<libraryCode></libraryCode>
</patronRecord>
<items>
<overdue barcode="0000001" summary=”…” reason="超期。超 335天; 违约金因子: CNY1.0/day" overduePeriod="335day" price="CNY335" borrowDate="Tue, 01 Dec 2015 14:09:33 +0800" borrowPeriod="31day" returnDate="Thu, 01 Dec 2016 14:09:52 +0800" borrowOperator="supervisor" operator="supervisor" id="635845758236835562-1" />
</items>
</root>
*/
string patronName = "";
List<string> weiXinIdList = this.GetWeiXinIds(bodyDom, out patronName);
if (weiXinIdList.Count == 0)
{
strError = "未绑定微信id";
return 0;
}
XmlNode root = bodyDom.DocumentElement;
XmlNode nodeOperTime = root.SelectSingleNode("operTime");
if (nodeOperTime == null)
{
strError = "尚未定义<borrowDate>节点";
return -1;
}
string operTime = DomUtil.GetNodeText(nodeOperTime);
operTime = DateTimeUtil.ToLocalTime(operTime, "yyyy/MM/dd");
XmlNodeList listOverdue = root.SelectNodes("items/overdue");
string barcodes = "";
double totalPrice = 0;
foreach (XmlNode node in listOverdue)
{
string oneBarcode = DomUtil.GetAttr(node, "barcode");
if (barcodes != "")
barcodes += ",";
barcodes += oneBarcode;
string price = DomUtil.GetAttr(node, "price");
if (String.IsNullOrEmpty(price) == false && price.Length > 3)
{
double dPrice = Convert.ToDouble(price.Substring(3));
totalPrice += dPrice;
}
}
foreach (string weiXinId in weiXinIdList)
{
try
{
var accessToken = AccessTokenContainer.GetAccessToken(this.weiXinAppId);
//{{first.DATA}}
//退款原因:{{reason.DATA}}
//退款金额:{{refund.DATA}}
//{{remark.DATA}}
var msgData = new ReturnPayTemplateData()
{
first = new TemplateDataItem("━━━━━━$━━━━━━", "#B8860B"), // ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ dark golden rod//this._msgFirstLeft + "撤消交费成功!"
reason = new TemplateDataItem("撤消[" + barcodes + "]交费。", "#000000"),//text.ToString()),// "请让我慢慢长大"),
refund = new TemplateDataItem("CNY" + totalPrice, "#000000"),
remark = new TemplateDataItem(this._msgRemark, "#CCCCCC")
};
// 发送模板消息
var result1 = TemplateApi.SendTemplateMessage(accessToken,
weiXinId,
dp2CmdService2.C_Template_ReturnPay,
"#FF0000",
"",//不出现详细了
msgData);
if (result1.errcode != 0)
{
strError = result1.errmsg;
return -1;
}
}
catch (Exception ex)
{
this.WriteErrorLog("给读者" + patronName + "发送'撤消交费成功'通知异常:" + ex.Message);
}
}
return 1;
}
/// <returns>
/// -1 出错,格式出错或者发送模板消息出错
/// 0 未绑定微信id
/// 1 成功
/// </returns>
private int SendPayMsg(XmlDocument bodyDom, out string strError)
{
strError = "";
/*
<?xml version="1.0" encoding="utf-8"?>
<root>
<type>交费</type>
<libraryCode></libraryCode>
<operation>amerce</operation>
<action>amerce</action>
<readerBarcode>R0000001</readerBarcode>
<operator>supervisor</operator>
<operTime>Sun, 22 May 2016 19:28:52 +0800</operTime>
<clientAddress>::1</clientAddress>
<version>1.02</version>
<patronRecord>
<barcode>R0000001</barcode>
<readerType>本科生</readerType>
<name>张三</name>
<refID>be13ecc5-6a9c-4400-9453-a072c50cede1</refID>
<department>/</department>
<address>address</address>
<cardNumber>C12345</cardNumber>
<refid>8aa41a9a-fb42-48c0-b9b9-9d6656dbeb76</refid>
<email>email:xietao@dp2003.com,weixinid:testwx2,testid:123456</email>
<idCardNumber>1234567890123</idCardNumber>
<tel>13641016400</tel>
<libraryCode></libraryCode>
</patronRecord>
<items>
<overdue barcode="0000001" summary=”…” reason="超期。超 335天; 违约金因子: CNY1.0/day" overduePeriod="335day" price="CNY335" borrowDate="Tue, 01 Dec 2015 14:09:33 +0800" borrowPeriod="31day" returnDate="Thu, 01 Dec 2016 14:09:52 +0800" borrowOperator="supervisor" operator="supervisor" id="635845758236835562-1" />
</items>
</root>
*/
string patronName = "";
List<string> weiXinIdList = this.GetWeiXinIds(bodyDom, out patronName);
if (weiXinIdList.Count == 0)
{
strError = "未绑定微信id";
return 0;
}
XmlNode root = bodyDom.DocumentElement;
XmlNode nodeOperTime = root.SelectSingleNode("operTime");
if (nodeOperTime == null)
{
strError = "尚未定义<borrowDate>节点";
return -1;
}
string operTime = DomUtil.GetNodeText(nodeOperTime);
operTime = DateTimeUtil.ToLocalTime(operTime, "yyyy/MM/dd");
XmlNodeList listOverdue = root.SelectNodes("items/overdue");
string barcodes = "";
double totalPrice = 0;
string reasons = "";
foreach (XmlNode node in listOverdue)
{
string oneBarcode = DomUtil.GetAttr(node, "barcode");
if (barcodes != "")
barcodes += ",";
barcodes += oneBarcode;
string price = DomUtil.GetAttr(node, "price");
if (String.IsNullOrEmpty(price) == false && price.Length > 3)
{
double dPrice = Convert.ToDouble(price.Substring(3));
totalPrice += dPrice;
}
string oneReason = DomUtil.GetAttr(node, "reason");
if (reasons != "")
reasons += ",";
reasons += oneReason;
}
foreach (string weiXinId in weiXinIdList)
{
try
{
var accessToken = AccessTokenContainer.GetAccessToken(this.weiXinAppId);
//{{first.DATA}}
//订单号:{{keyword1.DATA}}
//缴费人:{{keyword2.DATA}}
//缴费金额:{{keyword3.DATA}}
//费用类型:{{keyword4.DATA}}
//缴费时间:{{keyword5.DATA}}
//{{remark.DATA}}
//您好,您已缴费成功!
//订单号:书名(册条码号)
//缴费人:张三
//缴费金额:¥100.00
//费用类型:违约
//缴费时间:2015-12-27 13:15
//如有疑问,请联系学校管理员,感谢您的使用!、
var msgData = new PayTemplateData()
{
first = new TemplateDataItem("++++++$++++++", "#556B2F"),//★★★★★★★★★★★★★★★ dark olive green//this._msgFirstLeft+"您已交费成功!"
keyword1 = new TemplateDataItem(barcodes, "#000000"),//text.ToString()),// "请让我慢慢长大"),
keyword2 = new TemplateDataItem(patronName, "#000000"),
keyword3 = new TemplateDataItem("CNY" + totalPrice, "#000000"),
keyword4 = new TemplateDataItem(reasons, "#000000"),
keyword5 = new TemplateDataItem(operTime, "#000000"),
remark = new TemplateDataItem(this._msgRemark, "#CCCCCC")
};
// 发送模板消息
var result1 = TemplateApi.SendTemplateMessage(accessToken,
weiXinId,
dp2CmdService2.C_Template_Pay,
"#FF0000",
"",//不出现详细了
msgData);
if (result1.errcode != 0)
{
strError = result1.errmsg;
return -1;
}
}
catch (Exception ex)
{
this.WriteErrorLog("给读者" + patronName + "发送交费成功通知异常:" + ex.Message);
}
}
return 1;
}
/// <returns>
/// -1 出错,格式出错或者发送模板消息出错
/// 0 未绑定微信id
/// 1 成功
/// </returns>
private int SendReturnMsg(XmlDocument bodyDom, out string strError)
{
strError = "";
/*
<?xml version="1.0" encoding="utf-8"?>
<root>
<type>还书成功</type>
<libraryCode></libraryCode>
<operation>return</operation>
<action>return</action>
<itemBarcode>0000001</itemBarcode>
<readerBarcode>R0000001</readerBarcode>
<operator>supervisor</operator>
<operTime>Sun, 22 May 2016 13:11:33 +0800</operTime>
<clientAddress>::1</clientAddress>
<version>1.02</version>
<uid>4a9730b1-a6d7-4fd5-9e6f-57c074f73661</uid>
<patronRecord>
<barcode>R0000001</barcode>
<readerType>本科生</readerType>
<name>张三</name>
<borrows>
</borrows>
<refID>be13ecc5-6a9c-4400-9453-a072c50cede1</refID>
<reservations>
</reservations>
<department>/</department>
<address>address</address>
<cardNumber>C12345</cardNumber>
<overdues>
</overdues>
<refid>8aa41a9a-fb42-48c0-b9b9-9d6656dbeb76</refid>
<email>email:xietao@dp2003.com,weixinid:testwx2,testid:123456</email>
<idCardNumber>1234567890123</idCardNumber>
<tel>13641016400</tel>
<libraryCode></libraryCode>
</patronRecord>
<itemRecord>
<parent>602</parent>
<refID>59b613c6-fe09-4280-8884-43f2b045c41c</refID>
<barcode>0000001</barcode>
<location>流通库</location>
<price>$4.65</price>
<bookType>普通</bookType>
<accessNo>U664.121/N590</accessNo>
<summary>船舶柴油机 / 聂云超主编. -- ISBN 7-81007-115-7 : $4.65</summary>
</itemRecord>
</root>
*/
string patronName = "";
List<string> weiXinIdList = this.GetWeiXinIds(bodyDom, out patronName);
if (weiXinIdList.Count == 0)
{
strError = "未绑定微信id";
return 0;
}
XmlNode root = bodyDom.DocumentElement;
//<itemBarcode>0000001</itemBarcode>
//<borrowDate>Sun, 22 May 2016 19:48:01 +0800</borrowDate>
//<borrowPeriod>31day</borrowPeriod>
//<returningDate>Wed, 22 Jun 2016 12:00:00 +0800</returningDate>
XmlNode nodeItemBarcode = root.SelectSingleNode("itemBarcode");
if (nodeItemBarcode == null)
{
strError = "尚未定义<itemBarcode>节点";
return -1;
}
string itemBarcode = DomUtil.GetNodeText(nodeItemBarcode);
XmlNode nodeOperTime = root.SelectSingleNode("operTime");
if (nodeOperTime == null)
{
strError = "尚未定义<borrowDate>节点";
return -1;
}
string operTime = DomUtil.GetNodeText(nodeOperTime);
operTime = DateTimeUtil.ToLocalTime(operTime, "yyyy/MM/dd");
XmlNode nodeSummary = root.SelectSingleNode("itemRecord/summary");
if (nodeSummary == null)
{
strError = "尚未定义itemRecord/summary节点";
return -1;
}
string summary = DomUtil.GetNodeText(nodeSummary);
// 检查是否有超期信息
string remark = "\n" + patronName + ",感谢及时归还,欢迎继续借书。";
XmlNodeList listOverdue = root.SelectNodes("patronRecord/overdues/overdue");
if (listOverdue.Count > 0)
{
remark = "\n"+patronName+",您有" + listOverdue.Count + "笔超期违约记录,请履行超期手续。";
}
foreach (string weiXinId in weiXinIdList)
{
try
{
var accessToken = AccessTokenContainer.GetAccessToken(this.weiXinAppId);
//{{first.DATA}}
//书名:{{keyword1.DATA}}
//归还时间:{{keyword2.DATA}}
//借阅人:{{keyword3.DATA}}
//{{remark.DATA}}
//您好,你借阅的图书已确认归还.
//书名:算法导论
//归还时间:2015-10-10 12:14
//借阅人:李明
//欢迎继续借书!
var msgData = new ReturnTemplateData()
{
first = new TemplateDataItem("▉▊▋▍▎▉▊▋▍▎▉▊▋▍▎", "#00008B"), // dark blue//this._msgFirstLeft + "您借出的图书已确认归还。"
keyword1 = new TemplateDataItem(summary, "#000000"),//text.ToString()),// "请让我慢慢长大"),
keyword2 = new TemplateDataItem(operTime, "#000000"),
keyword3 = new TemplateDataItem(patronName, "#000000"),
remark = new TemplateDataItem(remark, "#CCCCCC")
};
// 发送模板消息
var result1 = TemplateApi.SendTemplateMessage(accessToken,
weiXinId,
dp2CmdService2.C_Template_Return,
"#00008B",
"",//不出现详细了
msgData);
if (result1.errcode != 0)
{
strError = result1.errmsg;
return -1;
}
}
catch (Exception ex)
{
this.WriteErrorLog("给读者" + patronName + "发送还书成功通知异常:" + ex.Message);
}
}
return 1;
}
/// <returns>
/// -1 出错,格式出错或者发送模板消息出错
/// 0 未绑定微信id
/// 1 成功
/// </returns>
private int SendBorrowMsg(XmlDocument bodyDom, out string strError)
{
strError = "";
/*
<root>
<type>借书成功</type>
<libraryCode></libraryCode>
<operation>borrow</operation>
<action>borrow</action>
<readerBarcode>R0000001</readerBarcode>
<itemBarcode>0000001</itemBarcode>
<borrowDate>Sun, 22 May 2016 19:48:01 +0800</borrowDate>
<borrowPeriod>31day</borrowPeriod>
<returningDate>Wed, 22 Jun 2016 12:00:00 +0800</returningDate>
<price>$4.65</price>
<no>0</no>
<bookType>普通</bookType>
<operator>supervisor</operator>
<operTime>Sun, 22 May 2016 19:48:01 +0800</operTime>
<clientAddress>::1</clientAddress>
<version>1.02</version>
<uid>062724d8-80c1-4752-979a-b1cd548466be</uid>
<patronRecord>
<barcode>R0000001</barcode>
<readerType>本科生</readerType>
<name>张三</name>
<borrows>