Thinkphp 微信公众号开发3-微信登录源码
微信官方的开发文档
官方提供的方式
那我们要做的流程如下
虽然官方写了一堆文字看的一脸懵
但是我们只需要清楚流程就可以了
第一步
先传值给微信api获取code
填入appid、redirect_uri(返回url)就可以了
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
第二步
此时微信api会返回code值给到我们
我们要用appid、appSecret、code去读取用户资料
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
第三步
到这里了我们已经取得了用户资料了
主要就是保存用户的openid跟需要的信息
接下来是源码部分
这边控制器我命名为WxController
提供一个登录页面选择登录的简易源码
CSS大家请自行美化
登录页的html源码
<body>
<div class="Login">
<div style="margin-top:30px;">
<div class="clearfix others-login" style="margin-top:80px;">
<p align="center"><font color="gray">第三方登录</font></p>
<div class="others-login-method" style="margin-top:80px;">
<a href="#">
<img src="__WIMGLOGO__/qq.png" style="margin-top: 5px;"></img>
</a>
<a href="{:U('Login/wxstart')}">
<img src="__WIMGLOGO__/weixin.png" style="margin-top: 10px;"></img>
</a>
<a href="#">
<img src="__WIMGLOGO__/weibo.png" style="margin-top: 10px;"></img>
</a>
</div>
</div>
</div>
</div>
</body>
我这边CSS设计好的效果
Thinkphp控制器源码
<?php
namespace Home\Controller;
use Think\Controller;
class WxController extends Controller
{
public function wxstart(){
$appId = 'wxxxxxxxxxxxxxxxxx';//微信公众号开发的appid
$redirect_uri = urlencode('http://xxx.xxx/Wx/getUserInfo');//前面改成自己的域名
//跳转微信回调到redirect_uri获取code
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
echo "<script>location.href='$url';</script>";
}
public function getUserInfo(){
//获取code
$code = $_GET["code"];
// appId与appSecret
$appId = 'wxxxxxxxxxxxxxxxxx';//微信公众号开发的appid
$appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//微信公众号开发的appSecret密钥
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$appSecret&code=$code&grant_type=authorization_code";
$res = $this->sendRequest($url);
//var_dump($res);
$access_token = $res["access_token"];
$openId = $res['openid'];
$getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN";
//得到用户信息
$user_info = $this->sendRequest($getUserInfo);
//dump($user_info);
$openid = $user_info['openid'];
$uinfo = M('user')->where(array('openid' => $openid))->field('openid')->find();
//这里是查询这个openid是否存在
if($uinfo['openid']){//如果存在 更新目前的会员信息
$usersave = M("user");
$data['openid'] = $user_info['openid'];
$data['nickname'] = $user_info['nickname'];
$data['sex'] = $user_info['sex'];
$data['city'] = $user_info['city'];
$data['province'] = $user_info['province'];
$data['headimgurl'] = $user_info['headimgurl'];
$usersave->where(array('openid' => $openid))->save($data);
session('openid',$openid,86400);
$this->redirect('Index/index');//转跳的页面
}else{//如果不存在 添加目前的会员信息
$useradd = M("user");
$data['openid'] = $user_info['openid'];
$data['nickname'] = $user_info['nickname'];
$data['sex'] = $user_info['sex'];
$data['city'] = $user_info['city'];
$data['province'] = $user_info['province'];
$data['headimgurl'] = $user_info['headimgurl'];
$useradd->add($data);
session('openid',$openid,86400);
$this->redirect('Index/index');//转跳的页面
}
}
public function sendRequest($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
/**
* 重新加载
*
*/
public function reload()
{
cookie('msg',null);
session(null);
$this->redirect('Login/wxstart');
}
/**
* 注销
*
*/
public function logout()
{
cookie('msg',null);
session(null);
$this->redirect('Index/index');
}
}