接入微信公众号登录-回调
/**
* 微信处理回调
*/
public static function callBack($data)
{
if (!empty($data)) {
if (isset($data['MsgType']) && $data['MsgType'] == 'event') {
//获取用户信息
$userInfo = login::get_user_info($data['FromUserName']);
$userInfo = json_decode($userInfo, true);
//更换/绑定手机号
if (stristr($data['EventKey'],'bindingPhone')){
$arr = array(
'EventKey' => $data['EventKey'],//唯一标识
);
if ($data['Event'] == 'SCAN') {
$res = Db::table('user')->where('wx_open_id', $data['FromUserName'])->update($arr);
if ($res !== false) {
login::replacePhone($data);
return true;
}
}
}
if (stristr($data['EventKey'],'unbindPhone')){
$arr = array(
'EventKey' => $data['EventKey'],//唯一标识
'phone'=>'',
'password'=>''
);
if ($data['Event'] == 'SCAN') {
$res = Db::table('user')->where('wx_open_id', $data['FromUserName'])->update($arr);
if ($res !== false) {
login::unbindPhone($data);
return true;
}
}
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if ($data['Event'] == 'subscribe') {
$str = '提示消息';
$time = time();
$resultStrq = sprintf($textTpl, $data['FromUserName'], 'czh_sjpt', $time, 'text', $str);
echo $resultStrq;
}
if ($data['Event'] == 'SCAN') {
$str = '登录成功';
$time = time();
$resultStrq = sprintf($textTpl, $data['FromUserName'], 'czh_sjpt', $time, 'text', $str);
echo $resultStrq;
}
$arr = array(
'EventKey' => $data['EventKey'],//唯一标识
'event' => $data['Event'],//微信事件类型
'head_portrait' => $userInfo['headimgurl'],//头像
'nickname' => login::filterEmoji($userInfo['nickname']),//昵称
'wx_open_id' => $data['FromUserName'],//微信id
);
return true;
}
}
}
/**
* @param $str
* @return string|string[]|null
* 获取用户信息
*/
public static function get_user_info($openId){
$accessToken = login::get_access_token();//获取token
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
$data = login::httpRequest($url);
return $data;
}
~~~
本作品采用《CC 协议》,转载必须注明作者和本文链接