-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadGetUserToken.pas
More file actions
233 lines (175 loc) · 5.53 KB
/
Copy pathThreadGetUserToken.pas
File metadata and controls
233 lines (175 loc) · 5.53 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
unit ThreadGetUserToken;
interface
uses
ActiveX, Classes, SysUtils, ShlObj, Windows, XMLDom, XMLIntf, MSXMLDom, XMLDoc,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Forms,
StrUtils, HTTPApp, Variants;
type
ThreadUserTockenGet = class(TThread)
private
{ Private declarations }
protected
procedure InitIndyObject;
procedure FreeIndyObject;
procedure Execute; override;
procedure APIGetUserTocken();//获取用户TOKEN
procedure ShowAlertMsgForm();//调用显示消息窗体
var
IdHttpMain:TIdHTTP; //HTTP RFC
XmlDocumentMain:TXMLDocument; //XML对象
end;
implementation
uses MD5, FunctionCommon, SystemConfig;
{
Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure ThreadGetUserTocken.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
or
Synchronize(
procedure
begin
Form1.Caption := 'Updated in thread via an anonymous method'
end
)
);
where an anonymous method is passed.
Similarly, the developer can call the Queue method with similar parameters as
above, instead passing another TThread class as the first parameter, putting
the calling thread in a queue with the other thread.
}
{ ThreadGetUserTocken }
procedure ThreadUserTockenGet.APIGetUserTocken;
var
Param:TStringList;
RStream:TStringStream;
XmlUserInfo: string;
Node, mNode:IXMLNode;
UserSign: RawByteString;
iMsgAt, iMsgNewPrice, iMsgShopGoods: integer;
begin
InitIndyObject;//初始化HTTP对象
//XML
XMLDocumentMain:=TXMLDocument.Create(Application);
XMLDocumentMain.Active:=False;
XmlUserInfo:='';
//URL参数
Param:=TStringList.Create;
RStream:=TStringStream.Create('',TEncoding.UTF8); //UTF-8
//中文处理
DebugTrace('签名:=', API_USER_SIGN);
UserSign:=UTF8Encode(API_USER_SIGN);
SetCodePage(UserSign, 0, False);//重要注册处理
UserSign:=StrToMD5(UserSign, 9);
DebugTrace('签名MD5:=', UserSign);
//提交字段
Param.Add('app_id=' + IntToStr(API_ID));
Param.Add('uid='+ IntToStr(USER_ID));
Param.Add('username=' + USER_NAME);
Param.Add('login_key=' + USER_LOGIN_KEY);
Param.Add('sign=' + UserSign);
try
try
IdHttpMain.Post(API_URL_USERINFO, Param, RStream);
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
XmlUserInfo:= RStream.DataString;
end;
FreeIndyObject; //释放HTTP对象
FreeAndNil(Param);
FreeAndNil(RStream);
//获取用户Token
if (XmlUserInfo <> '') then begin
DebugTrace('获取用户TOKEN接口', XmlUserInfo);
XmlDocumentMain.XML.Text:= XmlUserInfo;
try
try
XmlDocumentMain.Active:= True;
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
try
Node:=XmlDocumentMain.DocumentElement.ChildNodes.Get(4);
mNode:= Node;
Node:=Node.ChildNodes.Get(0);
except on E: Exception do begin
Log(E.Message);
end;
end;
API_USER_TOCKEN:= Node.NodeValue;
{
<new><!—用户新提醒-->
<new_at></new_at><!—新@->
<new_msg></new_msg><!—新私信->
<new_beloved></new_beloved><!—新被喜欢-->
<new_reply></new_reply><!—新评论-->
<new_fans></new_fans><!—新粉丝-->
</ new >
}
//获取用户消息数量
try
mNode:= mNode.ChildNodes.Get(4);
Node:= mNode;
mNode:=Node.ChildNodes.Get(0);
iMsgAt:= StrToInt(mNode.NodeValue);
mNode:=Node.ChildNodes.Get(1);
iMsgNewPrice:= StrToInt(mNode.NodeValue);
mNode:=Node.ChildNodes.Get(2);
iMsgShopGoods:= StrToInt(mNode.NodeValue);
except on E: Exception do begin
Log(E.Message);
end;
end;
//匹配设置需要报告的数量
if (CONF_DISCOUNT_MSG = 0) then iMsgNewPrice:=0;
if (CONF_AT_MSG = 0) then iMsgAt:=0;
if (CONF_MALL_MSG = 0) then iMsgShopGoods:=0;
API_USER_MSG_COUNT:= iMsgAt + iMsgNewPrice + iMsgShopGoods;
DebugTrace('最终设置需要提醒的消息数量=', IntToStr(API_USER_MSG_COUNT));
end;
end;
//释放XML对象
FreeAndNil(XmlDocumentMain);
end;
{*******************************************************************************}
procedure ThreadUserTockenGet.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=True;
APIGetUserTocken;
if API_USER_MSG_COUNT > 0 then
Synchronize(ShowAlertMsgForm); //线程同步
end;
{*******************************************************************************}
procedure ThreadUserTockenGet.FreeIndyObject;
begin
FreeAndNil(IdHttpMain);
end;
{*******************************************************************************}
procedure ThreadUserTockenGet.InitIndyObject;
begin
//动态签名
API_USER_SIGN:='app_id' + IntToStr(API_ID) + 'login_key' + USER_LOGIN_KEY
+ 'uid' + IntToStr(USER_ID) + 'username' + USER_NAME
+ API_KEY;
IdHttpMain := TIdHTTP.Create(nil);
IdHttpMain.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 AlexaToolbar/alxg-3.1';
IdHttpMain.HandleRedirects := True;
IdHttpMain.ReadTimeout := 5000;
end;
{*******************************************************************************}
procedure ThreadUserTockenGet.ShowAlertMsgForm;
begin
ShowFormAlertMessage;
end;
{*******************************************************************************}
end.