113 lines
3.6 KiB
PHP
113 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace app\common\controller;
|
||
|
||
use think\Cache;
|
||
use think\Controller;
|
||
use think\Db;
|
||
use app\common\library\Token as TokenLib;
|
||
|
||
|
||
class BaseCom extends Controller
|
||
{
|
||
protected $uid = 0;
|
||
|
||
/**
|
||
* 初始化方法
|
||
*/
|
||
protected function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
|
||
//检测系统是否维护中
|
||
//先从cache中获取
|
||
$is_maintenance = Cache::get('is_maintenance');
|
||
if(!$is_maintenance){
|
||
$is_maintenance = get_system_config_value('is_maintenance');
|
||
//缓存 并设置缓存时间
|
||
Cache::set('is_maintenance',$is_maintenance,3600);
|
||
}
|
||
|
||
if($is_maintenance == 2){
|
||
return V(0, '系统维护中');
|
||
}
|
||
|
||
//检测是什么系统
|
||
$system = request()->header('system');
|
||
|
||
if ($system == 'iOS') {
|
||
//版本号
|
||
$version = input('App-Version','');
|
||
if(empty($version)){
|
||
$version = request()->header('App-Version');
|
||
}
|
||
|
||
$api_versions = db::name('version')->where(['type' => 2, 'status' => 1])->order('id', 'desc')->find();
|
||
//app的版本和用户使用的当前版本比对
|
||
//$api_versions['newversion'] 是数据库当前的版本 也是用户使用的版本
|
||
//$app_version 有可能是appstore里面的审核版本 审核版本比用户的版本高
|
||
//审核版本给前端返回1
|
||
$result = version_compare($api_versions['newversion'],$version);
|
||
if ($result < 0) {//-1:前面版本小于后面版本,0:相等,1:前面版本大于后面版本
|
||
//请求的接口
|
||
$api = request()->controller().'/'.request()->action();
|
||
if($api == 'Index/index_banner' || $api == 'Index/room_type_list' || $api == 'Index/room_list' || $api == 'UserZone/expand_zone' || $api == 'UserZone/zone_list' || $api == 'UserZone/topic_list' || $api == 'UserZone/get_zone_topic'){
|
||
$this->uid = 0;
|
||
//定义一个常量
|
||
define('UID', $this->uid);
|
||
}
|
||
}else{
|
||
$this->check_login($is_maintenance) ;
|
||
}
|
||
}else{
|
||
$this->check_login($is_maintenance) ;
|
||
}
|
||
}
|
||
|
||
|
||
//检测登录
|
||
public function check_login($is_maintenance)
|
||
{
|
||
// 2. 从请求中获取Token(支持多种方式)
|
||
$token = request()->header('token');
|
||
if (empty($token)) {
|
||
$token = input('token', '');
|
||
}
|
||
|
||
if (empty($token)) {
|
||
// 3. Token为空,拒绝访问
|
||
return V(0, 'Token不能为空');
|
||
}
|
||
|
||
// 4. 核心验证:检查Token是否有效
|
||
$tokenData = TokenLib::get($token);
|
||
if (!$tokenData) {
|
||
// Token不存在或已过期
|
||
return V(301, '登录失效,请重新登录');
|
||
}
|
||
|
||
// 5. 验证成功,提取并存储用户信息
|
||
$this->uid = $tokenData['user_id'];
|
||
|
||
//获取内侧账号
|
||
//先从cache中获取
|
||
$inside_uid = Cache::get('inside_uid');
|
||
if(!$inside_uid){
|
||
$inside_uid = get_system_config_value('inside_uid');
|
||
//缓存 并设置缓存时间
|
||
Cache::set('inside_uid',$inside_uid,3600);
|
||
}
|
||
|
||
if($inside_uid && $is_maintenance == 3){
|
||
//先转为数组 不是内侧账号 返回301
|
||
$inside_uid = explode(',',$inside_uid);
|
||
if(!in_array($this->uid,$inside_uid)){
|
||
return V(0, '系统维护中');
|
||
}
|
||
}
|
||
//定义一个常量
|
||
define('UID', $this->uid);
|
||
|
||
}
|
||
|
||
} |