<?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('授权出错,请重试!');
}
}
}