crud调整
This commit is contained in:
@@ -2,97 +2,116 @@
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\validate\RoleValidate;
|
||||
use app\common\service\RoleService;
|
||||
use support\Request;
|
||||
use app\common\controller\BaseController;
|
||||
use app\admin\service\RoleService;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
|
||||
class RoleController extends \app\common\controller\BaseController
|
||||
class RoleController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取角色列表
|
||||
*/
|
||||
public function list(Request $request)
|
||||
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
$params = $request->all();
|
||||
$result = (new RoleService())->getRoleList($params);
|
||||
// 定义后处理器
|
||||
$postProcessor = function($formattedData) {
|
||||
if (isset($formattedData['created_at']))
|
||||
$formattedData['created_at'] = date('Y-m-d H:i:s', $formattedData['created_at']);
|
||||
if (isset($formattedData['updated_at']))
|
||||
$formattedData['updated_at'] = date('Y-m-d H:i:s', $formattedData['updated_at']);
|
||||
return $formattedData;
|
||||
};
|
||||
return $this->success($this->transformData($result, $postProcessor), '角色列表获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
$this->service = new RoleService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色详情
|
||||
*/
|
||||
public function detail(Request $request)
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
$result = (new RoleService())->getRoleDetail($roleId);
|
||||
return $this->success($result, '角色详情获取成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加角色
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_create');
|
||||
$result = (new RoleService())->createRole($data);
|
||||
return $this->success($result, '角色创建成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 获取角色列表
|
||||
// */
|
||||
// public function list(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $params = $request->all();
|
||||
// $result = (new RoleService())->getRoleList($params);
|
||||
// // 定义后处理器
|
||||
// $postProcessor = function($formattedData) {
|
||||
// if (isset($formattedData['created_at']))
|
||||
// $formattedData['created_at'] = date('Y-m-d H:i:s', $formattedData['created_at']);
|
||||
// if (isset($formattedData['updated_at']))
|
||||
// $formattedData['updated_at'] = date('Y-m-d H:i:s', $formattedData['updated_at']);
|
||||
// return $formattedData;
|
||||
// };
|
||||
// return $this->success($this->transformData($result, $postProcessor), '角色列表获取成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 修改角色
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate(new RoleValidate(), $request->post(), 'role_update');
|
||||
$result = (new RoleService())->updateRole($data);
|
||||
return $this->success($result, '角色更新成功');
|
||||
} catch (\think\exception\ValidateException $e) {
|
||||
return $this->error($e->getError(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 获取角色详情
|
||||
// */
|
||||
// public function detail(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $roleId = $request->get('role_id');
|
||||
// if (!$roleId) {
|
||||
// return $this->error('缺少角色ID', 422);
|
||||
// }
|
||||
// $result = (new RoleService())->getRoleDetail($roleId);
|
||||
// return $this->success($result, '角色详情获取成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
try {
|
||||
$roleId = $request->get('role_id');
|
||||
if (!$roleId) {
|
||||
return $this->error('缺少角色ID', 422);
|
||||
}
|
||||
(new RoleService())->deleteRole($roleId);
|
||||
return $this->success([], '角色删除成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 增加角色
|
||||
// */
|
||||
// public function create(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $data = $this->validate(new RoleValidate(), $request->post(), 'role_create');
|
||||
// $result = (new RoleService())->createRole($data);
|
||||
// return $this->success($result, '角色创建成功');
|
||||
// } catch (\think\exception\ValidateException $e) {
|
||||
// return $this->error($e->getError(), 422);
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 修改角色
|
||||
// */
|
||||
// public function update(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $data = $this->validate(new RoleValidate(), $request->post(), 'role_update');
|
||||
// $result = (new RoleService())->updateRole($data);
|
||||
// return $this->success($result, '角色更新成功');
|
||||
// } catch (\think\exception\ValidateException $e) {
|
||||
// return $this->error($e->getError(), 422);
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 删除角色
|
||||
// */
|
||||
// public function delete(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $roleId = $request->get('role_id');
|
||||
// if (!$roleId) {
|
||||
// return $this->error('缺少角色ID', 422);
|
||||
// }
|
||||
// (new RoleService())->deleteRole($roleId);
|
||||
// return $this->success([], '角色删除成功');
|
||||
// } catch (\Throwable $e) {
|
||||
// return $this->error($e->getMessage());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\validate\CorpValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Corp;
|
||||
use support\Request;
|
||||
|
||||
class CorpService
|
||||
{
|
||||
@@ -15,6 +15,19 @@ class CorpService
|
||||
*/
|
||||
protected string $modelClass = Corp::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = CorpValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 如果需要自定义,比如强制加一个 where['status']=1,
|
||||
@@ -27,33 +40,6 @@ class CorpService
|
||||
// return $config;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 如果需要自定义“哪些字段可筛选”或额外联表,
|
||||
* 只需重写 buildQueryConfig:
|
||||
*/
|
||||
// protected function buildQueryConfig(Request $request): array
|
||||
// {
|
||||
// $config = parent::buildQueryConfig($request);
|
||||
// // 强制只看 status=1 的记录
|
||||
// $config['where']['status'] = 1;
|
||||
// // 自动 eager load company relation
|
||||
// $config['with'][] = 'company';
|
||||
// return $config;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 复杂查询,覆盖 complexQuery 方法
|
||||
*/
|
||||
// protected function complexQuery(array $params): array
|
||||
// {
|
||||
// return $this->logicComplex(function($q) use ($params) {
|
||||
// // 自定义 join、where、select
|
||||
// return $q->where('status', 1)
|
||||
// ->orderBy('created_at','desc')
|
||||
// ->paginate($params['limit'] ?? 15);
|
||||
// })->toArray();
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 复杂查询示例:
|
||||
// * 只要在 URL 里添加 ?complex_query=1,
|
||||
@@ -100,23 +86,4 @@ class CorpService
|
||||
// $res['data'] = array_map(fn($i)=>[...], $res['data']);
|
||||
// return $res;
|
||||
// }
|
||||
|
||||
// protected function afterServiceShow($item)
|
||||
// {
|
||||
// // 对详情对象处理
|
||||
// $item['extra']=123;
|
||||
// return $item;
|
||||
// }
|
||||
|
||||
// afterServiceStore/Update/Destroy 同理
|
||||
|
||||
|
||||
// protected function afterServiceList(mixed $result): array
|
||||
// {
|
||||
// if ($result['records']) {
|
||||
// $result['records'] = array_map(fn($i) => [...$i], $result['records']);
|
||||
// }
|
||||
|
||||
// return $result;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\validate\RoleValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Role;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Role::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = RoleValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
}
|
||||
@@ -46,4 +46,14 @@ class Corp extends Model
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
}
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'abbr',
|
||||
'tenant_key',
|
||||
'sort',
|
||||
'status'
|
||||
];
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class BasicDataService
|
||||
public function checkAndCacheBaseParams($loginType, $systemCode, $corpAbbr)
|
||||
{
|
||||
if (!$loginType && !$systemCode && !$corpAbbr) {
|
||||
throw new BusinessException(htmlpage());
|
||||
throw new BusinessException('header缺少必要参数');
|
||||
}
|
||||
|
||||
// 检查并缓存登录类型
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\logic\RoleLogic;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
protected $roleLogic;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->roleLogic = (new RoleLogic());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色列表
|
||||
*/
|
||||
public function getRoleList(array $params)
|
||||
{
|
||||
return $this->roleLogic->getRoleList($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色详情
|
||||
*/
|
||||
public function getRoleDetail(int $roleId)
|
||||
{
|
||||
return $this->roleLogic->getRoleDetail($roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
*/
|
||||
public function createRole(array $data)
|
||||
{
|
||||
return $this->roleLogic->createRole($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色
|
||||
*/
|
||||
public function updateRole(array $data)
|
||||
{
|
||||
return $this->roleLogic->updateRole($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*/
|
||||
public function deleteRole(int $roleId)
|
||||
{
|
||||
return $this->roleLogic->deleteRole($roleId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\traits;
|
||||
|
||||
use think\Validate;
|
||||
use support\Request;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
trait RequestTrait
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 验证数据
|
||||
*
|
||||
* @param Validate $validator 验证器实例
|
||||
* @param array $data 待验证的数据
|
||||
* @param string $scene 验证场景
|
||||
* @return array 验证后的数据
|
||||
* @throws BusinessException 验证失败时抛出业务异常
|
||||
*/
|
||||
public function validate(Validate $validator, array $data, string $scene = ''): array
|
||||
{
|
||||
// 如果指定了验证场景,并且验证器支持场景方法,则设置验证场景
|
||||
if ($scene && method_exists($validator, 'scene')) {
|
||||
$validator->scene($scene);
|
||||
}
|
||||
// 执行数据验证
|
||||
if (!$validator->check($data)) {
|
||||
// 验证失败,抛出业务异常,异常信息为验证器返回的错误信息
|
||||
throw new BusinessException($validator->getError());
|
||||
}
|
||||
// 验证成功,返回原始数据
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,22 +11,26 @@ use support\Request;
|
||||
trait CrudControllerTrait
|
||||
{
|
||||
/**
|
||||
* 获取资源列表(GET /resource)
|
||||
* 获取列表
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回分页数据及总数
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$raw = $this->service->serviceList($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
||||
? [$this, 'indexFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
try {
|
||||
$raw = $this->service->serviceList($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
||||
? [$this, 'indexFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定资源详情(GET /resource/{id})
|
||||
* 获取详情
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @param int|string $id 资源 ID
|
||||
@@ -34,47 +38,65 @@ trait CrudControllerTrait
|
||||
*/
|
||||
public function show(Request $request)
|
||||
{
|
||||
$raw = $this->service->serviceShow($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'showFormat')
|
||||
? [$this, 'showFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
try {
|
||||
$raw = $this->service->serviceShow($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'showFormat')
|
||||
? [$this, 'showFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新资源(POST /resource)
|
||||
* 创建
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回创建成功的数据或信息
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$res = $this->service->serviceStore($request);
|
||||
return $this->success($res, '创建成功');
|
||||
try {
|
||||
$data = $this->validate($this->service->getValidator(), $request->post(), 'create');
|
||||
$res = $this->service->serviceStore($request, $data);
|
||||
return $this->success($res, '创建成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新指定资源(PUT/PATCH /resource/{id})
|
||||
* 更新
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回更新成功的数据或信息
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$res = $this->service->serviceUpdate($request);
|
||||
return $this->success($res, '更新成功');
|
||||
try {
|
||||
$data = $this->validate($this->service->getValidator(), $request->post(), 'update');
|
||||
$res = $this->service->serviceUpdate($request, $data);
|
||||
return $this->success([], '更新成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定资源(DELETE /resource/{id})
|
||||
* 删除
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回删除操作结果
|
||||
*/
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$count = $this->service->serviceDestroy($request);
|
||||
return $this->success([], "删除了 {$count} 条");
|
||||
try {
|
||||
$count = $this->service->serviceDestroy($request);
|
||||
return $this->success([], "成功删除了 {$count} 条");
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,14 @@ trait CrudServiceTrait
|
||||
|
||||
/** 具体 Service 里定义:protected string $modelClass = YourModel::class; */
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/** 分页列表 */
|
||||
public function serviceList(Request $request)
|
||||
{
|
||||
@@ -52,18 +60,18 @@ trait CrudServiceTrait
|
||||
}
|
||||
|
||||
/** 创建 */
|
||||
public function serviceStore(Request $request)
|
||||
public function serviceStore(Request $request, array $data = [])
|
||||
{
|
||||
$data = $request->post();
|
||||
// $data = $request->post();
|
||||
$dl = $this->getDataLimitFromRequest($request);
|
||||
$created = $this->logicCreate($data, $dl);
|
||||
return $this->postProcess('Store', $created);
|
||||
}
|
||||
|
||||
/** 更新 */
|
||||
public function serviceUpdate(Request $request)
|
||||
public function serviceUpdate(Request $request, array $data = [])
|
||||
{
|
||||
$data= $request->post();
|
||||
// $data= $request->post();
|
||||
$config = $this->buildQueryConfig($request, 'update');
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
return $this->postProcess('Update', $updated);
|
||||
@@ -100,13 +108,13 @@ trait CrudServiceTrait
|
||||
switch ($scene) {
|
||||
case 'default':
|
||||
$config['where']['status'] = 1;
|
||||
$config['order']['sort'] = ['desc'];
|
||||
$config['order']['id'] = ['desc'];
|
||||
$config['order']['sort'] = 'desc';
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'index':
|
||||
$config['where']['status'] = 1;
|
||||
$config['order']['sort'] = ['desc'];
|
||||
$config['order']['id'] = ['desc'];
|
||||
$config['order']['sort'] = 'desc';
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class CorpValidate extends Validate
|
||||
{
|
||||
protected $rule = [
|
||||
'id' => 'require|number',
|
||||
'name' => 'require|max:255',
|
||||
'abbr' => 'require|in:hesu,xinyouhe,paika,simate,mbaf',
|
||||
'tenant_key' => 'string',
|
||||
'sort' => 'number',
|
||||
'status' => 'require|in:0,1'
|
||||
];
|
||||
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.number' => 'ID必须是数字',
|
||||
'name.require' => '企业名称不能为空',
|
||||
'name.max' => '企业名称长度不能超过255个字符',
|
||||
'abbr.require' => '企业缩写不能为空',
|
||||
'abbr.in' => '企业缩写必须是 hesu,xinyouhe,paika,simate,mbaf 中的一个',
|
||||
'tenant_key.string' => '飞书企业标识必须是字符串',
|
||||
'sort.number' => '排序值必须是数字',
|
||||
'status.require' => '状态不能为空',
|
||||
'status.in' => '状态必须是0或1'
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'create' => ['name', 'abbr', 'tenant_key', 'sort', 'status'],
|
||||
'update' => ['id', 'name', 'abbr', 'tenant_key', 'sort', 'status']
|
||||
];
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class RoleValidate extends Validate
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'role_create' => ['system_id', 'name', 'desc', 'sort'],
|
||||
'role_update' => ['id', 'system_id', 'name', 'desc', 'sort', 'status'],
|
||||
'create' => ['system_id', 'name', 'desc', 'sort'],
|
||||
'update' => ['id', 'system_id', 'name', 'desc', 'sort', 'status'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ use ReflectionClass;
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
use app\common\service\BasicDataService;
|
||||
|
||||
class AuthCheck implements MiddlewareInterface
|
||||
{
|
||||
@@ -16,40 +15,11 @@ class AuthCheck implements MiddlewareInterface
|
||||
$properties = $controller->getDefaultProperties();
|
||||
$noNeedLogin = $properties['noNeedLogin'] ?? [];
|
||||
$noNeedAuth = $properties['noNeedAuth'] ?? [];
|
||||
$noNeedHeaderData = $properties['noNeedHeaderData'] ?? [];
|
||||
|
||||
// header数据
|
||||
if (!in_array($request->action, $noNeedHeaderData)) { //如果需要header数据
|
||||
try {
|
||||
$corpAbbr = $request->header('Fdesk-Corp-Abbr');
|
||||
$systemCode = $request->header('Fdesk-System-Code');
|
||||
$loginType = $request->header('Fdesk-Login-Type');
|
||||
$basicDataService = new BasicDataService();
|
||||
if ($basicDataService->checkAndCacheBaseParams($loginType, $systemCode, $corpAbbr)) {
|
||||
$request->login_type = $loginType;
|
||||
$request->system_code = $systemCode;
|
||||
$request->corp_abbr = $corpAbbr;
|
||||
$request->basic_data = [
|
||||
'login_type' => $loginType,
|
||||
'system_code' => $systemCode,
|
||||
'corp_abbr' => $corpAbbr
|
||||
];
|
||||
}
|
||||
return $handler($request);
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
$body = [
|
||||
'status' => 'fail',
|
||||
'code' => 401,
|
||||
'message' => $th->getMessage(),
|
||||
'data' => [],
|
||||
];
|
||||
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
}
|
||||
|
||||
// 不登陆&不鉴权
|
||||
if (!in_array($request->action, $noNeedLogin)) { //如果需要登陆&鉴权
|
||||
|
||||
if (in_array($request->action, $noNeedLogin)) {
|
||||
// 白名单
|
||||
return $handler($request);
|
||||
} else {
|
||||
try {
|
||||
$authHeader = $request->header('Fdesk-Auth-Token');
|
||||
if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
|
||||
@@ -75,22 +45,6 @@ class AuthCheck implements MiddlewareInterface
|
||||
}
|
||||
}
|
||||
|
||||
// 登陆&不鉴权
|
||||
// if (!in_array($request->action, $noNeedAuth)) { //如果需要登陆&不鉴权
|
||||
// try {
|
||||
|
||||
// return $handler($request);
|
||||
// } catch (\Throwable $e) {
|
||||
// $body = [
|
||||
// 'status' => 'fail',
|
||||
// 'code' => 401,
|
||||
// 'message' => $e->getMessage(),
|
||||
// 'data' => [],
|
||||
// ];
|
||||
// return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
||||
// }
|
||||
// }
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,9 @@ class BasicData implements MiddlewareInterface
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$controller = new ReflectionClass($request->controller);
|
||||
$noNeedHeader = $controller->getDefaultProperties()['noNeedHeader'] ?? [];
|
||||
$noNeedHeaderData = $controller->getDefaultProperties()['noNeedHeaderData'] ?? [];
|
||||
|
||||
if (in_array($request->action, $noNeedHeader) || in_array($request->path(), config('whitelist.basi_cdata'))) {
|
||||
if (in_array($request->action, $noNeedHeaderData)) {
|
||||
// 白名单
|
||||
return $handler($request);
|
||||
} else {
|
||||
@@ -35,7 +35,13 @@ class BasicData implements MiddlewareInterface
|
||||
}
|
||||
return $handler($request);
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
$body = [
|
||||
'status' => 'fail',
|
||||
'code' => 403,
|
||||
'message' => $th->getMessage(),
|
||||
'data' => [],
|
||||
];
|
||||
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
return [
|
||||
'' => [
|
||||
\app\middleware\AccessControl::class,
|
||||
\app\middleware\BasicData::class,
|
||||
\app\middleware\AuthCheck::class
|
||||
]
|
||||
];
|
||||
+10
-6
@@ -28,6 +28,7 @@ use Webman\Route;
|
||||
// 调试
|
||||
Route::any('/perm', [app\controller\IndexController::class, 'perm']);
|
||||
|
||||
// 公共
|
||||
Route::group('/common', function () {
|
||||
Route::group('/auth', function () {
|
||||
Route::any('/login', [app\common\controller\AuthController::class, 'login']);
|
||||
@@ -50,20 +51,23 @@ Route::group('/common', function () {
|
||||
});
|
||||
});
|
||||
|
||||
// 后台管理
|
||||
Route::group('/admin', function () {
|
||||
// 角色
|
||||
Route::group('/role', function () {
|
||||
Route::any('/list', [app\admin\controller\RoleController::class, 'list']);
|
||||
Route::any('/detail', [app\admin\controller\RoleController::class, 'detail']);
|
||||
Route::any('/create', [app\admin\controller\RoleController::class, 'create']);
|
||||
Route::any('/list', [app\admin\controller\RoleController::class, 'index']);
|
||||
Route::any('/show', [app\admin\controller\RoleController::class, 'show']);
|
||||
Route::any('/store', [app\admin\controller\RoleController::class, 'store']);
|
||||
Route::any('/update', [app\admin\controller\RoleController::class, 'update']);
|
||||
Route::any('/delete', [app\admin\controller\RoleController::class, 'delete']);
|
||||
Route::any('/destroy', [app\admin\controller\RoleController::class, 'destroy']);
|
||||
});
|
||||
// 公司
|
||||
Route::group('/corp', function () {
|
||||
Route::any('/list', [app\admin\controller\CorpController::class, 'index']);
|
||||
Route::any('/show', [app\admin\controller\CorpController::class, 'show']);
|
||||
// Route::any('/create', [app\admin\controller\CorpController::class, 'store']);
|
||||
Route::any('/store', [app\admin\controller\CorpController::class, 'store']);
|
||||
Route::any('/update', [app\admin\controller\CorpController::class, 'update']);
|
||||
// Route::any('/delete', [app\admin\controller\CorpController::class, 'destroy']);
|
||||
Route::any('/destroy', [app\admin\controller\CorpController::class, 'destroy']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user