ThirdMessageController.class.php
5.94 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
<?php
/**
* 由Uc中转的三方消息回调
* User: zhuxun37
* Date: 16/8/11
* Time: 下午3:41
*/
namespace Frontend\Controller\Callback;
use Common\Common\Cache;
use Common\Common\Msg;
use Common\Common\User;
use Common\Service\InviteUserService;
use Think\Log;
use VcySDK\Config;
class ThirdMessageController extends AbstractController
{
public function before_action($action = '')
{
if (! parent::before_action($action)) {
return false;
}
// 微信消息 Json 解析
$this->callBackData['wxMessage'] = json_decode($this->callBackData['wxMessage'], true);
Log::record(sprintf('---%s %s ThirdMessage START---', QY_DOMAIN, APP_IDENTIFIER), Log::INFO);
Log::record(var_export($this->callBackData, true), Log::INFO);
Log::record(sprintf('---%s %s ThirdMessage END ---', QY_DOMAIN, APP_IDENTIFIER), Log::INFO);
return true;
}
/**
* 三方消息回调
*
* wxMessage 为第三方消息内容, $this->callBackData 格式如下:
* {
* "msg":"成功",
* "code":"SUCCESS",
* "wxMessage": {
* "agentId":114,
* "toUserName":"wx276cb16432f0bdd9",
* "fromUserName":"zhuxun37",
* "createTime":1470972131,
* "msgType":"text",
* "content":"讲话稿",
* "msgId":4617283198342698634
* },
* "memUid":"78D547B77F00000162CAAD4E526287C7",
* "epEnumber":"t5thr"
* }
*
* @return bool
*/
public function Index()
{
switch ($this->callBackData['wxMessage']['msgType']) {
case cfg('UC_CALLBACK_MSG_TYPE_TEXT'):
$this->search();
break;
case cfg('UC_CALLBACK_MSG_TYPE_SUBSCRIBE'):
$this->notice();
break;
case cfg('UC_CALLBACK_MSG_TYPE_UNSUBSCRIBE'):
$this->deleteUser();
break;
}
$this->clearCache();
exit('SUCCESS');
}
/**
* 搜索人员
* @author zhonglei
* @return bool
*/
private function search()
{
$uid = $this->callBackData['memUid'];
$content = trim($this->callBackData['wxMessage']['content']);
if (empty($content) || strlen($content) > 20) {
return false;
}
$userServ = new User();
$result = $userServ->listByConds(['memUsername' => $content]);
$total = $result['total'];
if ($total == 0) {
$text = '没有找到相关联系人';
} elseif ($total == 1) {
$memUid = $result['list'][0]['memUid'];
$user = $userServ->getByUid($memUid);
$deptname = $user['dpName'] ? $user['dpName'][0]['dpName'] : '';
$text = "姓名:{$user['memUsername']}\n部门:{$deptname}";
if ($user['memMobile']) {
$text .= "\n手机:{$user['memMobile']}";
}
if ($user['memEmail']) {
$text .= "\n邮箱:{$user['memEmail']}";
}
$url = frontUrl('/app/page/contacts/member-detail', ['uid' => $memUid]);
$link = "<a href=\"{$url}\">点击查看详情</a>";
$text .= "\n{$link}";
} else {
$url = frontUrl('/app/page/contacts/member-list', ['kw' => $content]);
$link = "<a href=\"{$url}\">点击查看详情</a>";
$text = "找到{$total}个联系人\n{$link}";
}
// 消息发至管理助手
$app = Config::instance()->config['pluginIdentifier'];
Config::instance()->setConfig(['pluginIdentifier' => 'yqassistant']);
$msgServ = new Msg();
$msgServ->sendText($uid, [], $text);
// 改回应用标识
Config::instance()->setConfig(['pluginIdentifier' => $app]);
return true;
}
/**
* 关注通知
* @author zhonglei
* @return bool
*/
private function notice()
{
$uid = $this->callBackData['memUid'];
// 获取邀请数据
$inviteUserServ = new InviteUserService();
$invite = $inviteUserServ->get_by_conds([
'uid' => $uid,
'is_notice' => InviteUserService::INVITER_IS_NOTICE_FALSE
]);
if ($invite) {
$userServ = new User();
$user = $userServ->getByUid($uid);
// 消息发至管理助手
$app = Config::instance()->config['pluginIdentifier'];
Config::instance()->setConfig(['pluginIdentifier' => 'yqassistant']);
// 发送消息
$msgServ = new Msg();
$msgServ->sendNews($invite['invite_uid'], '', [
[
'title' => "{$user['memUsername']}已经成功加入企业号",
'description' => "姓名:{$user['memUsername']}\n微信号:{$user['memWeixin']}\n职位:{$user['jobName']}",
'url' => frontUrl('/app/page/contacts/myinvite-audit', ['list_type' => InviteUserService::MY_INVITE_LIST]),
]
]);
// 改回应用标识
Config::instance()->setConfig(['pluginIdentifier' => $app]);
// 更新邀请数据
$inviteUserServ->update_by_conds(['invite_id' => $invite['invite_id']], ['is_notice' => InviteUserService::INVITER_IS_NOTICE_TRUE]);
}
}
/**
* 删除人员
* @author zhonglei
* @return void
*/
private function deleteUser()
{
$uid = $this->callBackData['memUid'];
$userServ = new User();
$userServ->clearUserCache($uid);
}
/**
* 清理缓存应用权限缓存
*
* @return bool
*/
protected function clearCache()
{
$cache = &Cache::instance();
// 人员关注事件
if ($this->callBackData['wxMessage']['msgType'] == cfg('UC_CALLBACK_MSG_TYPE_SUBSCRIBE')) {
// 清除应用权限信息
$cache->set('Common.Jurisdiction', null);
}
return true;
}
}