基类和工具类
This commit is contained in:
23
app/BaseApp/BaseClient.php
Normal file
23
app/BaseApp/BaseClient.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\BaseApp;
|
||||
|
||||
class BaseClient
|
||||
{
|
||||
private static mixed $_instance;
|
||||
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
}
|
||||
165
app/BaseApp/BaseController.php
Executable file
165
app/BaseApp/BaseController.php
Executable file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\BaseApp;
|
||||
|
||||
use App\Service\UtilsService;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class BaseController
|
||||
{
|
||||
protected array $insertField = [];
|
||||
protected array $notRequest = [];
|
||||
protected array $updateField = [];
|
||||
protected $service;
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @Method GET
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function list(): JsonResponse
|
||||
{
|
||||
return jok(
|
||||
$this->service->list(),
|
||||
'列表获取成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下拉列表
|
||||
* @Method GET
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function option(): mixed
|
||||
{
|
||||
return jok(
|
||||
$this->service->option(),
|
||||
'列表获取成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情
|
||||
*
|
||||
* @Method GET
|
||||
* @return JsonResponse
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function detail(): JsonResponse
|
||||
{
|
||||
$id = request()->get('id');
|
||||
if (empty($id)) UtilsService::getInstance()->notFound('参数错误');
|
||||
return jok(
|
||||
$this->service->detail($id),
|
||||
'详情获取成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建
|
||||
*
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(): JsonResponse
|
||||
{
|
||||
$params = $this->checkRequiredFields(request()->post());
|
||||
return jok(
|
||||
$this->service->create($params),
|
||||
'创建成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新
|
||||
*
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(): JsonResponse
|
||||
{
|
||||
$params = $this->checkRequiredFields(request()->post(), 'update');
|
||||
$id = $params['id'];
|
||||
unset($params['id']);
|
||||
return jok(
|
||||
$this->service->update($id, $params),
|
||||
'更新成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(): JsonResponse
|
||||
{
|
||||
$id = request()->post('ids');
|
||||
if (empty($id)) UtilsService::getInstance()->notFound('参数错误');
|
||||
return jok(
|
||||
$this->service->delete($id),
|
||||
'删除成功'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查必填字段
|
||||
*
|
||||
* @Method NO
|
||||
* @param array $data
|
||||
* @param string $checkType
|
||||
* @param bool $throw
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkRequiredFields(array $data = [], string $checkType = 'create', bool $throw = true): array
|
||||
{
|
||||
$checkFields = $checkType == 'create' ? $this->insertField : $this->updateField;
|
||||
|
||||
if (!empty($this->notRequest)) {
|
||||
$checkFields = array_merge($this->notRequest, $checkFields);
|
||||
}
|
||||
|
||||
$requiredFields = [];
|
||||
$result = [];
|
||||
foreach ($checkFields as $fieldName) {
|
||||
$checkValue = $data[$fieldName] ?? '';
|
||||
if (is_array($checkValue)) {
|
||||
$checkValue = json_encode($checkValue);
|
||||
}
|
||||
if (mb_strlen(strval($checkValue)) <= 0 && !in_array($fieldName, $this->notRequest)) {
|
||||
$requiredFields[] = $fieldName;
|
||||
} else {
|
||||
$result[$fieldName] = $checkValue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($throw && $requiredFields) {
|
||||
UtilsService::getInstance()->notFound('未填写的字段: ' . implode(',', $requiredFields));
|
||||
}
|
||||
|
||||
if (!empty($requiredFields)) {
|
||||
return $requiredFields;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
26
app/BaseApp/BaseModel.php
Executable file
26
app/BaseApp/BaseModel.php
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\BaseApp;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BaseModel extends Model
|
||||
{
|
||||
protected $connection = 'mysql';
|
||||
public $timestamps = false;
|
||||
public function getCreatedAtAttribute($value): string
|
||||
{
|
||||
if (empty($value)) {
|
||||
return '';
|
||||
}
|
||||
return date('Y-m-d H:i:s', $value);
|
||||
}
|
||||
// 定义访问器,将 created_at 字段格式化为指定格式
|
||||
public function getUpdatedAtAttribute($value): string
|
||||
{
|
||||
if (empty($value)) {
|
||||
return '';
|
||||
}
|
||||
return date('Y-m-d H:i:s', $value);
|
||||
}
|
||||
}
|
||||
70
app/BaseApp/BaseService.php
Executable file
70
app/BaseApp/BaseService.php
Executable file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\BaseApp;
|
||||
|
||||
use App\Models\UserModel;
|
||||
use App\Service\UtilsService;
|
||||
|
||||
class BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var mixed 单例实例,确保该类只有一个全局实例
|
||||
*/
|
||||
private static mixed $_instance;
|
||||
/**
|
||||
* @var object 工具类对象,用于提供通用的功能方法
|
||||
*/
|
||||
protected object $utils;
|
||||
/**
|
||||
* @var int 用户ID,默认值为0,表示未登录状态
|
||||
*/
|
||||
protected int $userId = 0;
|
||||
/**
|
||||
* @var array 用户信息
|
||||
*/
|
||||
protected array $userInfo = [];
|
||||
/**
|
||||
* @var object 数据模型对象,用于数据库操作
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
protected $isAuth = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if ($this->isAuth) {
|
||||
// 如果是home的接口可以不执行这个方法
|
||||
if (!\request()->is('api/home/*')) {
|
||||
$this->authUser();
|
||||
}
|
||||
}
|
||||
$this->utils = UtilsService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
|
||||
private function authUser()
|
||||
{
|
||||
$token = \request()->header('Authorization');
|
||||
$token = explode(' ', $token)[1];
|
||||
$userModel = UserModel::where('session_key', $token)->first();
|
||||
if (!$userModel) {
|
||||
jexpire('token失效');
|
||||
}
|
||||
$this->userId = $userModel->id;
|
||||
$this->userInfo = $userModel->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user