>[info] 项目开发完成之前,公众号本身就有[用户](https://www.easywechat.com/4.x/official-account/user.html)的情况是存在的,这时候就要拉取[用户](https://www.easywechat.com/4.x/official-account/user.html)到数据库了,相关代码如下所示:
##### 执行完以下方法就完事,想看结果就把我打印的部分的注释打开即可
~~~
/**
* 第一次拉取公众号关注用户到数据库
* /api/Easycenter/get_wx_user
* 必须要获得权限
*/
public function get_wx_user()
{
$app = $this->app;
$list = $app->user->list($nextOpenId = null);
if ($list) {
$openlist = $list['data']['openid'];
//$userlist = [];
if ($openlist) {
foreach ($openlist as $k => $v) {
$ck = \app\admin\model\User::where(['openid' => $v])->find();
if (!$ck) {
$userinfo = $app->user->get($v);
//array_push($userlist, $userinfo);
$user = new \app\admin\model\User();
$user->data([
'username' => $userinfo['nickname'],
'nickname' => $userinfo['nickname'],
'openid' => $v,
'status' => 'hidden',
'avatar' => $userinfo['headimgurl'],
]);
$user->save();
}
}
}
//echo "<pre>";
//var_dump($userlist);
//echo "</pre>";
}
// $this->success('', $this->app->user->list($nextOpenId = null));
}
~~~