Wechat.php
2.6 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
<?php
namespace addons\shopro\job;
use think\queue\Job;
/**
* 微信任务
*/
class Wechat extends BaseJob
{
/**
* 异步分批创建新的队列任务
*/
public function createQueueByOpenIdsArray(Job $job, $openIdsArray)
{
try {
$count = count($openIdsArray);
if ($count > 0) {
$page = ceil($count / 100);
for ($i = 0; $i < $page; $i++) {
\think\Queue::push('\addons\shopro\job\Wechat@saveSubscribeUserInfo', array_slice($openIdsArray, $i * 100, 100), 'ant');
}
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-createQueueByOpenIdsArray' . ':执行失败,错误信息:' . $e->getMessage());
}
}
/**
* 保存更新关注用户
*/
public function saveSubscribeUserInfo(Job $job, $openIdsArray)
{
try {
$wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
$result = $wechat->getSubscribeUserInfoByOpenId($openIdsArray);
if (isset($result['user_info_list'])) {
$userInfoList = $result['user_info_list'];
$insertData = [];
foreach ($userInfoList as $u) {
$wechatFans = \app\admin\model\shopro\wechat\Fans::get(['openid' => $u['openid']]);
if ($wechatFans) {
$wechatFans->save([
'nickname' => $u['nickname'],
'headimgurl' => $u['headimgurl'],
'sex' => $u['sex'],
'country' => $u['country'],
'province' => $u['province'],
'city' => $u['city'],
'subscribe' => 1,
'subscribe_time' => $u['subscribe_time']
]);
}else{
$insertData[] = $u;
}
}
if (count($insertData) > 0) {
$wechatFans = new \app\admin\model\shopro\wechat\Fans;
$wechatFans->allowField(true)->saveAll($insertData);
}
}
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-saveSubscribeUserInfo' . ':执行失败,错误信息2:' . $e->getMessage());
}
}
}