在网站注册登陆这环节,增加微信扫码注册登陆,普通的方法需要开通微信开发者平台,生成二维码扫码后才能获取用户的uinonid或openid,实现注册登陆,但这样比较麻烦还要企业认证交费开发者平台,而且没有和公众号强制绑定,不能实现公众号粉丝积累。
下面详情给大家说下原理和代码实现,【建议点关注收藏】,更多实用技术分享给大家。
可以实现网站微信扫码关注公众号后自动注册登陆实现方法,同时,可以多个网站公用一个公众号扫码登陆,做一个域名转发就行。
实现的原理:
- 配置公众号后台服务器接口地址。
- 每次网站打开生成的二维码带上一个随机数,一个码对应一个随机数保存到数据库。
- 网站接口实现接收微信服务器发送过来的信息中带有随机数和openid信息,已关注用户自动登陆设置随机码状态为登陆,未关注公众号实现注册登陆后设置随机码状态为登陆。
- 网站前端轮询随机码看是否登陆状态,是就设置登陆。
具体实现步骤:
微信公众号服务器配置,设置与开发-服务器配置,填入你的网站的接收微信发送信息地址。
代码实现:
代码里用的是php的 EasyWeChat
第三方组件库
public function index()
{
$this->app->server->push(function ($message) {
switch ($message['MsgType']) {
case 'event':
//接收事件推送: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
switch ($message['Event']) {
case 'subscribe': //关注事件, 扫描带参数二维码事件(用户未关注时,进行关注后的事件推送)
$message['EventKey'] = str_replace('qrscene_','',$message['EventKey']);
//别的站扫码登陆 域名转发
if(strpos($message['EventKey'],'antcome')!==false || strpos($message['EventKey'],'axure9')!==false || strpos($message['EventKey'],'aichat')!==false)
{
$post['code'] = $message['EventKey'];
$post['openid'] = $message['FromUserName'];
self::transfer_scan_code($post);
}
else
{
$WechatScan = new WechatScanModel();
$find_user = Db::name('third_party_user')->where('openid',$message['FromUserName'])->find();
if($find_user)
{
$WechatScan->checkScan($message['EventKey'], $find_user['user_id']);
}
else
{
//官方接口调整无法取得用户头像和昵称,https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html
$userinfo = $this->app->user->get($message['FromUserName']);
$userinfo['headimgurl'] = '/static/images/user.png';
$uid = Db::name('user')->order('id desc')->value('id');
$userinfo['nickname'] = 'uihtm_'.($uid+1);
//分销pid
$scanCode = $WechatScan->getScanCode($message['EventKey']);
if(!empty($scanCode['pid']))
$userinfo['pid'] = $scanCode['pid'];
$user_id = $WechatScan->saveUserInfo($userinfo);
$WechatScan->checkScan($message['EventKey'], $user_id);
//$WechatScan->checkScan($message['EventKey']);
}
}
return "谢谢关注!\n"."<a href='".cmf_get_domain()."/wechat/index/user_auth'>点击授权登陆</a>";
break;
case 'unsubscribe': //取消关注事件
break;
case 'SCAN': //扫描带参数二维码事件(用户已关注时的事件推送)
//别的站扫码登陆 域名转发
if(strpos($message['EventKey'],'antcome')!==false || strpos($message['EventKey'],'axure9')!==false || strpos($message['EventKey'],'aichat')!==false)
{
$post['code'] = $message['EventKey'];
$post['openid'] = $message['FromUserName'];
self::transfer_scan_code($post);
}
else
{
$WechatScan = new WechatScanModel();
$find_user = Db::name('third_party_user')->where('openid',$message['FromUserName'])->find();
if($find_user)
{
$WechatScan->checkScan($message['EventKey'], $find_user['user_id']);
}
else
{
$userinfo = $this->app->user->get($message['FromUserName']);
$userinfo['headimgurl'] = '/static/images/user.png';
$uid = Db::name('user')->order('id desc')->value('id');
$userinfo['nickname'] = 'uihtm_'.($uid+1);
$user_id = $WechatScan->saveUserInfo($userinfo);
$WechatScan->checkScan($message['EventKey'], $user_id);
}
}
//更新用户昵称和头像
if(strpos($find_user['nickname'], 'uihtm')!==false) {
return "谢谢关注!\n" . "<a href='" . cmf_get_domain() . "/wechat/xxxxx'>点击授权登陆</a>";
}
else
return "谢谢关注!\n";
break;
case 'LOCATION': //上报地理位置事件
return "经度: " . $message['Longitude'] . "\n纬度: " . $message['Latitude'] . "\n精度: " . $message['Precision'];
break;
case 'CLICK': //自定义菜单事件(点击菜单拉取消息时的事件推送)
return "事件KEY值: " . $message['EventKey'];
break;
case 'VIEW': //自定义菜单事件(点击菜单拉取消息时的事件推送)
return "跳转URL: " . $message['EventKey'];
break;
case 'ShakearoundUserShake':
Log::info('摇一摇周边事件');
//摇一摇事件通知: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443448066
return 'ChosenBeacon\n' . 'Uuid: ' . $message['ChosenBeacon']['Uuid'] . 'Major: ' . $message['ChosenBeacon']['Major'] . 'Minor: ' . $message['ChosenBeacon']['Minor'] . 'Distance: ' . $message['ChosenBeacon']['Distance'];
break;
default:
return $message['Event'];
break;
}
break;
//接收普通消息: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
case 'text':
return "Content: " . $message['Content'];
break;
case 'image':
return "MediaId: " . $message['MediaId'] . "\nPicUrl: " . $message['PicUrl'];
break;
case 'voice':
return "MediaId: " . $message['MediaId'] . "\nFormat: " . $message['Format'] . "\nRecognition: " . $message['Recognition'];
break;
case 'video':
return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
break;
case 'shortvideo':
return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
break;
case 'location':
return "Location_X: " . $message['Location_X'] . "\nLocation_Y: " . $message['Location_Y'] . "\nScale: " . $message['Scale'] . "\nLabel: " . $message['Label'];
break;
case 'link':
return "Title: " . $message['Title'] . "\nDescription: " . $message['Description'] . "\nUrl: " . $message['Url'];
break;
default:
return $message['MsgType'];
break;
}
//return "您好!验证码: ";
});
// $this->app->server->setMessageHandler(function ($message) {
// return "您好!欢迎使用 EasyWeChat!";
// });
return $this->app->server->serve()->send();
}
//多网站公用转发code
private function transfer_scan_code($post=[])
{
if(strpos($post['code'],'uihtm.com')!==false)
HttpService::post
('https://www.uihtm.com/api/xxxxx', $post);
if(strpos($post['code'],'antcome.com')!==false)
HttpService::post
('https://www.antcome.com/api/xxxxx', $post);
if(strpos($post['code'],'axure9.com')!==false)
HttpService::post('https://www.axure9.com/api/xxxxxx', $post);
if(strpos($post['code'],'aichat')!==false)
HttpService::post('https://chat.abc.com/web.php/wxmp/xxxxxx', $post);
}
具体的HttpService
用curl实现,自行百度找,这是就不啰嗦了。具体的演示到代码的里transfer_scan_code
方法里网站域名测试