function GetAccessToken(http: TIdHTTP; var token: string; var jgTime: DWORD): Boolean;
var
i,index: integer;
RetStr,access_token,expires_in: string;
begin
Result:=False;
RetStr:=http.Get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wxc2f5f2ccb6eec512&corpsecret=069ErVSyom2Yp375azN4grsOfFMAiRM_sGXWP3-qCv6-3q0K96zJwfNfo_iTgZQZ');
RetStr := UTF8Decode(RetStr);
access_token:='';
index:=Pos('access_token',RetStr);
if index>0 then
begin
index:=index+Length('access_token')+3;
for i:=index to Length(RetStr) do
if RetStr[i]<>'"' then
access_token:=access_token+RetStr[i]
else
begin
token:=access_token;
Break;
end;
if access_token<>'' then
begin
expires_in:='';
index:=Pos('expires_in',RetStr);
if index>0 then
begin
index:=index+Length('expires_in')+2;
for i:=index to Length(RetStr) do
if RetStr[i] in['0'..'9'] then
expires_in:=expires_in+RetStr[i]
else
begin
if expires_in<>'' then
begin
jgTime:=StrToInt(expires_in);
Result:=True;
end;
Break;
end;
end;
end;
end;
end;
if not GetAccessToken(http,token,jgTime) then
begin
token:='';
jgTime:=0;
end;
LastGetTokenTick:=GetTickCount;
while not isThreadExit do
begin
lpNotifyWxNode:=nil;
try
EnterCriticalSection(NotifyWxListSection);
try
if NotifyWxList.Count>0 then
begin
lpNotifyWxNode:=NotifyWxList.Items[0];
NotifyWxList.Delete(0);
end;
finally
LeaveCriticalSection(NotifyWxListSection);
end;
if lpNotifyWxNode=nil then
Sleep(1000)
else
begin
if (token='')or(GetMilSecond(LastGetTokenTick,GetTickCount)>(jgTime*1000)) then
begin
if not GetAccessToken(http,token,jgTime) then
begin
token:='';
jgTime:=0;
end;
LastGetTokenTick:=GetTickCount;
end;
ASource:='{"touser": "","toparty": "'+lpNotifyWxNode^.AryDeptID+'","totag": "",';
ASource:=ASource+'"msgtype": "text","agentid": 1,"text": {"content": "'+UTF8Encode(lpNotifyWxNode^.Content)+'"},"safe":0}';
sndStrings.Clear;
sndStrings.Add(ASource);
Dispose(lpNotifyWxNode);
if token<>'' then
http.Post('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+token,sndStrings);
end;
except