[更新]修改微信粉丝同步

This commit is contained in:
Anyon 2019-08-28 10:17:29 +08:00
parent f4db569a36
commit b6b433b2b3
3 changed files with 32 additions and 43 deletions

View File

@ -28,11 +28,6 @@ use think\Db;
*/ */
class WechatQueue extends Queue class WechatQueue extends Queue
{ {
/**
* 当前任务URI
*/
const URI = self::class;
/** /**
* 当前操作APPID * 当前操作APPID
* @var string * @var string
@ -51,27 +46,30 @@ class WechatQueue extends Queue
*/ */
public function execute(Input $input, Output $output, array $data) public function execute(Input $input, Output $output, array $data)
{ {
$this->appid = $data['appid']; $this->appid = $data['appid'];
$wechat = WechatService::WeChatUser($this->appid); $wechat = WechatService::WeChatUser($this->appid);
$next = ''; // 获取远程粉丝 // 获取远程粉丝
list($next, $done) = ['', 0];
$output->writeln('Start synchronizing fans from the Wechat server'); $output->writeln('Start synchronizing fans from the Wechat server');
while (is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) { while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
foreach (array_chunk($result['data']['openid'], 100) as $chunk) $done += $result['count'];
if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
foreach ($list['user_info_list'] as $user) FansService::set($user, $this->appid); foreach ($list['user_info_list'] as $user) FansService::set($user, $this->appid);
if (in_array($result['next_openid'], $result['data']['openid'])) break; }
$next = $result['next_openid']; }
$next = $result['total'] > $done ? $result['next_openid'] : null;
} }
$next = ''; // 同步粉丝黑名单 // 同步粉丝黑名单
list($next, $done) = ['', 0];
$output->writeln('Start synchronizing black from the Wechat server'); $output->writeln('Start synchronizing black from the Wechat server');
while (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'];
foreach (array_chunk($result['data']['openid'], 100) as $chunk) { foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
$where = [['is_black', 'eq', '0'], ['openid', 'in', $chunk]]; $where = [['is_black', 'eq', '0'], ['openid', 'in', $chunk]];
Db::name('WechatFans')->where($where)->update(['is_black' => '1']); Db::name('WechatFans')->where($where)->update(['is_black' => '1']);
} }
if (in_array($result['next_openid'], $result['data']['openid'])) break; $next = $result['total'] > $done ? $result['next_openid'] : null;
$next = $result['next_openid'];
} }
// 同步粉丝标签列表 // 同步粉丝标签列表
$output->writeln('Start synchronizing tags from the Wechat server'); $output->writeln('Start synchronizing tags from the Wechat server');

View File

@ -43,11 +43,9 @@ class Fans extends Command
*/ */
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
# $output->writeln('preparing for synchronization of fan command...');
foreach ($this->module as $m) { foreach ($this->module as $m) {
if (method_exists($this, $fun = "_{$m}")) $this->$fun(); if (method_exists($this, $fun = "_{$m}")) $this->$fun();
} }
# $output->writeln('synchronized fans command completion.');
} }
@ -64,20 +62,21 @@ class Fans extends Command
{ {
$appid = WechatService::getAppid(); $appid = WechatService::getAppid();
$wechat = WechatService::WeChatUser(); $wechat = WechatService::WeChatUser();
$this->output->comment('preparing synchronize fans list ...'); $this->output->comment('开始同步微信粉丝数据 ...');
while ($next !== null && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) { while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
foreach (array_chunk($result['data']['openid'], 100) as $chunk) { foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) { if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
foreach ($list['user_info_list'] as $user) { foreach ($list['user_info_list'] as $user) {
$indexString = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT); $indexString = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT);
$this->output->writeln("({$indexString}/{$result['total']}) updating wechat user {$user['openid']} {$user['nickname']}"); $this->output->writeln("({$indexString}/{$result['total']}) 正在更新粉丝 {$user['openid']} {$user['nickname']}");
\app\wechat\service\FansService::set($user, $appid); \app\wechat\service\FansService::set($user, $appid);
} }
} }
} }
$next = $result['total'] > $done ? $result['next_openid'] : null; $next = $result['total'] > $done ? $result['next_openid'] : null;
} }
$this->output->comment('synchronized fans list successful.'); $this->output->comment('微信粉丝数据同步完成');
$this->output->newLine();
} }
/** /**
@ -92,18 +91,17 @@ 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('prepare synchronize fans black ...'); $this->output->comment('开始同步微信黑名单数据 ...');
while ($next !== null && 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'];
foreach (array_chunk($result['data']['openid'], 100) as $chunk) { foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
foreach ($chunk as $openid) {
$indexString = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT);
$this->output->writeln("({$indexString}/{$result['total']}) updating wechat black {$openid}");
}
Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']); Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
} }
$this->output->writeln("--> 共计同步微信黑名单{$result['total']}");
$next = $result['total'] > $done ? $result['next_openid'] : null; $next = $result['total'] > $done ? $result['next_openid'] : null;
} }
$this->output->comment('synchronized fans black successful.'); $this->output->comment('微信黑名单数据同步完成');
$this->output->newLine();
} }
/** /**
@ -118,18 +116,19 @@ class Fans extends Command
{ {
$appid = WechatService::getAppid(); $appid = WechatService::getAppid();
$wechat = WechatService::WeChatTags(); $wechat = WechatService::WeChatTags();
$this->output->comment('prepare synchronize fans tags ...'); $this->output->comment('同步微信粉丝标签数据...');
if (is_array($list = $wechat->getTags()) && !empty($list['tags'])) { if (is_array($list = $wechat->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;
$indexString = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT); $indexString = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT);
$this->output->writeln("({$indexString}/{$count}) updating wechat tags {$tag['name']}"); $this->output->writeln("({$indexString}/{$count}) 更新粉丝标签 {$tag['name']}");
} }
Db::name('WechatFansTags')->where(['appid' => $appid])->delete(); Db::name('WechatFansTags')->where(['appid' => $appid])->delete();
Db::name('WechatFansTags')->insertAll($list['tags']); Db::name('WechatFansTags')->insertAll($list['tags']);
} }
$this->output->comment('synchronized fans tags successful.'); $this->output->comment('微信粉丝标签数据同步完成');
$this->output->newLine();
} }
} }

View File

@ -28,17 +28,12 @@ use think\Db;
*/ */
class WechatQueue extends Queue class WechatQueue extends Queue
{ {
/**
* 当前任务URI
*/
const URI = self::class;
/** /**
* 执行任务 * 执行任务
* @param Input $input * @param Input $input
* @param Output $output * @param Output $output
* @param array $data * @param array $data
* @return boolean
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
* @throws \WeChat\Exceptions\LocalCacheException * @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\Exception * @throws \think\Exception
@ -50,8 +45,7 @@ class WechatQueue extends Queue
$wechat = WechatService::WeChatUser(); $wechat = WechatService::WeChatUser();
// 获取远程粉丝 // 获取远程粉丝
list($next, $done) = ['', 0]; list($next, $done) = ['', 0];
$output->writeln('Start synchronizing fans from the Wechat server'); while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
while ($next !== null && is_array($result = $wechat->getUserList($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) {
if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) { if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
@ -62,16 +56,14 @@ class WechatQueue extends Queue
} }
// 同步粉丝黑名单 // 同步粉丝黑名单
list($next, $done) = ['', 0]; list($next, $done) = ['', 0];
$output->writeln('Start synchronizing black from the Wechat server'); while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
while ($next !== null && 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) {
Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']); Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
} }
$next = $result['total'] > $done ? $result['next_openid'] : null; $next = $result['total'] > $done ? $result['next_openid'] : null;
} }
// 同步粉丝标签列表 // 同步粉丝标签
$output->writeln('Start synchronizing tags from the Wechat server');
if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) { if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
foreach ($list['tags'] as &$tag) $tag['appid'] = $appid; foreach ($list['tags'] as &$tag) $tag['appid'] = $appid;
Db::name('WechatFansTags')->where('1=1')->delete(); Db::name('WechatFansTags')->where('1=1')->delete();