lihai-oa/app/api/ApiController.php

171 lines
4.3 KiB
PHP
Raw Normal View History

2023-10-27 16:34:12 +08:00
<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api;
use think\App;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use think\facade\Db;
2023-10-28 17:15:23 +08:00
use think\facade\Cache;
2023-10-27 16:34:12 +08:00
use think\Response;
2023-10-28 17:15:23 +08:00
use systematic\Systematic;
2023-10-27 16:34:12 +08:00
/**
* 控制器基础类
*/
abstract class ApiController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 分页数量
* @var string
*/
protected $pageSize = '';
/**
* jwt配置
* @var string
*/
protected $jwt_conf = [
'secrect' => 'lihaioa',
'iss' => 'ceshi-oa.lihaink.cn', //签发者 可选
'aud' => 'lihaioa', //接收该JWT的一方可选
'exptime' => 7 * 86400, //过期时间,这里设置7天
];
/**
* 构造方法
* @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->did = 0;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
//每页显示数据量
$this->pageSize = Request::param('page_size', \think\facade\Config::get('app.page_size'));
}
/**
* 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);
}
2023-10-28 17:15:23 +08:00
/**
* 验证用户访问权限
*/
protected function checkAuth()
{
$uid = JWT_UID;
2023-10-30 17:35:21 +08:00
$loginAdmin = Db::name('Admin')->where(['id' => $uid])->find();
if(!$loginAdmin['status']){
2023-10-28 17:15:23 +08:00
$this->apiError('用户已禁止登录');
}
$baseUrl = explode('/', request()->baseUrl());
$action = $baseUrl[count($baseUrl)-1] ?? '-' ;
$controller = $baseUrl[count($baseUrl)-2] ?? '-';
$controllerArray = explode('_', $controller);
2023-10-28 18:02:47 +08:00
$prefixMod = strtolower($controllerArray[0] ?? '-');
$conMod = strtolower($controllerArray[1] ?? '-');
2023-10-30 17:35:21 +08:00
$gougu = new Systematic();
$gougu->auth($uid);
$authListAll = Cache::get('RulesSrc0');
$authList = Cache::get('RulesSrc' . $uid);
2023-10-28 17:15:23 +08:00
$pathUrl = $prefixMod . '/' . $conMod . '/' . $action;
2023-10-30 17:35:21 +08:00
if (!in_array($pathUrl, $authList)) {
2023-11-04 14:27:57 +08:00
$this->apiError('用户无权限', [], 2);
2023-10-28 17:15:23 +08:00
}
}
2023-10-27 16:34:12 +08:00
}