Files
yusheng-php/application/common/controller/BaseCom.php

115 lines
3.7 KiB
PHP
Raw Normal View History

2025-08-07 20:21:47 +08:00
<?php
namespace app\common\controller;
2025-12-24 16:21:07 +08:00
use think\Cache;
2025-08-07 20:21:47 +08:00
use think\Controller;
2025-08-11 15:55:11 +08:00
use think\Db;
2025-12-24 16:21:07 +08:00
use app\common\library\Token as TokenLib;
2025-08-07 20:21:47 +08:00
class BaseCom extends Controller
{
2025-12-24 16:21:07 +08:00
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){
2026-01-15 01:12:15 +08:00
return V(301, '系统维护中');
2025-12-24 16:21:07 +08:00
}
//检测是什么系统
2026-01-08 15:48:08 +08:00
$system = request()->header('system');
2025-12-24 16:21:07 +08:00
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);
2026-01-09 17:39:46 +08:00
}else{
$this->check_login($is_maintenance) ;
2025-12-24 16:21:07 +08:00
}
}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);
}
2025-08-07 20:21:47 +08:00
}