Thinkphp 微信公众号开发3-微信登录源码

Thinkphp 微信公众号开发3-微信登录源码

微信官方的开发文档

微信官方开发文档传送门

官方提供的方式

在这里插入图片描述

那我们要做的流程如下

虽然官方写了一堆文字看的一脸懵
但是我们只需要清楚流程就可以了

获取code
使用code换取access_token
access_token获取snsapi_userinfo
第一步

先传值给微信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');
    }
}
微信公众平台 PHP 开发包,细化了微信的各项接口操作。调用官方API,具有更灵活的消息分类响应方式,支持链式调用操作 。 主要功能 接入验证 (初级权限) 自动回复(文本、图片、语音、视频、音乐、图文) (初级权限) 菜单操作(查询、创建、删除) (菜单权限) 客服消息(文本、图片、语音、视频、音乐、图文) (认证权限) 二维码(创建临时、永久二维码,获取二维码URL) (服务号、认证权限) 长链接转短链接接口 (服务号、认证权限) 分组操作(查询、创建、修改、移动用户到分组) (认证权限) 网页授权(基本授权,用户信息授权) (服务号、认证权限) 用户信息(查询用户基本信息、获取关注者列表) (认证权限) 多客服功能(客服管理、获取客服记录、客服会话管理) (认证权限) 媒体文件(上传、获取) (认证权限) 高级群发 (认证权限) 模板消息(设置所属行业、添加模板、发送模板消息) (服务号、认证权限) 卡券管理(创建、修改、删除、发放、门店管理等) (认证权限) 语义理解 (服务号、认证权限) 获取微信服务器IP列表 (初级权限) 微信JSAPI授权(获取ticket、获取签名) (初级权限) 数据统计(用户、图文、消息、接口分析数据) (认证权限) > 备注: > 初级权限:基本权限,任何正常的公众号都有此权限 > 菜单权限:正常的服务号、认证后的订阅号拥有此权限 > 认证权限:分为订阅号、服务号认证,如前缀服务号则仅认证的服务号有此权限,否则为认证后的订阅号、服务号都有此权限 > 支付权限:仅认证后的服务号可以申请此权限 初始化动作  $options = array(     'token'=>'tokenaccesskey', //填写你设定的key     'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey     'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询     'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥     );  $weObj = new Wechat($options); //创建实例对象  //TODO:调用$weObj各实例方法   标签:wechat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值