TP5前后端分离微信公众号授权登录

\application\mobile\controller\Wx.php

<?php
/**
 * Created by PhpStorm.
 * User: Pasa吴
 * Date: 2019/10/31
 * Time: 01:01
 */

namespace app\mobile\controller;

use think\Controller;

use think\facade\Session;
use app\common\model\User AS UserModel;

/**
 * 授权登录
 */
class Wx extends Controller
{
    protected $config;

    public function __construct()
    {
        parent::__construct();

        $this->config = [
            'appid'     => 'xxx',
            'appsecret' => 'xxx',
        ];
    }

    public function login()
    {
        $_url = $this->request->param('url');
        if(empty($_url)){
            die('url empty');
        }
        Session::set('_url',$_url);
        $openid = Session::get('openid');

        if (!empty($openid)) {
            header('location:' . $_url.'?openid=' . $openid);
        } else {
            $wechat = $this->config;
            $url    = 'https://open.weixin.qq.com/connect/oauth2/authorize';
            $url    .= '?appid=' . $wechat['appid'];
            $url    .= '&redirect_uri=' . urlencode('http://' . $_SERVER['HTTP_HOST'].'/mobile/wx/auth');
            $url    .= '&response_type=code';
            $url    .= '&scope=snsapi_userinfo';
            $url    .= '&state=' . rand(10, 99);
            $url    .= '#wechat_redirect';
            $this->redirect($url);
        }
    }

    public function auth($code = '')
    {
        $wechat = $this->config;
        $url    = 'https://api.weixin.qq.com/sns/oauth2/access_token';
        $url    .= '?appid=' . $wechat['appid'];
        $url    .= '&secret=' . $wechat['appsecret'];
        $url    .= '&code=' . $code;
        $url    .= '&grant_type=authorization_code';
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        if (isset($result['errcode'])) {
            $this->error('失效的参数,请重新授权');
        }
        //通过access_token和openid获取用户信息
        $url    = 'https://api.weixin.qq.com/sns/userinfo';
        $url    .= '?access_token=' . $result['access_token'];
        $url    .= '&openid=' . $result['openid'];
        $url    .= '&lang=zh_CN';
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        if (isset($result['errcode'])) {
            $this->error('失效的参数,请重新授权');
        }

        //查询对应的openid是否已经有了
        $user = UserModel::where(['openid' => $result['openid']])->find();
        $_url = Session::get('_url');
        if ($user) {
//            UserModel::where(['openid' => $result['openid']])->update(['user_token' => $user_token]);
            header('location:' . $_url.'?openid=' . $result['openid']);
        } else {
            UserModel::create([
                'openid'    => $result['openid'],
                'nickName'  => $result['nickname'],
                'avatarUrl' => $result['headimgurl'],
            ]);
            header('location:' . $_url.'?openid=' . $openid);
        }
    }


}

使用方法:
http://你的域名/mobile/wx/login?url=前端接收openid地址

例如 https://king.pasawu.top/mobile/wx/login?url=https://pasawu.top
最后会跳转到https://pasawu.top并且带上openid
https://pasawu.top/?openid=o8rD4vgyRnRasdastqF7ipZgEBDx8U

Pasa吴PHP博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论