微信登录1

流程1:获取请求网址就是一个有二维码的网页

流程2:扫码授权后获取code

流程3:通过code获取微信基本信息及openid

<?php

namespace appapicontroller;

use thinkController;

//这是一个微信登录的处理类,用的是微信开放平台

class Weixin extends Controller

{

private $AppID = '';//开发平台有

private $AppSecret = '';//开发平台也有

private $Redirect_uri = '';//回调地址

private $scope = 'snsapi_login';//这里不用动如果是微信登录

//前端请求这个接口,获取登录的url,这个url可以直接弹出带二维码的网页

public function openWx()

{

return json(['status' => 'success', 'url' => "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->AppID . "&redirect_uri=" . $this->Redirect_uri . "&response_type=code&scope=" . $this->scope . "&state=STATE#wechat_redirect"]);

}

//这里就是接收code还有state。用来做操作

public function getToken()

{

$code = $_GET['code'];

if (empty($code)){

$this->error('授权失败');

}

$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->AppID . '&secret=' . $this->AppSecret . '&code=' . $code . '&grant_type=authorization_code';

//获取token,为了获取access_token 如果没有就弹出错误

$token = json_decode(file_get_contents($token_url));

if (isset($token->errcode)) {

echo '<h1>错误:</h1>' . $token->errcode;

echo '<br/><h2>错误信息:</h2>' . $token->errmsg;

exit;

}

$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $this->AppID . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token;

//获取access_token ,为了获取微信的个人信息,如果没有就弹出错误

$access_token = json_decode(file_get_contents($access_token_url));

if (isset($access_token->errcode)) {

echo '<h1>错误:</h1>' . $access_token->errcode;

echo '<br/><h2>错误信息:</h2>' . $access_token->errmsg;

exit;

}

$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token->access_token . '&openid=' . $access_token->openid . '&lang=zh_CN';

//获取用户信息

if (isset($user_info->errcode)) {

echo '<h1>错误:</h1>' . $user_info->errcode;

echo '<br/><h2>错误信息:</h2>' . $user_info->errmsg;

exit;

}

//这里转换为数组

$rs = (array)$user_info;

//返回用户信息

return $rs;

}

23

namespace appapicontroller;

use thinkController;

//这是一个微信登录的处理类,用的是微信开放平台

class Weixin extends Controller

{

private $AppID = '';//开发平台有

private $AppSecret = '';//开发平台也有

private $Redirect_uri = '';//回调地址

private $scope = 'snsapi_login';//这里不用动如果是微信登录

//前端请求这个接口,获取登录的url,这个url可以直接弹出带二维码的网页

public function openWx()

{

return json(['status' => 'success', 'url' => "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->AppID . "&redirect_uri=" . $this->Redirect_uri . "&response_type=code&scope=" . $this->scope . "&state=STATE#wechat_redirect"]);

}

//这里就是接收code还有state。用来做操作

public function getToken()

{

$code = $_GET['code'];

//判断是否授权

if (empty($code)){

$this->error('授权失败');

}

$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->AppID . '&secret=' . $this->AppSecret . '&code=' . $code . '&grant_type=authorization_code';

//获取token,为了获取access_token 如果没有就弹出错误

$token = json_decode(file_get_contents($token_url));

if (isset($token->errcode)) {

echo '<h1>错误:</h1>' . $token->errcode;

echo '<br/><h2>错误信息:</h2>' . $token->errmsg;

exit;

}

$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $this->AppID . '&grant_type=refresh_token&refresh_token=' . $token->refresh_token;

//获取access_token ,为了获取微信的个人信息,如果没有就弹出错误

$access_token = json_decode(file_get_contents($access_token_url));

if (isset($access_token->errcode)) {

echo '<h1>错误:</h1>' . $access_token->errcode;

echo '<br/><h2>错误信息:</h2>' . $access_token->errmsg;

exit;

}

$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token->access_token . '&openid=' . $access_token->openid . '&lang=zh_CN';

//获取用户信息

$user_info = json_decode(file_get_contents($user_info_url));

if (isset($user_info->errcode)) {

echo '<h1>错误:</h1>' . $user_info->errcode;

echo '<br/><h2>错误信息:</h2>' . $user_info->errmsg;

exit;

}

//这里转换为数组

$rs = (array)$user_info;

//返回用户信息

return $rs;

}

这里说明一下,传code我不是直接传到这里的,我还有另外一个操作方法,可以存入数据库或者其他操作

//另外一个方法,code还有state传到这个方法里面来

public function getWx()

{

header('Access-Control-Allow-Origin:*');//允许跨域请求

$code = $_GET['code'];//接收code

$weixin = Controller("Weixin");//实例化上个类

//获取用户信息

$info = $weixin->getToken($code);接受返回值,数组的

$res = Db::name('user')

->where('user_id', $checkToken['data']['data']['user_id'])

->update(array('openid' => $info['openid'], 'nickname' => $info['nickname']));//存入数据库

$data = Db::name('user')

->where('user_id', $checkToken['data']['data']['user_id'])->find();//查一下,可能办法有点笨

if ($res) {

return json(['status' => 'success', 'msg' => $data]);

} else {

return json(['status' => 'error', 'msg' => '绑定失败']);

}

}

//另外一个方法,code还有state传到这个方法里面来

public function getWx()

{

header('Access-Control-Allow-Origin:*');//允许跨域请求

$code = $_GET['code'];//接收code

$weixin = Controller("Weixin");//实例化上个类

//获取用户信息

$info = $weixin->getToken($code);接受返回值,数组的

$res = Db::name('user')

->where('user_id', $checkToken['data']['data']['user_id'])

->update(array('openid' => $info['openid'], 'nickname' => $info['nickname']));//存入数据库

$data = Db::name('user')

->where('user_id', $checkToken['data']['data']['user_id'])->find();//查一下,可能办法有点笨

if ($res) {

return json(['status' => 'success', 'msg' => $data]);

} else {

return json(['status' => 'error', 'msg' => '绑定失败']);

}

}

————————————————

版权声明:本文为CSDN博主「浅州」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Jouzeyu/article/details/92839025

以上就是比较完整的操作步骤了,如果喜欢可以评论一下,点个关注,博主发的所有波本都是自己做过的,质量保证

————————————————

版权声明:本文为CSDN博主「浅州」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Jouzeyu/article/details/92839025

原文地址:https://www.cnblogs.com/smilevv/p/13260946.html