-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
247 lines (209 loc) · 8.57 KB
/
FormMain.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
using creditcard.Util;
namespace creditcard
{
public partial class FormMain : Form
{
string tag = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName;
FormProgressDialog mFormProgressDialog = null;
Context mContext = new Context();
List<GoodList> mList = null;
System.Threading.Timer timer = null;
string selectedValue = "";
int count_add_new = 0;
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
mFormProgressDialog = new FormProgressDialog();
initData();
initEvent();
}
private void initData()
{
comboBox1.DisplayMember = "type_name";
comboBox1.ValueMember = "type_id";
comboBox1.DataSource = DBHelper.GetDataSet("SELECT * FROM tb_good_type");
}
private void initEvent()
{
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (timer != null)
{
timer.Dispose();
timer = null;
}
log("webBrowser1_DocumentCompleted");
// MessageBox.Show(webBrowser1.DocumentText);
string html = webBrowser1.Document.Body.OuterHtml;
richTextBox1.Text = html;
}
private void saveData(object html){
mContext.Html = html.ToString();
this.BeginInvoke(new UIHandler1(showProgressDialog), new string[] { string.Format("正在解析数据") });
mList = mContext.parse();
if (mList == null || mList.Count <= 0) return;
log("showData");
count_add_new = 0;
this.BeginInvoke(new UIHandler1(setProgressDialogMsg), new string[] { string.Format("正在插入数据") });
foreach (GoodList good in mList)
{
string queryString = string.Format("SELECT good_id FROM tb_good WHERE good_name = '{0}' and good_no = '{1}'", good.Title,good.No);
DataTable dt = DBHelper.GetDataSet(queryString);
if (dt.Rows.Count > 0)
{
continue;
}
++count_add_new;
add(good);
}
this.Invoke(new UIHandler1(closeProgressDialog), new string[] { string.Format("数据录入完毕,新增{0}条数据",count_add_new)});
Console.WriteLine(string.Format("{0} in total", mList.Count));
}
public delegate void UIHandler1(string msg);
public delegate void UIHandler2(int total,int current);
void showProgressDialog(string msg){
log("showProgressDialog"+msg);
lblState.Text = msg;
if (mFormProgressDialog == null)
{
mFormProgressDialog = new FormProgressDialog();
}
mFormProgressDialog.Title = "提示";
mFormProgressDialog.Message = msg;
mFormProgressDialog.StartPosition = FormStartPosition.Manual;
mFormProgressDialog.Location = new Point(this.Location.X + this.Width / 2 - mFormProgressDialog.Width / 2, this.Location.Y + this.Height / 2 + mFormProgressDialog.Height/2);
mFormProgressDialog.ShowDialog();
}
void setProgressDialogMsg(string msg)
{
log("showProgressDialog" + msg);
lblState.Text = msg;
if (mFormProgressDialog == null)
{
return;
}
mFormProgressDialog.Title = "提示";
mFormProgressDialog.Message = msg;
mFormProgressDialog.StartPosition = FormStartPosition.Manual;
mFormProgressDialog.Location = new Point(this.Location.X + this.Width / 2 - mFormProgressDialog.Width / 2, this.Location.Y + this.Height / 2 + mFormProgressDialog.Height / 2);
}
void closeProgressDialog(string msg) {
if (timer != null)
{
timer.Dispose();
timer = null;
}
if (mFormProgressDialog != null)
{
mFormProgressDialog.Message = msg;
mFormProgressDialog.Close();
mFormProgressDialog = null;
}
log("closeProgressDialog " + msg);
lblState.Text = msg ;
}
void timeout(object obj)
{
this.Invoke(new UIHandler1(closeProgressDialog), new string[] { obj.ToString()});
}
private void add(GoodList good) {
good.Typeid = selectedValue;
string executeString = string.Format("INSERT INTO `tb_good`(good_name,good_integral,good_no,good_imgurl,good_detailedurl,good_typeid,good_cash) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', {5},'{6}')",
good.Title, good.Integral, good.No, good.ImgUrl, good.DetailedUrl, good.Typeid, good.Cash);
DBHelper.ExecuteCommand(executeString);
}
private void button1_Click(object sender, EventArgs e)
{
log("Navigate");
selectedValue = comboBox1.SelectedValue.ToString();
switch (Convert.ToInt32(comboBox1.SelectedValue.ToString()))
{
case 1:
mContext.Parser = new PingAnHtmlParser();
webBrowser1.Navigate(CommonHttp.URL_PING_AN);
break;
case 2:
mContext.Parser = new ABCHtmlParser();
webBrowser1.Navigate(CommonHttp.URL_ABC);
break;
case 3:
mContext.Parser = new BOCHtmlParser();
webBrowser1.Navigate(CommonHttp.URL_BOC);
break;
case 4:
mContext.Parser = new CMBCHtmlParser();
webBrowser1.Navigate(CommonHttp.URL_CMBC);
break;
default:
return;
}
showProgressDialog("正在载入页面,请稍候...");
if (timer == null)
{
timer = new System.Threading.Timer(new TimerCallback(timeout), "请求已超时", 5000, 1000);
}
}
void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
log("webBrowser1_Navigating");
mFormProgressDialog.Message = "正在载入中,请稍后...";
}
void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
log("webBrowser1_Navigated");
closeProgressDialog("页面载入完毕!");
txtSearch.Text = e.Url.ToString();
}
void log(String msg)
{
Console.WriteLine(tag+"|"+msg);
}
private void button2_Click(object sender, EventArgs e)
{
string html = "";
HtmlDocument obj =webBrowser1.Document;
if (obj == null)
{
MessageBox.Show("数据为空,请先载入页面");
return;
}
html = obj.Body.OuterHtml;
richTextBox1.Text = html;
Thread thread = new Thread(new ParameterizedThreadStart(saveData));
thread.Start(html);
}
private void button3_Click(object sender, EventArgs e)
{
/*Context context = new Context();
string html = webBrowser1.Document.Body.OuterHtml;
richTextBox1.Text = html;
context.Html = html;
context.Parser = new BOCHtmlParser();
context.parse();*/
}
private void btSearch_Click(object sender, EventArgs e)
{
if (txtSearch.Text.Trim().Equals("")) {
MessageBox.Show("地址栏不能为空!");
return;
}
webBrowser1.Navigate(txtSearch.Text.Trim());
}
}
}