projectmanager/app/apiout/BaseController.php
2025-06-25 11:52:01 +08:00

224 lines
6.3 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @author 李志强
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\apiout;
use think\App;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use think\Response;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
protected $module;
protected $controller;
protected $action;
protected $param;
protected $uid;
protected $did;
protected $name;
/**
* 分页数量
* @var string
*/
protected $pageSize = '';
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
$this->module = strtolower(app('http')->getName());
$this->controller = strtolower($this->request->controller());
$this->action = strtolower($this->request->action());
$this->uid = 0;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
// 检测权限
$this->checkLogin();
$this->param = $this->request->param();
}
/**
* 显示当前登录账户信息
*/
protected function showLoginUserInfo()
{
$session_admin = get_config('app.session_admin');
if (Session::has($session_admin)) {
$loginUser = Session::get($session_admin);
// 输出当前登录账户信息
// echo '当前登录账户信息:' . $loginUser['username'];
}
}
/**
*验证用户登录
*/
protected function checkLogin()
{
// 定义一个不需要登录验证的接口白名单
$noNeedLogin = [
'apiout/businessinfo/product_info',
'apiout/businessinfo/bifill',
'apiout/index/getpicbedfolder',
'apiout/download/catelist',
'apiout/download/cateinfo',
];
// 当前请求的路径
$currentPath = $this->module . '/' . $this->controller . '/' . $this->action;
// 检查当前路径是否在白名单中
if (in_array($currentPath, $noNeedLogin)) {
return true;
}
if ($this->controller !== 'login' && $this->controller !== 'captcha') {
$session_admin = get_config('app.session_admin');
if (!Session::has($session_admin)) {
if ($this->request->isAjax()) {
return to_assign(404, '请先登录');
} else {
// redirect('/home/login/index.html')->send();
// exit;
return to_assign(404, '请先登录');
}
} else {
$loginUser = Session::get($session_admin);
$loginInfo = Db::name('Admin')->where('id', $loginUser['id'])->find();
$this->uid = $loginInfo['id'];
$params = [
'uid' => $this->uid,
'name' => $loginInfo['name'],
'thumb' => $loginInfo['thumb'],
'module' => $this->module,
'controller' => $this->controller,
'action' => $this->action,
'url' => $this->module . '/' . $this->controller . '/' . $this->action,
'version' => get_system_config('web', 'version')
];
View::assign('params', $params);
// 验证用户访问权限
if (($this->module == 'api') || ($this->module == 'home') || ($this->module == 'apiout')) {
return true;
} else {
$reg_pwd = Db::name('Admin')->where(['id' => $this->uid])->value('reg_pwd');
if ($reg_pwd !== '') {
redirect('/home/user/edit_password.html')->send();
exit;
}
if (!$this->checkAuth()) {
if ($this->request->isAjax()) {
return to_assign(202, '你没有权限,请联系管理员或者人事部');
} else {
echo '<div style="text-align:center;color:red;margin-top:20%;">你没有权限,请联系管理员或者人事部</div>';
exit;
}
}
}
}
}
}
/**
* Api处理成功结果返回方法
* @param $message
* @param null $redirect
* @param null $extra
* @return mixed
* @throws ReturnException
*/
protected function apiSuccess($msg = 'success', $data = [])
{
return $this->apiReturn($data, 0, $msg);
}
/**
* Api处理结果失败返回方法
* @param $error_code
* @param $message
* @param null $redirect
* @param null $extra
* @return mixed
* @throws ReturnException
*/
protected function apiError($msg = 'fail', $data = [], $code = 1)
{
return $this->apiReturn($data, $code, $msg);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
}