>[info]关注更新用户数据、自动回复
~~~
$app->server->push(function ($message) use ($app) {
if ($message["MsgType"] == "event") {
if ($message["Event"] == "subscribe") {
/*关注*/
$ck = \app\admin\model\User::where(["openid" => $message["FromUserName"]])->find();
if (!$ck) {
//$userinfo = $app->user->get($message["FromUserName"]);
$user = new \app\admin\model\User();
$user->data([
//"username" => isset($userinfo["nickname"]) ? $userinfo["nickname"] : '未获取到',
//"nickname" => isset($userinfo["nickname"]) ? $userinfo["nickname"] : '未获取到',
"openid" => $message["FromUserName"],
//"avatar" => isset($userinfo["headimgurl"]) ? $userinfo["headimgurl"] : '',
"status" => "normal",
//"status" => "hidden",
]);
$user->save();
} else {
if ($ck["subscribe"] == 0) {
$ck->subscribe = 1;
$ck->save();
}
}
return "欢迎关注老吴线报\n做线报我是认真的\n记得每日来签到打卡查看线报";
}
}
}
~~~
>[warning] 由于是测试微信号,无法获取昵称跟头像,只能拿到openid,所以这里做了注释,服务号则没有这些问题,可以打开注释。
#### 温馨提示,上方代码中的 `$message` 可以打印出来以下结果:
~~~
array (
'ToUserName' => 'gh_4f212d728919',
'FromUserName' => 'oYr-IjpFjg4U0Ukfp1xsYXiD8kFM',
'CreateTime' => '1638513465',
'MsgType' => 'text',
'Content' => '55555555',
'MsgId' => '23457872284094365',
)
~~~