修改粉丝同步任务

This commit is contained in:
Anyon 2020-09-25 10:36:22 +08:00
parent 0b98f720b1
commit 626aad5277
4 changed files with 45 additions and 29 deletions

View File

@ -74,23 +74,32 @@ class Fans extends Command
protected function _list($next = '', $done = 0) protected function _list($next = '', $done = 0)
{ {
$appid = WechatService::instance()->getAppid(); $appid = WechatService::instance()->getAppid();
$this->output->comment('--> Start to synchronize wechat user data'); $this->output->comment('开始获取微信用户数据');
while (!is_null($next) && is_array($result = WechatService::WeChatUser()->getUserList($next)) && !empty($result['data']['openid'])) { while (is_string($next)) {
foreach (array_chunk($result['data']['openid'], 100) as $openids) { $result = WechatService::WeChatUser()->getUserList($next);
if (is_array($list = WechatService::WeChatUser()->getBatchUserInfo($openids)) && !empty($list['user_info_list'])) { if (is_array($result) && !empty($result['data']['openid'])) {
foreach ($list['user_info_list'] as $user) { $total = intval($result['total']);
$string = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT); foreach (array_chunk($result['data']['openid'], 100) as $openids) {
$message = "({$string}/{$result['total']}) -> {$user['openid']} {$user['nickname']}"; $list = WechatService::WeChatUser()->getBatchUserInfo($openids);
$this->setQueueProgress($message, $done * 100 / $result['total']); if (is_array($list) && !empty($list['user_info_list'])) {
FansService::instance()->set($user, $appid); foreach ($list['user_info_list'] as $user) {
$this->queue->message($total, ++$done, "-> {$user['openid']} {$user['nickname']}");
FansService::instance()->set($user, $appid);
}
} }
} }
$next = $total > $done ? $result['next_openid'] : null;
} else {
$next = null;
} }
$next = $result['total'] > $done ? $result['next_openid'] : null;
} }
$this->output->comment('--> Wechat user data synchronization completed'); if ($done > 0) {
$this->output->comment('微信用户数据获取完成');
} else {
$this->output->comment('未获取到微信用户数据');
}
$this->output->newLine(); $this->output->newLine();
return "同步{$done}个用户数据"; return "共获取{$done}个用户数据";
} }
/** /**
@ -105,16 +114,20 @@ class Fans extends Command
public function _black($next = '', $done = 0) public function _black($next = '', $done = 0)
{ {
$wechat = WechatService::WeChatUser(); $wechat = WechatService::WeChatUser();
$this->output->comment('--> Start to synchronize wechat blacklist data'); $this->output->comment('开始更新黑名单的微信用户');
while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) { while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
$done += $result['count']; $done += $result['count'];
foreach (array_chunk($result['data']['openid'], 100) as $chunk) { foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
$this->app->db->name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']); $this->app->db->name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
} }
$this->setQueueProgress("共计同步微信黑名单{$result['total']}"); $this->setQueueProgress("--> 共计同步微信黑名单{$result['total']}");
$next = $result['total'] > $done ? $result['next_openid'] : null; $next = $result['total'] > $done ? $result['next_openid'] : null;
} }
$this->output->comment('--> Wechat blacklist data synchronization completed'); if ($done > 0) {
$this->output->comment('黑名单的微信用户更新成功');
} else {
$this->output->comment('未获取到黑名单微信用户哦');
}
$this->output->newLine(); $this->output->newLine();
if (empty($result['total'])) { if (empty($result['total'])) {
return '其中黑名单0人'; return '其中黑名单0人';
@ -125,7 +138,7 @@ class Fans extends Command
/** /**
* 同步粉丝标签列表 * 同步粉丝标签列表
* @param integer $index * @param integer $done
* @return string * @return string
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException * @throws \WeChat\Exceptions\LocalCacheException
@ -134,23 +147,26 @@ class Fans extends Command
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function _tags($index = 0) public function _tags($done = 0)
{ {
$appid = WechatService::instance()->getAppid(); $appid = WechatService::instance()->getAppid();
$this->output->comment('--> Start to synchronize wechat tag data'); $this->output->comment('开始获取微信用户标签数据');
if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) { if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
$count = count($list['tags']); $count = count($list['tags']);
foreach ($list['tags'] as &$tag) { foreach ($list['tags'] as &$tag) {
$tag['appid'] = $appid; $tag['appid'] = $appid;
$progress = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT); $this->queue->message($count, ++$done, "-> {$tag['name']}");
$this->setQueueProgress("({$progress}/{$count}) -> {$tag['name']}");
} }
$this->app->db->name('WechatFansTags')->where(['appid' => $appid])->delete(); $this->app->db->name('WechatFansTags')->where(['appid' => $appid])->delete();
$this->app->db->name('WechatFansTags')->insertAll($list['tags']); $this->app->db->name('WechatFansTags')->insertAll($list['tags']);
} }
$this->output->comment('--> Wechat tag data synchronization completed'); if ($done > 0) {
$this->output->comment('微信用户标签数据获取完成');
} else {
$this->output->comment('未获取到微信用户标签数据');
}
$this->output->newLine(); $this->output->newLine();
return ",同步{$index}个标签。"; return "获取到{$done}个标签。";
} }
} }

View File

@ -963,12 +963,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "3eb8593446d09583fd3bb431b48b861a2676409a" "reference": "08ab65c4044435fda2db2484bd3a92ead39ffaa4"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/3eb8593446d09583fd3bb431b48b861a2676409a", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/08ab65c4044435fda2db2484bd3a92ead39ffaa4",
"reference": "3eb8593446d09583fd3bb431b48b861a2676409a", "reference": "08ab65c4044435fda2db2484bd3a92ead39ffaa4",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -985,7 +985,7 @@
"ext-mbstring": "*", "ext-mbstring": "*",
"topthink/framework": "^6.0" "topthink/framework": "^6.0"
}, },
"time": "2020-09-24T08:42:51+00:00", "time": "2020-09-24T08:51:05+00:00",
"type": "library", "type": "library",
"extra": { "extra": {
"think": { "think": {

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php <?php
// This file is automatically generated at:2020-09-24 16:53:08 // This file is automatically generated at:2020-09-25 09:55:13
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'think\\admin\\Library', 0 => 'think\\admin\\Library',

View File

@ -306,8 +306,8 @@ class Queue extends Command
/** /**
* 修改当前任务状态 * 修改当前任务状态
* @param int $status 任务状态 * @param integer $status 任务状态
* @param mixed $message 消息内容 * @param string $message 消息内容
* @param boolean $isSplit 是否分隔 * @param boolean $isSplit 是否分隔
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
*/ */