Corp.php
5.1 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
<?php
/**
* Corp.php
*
* @Author: Ghaoo
* @Date 2020/7/27 0027
*/
namespace Weasy\Core;
use addons\qyexternal\model\Corp as Qywx;
use addons\qyexternal\model\FollowUser;
use addons\qyexternal\model\UserBehavior;
use think\Request;
use Weasy\External\Api\CorpAPI;
use Weasy\External\Callback\WXBizMsgCrypt;
use Weasy\External\Utils\Utils;
class Corp
{
public $corp;
/**
* 创建Corp实例.
* @param int $id
* @param string $corp_id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function __construct($id, $corp_id = "")
{
if ($id > 0) {
$qywx = Qywx::where("id", $id)->find();
} else {
$qywx = Qywx::where("corp_id", $corp_id)->find();
}
if (!$qywx) {
throw new \Exception("企业微信实例未找到,ID: $id, CorpId: $corp_id");
}
$this->corp = $qywx;
}
public function sync($type = "")
{
$sync = new Sync($this->corp);
switch ($type) {
case "sync_follow_user_list":
$sync->FollowUserList();
break;
case "sync_contacts_list":
//$sync->ContactsList();
break;
case "sync_group_chat_list":
$sync->GroupChatList();
break;
case "sync_user_behavior_data":
$this->syncUserBehaviorData($sync);
break;
default:
$sync->FollowUserList();
//$sync->ContactsList();
$sync->GroupChatList();
$this->syncUserBehaviorData($sync);
}
return true;
}
public function getGroupMembers($chat_id)
{
$api = new CorpAPI($this->corp->corp_id, $this->corp->external_secret);
$groupMembers = $api->ExternalContactGroupChatGet($chat_id);
return $groupMembers["member_list"];
}
protected function syncUserBehaviorData(Sync $sync)
{
$days = 30;
$follow = FollowUser::where("corp_id", $this->corp->corp_id)->select();
$lastData = UserBehavior::where("corp_id", $this->corp->corp_id)->order("date", "desc")->find();
// 存在同步数据
if ($lastData) {
$start = $lastData->date;
$end = strtotime(date("Y-m-d", strtotime("-1 day"))); // 企业微信数据统计只统计到前一天
$days = intval(ceil(($end - $start)/84600));
// 是否超过30天
if ($days <= 30) {
// 是否需要同步
if ($start < $end) {
foreach ($follow as $user) {
$sync->UserBehaviorData($user->userid, $start, $end);
}
}
return;
}
}
$start_time = strtotime(date("Y-m-d", strtotime("-$days day")));
$end_time = strtotime(date("Y-m-d", strtotime("-1 day")));
foreach ($follow as $user) {
$sync->UserBehaviorData($user->userid, $start_time, $end_time);
}
}
public function go(Request $request)
{
if ($request->isGet() && isset($_GET["echostr"])) {
$echoStr = $this->verifyUrl();
if (!$echoStr) {
throw new \Exception("验证失败");
}
echo $echoStr;
exit();
}
if ($request->isPost()) {
if (!isset($_GET["msg_signature"]) || !isset($_GET["timestamp"]) || !isset($_GET["nonce"])) {
return false;
}
$sReqMsgSig = $_GET["msg_signature"];
$sReqTimeStamp = $_GET["timestamp"];
$sReqNonce = $_GET["nonce"];
$sReqData = $request->getContent();
$sMsg = ""; // 解析之后的明文
$wxcpt = new WXBizMsgCrypt($this->corp->token, $this->corp->key, $this->corp->corp_id);
$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg);
if ($errCode == 0) {
$msg = Utils::Xml2Array($sMsg);
if (isset($msg["MsgType"]) && $msg["MsgType"] == "event") {
new Payload($msg);
}
} else {
return new \Exception("解析消息失败");
}
}
}
protected function verifyUrl() {
if (!isset($_GET["msg_signature"]) || !isset($_GET["timestamp"]) || !isset($_GET["nonce"]) || !isset($_GET["echostr"])) {
return false;
}
$sVerifyMsgSig = $_GET["msg_signature"];
$sVerifyTimeStamp = $_GET["timestamp"];
$sVerifyNonce = $_GET["nonce"];
$sVerifyEchoStr = $_GET["echostr"];
$sEchoStr = "";
$wxcpt = new WXBizMsgCrypt($this->corp->token, $this->corp->key, $this->corp->corp_id);
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
return $sEchoStr;
} else {
return false;
}
}
}