数据缓存处理
This commit is contained in:
@@ -2,16 +2,17 @@
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
use think\Cache;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use app\common\library\Token as TokenLib;
|
||||
|
||||
|
||||
class BaseCom extends Controller
|
||||
{
|
||||
public $uid = 0;
|
||||
public $redis = '';
|
||||
protected $uid = 0;
|
||||
//初始化
|
||||
protected function _initialize()
|
||||
protected function _initializes()
|
||||
{
|
||||
//允许跨域
|
||||
header("Access-Control-Allow-Origin: *"); // 允许所有域访问
|
||||
@@ -24,6 +25,7 @@ class BaseCom extends Controller
|
||||
|
||||
//获取内侧账号
|
||||
$inside_uid = get_system_config_value('inside_uid');
|
||||
|
||||
//检测是什么系统
|
||||
$system = input('system','');
|
||||
if(empty($system)){
|
||||
@@ -104,4 +106,106 @@ class BaseCom extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
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 = input('system','未知');
|
||||
if(empty($system)){
|
||||
$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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user