Files
spacestation/home/controllers/h5/paic/Base.php
T
2025-03-12 10:34:24 +08:00

136 lines
6.0 KiB
PHP

<?php
defined('APPPATH') or exit('No direct script access allowed');
abstract class Base extends HD_Controller
{
protected $log_dir = 'paic';
const OPEN_ID_SESSION_KEY = "OPEN_ID_SESSION_KEY";
const LOGIN_USER_INFO = "LOGIN_USER_INFO";
protected $open_id = '';
public $uid = '';
protected $white_login_method = []; //授权白名单
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('app/paic/app_paic_users_model', 'user_model');
$this->load->library('hd_exception');
if (checkua() == 'wx') {
if (!$this->session->has_userdata(self::OPEN_ID_SESSION_KEY) && !$this->session->userdata(self::LOGIN_USER_INFO)) {
$req = $this->set_auth();
$this->open_id = $req['openid'];
$this->session->set_userdata(self::OPEN_ID_SESSION_KEY, $this->open_id);
if ($this->open_id) {
$user = $this->user_model->get(['open_id' => $this->open_id, 'status' => App_paic_users_model::STATUS_NORMAL]);
if ($user) {
$this->session->set_userdata(self::LOGIN_USER_INFO, $user);
$this->uid = $user['id'];
}
}
} else {
$this->open_id = $this->session->userdata(self::OPEN_ID_SESSION_KEY);
$user = $this->session->userdata(self::LOGIN_USER_INFO);
$this->uid = $user['id'];
}
}
}
public function _remap($method)
{
try {
$user = $this->session->userdata(self::LOGIN_USER_INFO);
$this->uid = $user['id'];
if (!in_array($method, $this->white_login_method) && !$user && !$this->uid) {
if (checkua() == 'wx') {
if (!$this->session->userdata(self::OPEN_ID_SESSION_KEY)) {
$req = $this->set_auth();
$this->open_id = $req['openid'];
$this->session->set_userdata(self::OPEN_ID_SESSION_KEY, $this->open_id);
}
if ($this->open_id) {
$user = $this->user_model->get(['open_id' => $this->open_id, 'status' => App_paic_users_model::STATUS_NORMAL]);
if ($user) {
$this->session->set_userdata(self::LOGIN_USER_INFO, $user);
$this->uid = $user['id'];
}
}
}
header('Location:/h5/paic/login');
exit;
}
return $this->$method();
} catch (Exception $e) {//处理异常
$msg = $e->getMessage();
$data = array('heading' => 'Warning', 'message' => $msg);
return $this->load->view('errors/html/error_404', $data);
}
}
/**
* @param $url 回调url地址
* @param $auth 是否信息授权
* @return void
* @throws Hd_exception
*/
protected function set_auth($url = '', $auth = 0)
{
$this->load->config('wechat');
$config = $this->config->item('default');
$code = $this->input->get('code');
!$url && $url = http_host_com('home') . $_SERVER['REQUEST_URI'];
$auth && $url = $_SERVER['QUERY_STRING'] ? $url . "&auth={$auth}" : $url . "?auth={$auth}";
if ($code) {//授权码获取微信信息
$auth_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$config['appid']}&secret={$config['appSecret']}&code={$code}&grant_type=authorization_code";
$res = file_get_contents($auth_url);
$ret = json_decode($res, true);
if ($ret['errcode'] == 40163) { //code已被使用
$param_arr = explode("&", $_SERVER['QUERY_STRING']);
$query = [];
$query_string = '';
if ($param_arr) {
foreach ($param_arr as $item) {
if (strpos($item, 'code') === false && strpos($item, 'state=STATE')) {
$query[] = $item;
}
}
$query && $query_string = '?' . implode('&', $query);
}
$url = http_host_com('home') . $_SERVER['PATH_INFO'] . $query_string;
redirect($url);
}
$access_token = $ret['access_token'];
$openid = $ret['openid'];
$unionid = $ret['unionid'];
if ($this->input->get('auth') && $access_token) {
$u_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$u_ret = file_get_contents($u_info_url);
$ret = json_decode($u_ret, true);
}
if (!$openid) {
debug_log("[error]# " . $res, __FUNCTION__, $this->log_dir);
//异常处理
}
return $ret;
} elseif ($auth) {//信息授权获取用户微信昵称/头像
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&forcePopup=true";
redirect($auth_url);
} else {//静默授权获取用户openid
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
redirect($auth_url);
}
}
/**
* @param $view
*/
protected function show_view($view)
{
$this->load->view('h5/paic/header', $this->data);
$this->load->view($view);
$this->load->view('h5/paic/footer');
}
}