Files
2026-04-26 09:24:08 +08:00

58 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace app\smt\controller;
use think\App;
use think\exception\ValidateException;
use think\Validate;
abstract class BaseController
{
protected $app;
protected $request;
public function __construct(App $app)
{
$this->app = $app;
$this->request = $app->request;
}
protected function validate(array $data, $validate, array $message = [], bool $batch = false): array
{
if (is_array($validate)) {
$validator = new Validate();
$validator->rule($validate);
} else {
$validator = new $validate();
}
$validator->message($message)->batch($batch);
if (!$validator->check($data)) {
throw new ValidateException($validator->getError());
}
return $data;
}
protected function getCurrentSmtUserId(): int
{
$userId = (int) $this->request->middleware('smt_user_id', 0);
if ($userId <= 0) {
throw new \RuntimeException('未登录或登录已过期', 401);
}
return $userId;
}
protected function getCurrentSmtUser(): array
{
$user = $this->request->middleware('smt_user', []);
if (!is_array($user) || empty($user['id'])) {
throw new \RuntimeException('未登录或登录已过期', 401);
}
return $user;
}
}