-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cs
1124 lines (1064 loc) · 49 KB
/
main.cs
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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _12306Helper.Properties;
using _12306Helper.Helper;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Collections;
using HtmlAgilityPack;
using System.Threading;
using Crack12306Captcha;
namespace _12306Helper
{
public partial class main : Form
{
DataTable dt = new DataTable();
string name = string.Empty;
Dictionary<string, string> mydic = new Dictionary<string, string>();
DataTable dtTickts = new DataTable();
List<Checi> myCheci = new List<Checi>();
public PassengerList pList = new PassengerList();
public List<Passenger> myList = new List<Passenger>();
string leftTicketStr = string.Empty;
string token = "";
string piaojia = "";
string myFromno, myTono, myFrom, myTo, myStartTime, myEndTime, myDate, myTrainno, myTraincode;
string tjdata = "";
Dictionary<string, string> dicXiBie = new Dictionary<string, string>();
Dictionary<string, string> dicPiaozhong = new Dictionary<string, string>();
Thread queryThread;
public main()
{
InitializeComponent();
Init();
}
public main(string _name)
{
InitializeComponent();
Init();
name = _name;
linkOpenInIE.Text = name;
}
private void Init()
{
//初始化控件的值
this.cmboxCFSJ.SelectedIndex = 0;
dateCFRQ.MinDate = DateTime.Now;
dateCFRQ.MaxDate = DateTime.Now.AddDays(19);
//加载城市
string cities = Resources.city;
string[] _cities = cities.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
dt.Columns.Add("city");
dt.Columns.Add("code");
dt.Columns.Add("filter");
for (int i = 0; i < _cities.Length; i++)
{
string[] lines = _cities[i].Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
DataRow dr = dt.NewRow();
dr["city"] = lines[1];
dr["code"] = lines[2];
dr["filter"] = lines[1] + ";" + lines[3] + ";" + lines[4];
dt.Rows.Add(dr);
}
dt.AcceptChanges();
mydic.Add("全部", "QB");
mydic.Add("动车", "D");
mydic.Add("Z字头", "Z");
mydic.Add("T字头", "T");
mydic.Add("K字头", "K");
mydic.Add("其它", "QT");
int controlX = 7;
int controlY = 7;
foreach (string key in mydic.Keys)
{
CheckBox cbox = new CheckBox();
cbox.Checked = true;
cbox.Text = key;
cbox.Tag = mydic[key];
cbox.Location = new Point(controlX, controlY);
controlX += 48;
cbox.Size = new Size(48, 16);
cbox.Name = "cboxCheCi" + mydic[key];
cbox.CheckedChanged += new EventHandler(cbox_CheckedChanged);
this.panel2.Controls.Add(cbox);
}
//G525,[始]北京西17:14,汉口22:41,05:27,11,--,有,有,--,--,--,--,--,--,--,预订
dtTickts.Columns.Add("checi");
dtTickts.Columns.Add("fazhan");
dtTickts.Columns.Add("daozhan");
dtTickts.Columns.Add("lishi");
dtTickts.Columns.Add("shangwuzuo");
dtTickts.Columns.Add("tedengzuo");
dtTickts.Columns.Add("yidengzuo");
dtTickts.Columns.Add("erdengzuo");
dtTickts.Columns.Add("gaojiruanwo");
dtTickts.Columns.Add("ruanwo");
dtTickts.Columns.Add("yingwo");
dtTickts.Columns.Add("ruanzuo");
dtTickts.Columns.Add("yingzuo");
dtTickts.Columns.Add("wuzuo");
dtTickts.Columns.Add("qita");
dtTickts.Columns.Add("goupiao");
dtTickts.Columns.Add("hideStr");
this.dataGridView1.Height = this.dataGridView1.Height + this.panel3.Height;
dicXiBie.Add("商务座", "9");
dicXiBie.Add("特等座", "p");
dicXiBie.Add("一等座", "M");
dicXiBie.Add("二等座", "O");
dicXiBie.Add("高级软卧", "6");
dicXiBie.Add("软卧", "4");
dicXiBie.Add("硬卧", "3");
dicXiBie.Add("软座", "2");
dicXiBie.Add("硬座", "1");
dicPiaozhong.Add("成人票", "1");
dicPiaozhong.Add("学生票", "2");
dicPiaozhong.Add("儿童票", "3");
}
void cbox_CheckedChanged(object sender, EventArgs e)
{
CheckBox cbox = sender as CheckBox;
if (cbox.Tag.ToString() == "QB")
{
for (int i = 0; i < this.panel2.Controls.Count; i++)
{
CheckBox _cbox = (CheckBox)this.panel2.Controls[i];
_cbox.Checked = cbox.Checked;
}
}
}
private void txtCFD_TextChanged(object sender, EventArgs e)
{
TextChangeEvent(sender);
}
private void txtCFD_Leave(object sender, EventArgs e)
{
if (this.ActiveControl.GetType().Name != "ListBox")
{
this.panel1.Hide();
}
}
private void listBox1_Leave(object sender, EventArgs e)
{
}
private void panel1_Leave(object sender, EventArgs e)
{
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.panel1.Hide();
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
GetCity();
}
private void TextChangeEvent(object sender)
{
TextBox tbox = sender as TextBox;
if (tbox.Text != string.Empty)
{
//查找
this.listBox1.Items.Clear();
string filter = tbox.Text.Trim();
DataRow[] rows = dt.Select("filter like '%" + filter + "%'");
if (rows.Length > 0)
{
for (int i = 0; i < rows.Length; i++)
{
string city = rows[i]["city"].ToString();
string code = rows[i]["code"].ToString();
string space = string.Empty;
int length = 10;
int sLength = length - (city.Length + code.Length);
for (int j = 0; j < sLength; j++)
{
space = space + " ";
}
string str = city + space + code;
this.listBox1.Items.Add(str);
}
}
else
tbox.Tag = null;
Point p1 = tbox.Location;
this.panel1.Location = new Point(p1.X, p1.Y + tbox.Height);
this.panel1.Show();
this.panel1.BringToFront();
}
else
this.panel1.Hide();
}
/// <summary>
/// 获取城市
/// </summary>
private void GetCity()
{
string text = this.listBox1.Text;
string[] list = text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string city = list[0];
string code = list[1];
if (this.panel1.Location.X == txtCFD.Location.X)
{
txtCFD.Text = city;
txtCFD.Tag = code;
}
else
{
txtMDD.Text = city;
txtMDD.Tag = code;
}
LoadCheci();
this.panel1.Hide();
}
//加载验证码
private void GetLoginCode()
{
bool flag = false;
while (!flag)
{
string yzm = GetYZM();
flag = yzm.Length == 4;
}
}
private string GetYZM()
{
string url = "https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=randp";
Stream stream = null;
stream = HttpHelper.GetResponseImage(url);
Image image = Image.FromStream(stream);
this.BeginInvoke((ThreadStart)delegate() { this.picYZM.Image = image; });
Cracker cracker = new Cracker();
var result = cracker.Read((Bitmap)image);
SetYzm(result);
return result;
}
private delegate void WriteYZMDelegate(object yzm);
private void SetYzm(object text)
{
this.txtYZM.Invoke(new WriteYZMDelegate(WriteYZM), text);
}
private void WriteYZM(object text)
{
txtYZM.Text = text.ToString();
}
private void LoadCheci()
{
if (this.IsHandleCreated)
{
this.BeginInvoke((ThreadStart)delegate()
{
if (txtCFD.Tag != null && txtMDD.Tag != null)
{
string url = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=queryststrainall";
string from = txtCFD.Tag.ToString();
string to = txtMDD.Tag.ToString();
string date = dateCFRQ.Value.ToString("yyyy-MM-dd");
string time = cmboxCFSJ.SelectedItem.ToString();
time = System.Web.HttpUtility.UrlEncode(time);
string p = "date={0}&fromstation={1}&tostation={2}&starttime={3}";
p = string.Format(p, date, from, to, time);
ArrayList mylist = new ArrayList();
this.BeginInvoke((ThreadStart)delegate()
{
string checiJson = HttpHelper.GetResponse(url, "POST", p);
if (checiJson != "")
{
myCheci = JsonHelper.DeserializeToObj<List<Checi>>(checiJson);
mylist.Add(new DictionaryEntry("", ""));
for (int i = 0; i < myCheci.Count; i++)
{
string line = myCheci[i].value + "(" + myCheci[i].start_station_name + myCheci[i].start_time + "→" + myCheci[i].end_station_name + myCheci[i].end_time + ")";
string key = myCheci[i].id;
mylist.Add(new DictionaryEntry(key, line));
}
this.BeginInvoke(new MethodInvoker(delegate()
{
this.cmbCFCHECI.DataSource = mylist;
this.cmbCFCHECI.DisplayMember = "name";
this.cmbCFCHECI.ValueMember = "value";
}));
}
});
}
});
}
}
private void txtCFD_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
GetCity();
}
}
private void txtCFD_KeyUp(object sender, KeyEventArgs e)
{
//↑ code 38 ↓ code 40
if (e.KeyValue == 38)
{
if (this.panel1.Visible == true && this.listBox1.Items.Count > 0)
{
int index = this.listBox1.SelectedIndex;
if (index > 0)
{
this.listBox1.SelectedIndex = index - 1;
}
}
}
//↑ code 38 ↓ code 40
if (e.KeyValue == 40)
{
if (this.panel1.Visible == true && this.listBox1.Items.Count > 0)
{
int index = this.listBox1.SelectedIndex;
if (index < this.listBox1.Items.Count - 1)
{
this.listBox1.SelectedIndex = index + 1;
}
}
}
}
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
GetCity();
}
}
private void txtMDD_TextChanged(object sender, EventArgs e)
{
TextChangeEvent(sender);
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void pboxChange_Click(object sender, EventArgs e)
{
string temp = txtMDD.Text;
txtMDD.Text = txtCFD.Text;
txtCFD.Text = temp;
string tag = txtMDD.Tag != null ? txtMDD.Tag.ToString() : "";
txtMDD.Tag = txtCFD.Tag;
txtCFD.Tag = tag;
}
private void OpenInIE()
{
foreach (Cookie cookie in HttpHelper.CookieContainers.GetCookies(new Uri("https://dynamic.12306.cn/otsweb/loginAction.do?method=login")))
{
API.InternetSetCookie("https://" + cookie.Domain.ToString(), cookie.Name.ToString(), cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");
}
Process.Start("IExplore.exe", "https://dynamic.12306.cn/otsweb/");
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenInIE();
}
int querycount = 0;
delegate void BindCallBack(DataGridView grid, DataTable dt);
private void DataGridViewBind(DataGridView grid, DataTable dt)
{
if (grid.InvokeRequired)
{
BindCallBack method = new BindCallBack(this.DataGridViewBind);
base.Invoke(method, new object[]{grid, dt});
}
else
{
grid.DataSource = dt;
}
}
delegate void SetQueryInfoCallBack(Label lb, string txt);
private void SetQueryInfo(Label lb, string txt)
{
if (lb.InvokeRequired)
{
SetQueryInfoCallBack method = new SetQueryInfoCallBack(SetQueryInfo);
base.Invoke(method, new object[] {lb,txt});
}
else
lb.Text = txt;
}
private void BindCheCi()
{
queryThread = new Thread((ThreadStart)delegate()
{
do
{
querycount++;
SetQueryInfo(lbQueryInfo, "第" + querycount + "次查询!");
string[] list = DoQuery();
this.BeginInvoke(new MethodInvoker(delegate()
{
dataGridView1.Rows.Clear();
}));
if (list != null && list.Length > 0)
{
for (int i = 0; i < list.Length; i++)
{
string line = list[i];
if (line.Length > 10)
{
string[] info = line.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
for (int j = 1; j < info.Length - 1; j++)
{
row.Cells[j - 1].Value = info[j];
}
string str = info[info.Length - 1];
string startStr = "getSelected(\'";
string endStr = "\')>";
int startIndex = str.IndexOf(startStr);
int endIndex = str.IndexOf(endStr);
row.Cells[info.Length - 2].Value = "预定";
string hideStr = "";
if (startIndex > -1)
{
hideStr = str.Substring(startIndex + startStr.Length, endIndex - (startIndex + startStr.Length));
}
row.Cells[info.Length - 1].Value = hideStr;
this.BeginInvoke(new MethodInvoker(delegate()
{
this.dataGridView1.Rows.Add(row);
}));
}
}
Thread.Sleep(200);
FormatDG();
Thread.Sleep(5000);
}
}
while (chkAuto.Checked);
});
queryThread.IsBackground=true;
queryThread.Start();
}
private void btnQuery_Click(object sender, EventArgs e)
{
if (chkAuto.Enabled)
{
btnQuery.Enabled = false;
btnStop.Enabled = true;
}
else
btnQuery.Enabled = true;
BindCheCi();
}
delegate void SetValueHandler(Control ctl,string key,string value);
SetValueHandler svh;
private void Set(Control ctl, string key, string value)
{
svh = new SetValueHandler(SetControlValue);
svh.Invoke(ctl,key,value);
svh.Invoke(ctl, key, value);
}
private void SetControlValue(Control ctl,string key, string value)
{
if (ctl.InvokeRequired)
{
ctl.Invoke(svh, new object[] {ctl, key, value });
}
else
{
ctl.Text = value;
}
}
private string[] DoQuery()
{
string from = txtCFD.Tag.ToString();
string to = txtMDD.Tag.ToString();
string checi = string.Empty;
for (int i = 0; i < this.panel2.Controls.Count; i++)
{
CheckBox cbox = (CheckBox)this.panel2.Controls[i];
if (cbox.Checked)
{
checi = checi + cbox.Tag.ToString() + "#";
}
}
string passtype = string.Empty;
if (radioGL.Checked) passtype = radioGL.Tag.ToString();
if (radiooQB.Checked) passtype = radiooQB.Tag.ToString();
if (radioSF.Checked) passtype = radioSF.Tag.ToString();
string train_no = "";
var checiIndex = (int)this.Invoke(new Func<int>(() =>
{
return cmbCFCHECI.SelectedIndex;
}));
if (checiIndex > 0)
{
train_no = (string)this.Invoke(new Func<string>(() =>
{
return cmbCFCHECI.SelectedValue.ToString();
}));
}
TimeSpan ts = dateCFRQ.Value - DateTime.Now;
string[] list = null;
if (ts.Days > 18)
{
MessageBox.Show("您选择的日期不在预售期内!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
string date = dateCFRQ.Value.ToString("yyyy-MM-dd");
list = Query(from, to, date, checi, train_no, passtype);
}
return list;
}
private string[] Query(string from, string to, string date, string checi, string train_no, string passtype)
{
string prarams = string.Empty;
prarams = prarams + "orderRequest.train_date=" + date + "&";
prarams = prarams + "orderRequest.from_station_telecode=" + from + "&orderRequest.to_station_telecode=" + to + "&";
prarams = prarams + "orderRequest.train_no=" + train_no + "&";
checi = System.Web.HttpUtility.UrlEncode(checi);
prarams = prarams + "trainPassType=" + passtype + "&trainClass=" + checi + "&";
string student = "00";
if (chkIsStudent.Checked)
{
student = "0X00";
}
string time = (string)this.Invoke(new Func<string>(() =>
{
return cmboxCFSJ.SelectedItem.ToString();
}));
time = System.Web.HttpUtility.UrlEncode(time);
prarams = prarams + "includeStudent=" + student + "&seatTypeAndNum=&orderRequest.start_time_str=" + time;
string url = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=queryLeftTicket&";
url = url + prarams;
string imgStart = "<img src='/otsweb/images/tips/first.gif'>";
string imgEnd = "<img src='/otsweb/images/tips/last.gif'>";
string txtStart = "[始]";
string txtEnd = "[终]";
string content = Helper.HttpHelper.GetResponse(url, "get", "");
content = content.Replace(imgStart, txtStart).Replace(imgEnd, txtEnd);
content = content.Replace("<a name='btn130_2' class='btn130_2' style='text-decoration:none;' onclick=javascript:", "").Replace("</a>", "");
content = Common.ClearHtml(content);
string[] list = content.Replace(" ", "").Split("\\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
return list;
}
private void dateCFRQ_ValueChanged(object sender, EventArgs e)
{
LoadCheci();
}
private void cmboxCFSJ_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCheci();
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender; //如果是"Link"列,被点击
if (dgv.Columns[e.ColumnIndex].Name == "colCheci")
{
}
if (dgv.Columns[e.ColumnIndex].Name == "colgoupiao")
{
DataGridViewDisableButtonCell cell1 = (DataGridViewDisableButtonCell)dgv[e.ColumnIndex, e.RowIndex];
if (cell1.Value.ToString() == "预定")
{
try
{
queryThread.Abort();
btnQuery.Enabled = true;
btnStop.Enabled = false;
}
catch
{
}
Application.DoEvents();
DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)dgv[e.ColumnIndex + 1, e.RowIndex];
string selectStr = cell.Value.ToString();
string[] selectStr_arr = selectStr.Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var station_train_code = selectStr_arr[0];
myTraincode = station_train_code;
var train_date = dateCFRQ.Value.ToString("yyyy-MM-dd");
myDate = train_date;
var lishi = selectStr_arr[1];
var starttime = selectStr_arr[2];
var trainno = selectStr_arr[3];
myTrainno = trainno;
var from_station_telecode = selectStr_arr[4];
var to_station_telecode = selectStr_arr[5];
var arrive_time = selectStr_arr[6];
var from_station_name = selectStr_arr[7];
myFrom = from_station_name;
var to_station_name = selectStr_arr[8];
myTo = to_station_name;
var from_station_no = selectStr_arr[9];
myFromno = from_station_telecode;
var to_station_no = selectStr_arr[10];
myTono = to_station_telecode;
var ypInfoDetail = selectStr_arr[11];
var mmStr = selectStr_arr[12];
var include_student = chkIsStudent.Checked ? "0X00" : "00";
var locationCode = selectStr_arr[13];
var round_start_time_str = cmboxCFSJ.SelectedItem.ToString();
string checi = string.Empty;
for (int i = 0; i < this.panel2.Controls.Count; i++)
{
CheckBox cbox = (CheckBox)this.panel2.Controls[i];
if (cbox.Checked)
{
checi = checi + cbox.Tag.ToString() + "#";
}
}
string url = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=submutOrderRequest";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("station_train_code", station_train_code);
dic.Add("train_date", train_date);
dic.Add("seattype_num", "");
dic.Add("from_station_telecode", from_station_telecode);
dic.Add("to_station_telecode", to_station_telecode);
dic.Add("include_student", include_student);
dic.Add("from_station_telecode_name", System.Web.HttpUtility.UrlEncode(from_station_name));
dic.Add("to_station_telecode_name", System.Web.HttpUtility.UrlEncode(to_station_name));
dic.Add("round_train_date", System.Web.HttpUtility.UrlEncode(round_start_time_str));
dic.Add("single_round_type", "1");
dic.Add("train_pass_type", "QB");
dic.Add("train_class_arr", System.Web.HttpUtility.UrlEncode(checi));
dic.Add("start_time_str", System.Web.HttpUtility.UrlEncode(round_start_time_str));
dic.Add("lishi", System.Web.HttpUtility.UrlEncode(lishi));
dic.Add("train_start_time", System.Web.HttpUtility.UrlEncode(starttime));
dic.Add("trainno4", trainno);
dic.Add("arrive_time", System.Web.HttpUtility.UrlEncode(arrive_time));
dic.Add("from_station_name", System.Web.HttpUtility.UrlEncode(from_station_name));
dic.Add("to_station_name", System.Web.HttpUtility.UrlEncode(to_station_name));
dic.Add("from_station_no", from_station_no);
dic.Add("to_station_no", to_station_no);
dic.Add("ypInfoDetail", ypInfoDetail);
dic.Add("mmStr", mmStr);
dic.Add("locationCode", locationCode);
string data = "";
foreach (string key in dic.Keys)
{
data = data + key + "=" + dic[key] + "&";
}
data = data.Substring(0, data.Length - 1);
this.BeginInvoke((ThreadStart)delegate()
{
lbCheciInfo.Text = "数据加载中。。。";
string str = HttpHelper.GetResponse(url, "POST", data);
if (!str.Contains("目前您还有未处理的订单"))
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(str);
HtmlNodeCollection node = doc.DocumentNode.SelectNodes("//tr[1]//td");
string checiinfo = "";
for (int i = 0; i < node.Count; i++)
{
string info = node[i].InnerText;
info = info.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
if (i == 2)
{
var temp = info;
myStartTime = temp.Replace(myFrom, "").Replace("开", "").Replace("(", "").Replace(")", "").Trim();
}
if (i == 3)
{
var temp = info;
myEndTime = temp.Replace(myTo, "").Replace("到", "").Replace("(", "").Replace(")", "").Trim();
}
checiinfo = checiinfo + info + " ";
}
lbCheciInfo.Text = checiinfo;
string _piaojia = doc.DocumentNode.SelectNodes("//tr[2]")[0].InnerText.Replace("\r", " ").Replace("\n", " ").Replace("\t", " ").Replace(" ", " ");
string[] _piaojiaList = _piaojia.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < _piaojiaList.Length; i++)
{
string info = _piaojiaList[i];
piaojia = piaojia + info + " ";
}
lbPiaojia.Text = piaojia;
this.dataGridView1.Height = this.dataGridView1.Height = this.panel3.Height;
this.panel3.Show();
// 更新 TOKEN
url = "https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=init";
data = string.Empty;
string html = HttpHelper.GetResponse(url, "POST", data);
string temp1 = html;
temp1 = temp1.Substring(temp1.IndexOf("name=\"org.apache.struts.taglib.html.TOKEN\" value=\"") + 50);
token = temp1.Substring(0, temp1.IndexOf('"')); // 获取令牌
temp1 = html;
int index = temp1.IndexOf("id=\"left_ticket\"");
temp1 = temp1.Substring(temp1.IndexOf("id=\"left_ticket\"") + 26);
leftTicketStr = temp1.Substring(0, temp1.IndexOf('"'));
}
else
MessageBox.Show("目前您还有未处理的订单,请先处理订单!");
});
}
}
}
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
}
private void LoadPasssenger()
{
BackgroundWorker Bw = new BackgroundWorker();
Bw.WorkerSupportsCancellation = true;
Bw.WorkerReportsProgress = true;
Bw.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
{
string url = "https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=getpassengerJson";
string content = HttpHelper.GetResponse(url, "GET", "");
pList = JsonHelper.DeserializeToObj<PassengerList>(content);
int controlX = 6;
int controlY = 6;
int i = 0;
foreach (Passenger key in pList.passengerJson)
{
CheckBox cbox = new CheckBox();
cbox.Checked = false;
cbox.Text = key.passenger_name;
cbox.Tag = key;
cbox.Location = new Point(controlX, controlY);
controlX += 70;
cbox.Size = new Size(70, 16);
cbox.CheckedChanged += new EventHandler(cboxChange);
cbox.Name = "cbox" + i;
i++;
this.panleUSER.Controls.Add(cbox);
}
});
Bw.RunWorkerAsync();
}
void cboxChange(object sender, EventArgs e)
{
string selected = "";
myList = new List<Passenger>();
int count = 0;
for (int i = 0; i < this.panleUSER.Controls.Count; i++)
{
CheckBox cb = this.panleUSER.Controls[i] as CheckBox;
if (cb.Checked)
{
count++;
if (count > 5)
{
MessageBox.Show("对不起,你最多只能选择5位联系人!");
selected = selected.Replace(cb.Text + " ", "");
myList.Remove(cb.Tag as Passenger);
cb.Checked = false;
break;
}
else
{
selected = selected + cb.Text + " ";
myList.Add(cb.Tag as Passenger);
}
}
else
{
selected = selected.Replace(cb.Text + " ", "");
myList.Remove(cb.Tag as Passenger);
}
}
Goupiao(myList);
}
private void FormatDG()
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
bool canOrder = false;
DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell)dataGridView1[this.dataGridView1.ColumnCount-2, i];
DataGridViewLinkCell cellCheci = (DataGridViewLinkCell)dataGridView1[0, i];
for (int j = 4; j < this.dataGridView1.ColumnCount - 2; j++)
{
string value = dataGridView1.Rows[i].Cells[j].Value.ToString();
if (Common.IsNumeric(value) || value.IndexOf("有") > -1)
{
//dataGridView1.Rows[i].Cells[j].Style.BackColor = System.Drawing.Color.Red;
canOrder = true;
}
else
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = System.Drawing.Color.Gray;
continue;
}
}
if (canOrder)
{
cell.Tag = cellCheci.Value;
cell.Enabled = true;
cell.Style.ForeColor = System.Drawing.Color.Red;
}
else
{
cell.Enabled = false;
cell.Value = "无票";
}
}
}
private void main_Load(object sender, EventArgs e)
{
GetLoginCode();
LoadPasssenger();
}
private void picYZM_Click(object sender, EventArgs e)
{
GetLoginCode();
}
public void Goupiao(List<Passenger> list)
{
//1.绑定席别
DataTable dtxibie = new DataTable();
dtxibie.Columns.Add("xibie");
dtxibie.Columns.Add("code");
string[] piaojiaList = piaojia.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < piaojiaList.Length; i++)
{
string str = piaojiaList[i];
int start = str.IndexOf("(");
string xibie = str.Substring(0, start);
int end = str.IndexOf(")");
string piaoj = str.Substring(end + 1);
if (piaoj.IndexOf("张票") > -1 || piaoj.IndexOf("有") > -1)
{
xibie = xibie == "无座" ? "硬座" : xibie;
DataRow row = dtxibie.NewRow();
row["xibie"] = xibie;
row["code"] = dicXiBie[xibie];
dtxibie.Rows.Add(row);
}
}
if (dtxibie.Rows.Count > 0)
{
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).DataSource = dtxibie;
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).ValueMember = "code";
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).DisplayMember = "xibie";
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).DataPropertyName = "xibie";
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).DefaultCellStyle.NullValue = dtxibie.Rows[0]["xibie"];
((DataGridViewComboBoxColumn)dataGridView2.Columns["colxiebie"]).DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
}
DataTable dtPiaozhong = new DataTable();
dtPiaozhong.Columns.Add("name");
dtPiaozhong.Columns.Add("value");
foreach (string key in dicPiaozhong.Keys)
{
DataRow row = dtPiaozhong.NewRow();
row["name"] = key;
row["value"] = dicPiaozhong[key];
dtPiaozhong.Rows.Add(row);
}
((DataGridViewComboBoxColumn)dataGridView2.Columns["colpiaozhong"]).DataSource = dtPiaozhong;
((DataGridViewComboBoxColumn)dataGridView2.Columns["colpiaozhong"]).ValueMember = "value";
((DataGridViewComboBoxColumn)dataGridView2.Columns["colpiaozhong"]).DisplayMember = "name";
((DataGridViewComboBoxColumn)dataGridView2.Columns["colpiaozhong"]).DefaultCellStyle.NullValue = dtPiaozhong.Rows[0]["name"];
((DataGridViewComboBoxColumn)dataGridView2.Columns["colpiaozhong"]).DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
//绑定
DataTable dtGoupiao = new DataTable();
dtGoupiao.Columns.Add("xh");
dtGoupiao.Columns.Add("xm");
dtGoupiao.Columns.Add("zjhm");
dtGoupiao.Columns.Add("sjhm");
dtGoupiao.Columns.Add("zjlx");
dtGoupiao.Columns.Add("del");
for (int i = 0; i < list.Count; i++)
{
DataRow row = dtGoupiao.NewRow();
string xm = list[i].passenger_name;
string xh = "第" + (i + 1) + "位";
string zjhm = list[i].passenger_id_no;
string zjlx = "二代身份证";
string sjhm = list[i].mobile_no;
row["xh"] = xh;
row["xm"] = xm;
row["zjhm"] = zjhm;
row["zjlx"] = zjlx;
row["sjhm"] = sjhm;
row["del"] = "删除";
dtGoupiao.Rows.Add(row);
}
this.dataGridView2.DataSource = dtGoupiao;
this.dataGridView2.EditMode = DataGridViewEditMode.EditOnEnter;
}
private void btnOrder_Click(object sender, EventArgs e)
{
//循环获取乘客信息
GetLoginCode();
string pssengerstr = string.Empty;
if (this.dataGridView2.Rows.Count > 0)
{
for (int i = 0; i < this.dataGridView2.Rows.Count; i++)
{
string xiebie = this.dataGridView2.Rows[i].Cells[0].FormattedValue.ToString();
xiebie = dicXiBie[xiebie];
string seat = this.dataGridView2.Rows[i].Cells[1].FormattedValue.ToString();
seat = dicPiaozhong[seat];
string xm = this.dataGridView2.Rows[i].Cells["colxm"].Value.ToString();
string zjhm = this.dataGridView2.Rows[i].Cells["colzjhm"].Value.ToString();
string sjhm = this.dataGridView2.Rows[i].Cells["colsjhm"].Value.ToString();
string data = "passengerTickets=" + System.Web.HttpUtility.UrlEncode(xiebie + ",undefined," + seat + "," + xm + ",1," + zjhm + "," + sjhm + ",Y");
data += "&oldPassengers=" + System.Web.HttpUtility.UrlEncode(xm + ",1," + zjhm);
data += "&passenger_" + (i + 1) + "_seat=" + System.Web.HttpUtility.UrlEncode(xiebie); // 1硬座/3硬卧/4软卧
data += "&passenger_" + (i + 1) + "_ticket=" + System.Web.HttpUtility.UrlEncode(seat);
data += "&passenger_" + (i + 1) + "_name=" + System.Web.HttpUtility.UrlEncode(xm);
data += "&passenger_" + (i + 1) + "_cardtype=1";
data += "&passenger_" + (i + 1) + "_cardno=" + zjhm;
data += "&passenger_" + (i + 1) + "_mobileno=" + sjhm;
data += "&checkbox9=Y";
pssengerstr = pssengerstr + data + "&";
}
int leftcount = 5 - this.dataGridView2.Rows.Count;
for (int i = 0; i < leftcount; i++)
{
pssengerstr = pssengerstr + "oldPassengers=" + "&checkbox9=Y&";
}
pssengerstr = pssengerstr.Substring(0, pssengerstr.Length - 1);
string html = Order(myDate, myTrainno, myTraincode, myFromno, myFrom, myTono, myTo, myStartTime, myEndTime, pssengerstr);
//string result = html.Split('"')[15];
if (html.Contains("对不起,由于您取消次数过多"))
{
Output("对不起,由于您取消次数过多,今日将不能继续受理您的订票请求,程序结束");
GetLoginCode();
}
else if (html.Contains("输入的验证码不正确") || html.Contains("验证码 必须输入"))
{
Output("订单验证码比对失败,重新加载");
GetLoginCode();
}
else
{
new Thread(new ThreadStart(Buy)).Start();
}
}
else
MessageBox.Show("请至少选择一名乘客信息!");
}
public void Buy() // 购票
{
//查询余票
string url = "https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=getQueueCount";
string xibie = this.dataGridView2.Rows[0].Cells[0].FormattedValue.ToString();
xibie = dicXiBie[xibie];
string data = "train_date=" + myDate;
data += "&train_no=" + myTrainno;
data += "&station=" + myTraincode;
data += "&seat=" + xibie;
data += "&from=" + myFromno;
data += "&to=" + myTono;
data += "&ticket=" + leftTicketStr;
string html = HttpHelper.GetResponse(url, "Post", data);
Output("购票");