/** * 分段更新本地用户资料. * * @param int $total * @param FanModel $fanModel * @param Account $accountId */ public function performUpdateUserDetail($total, $fanModel, $account) { $this->info("\n开始更新用户资料..."); $this->output->progressStart($total); $userService = new User($account->app_id, $account->app_secret); $fanService = new FanService(); $accountId = $account->id; $fanModel->where('account_id', $accountId)->orderBy('id', 'desc')->chunk(100, function ($fans) use($userService, $accountId, $fanModel, $fanService) { $fans = $userService->batchGet($fans->lists('openid')->toArray()); foreach ($fans as $fan) { $fan = $fanService->formatFromWeChat($fan); $fanModel->where('openid', $fan['openid'])->update($fan); $this->output->progressAdvance(); } }); $this->output->progressFinish(); $this->info("\n同步完成。"); }
/** * 粉丝更新完成后执行. * * @param \App\Models\Fan $fanModel */ public function updated(Fan $fanModel) { /* * 1 获取更新的属性 */ $updateArr = $fanModel->getDirty(); if (isset($updateArr['group_id'])) { /* * 1 移动用户到指定分组同步到线上 */ $this->group->moveUser($fanModel->openid, $updateArr['group_id']); } if (isset($updateArr['remark'])) { /* * 1 更改的粉丝备注同步到线上 */ $this->user->remark($fanModel->openid, $updateArr['remark']); } }
/** * Bootstrap the application services. */ public function boot() { User::observe('App\\Observers\\UserObserver'); Reply::observe('App\\Observers\\ReplyObserver'); Event::observe('App\\Observers\\EventObserver'); Material::observe('App\\Observers\\MaterialObserver'); Account::observe('App\\Observers\\AccountObserver'); FanGroup::observe('App\\Observers\\FanGroupObserver'); Fan::observe('App\\Observers\\FanObserver'); }
/** * Execute the console command. * * @return mixed */ public function handle() { $accountId = $this->argument('account_id'); $openId = $this->argument('openid'); /* * 1 获取Account */ $account = $this->getAccount($accountId); $fanModel = new FanModel(); $FanService = new FanService(); /* * 2 初始化 SDK Config, 构建 SDK 对象 */ $userService = new User($account->app_id, $account->app_secret); $fan = $userService->get($openId); if (isset($fan['subscribe']) && $fan['subscribe']) { //subscribe=1 关注了公众号 $updateInput = $FanService->formatFromWeChat($fan); /* * 存入本地 */ $fanModel->where('account_id', $accountId)->where('openid', $openId)->update($updateInput); } }
/** * 同步粉丝数据到本地数据库. */ public function syncToLocal() { $easywechat = app('easywechat'); $user = $easywechat->user; $nextOpenid = ''; // 删除本地旧数据 FanModel::where('account_id', '=', get_chosed_account())->delete(); // 备忘:batchGet 批量获取粉丝信息时一次最多获取100个粉丝 do { $fansList = $user->lists($nextOpenid, 100); if (empty($fansList['next_openid'])) { break; } else { $nextOpenid = $fansList['next_openid']; } if ($fansList['count'] > 0 && $fansList['count'] <= 100) { // 粉丝 openid 列表 $openIds = $fansList->get('data.openid'); $fanList = $user->batchGet($openIds)->get('user_info_list'); $fans = array_map(function ($item) { $item['account_id'] = get_chosed_account(); $item['created_at'] = date('Y-md H:i:s'); $item['updated_at'] = date('Y-md H:i:s'); $item['subscribe_time'] = date('Y-m-d H:i:s', $item['subscribe_time']); return $item; }, $fanList); FanModel::insert($fans); } elseif ($fansList['count'] > 100) { // 粉丝 openid 列表 $openIds = $fansList->get('data.openid'); $openidsChunk = array_chunk($openIds, 100); foreach ($openidsChunk as $value) { $fanList = $user->batchGet($value)->get('user_info_list'); $fans = array_map(function ($item) { $item['account_id'] = get_chosed_account(); $item['created_at'] = date('Y-md H:i:s'); $item['updated_at'] = date('Y-md H:i:s'); $item['subscribe_time'] = date('Y-m-d H:i:s', $item['subscribe_time']); return $item; }, $fanList); FanModel::insert($fans); } } } while ($fansList['count'] > 0 && $fansList['next_openid']); return success('同步成功'); }