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

211 lines
7.8 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;
2025-08-07 20:21:47 +08:00
//初始化
2025-12-24 16:21:07 +08:00
protected function _initializes()
2025-08-07 20:21:47 +08:00
{
//允许跨域
header("Access-Control-Allow-Origin: *"); // 允许所有域访问
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Access-Control-Max-Age: 3600");
//检测系统是否维护中
$is_maintenance = get_system_config_value('is_maintenance');
2025-12-05 20:47:00 +08:00
//获取内侧账号
$inside_uid = get_system_config_value('inside_uid');
2025-12-24 16:21:07 +08:00
2025-08-11 15:55:11 +08:00
//检测是什么系统
$system = input('system','');
if(empty($system)){
$system = request()->header('system');
2025-08-07 20:21:47 +08:00
}
2025-08-11 15:55:11 +08:00
//版本号
$version = input('App-Version','');
if(empty($version)){
$version = request()->header('App-Version');
}
2026-01-08 15:18:05 +08:00
$zhenshi_version = db::name('version')->where('type', 2)->value('newversion');
2025-08-11 15:55:11 +08:00
$result = version_compare($version,$zhenshi_version);
2025-08-12 14:05:47 +08:00
//请求的接口
$api = request()->controller().'/'.request()->action();
2025-08-11 15:55:11 +08:00
if($system == 'iOS' && $result > 0){
2025-08-12 14:05:47 +08:00
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{
$token = request()->header('token');
if (empty($token)) {
$token = input('token', '');
if(empty($token)){
return V(301, '登录失效');
}
}
$reslut = model('UserToken')->check_login_token($token);
if($reslut['code'] != 1) {
model('UserToken')->where('token', $token)->update(['token' => 1]);
return V($reslut['code'], $reslut['msg'],$reslut['data']);
} else {
$this->uid = $reslut['data'];
//定义一个常量
define('UID', $this->uid);
2025-12-05 20:47:00 +08:00
//检测ID是否在内测中 $inside_uid 是逗号分隔的ID $is_maintenance ==3 是内测
if($inside_uid && $is_maintenance == 3){
//先转为数组 不是内侧账号 返回301
$inside_uid = explode(',',$inside_uid);
if(!in_array($this->uid,$inside_uid)){
return V(0, '系统维护中');
}
}elseif($is_maintenance == 2){
return V(0, '系统维护中');
}
2025-08-12 14:05:47 +08:00
}
}
2025-08-11 15:55:11 +08:00
}else{
2025-08-12 11:16:48 +08:00
$token = request()->header('token');
2025-08-11 15:55:11 +08:00
if (empty($token)) {
2025-08-12 11:16:48 +08:00
$token = input('token', '');
2025-08-11 15:55:11 +08:00
if(empty($token)){
return V(301, '登录失效');
}
}
$reslut = model('UserToken')->check_login_token($token);
if($reslut['code'] != 1) {
model('UserToken')->where('token', $token)->update(['token' => 1]);
return V($reslut['code'], $reslut['msg'],$reslut['data']);
} else {
$this->uid = $reslut['data'];
//定义一个常量
define('UID', $this->uid);
2025-12-05 20:47:00 +08:00
//检测ID是否在内测中 $inside_uid 是逗号分隔的ID $is_maintenance ==3 是内测
if($inside_uid && $is_maintenance == 3){
//先转为数组 不是内侧账号 返回301
$inside_uid = explode(',',$inside_uid);
if(!in_array($this->uid,$inside_uid)){
return V(0, '系统维护中');
}
}elseif($is_maintenance == 2){
return V(0, '系统维护中');
}
2025-08-11 15:55:11 +08:00
}
2025-08-07 20:21:47 +08:00
}
}
2025-12-24 16:21:07 +08:00
/**
* 初始化方法
*/
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);
}
2025-08-07 20:21:47 +08:00
}