最近主要在忙活微信与支付宝平台的对接与开发,本篇就基于后端层面来讲述一下微信的登录与绑定实现。
(一)、申请微信开放平台
最首先的话就是需要去微信开发中心,创建一个账号,然后创建自己的移动或网站应用。
创建完成后,就会需要腾讯的过年对联审核,整个过程在1-3天,基本上1天左右就能完成,审核通过如下图所示。
(二) 、封装微信相关接口
微信移动应用开发文档:https://developers.weixin.qq.com/doc/oplatform/mobile_app/wechat_login/authorized_api_call_unionid.html
审核通过后,就需要来封装微信授权、可信息获取的接口。
封装微信授权 && 用户信息获取
微信授权接口:
需要填写的参数如下:
下面通过我们的php代码实现:
<?phpnamespace app\helpers;u guzzlehttp\client;u海南文昌中学 illuminate\support\arr;class wechatapputils{ protected $client = null; protected $config = [数字记忆]; public function __construct() { $this->config = [ 'wechat_app' => [ 'appid' => env('wechat_appid'), //审核通过的appid 'cret' => env('wechat_cret'), //应用app cret 详情见上图 ], 'time_out' => 5, ]; $this->client = new client([ 'time_out' => $this->config['time_out'], ]); } /** * 获取微信用户access_token * * @param [string] $code * @return array */ public function accesstoken($code) { $accesstokenurl = '/d/file/titlepic/access_token'; $respon = $this->client->request('get', $accesstokenurl, [ 'query' => [ 'grant_type' => 'authorization_code', 'code' => $code, 'appid' => arr::get($this->config, 'wechat_app.appid'), 'cret' => arr::get($奥运作文this->config, 'wechat_app.cret'), ], ]); $result = $respon->getbody()->getcontents(); return empty($result) ? null : json_decode($result, true); } /** * 微信用户信息 * * @param [string] $accesstoken * @param [string] $openid * @return array */ public function urinfo($accesstoken, $openid) { $urinfourl = '/d/file/titlepic/urinfo'; $respon = $this->client->request('get', $urinfourl, [ 'query' => [ 'access_token' => $accesstoken, 'openid' => $openid, 'lang' => 'zh_cn', ], ]); $result = $respon->getbody()->getcontents(); return empty($result) ? null : json_decode($result, true); }}上面的accesstoken方法主要是实现用户授权,效验的code参数是客户端传递过来的,当成功获取收钱用户的授权信息后,可以根据用户的openid来调用urinfo方法查询相关用户的信息,包含了用户的昵称、头像、性别等等。
具体客户端开发文档可以参考这篇:https://developers.weixin.qq.com/doc/oplatform/mobile_app/wechat_login/development_guide.html。
上面的用到的http client是一个第三方拓展包,叫做guzzlehttp,是一个php http客户端,可以轻松发送http请求,并且可以轻松集成web服务。
我们可以通过compor一键安装:
compor require guzzlehttp/guzzle
(三)、完善用户微信授权登录
完成上述的封装操作后,我们便开始讲微信接入到我们自己的系统中与用户进行关联起来,下面是微信接入的一张时序图。
如果用户想使用微信登录,首先会通过客户端唤起微信,请求登录第三方应用,然后微信会询问用户是否成功授权给xx应用,授权成功后,客户端会得到一个授权码:code,然后客户端携带code请求我们的客户端api,进行授权绑定,授权成功后,会得到授权用户openid(应用下的唯一标识),反之抛出异常信息提示用户。
建立oauth表,用于储存用户的授权信息。
建立一张o_auths table 储存用户的授权信息,设计oauth_type字段使其成为一个多态模型,方便接入以后的微博、支付宝、qq接入等等。
schema::create('o_auths', function (blueprint $table) { $table->increments('id'); $table->unsignedinteger('ur_id')->index()->comment('用户id'); $table->morphs('o_auth'); $table->json('data')->nullable()->comment('授权信息'); $table->timestamps();});完善用户授权绑定
建立好o_auths table,下面开始完善用户授权绑定的逻辑:
function wechat(ur $ur, $code){ $utils = new wechatapputils; //获取微信token $accesstokens = $utils->accesstoken($code); throw_if(!arr::has($accesstokens, ['unionid', 'openid']), exception::class, '授权失败,请稍后再试!'); //建立oauth关联 $oauth = oauth::firstornew(['oauth_type' => 'wechat', 'oauth_id' => $accesstokens['openid']]); throw_if(ist($oauth->id),exception::class,'该微信已绑定,请直接登录!'); $oauth->ur_id = $ur->id; $oauth->data = arr::only($accesstokens, ['openid', 'refresh_token']); $oauth->save(); return $oauth;}首先会通过客户端传递过来的code获取当前用户授权,然后查询该用户是否已授权过,已授权过就提醒用户直接去登录,否则绑定授权信息,返回给客户端。
完善微信登录
完善好用户授权后,登录就显得非常容易了,只需要简单查询授权记录,存在则返回对应绑定的用户,否则抛出异常信息提示用户。
public function signin($ur, $code){ $utils = new wechatapputils; //获取微信token $accesstokens = $utils->accesstoken($code); throw_if(!arr::has($accesstokens, ['unionid', 'openid']), exception::class, '授权失败,请稍后再试!'); $oauth = $this->geturoauth($ur, 'wechat'); throw_if(is_null($oauth), urexception::class, '授权失败,该账户未绑定!'); return $oauth;}public function geturoauth(ur $ur, $oauthtype){ return oauth::where(['oauth_type' => $oauthtype, 'ur_id' => $ur->id])->first();}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-08 04:00:42,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7f62792461dc61899e6ac6b80009f7a4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel 集成微信用户登录和绑定的实现.doc
本文 PDF 下载地址:Laravel 集成微信用户登录和绑定的实现.pdf
| 留言与评论(共有 0 条评论) |