1.1.1、了解微信登录的条件:
需要有认证的服务号才可以,如果是订阅号或者是未认证的服务号就别瞎忙了。就不用继续了,或者先去开通认证的服务号,再来继续学习。
注意:
1、个人的为订阅号(认不认证都不可以),个人不能注册服务号;;
2、企业可以注册服务号,未认证的服务号不可以,只有进行微信认证的服务号才可以提供微信登录,微信支付等功能。
3、微信公众平台的登录:https://mp.weixin.qq.com/
1.2.1、实现微信登录的准备
第一、打开微信公众平台网址进入。https://mp.weixin.qq.com/(公众平台)。
第二、找到“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
第三、下载微信调试工具。
注意:授权回调域名配置规范为全域名,比如需要网页授权的域名为:www.qq.com,配置以后此域名下面的页面http://www.qq.com/music.html 、 http://www.qq.com/login.html 都可以进行OAuth2.0鉴权。但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com无法进行OAuth2.0鉴权
第三、配置好回调域名后我们就可以开始了
第一步、用户同意授权,获取code(代码如下)
public function index(){ //用户同意授权,获取code $appid = "微信公众号的appid"; //回调地址 $redirect_uri = urlencode("http://授权回调域名/index.php/Home/Index/getUserInfo"); //在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo) $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; //跳转到 $url header("location:".$url); } public function getUserInfo(){ $code = $_GET["code"]; dump($code); }
public function getUserInfo(){ $appid = "微信公众号的appid"; $appsecret = "微信公众号的appsecret"; $code = $_GET["code"]; //获取网页授权的access_token $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; //请求 $url 返回一个json json_decode不加 true 会将json转为对象,加true转为数组 $res = json_decode(file_get_contents($url),true); //获取access_token并赋值给变量 dump($res);}
public function getUserInfo(){ $appid = "微信公众号的appid"; $appsecret = "微信公众号的appsecret"; $code = $_GET["code"]; //获取网页授权的access_token $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; //请求 $url 返回一个json json_decode不加 true 会将json转为对象,加true转为数组 $res = json_decode(file_get_contents($url),true); //获取access_token并赋值给变量 $access_token = $res["access_token"]; //获取openid并赋值给变量 $openid = $res["openid"]; //拼接字符串并赋值给 $urls $urls = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; //请求用户详细信息,并赋值给 $userinfo $userinfo = file_get_contents($urls); dump($userinfo); }
namespace app\index\controller; class Index public function index(){ //用户同意授权,获取code $appid = "微信公众号的appid"; //回调地址 $redirect_uri = urlencode("http://授权回调域名/index.php/Index/getUserInfo"); //在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo) $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; //跳转到 $url header("location:".$url); } //通过code换取网页授权access_token public function getUserInfo(){ $appid = "微信公众号的appid"; $appsecret = "微信公众号的appsecret"; $code = $_GET["code"]; //获取网页授权的access_token $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; //请求 $url 返回一个json json_decode不加 true 会将json转为对象,加true转为数组 $res = json_decode(file_get_contents($url),true); $access_token = $res["access_token"]; //获取openid并赋值给变量 $openid = $res["openid"]; //拼接字符串并赋值给 $urls $urls = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; //请求用户详细信息,并赋值给 $userinfo $userinfo = file_get_contents($urls); dump($userinfo); } }
代码如下:
add($userinfos); if ($id) { return '新增成功'; } else { $this->error('新增失败'); } } }
注意:要建立数据库对应字段:
{"openid":"oGma60nE6CBfiiwrBX3cavbZIsIA",
"nickname":"天道酬勤",
"sex":1,"
language":"zh_CN",
"city":"德州",
"province":"山东",
"country":"中国",
"headimgurl":"http://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ercIY5oIGoOGbGAB94zUYozCapRIDUVG21BWBELugfGINVlt7y0KILoUpIR7UR6f9Slyd8tuDGbZQ/132",
"privilege":[]}"
注意:判断数据库是否由此用户。
class User extends Controller{/**之后跳转地址**/ public function index(Request $request){ $user = new UserModel; // dump(Session::get('user_openid'));exit; $ret = $user ->get(['openid'=>Session::get('user_openid')]); if(!$ret) { $this->redirect('index/index'); }else { // 获取session $openid=Session::get('user_openid'); $bis = $user ->show($openid); return $this->fetch('', [ 'userinfos' => $bis, ]); } }