一个基于thinkphp的微信授权登陆功能

共享一份基于thinkphp开发的用户授权登陆的功能代码,本实例使用thinkphp的第三方微信公众平台PHP-SDK,地址https://github.com/dodgepudding/wechat-php-sdk 自行下载引入即可

新建控制器 PublicController

<?php
namespace App\Controller;
use Think\Controller;
class PublicController extends Controller{
public function _initialize(){

if(!$_SESSION['openid']){
Vendor("Wechat.wechat#class");
$config = M ( "Wxconfig" )->where ( array (   
"id" => "1"
) )->find ();//获取微信的配置信息 appid,appsecret

$options = array (
'appid' => $config ["appid"], // 填写高级调用功能的app id
'appsecret' => $config ["appsecret"], // 填写高级调用功能的密钥
);
$weObj = new \Wechat ( $options );

$url=$_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
if(empty($_GET['code'])){
$url_get=$weObj->getOauthRedirect($url);
redirect($url_get);
}else{
$access_token=$weObj->getOauthAccessToken();
$userinfo=$weObjgetOauthUserinfo($access_token['access_token'],$access_token['openid']);
$data['openid']=$access_token['openid'];
$data['username']=$userinfo['nickname'];
$data['pic']=$userinfo['headimgurl'];
$result = R("Api/MemberApi/addOauth", array($data));
$_SESSION['openid']=$access_token['openid'];
if($result){
redirect($url.'&openid='.$access_token['openid']);
}
}
}

}

}

接下来只需继承该类就可以了

上一篇:关于加权的LIS问题


下一篇:MyBatis - 5.缓存机制