微信公众号(服务号)-网页授权

本文详细介绍了一种基于ThinkPHP框架实现的微信登录与用户授权流程。从初始化配置到获取code,再到通过code换取access_token及用户基本信息,最后完成用户信息的数据库存储与更新,整个过程清晰明了。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<?php

namespace app\student\controller;

use think\Controller;
use think\Db;

class Weixin extends Controller
{
    public $appid = "";
    public $appsecret = "";
    public $access_token = "";

    public function __construct()
    {
        $wx = Db::name('wx');
        $res = $wx->find();
        $this->appsecret = $res['appsecret'];
        $this->appid = $res['appid'];
    }

    /**
     * 用户同意授权,获取code
     * cn
     * @param $back_url
     */
    public function _auth($back_url)
    {
        $back_url = request()->domain() . $back_url;
        $back_url = urlencode($back_url);
        $login_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$back_url&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        $this->redirect($login_url);
    }

    /**
     * https请求
     * cn
     * @param $url
     * @param null $data
     * @return mixed
     */
    public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}

class Auth extends Controller
{

/**
* 微信登录
* cn
*/
public function auto_login()
{
$type = input('type');
if (is_wechat() || $type == 'wx') {
$weixin = new Weixin();
$weixin->_auth(url('wx_back'));
} else {
$this->error('请在微信中打开');
}
}

/**
* 授权回调
* cn
*/
public function wx_back()
{
try {
$weixin = new Weixin();
//通过code换取网页授权access_token,openid
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$weixin->appid&secret=$weixin->appsecret&code=" . $_GET['code'] . "&grant_type=authorization_code";
$res = $weixin->https_request($url);
$res = (json_decode($res, true));
//用户基本信息
$wx_user_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $res['access_token'] . "&openid=" . $res['openid'] . "&lang=zh_CN";
$row = $weixin->https_request($wx_user_url);
$row = (json_decode($row, true));
session('wx', $row);
session('login_type', 'wx');
$openid = $res['openid'];
//是否存在
$student = Db::name('student');
$student_wx = Db::name('student_wx');
$user = $student_wx->where('openid', $openid)->find();
if ($user) {
//存在
if ($user['phone_flag'] == 0) {
//未绑定
session('wx_id', $user['wx_id']);
session('wx_info', $user);
$this->redirect(url('student/home/bind'));
} else {
//已绑定
$s_data = $student->where('wx_id', $user['wx_id'])->find();
$student->where('student_id', $s_data['student_id'])->update(['login_num' => $s_data['login_num'] + 1]);
$stu_data = $student->where('student_id', $s_data['student_id'])->find();
$stu_data['openid'] = $user['openid'];
$stu_data['nickname'] = $user['nickname'];
$stu_data['phone_flag'] = $user['phone_flag'];
session('student_id', $stu_data['student_id']);
session('student_info', $stu_data);
//扫码
if (session('wid')) {
$this->redirect(url('search/code_study'));
} else {
$this->redirect(url('student/home/index'));
}
}
} else {
//不存在,未绑定
$user_arr = [];
$user_arr['nickname'] = $row['nickname'];
$user_arr['openid'] = $row['openid'];
$uid = $student_wx->insertGetId($user_arr);
$user_data = $student_wx->where('wx_id', $uid)->find();
session('wx_id', $uid);
session('wx_info', $user_data);
$this->redirect(url('student/home/bind'));
}
} catch (Exception $exception) {
$this->error('授权出错,请重试!');
}
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值