mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-24 11:04:26 +08:00
重构初始化
This commit is contained in:
113
app/service/system/access/AdminAuthService.php
Normal file
113
app/service/system/access/AdminAuthService.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace app\service\system\access;
|
||||
|
||||
use app\common\base\BaseService;
|
||||
use app\common\constant\AuthConstant;
|
||||
use app\common\constant\CommonConstant;
|
||||
use app\common\util\JwtTokenManager;
|
||||
use app\exception\ValidationException;
|
||||
use app\model\admin\AdminUser;
|
||||
use app\repository\system\user\AdminUserRepository;
|
||||
|
||||
/**
|
||||
* 管理员认证服务。
|
||||
*
|
||||
* 负责管理员账号校验、JWT 签发、登录态校验和主动注销。
|
||||
*/
|
||||
class AdminAuthService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 构造函数,注入对应依赖。
|
||||
*/
|
||||
public function __construct(
|
||||
protected AdminUserRepository $adminUserRepository,
|
||||
protected JwtTokenManager $jwtTokenManager
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验中间件传入的管理员登录 token。
|
||||
*/
|
||||
public function authenticateToken(string $token, string $ip = '', string $userAgent = ''): ?AdminUser
|
||||
{
|
||||
$result = $this->jwtTokenManager->verify('admin', $token, $ip, $userAgent);
|
||||
if ($result === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$adminId = (int) ($result['session']['admin_id'] ?? $result['claims']['sub'] ?? 0);
|
||||
if ($adminId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var AdminUser|null $admin */
|
||||
$admin = $this->adminUserRepository->find($adminId);
|
||||
if (!$admin || (int) $admin->status !== CommonConstant::STATUS_ENABLED) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验管理员账号密码并签发 JWT。
|
||||
*/
|
||||
public function authenticateCredentials(string $username, string $password, string $ip = '', string $userAgent = ''): array
|
||||
{
|
||||
$admin = $this->adminUserRepository->findByUsername($username);
|
||||
if (!$admin || (int) $admin->status !== CommonConstant::STATUS_ENABLED) {
|
||||
throw new ValidationException('管理员账号或密码错误');
|
||||
}
|
||||
|
||||
if (!password_verify($password, (string) $admin->password_hash)) {
|
||||
throw new ValidationException('管理员账号或密码错误');
|
||||
}
|
||||
|
||||
$admin->last_login_at = $this->now();
|
||||
$admin->last_login_ip = $ip;
|
||||
$admin->save();
|
||||
|
||||
return $this->issueToken((int) $admin->id, 86400, $ip, $userAgent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 撤销当前管理员登录 token。
|
||||
*/
|
||||
public function revokeToken(string $token): bool
|
||||
{
|
||||
return $this->jwtTokenManager->revoke('admin', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 签发新的管理员登录 token。
|
||||
*/
|
||||
public function issueToken(int $adminId, int $ttlSeconds = 86400, string $ip = '', string $userAgent = ''): array
|
||||
{
|
||||
/** @var AdminUser|null $admin */
|
||||
$admin = $this->adminUserRepository->find($adminId);
|
||||
if (!$admin) {
|
||||
throw new ValidationException('管理员不存在');
|
||||
}
|
||||
|
||||
$issued = $this->jwtTokenManager->issue('admin', [
|
||||
'sub' => (string) $adminId,
|
||||
'admin_id' => $adminId,
|
||||
'username' => (string) $admin->username,
|
||||
'is_super' => (int) $admin->is_super,
|
||||
], [
|
||||
'admin_id' => $adminId,
|
||||
'admin_username' => (string) $admin->username,
|
||||
'real_name' => (string) $admin->real_name,
|
||||
'is_super' => (int) $admin->is_super,
|
||||
'last_login_ip' => $ip,
|
||||
'user_agent' => $userAgent,
|
||||
], $ttlSeconds);
|
||||
|
||||
return [
|
||||
'token' => $issued['token'],
|
||||
'expires_in' => $issued['expires_in'],
|
||||
'admin' => $admin,
|
||||
];
|
||||
}
|
||||
}
|
||||
242
app/service/system/config/SystemConfigDefinitionService.php
Normal file
242
app/service/system/config/SystemConfigDefinitionService.php
Normal file
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
namespace app\service\system\config;
|
||||
|
||||
use app\common\base\BaseService;
|
||||
use RuntimeException;
|
||||
|
||||
class SystemConfigDefinitionService extends BaseService
|
||||
{
|
||||
protected const VIRTUAL_FIELD_PREFIX = '__';
|
||||
|
||||
/**
|
||||
* 已解析的标签页缓存。
|
||||
*/
|
||||
protected ?array $tabCache = null;
|
||||
|
||||
/**
|
||||
* 标签页键到定义的缓存。
|
||||
*/
|
||||
protected ?array $tabMapCache = null;
|
||||
|
||||
public function tabs(): array
|
||||
{
|
||||
if ($this->tabCache !== null) {
|
||||
return $this->tabCache;
|
||||
}
|
||||
|
||||
$definitions = (array) config('system_config', []);
|
||||
$tabs = [];
|
||||
$seenKeys = [];
|
||||
$seenFields = [];
|
||||
|
||||
foreach ($definitions as $groupCode => $definition) {
|
||||
if (!is_array($definition)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tab = $this->normalizeTab((string) $groupCode, $definition);
|
||||
if ($tab === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = $tab['key'];
|
||||
if (isset($seenKeys[$key])) {
|
||||
throw new RuntimeException(sprintf('系统配置标签 key 重复:%s', $key));
|
||||
}
|
||||
|
||||
foreach ($tab['rules'] as $rule) {
|
||||
$field = (string) ($rule['field'] ?? '');
|
||||
if ($field === '' || $this->isVirtualField($field)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($seenFields[$field])) {
|
||||
throw new RuntimeException(sprintf('系统配置项 key 重复:%s', $field));
|
||||
}
|
||||
|
||||
$seenFields[$field] = true;
|
||||
}
|
||||
|
||||
$seenKeys[$key] = true;
|
||||
$tabs[] = $tab;
|
||||
}
|
||||
|
||||
usort($tabs, static function (array $left, array $right): int {
|
||||
$leftSort = (int) ($left['sort'] ?? 0);
|
||||
$rightSort = (int) ($right['sort'] ?? 0);
|
||||
|
||||
return $leftSort <=> $rightSort;
|
||||
});
|
||||
|
||||
$this->tabCache = $tabs;
|
||||
$this->tabMapCache = [];
|
||||
|
||||
foreach ($tabs as $tab) {
|
||||
$key = (string) ($tab['key'] ?? '');
|
||||
if ($key !== '') {
|
||||
$this->tabMapCache[$key] = $tab;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->tabCache;
|
||||
}
|
||||
|
||||
public function tab(string $groupCode): ?array
|
||||
{
|
||||
$groupCode = strtolower(trim($groupCode));
|
||||
if ($groupCode === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->tabs();
|
||||
|
||||
return $this->tabMapCache[$groupCode] ?? null;
|
||||
}
|
||||
|
||||
public function hydrateRules(array $tab, array $values): array
|
||||
{
|
||||
$rules = [];
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = (string) ($rule['field'] ?? '');
|
||||
if ($field === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->isVirtualField($field)) {
|
||||
$rule['value'] = array_key_exists($field, $values) ? $values[$field] : ($rule['value'] ?? '');
|
||||
}
|
||||
$rules[] = $rule;
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function extractFormData(array $tab, array $values): array
|
||||
{
|
||||
$data = [];
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = (string) ($rule['field'] ?? '');
|
||||
if ($field === '' || $this->isVirtualField($field)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$field] = array_key_exists($field, $values) ? $values[$field] : ($rule['value'] ?? '');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function requiredFieldMessages(array $tab): array
|
||||
{
|
||||
$messages = [];
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field === '' || $this->isVirtualField($field)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ((array) ($rule['validate'] ?? []) as $validateRule) {
|
||||
if (!is_array($validateRule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($validateRule['required'])) {
|
||||
$messages[$field] = (string) ($validateRule['message'] ?? sprintf('%s 不能为空', (string) ($rule['title'] ?? $field)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
private function normalizeTab(string $groupCode, array $definition): ?array
|
||||
{
|
||||
$key = strtolower(trim((string) ($definition['key'] ?? $groupCode)));
|
||||
if ($key === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$rules = [];
|
||||
foreach ((array) ($definition['rules'] ?? []) as $rule) {
|
||||
$normalizedRule = $this->normalizeRule($rule);
|
||||
if ($normalizedRule !== null) {
|
||||
$rules[] = $normalizedRule;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'title' => (string) ($definition['title'] ?? $key),
|
||||
'icon' => (string) ($definition['icon'] ?? ''),
|
||||
'description' => (string) ($definition['description'] ?? ''),
|
||||
'sort' => (int) ($definition['sort'] ?? 0),
|
||||
'disabled' => (bool) ($definition['disabled'] ?? false),
|
||||
'submitText' => (string) ($definition['submitText'] ?? '保存配置'),
|
||||
'refreshAfterSubmit' => (bool) ($definition['refreshAfterSubmit'] ?? true),
|
||||
'rules' => $rules,
|
||||
];
|
||||
}
|
||||
|
||||
private function normalizeRule(mixed $rule): ?array
|
||||
{
|
||||
if (!is_array($rule)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$options = [];
|
||||
foreach ((array) ($rule['options'] ?? []) as $option) {
|
||||
if (!is_array($option)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[] = [
|
||||
'label' => (string) ($option['label'] ?? ''),
|
||||
'value' => (string) ($option['value'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
$validate = [];
|
||||
foreach ((array) ($rule['validate'] ?? []) as $validateRule) {
|
||||
if (!is_array($validateRule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$validate[] = $validateRule;
|
||||
}
|
||||
|
||||
$normalized = $rule;
|
||||
$normalized['type'] = (string) ($rule['type'] ?? 'input');
|
||||
$normalized['field'] = $field;
|
||||
$normalized['title'] = (string) ($rule['title'] ?? $field);
|
||||
$normalized['value'] = (string) ($rule['value'] ?? '');
|
||||
$normalized['props'] = is_array($rule['props'] ?? null) ? $rule['props'] : [];
|
||||
$normalized['options'] = $options;
|
||||
$normalized['validate'] = $validate;
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
private function isVirtualField(string $field): bool
|
||||
{
|
||||
return str_starts_with($field, self::VIRTUAL_FIELD_PREFIX);
|
||||
}
|
||||
}
|
||||
160
app/service/system/config/SystemConfigPageService.php
Normal file
160
app/service/system/config/SystemConfigPageService.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace app\service\system\config;
|
||||
|
||||
use app\common\base\BaseService;
|
||||
use app\exception\ValidationException;
|
||||
use app\repository\system\config\SystemConfigRepository;
|
||||
use Webman\Event\Event;
|
||||
|
||||
class SystemConfigPageService extends BaseService
|
||||
{
|
||||
public function __construct(
|
||||
protected SystemConfigRepository $systemConfigRepository,
|
||||
protected SystemConfigDefinitionService $systemConfigDefinitionService
|
||||
) {
|
||||
}
|
||||
|
||||
public function tabs(): array
|
||||
{
|
||||
$tabs = [];
|
||||
foreach ($this->systemConfigDefinitionService->tabs() as $tab) {
|
||||
unset($tab['rules']);
|
||||
$tabs[] = $tab;
|
||||
}
|
||||
|
||||
$defaultKey = '';
|
||||
foreach ($tabs as $tab) {
|
||||
if (!empty($tab['disabled'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$defaultKey = (string) ($tab['key'] ?? '');
|
||||
if ($defaultKey !== '') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'defaultKey' => $defaultKey !== '' ? $defaultKey : (string) ($tabs[0]['key'] ?? ''),
|
||||
'tabs' => $tabs,
|
||||
];
|
||||
}
|
||||
|
||||
public function detail(string $groupCode): array
|
||||
{
|
||||
$tab = $this->systemConfigDefinitionService->tab($groupCode);
|
||||
if (!$tab) {
|
||||
throw new ValidationException('系统配置标签不存在');
|
||||
}
|
||||
|
||||
$keys = [];
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field !== '' && !str_starts_with($field, '__')) {
|
||||
$keys[] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
$keys = array_values(array_unique($keys));
|
||||
if ($keys === []) {
|
||||
$rowMap = [];
|
||||
} else {
|
||||
$rows = $this->systemConfigRepository->query()
|
||||
->whereIn('config_key', $keys)
|
||||
->get(['config_key', 'config_value']);
|
||||
|
||||
$rowMap = [];
|
||||
foreach ($rows as $row) {
|
||||
$rowMap[strtolower((string) $row->config_key)] = (string) ($row->config_value ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
$tab['rules'] = $this->systemConfigDefinitionService->hydrateRules($tab, $rowMap);
|
||||
$tab['formData'] = $this->systemConfigDefinitionService->extractFormData($tab, $rowMap);
|
||||
|
||||
return $tab;
|
||||
}
|
||||
|
||||
public function save(string $groupCode, array $values): array
|
||||
{
|
||||
$tab = $this->systemConfigDefinitionService->tab($groupCode);
|
||||
if (!$tab) {
|
||||
throw new ValidationException('系统配置标签不存在');
|
||||
}
|
||||
|
||||
$formData = $this->systemConfigDefinitionService->extractFormData($tab, $values);
|
||||
$this->validateRequiredValues($tab, $formData);
|
||||
|
||||
$this->transaction(function () use ($tab, $formData): void {
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field === '' || str_starts_with($field, '__')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $this->stringifyValue($formData[$field] ?? '');
|
||||
$this->systemConfigRepository->updateOrCreate(
|
||||
['config_key' => $field],
|
||||
[
|
||||
'group_code' => (string) $tab['key'],
|
||||
'config_value' => $value,
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Event::emit('system.config.changed', [
|
||||
'group_code' => (string) $tab['key'],
|
||||
]);
|
||||
|
||||
return $this->detail((string) $tab['key']);
|
||||
}
|
||||
|
||||
protected function validateRequiredValues(array $tab, array $values): void
|
||||
{
|
||||
$messages = $this->systemConfigDefinitionService->requiredFieldMessages($tab);
|
||||
|
||||
foreach ($messages as $field => $message) {
|
||||
$value = $values[$field] ?? '';
|
||||
if ($this->isEmptyValue($value)) {
|
||||
throw new ValidationException($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function isEmptyValue(mixed $value): bool
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return $value === [];
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return trim($value) === '';
|
||||
}
|
||||
|
||||
return $value === null || $value === '';
|
||||
}
|
||||
|
||||
protected function stringifyValue(mixed $value): string
|
||||
{
|
||||
if (is_bool($value)) {
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
if (is_array($value) || is_object($value)) {
|
||||
throw new ValidationException('系统配置值暂不支持复杂类型');
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
}
|
||||
124
app/service/system/config/SystemConfigRuntimeService.php
Normal file
124
app/service/system/config/SystemConfigRuntimeService.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace app\service\system\config;
|
||||
|
||||
use app\common\base\BaseService;
|
||||
use app\repository\system\config\SystemConfigRepository;
|
||||
use support\Cache;
|
||||
use Throwable;
|
||||
|
||||
class SystemConfigRuntimeService extends BaseService
|
||||
{
|
||||
protected const CACHE_KEY = 'system_config:all';
|
||||
|
||||
public function __construct(
|
||||
protected SystemConfigRepository $systemConfigRepository,
|
||||
protected SystemConfigDefinitionService $systemConfigDefinitionService
|
||||
) {
|
||||
}
|
||||
|
||||
public function all(bool $refresh = false): array
|
||||
{
|
||||
if (!$refresh) {
|
||||
$cached = $this->readCache();
|
||||
if ($cached !== null) {
|
||||
return $cached;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->refresh();
|
||||
}
|
||||
|
||||
public function get(string $configKey, mixed $default = '', bool $refresh = false): string
|
||||
{
|
||||
$configKey = strtolower(trim($configKey));
|
||||
if ($configKey === '') {
|
||||
return (string) $default;
|
||||
}
|
||||
|
||||
$values = $this->all($refresh);
|
||||
|
||||
return (string) ($values[$configKey] ?? $default);
|
||||
}
|
||||
|
||||
public function refresh(): array
|
||||
{
|
||||
$values = $this->buildValueMap();
|
||||
$this->writeCache($values);
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
protected function buildValueMap(): array
|
||||
{
|
||||
$values = [];
|
||||
$tabs = $this->systemConfigDefinitionService->tabs();
|
||||
$keys = [];
|
||||
|
||||
foreach ($tabs as $tab) {
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field !== '' && !str_starts_with($field, '__')) {
|
||||
$keys[] = $field;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$keys = array_values(array_unique($keys));
|
||||
if ($keys === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->systemConfigRepository->query()
|
||||
->whereIn('config_key', $keys)
|
||||
->get(['config_key', 'config_value']);
|
||||
|
||||
$rowMap = [];
|
||||
foreach ($rows as $row) {
|
||||
$rowMap[strtolower((string) $row->config_key)] = (string) ($row->config_value ?? '');
|
||||
}
|
||||
|
||||
foreach ($tabs as $tab) {
|
||||
foreach ((array) ($tab['rules'] ?? []) as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = strtolower(trim((string) ($rule['field'] ?? '')));
|
||||
if ($field === '' || str_starts_with($field, '__')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$values[$field] = array_key_exists($field, $rowMap)
|
||||
? (string) $rowMap[$field]
|
||||
: (string) ($rule['value'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
protected function readCache(): ?array
|
||||
{
|
||||
try {
|
||||
$raw = Cache::get(self::CACHE_KEY);
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return is_array($raw) ? $raw : null;
|
||||
}
|
||||
|
||||
protected function writeCache(array $values): void
|
||||
{
|
||||
try {
|
||||
Cache::set(self::CACHE_KEY, $values);
|
||||
} catch (Throwable) {
|
||||
// Redis 不可用时不阻塞主流程。
|
||||
}
|
||||
}
|
||||
}
|
||||
196
app/service/system/user/AdminUserService.php
Normal file
196
app/service/system/user/AdminUserService.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace app\service\system\user;
|
||||
|
||||
use app\common\base\BaseService;
|
||||
use app\common\constant\CommonConstant;
|
||||
use app\exception\ResourceNotFoundException;
|
||||
use app\model\admin\AdminUser;
|
||||
use app\repository\system\user\AdminUserRepository;
|
||||
|
||||
/**
|
||||
* 管理员用户管理服务。
|
||||
*
|
||||
* 负责管理员账号的列表查询、新增、修改和删除,以及密码字段的统一处理。
|
||||
*/
|
||||
class AdminUserService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 构造函数,注入管理员用户仓库。
|
||||
*/
|
||||
public function __construct(
|
||||
protected AdminUserRepository $adminUserRepository
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询管理员用户。
|
||||
*/
|
||||
public function paginate(array $filters = [], int $page = 1, int $pageSize = 10)
|
||||
{
|
||||
$query = $this->adminUserRepository->query()->from('ma_admin_user as u');
|
||||
|
||||
$keyword = trim((string) ($filters['keyword'] ?? ''));
|
||||
if ($keyword !== '') {
|
||||
$query->where(function ($builder) use ($keyword) {
|
||||
$builder->where('u.username', 'like', '%' . $keyword . '%')
|
||||
->orWhere('u.real_name', 'like', '%' . $keyword . '%')
|
||||
->orWhere('u.mobile', 'like', '%' . $keyword . '%')
|
||||
->orWhere('u.email', 'like', '%' . $keyword . '%');
|
||||
});
|
||||
}
|
||||
|
||||
$status = (string) ($filters['status'] ?? '');
|
||||
if ($status !== '') {
|
||||
$query->where('u.status', (int) $status);
|
||||
}
|
||||
|
||||
$isSuper = (string) ($filters['is_super'] ?? '');
|
||||
if ($isSuper !== '') {
|
||||
$query->where('u.is_super', (int) $isSuper);
|
||||
}
|
||||
|
||||
$paginator = $query
|
||||
->select([
|
||||
'u.id',
|
||||
'u.username',
|
||||
'u.real_name',
|
||||
'u.mobile',
|
||||
'u.email',
|
||||
'u.is_super',
|
||||
'u.status',
|
||||
'u.last_login_at',
|
||||
'u.last_login_ip',
|
||||
'u.remark',
|
||||
'u.created_at',
|
||||
'u.updated_at',
|
||||
])
|
||||
->orderByDesc('u.id')
|
||||
->paginate(max(1, $pageSize), ['*'], 'page', max(1, $page));
|
||||
|
||||
$paginator->getCollection()->transform(function ($row) {
|
||||
$row->status_text = (string) ((int) $row->status === CommonConstant::STATUS_ENABLED ? '启用' : '禁用');
|
||||
$row->is_super_text = (string) ((int) $row->is_super === 1 ? '超级管理员' : '普通管理员');
|
||||
|
||||
return $row;
|
||||
});
|
||||
|
||||
return $paginator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询管理员用户。
|
||||
*/
|
||||
public function findById(int $id): ?AdminUser
|
||||
{
|
||||
return $this->adminUserRepository->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增管理员用户。
|
||||
*/
|
||||
public function create(array $data): AdminUser
|
||||
{
|
||||
return $this->adminUserRepository->create($this->normalizePayload($data, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改管理员用户。
|
||||
*/
|
||||
public function update(int $id, array $data): ?AdminUser
|
||||
{
|
||||
$current = $this->adminUserRepository->find($id);
|
||||
if (!$current) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$this->adminUserRepository->updateById($id, $this->normalizePayload($data, true))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->adminUserRepository->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除管理员用户。
|
||||
*/
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
return $this->adminUserRepository->deleteById($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员资料。
|
||||
*/
|
||||
public function profile(int $adminId, string $adminUsername = ''): array
|
||||
{
|
||||
$admin = $this->adminUserRepository->find($adminId);
|
||||
if (!$admin) {
|
||||
throw new ResourceNotFoundException('管理员不存在', ['admin_id' => $adminId]);
|
||||
}
|
||||
|
||||
$isSuper = (int) $admin->is_super === 1;
|
||||
$role = [
|
||||
'code' => 'admin',
|
||||
'name' => $isSuper ? '超级管理员' : '普通管理员',
|
||||
'admin' => $isSuper,
|
||||
'disabled' => false,
|
||||
];
|
||||
|
||||
$user = [
|
||||
'id' => (int) $admin->id,
|
||||
'deptId' => '0',
|
||||
'deptName' => '管理中心',
|
||||
'userName' => (string) ($admin->username !== '' ? $admin->username : trim($adminUsername)),
|
||||
'nickName' => (string) ($admin->real_name !== '' ? $admin->real_name : $admin->username),
|
||||
'email' => (string) ($admin->email ?? ''),
|
||||
'phone' => (string) ($admin->mobile ?? ''),
|
||||
'sex' => 2,
|
||||
'avatar' => '',
|
||||
'status' => (int) $admin->status,
|
||||
'description' => trim((string) ($admin->remark ?? '')) !== '' ? (string) $admin->remark : '平台后台管理员账号',
|
||||
'roles' => [$role],
|
||||
'loginIp' => (string) ($admin->last_login_ip ?? ''),
|
||||
'loginDate' => $this->formatDateTime($admin->last_login_at ?? null),
|
||||
'createBy' => '系统',
|
||||
'createTime' => $this->formatDateTime($admin->created_at ?? null),
|
||||
'updateBy' => null,
|
||||
'updateTime' => $this->formatDateTime($admin->updated_at ?? null),
|
||||
'admin' => $isSuper,
|
||||
];
|
||||
|
||||
return [
|
||||
'admin_id' => (int) $admin->id,
|
||||
'admin_username' => (string) ($admin->username !== '' ? $admin->username : trim($adminUsername)),
|
||||
'user' => $user,
|
||||
'roles' => ['admin'],
|
||||
'permissions' => $isSuper ? ['*:*:*'] : [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一整理写入字段,并处理密码哈希。
|
||||
*/
|
||||
private function normalizePayload(array $data, bool $isUpdate): array
|
||||
{
|
||||
$payload = [
|
||||
'username' => trim((string) ($data['username'] ?? '')),
|
||||
'real_name' => trim((string) ($data['real_name'] ?? '')),
|
||||
'mobile' => trim((string) ($data['mobile'] ?? '')),
|
||||
'email' => trim((string) ($data['email'] ?? '')),
|
||||
'is_super' => (int) ($data['is_super'] ?? 0),
|
||||
'status' => (int) ($data['status'] ?? CommonConstant::STATUS_ENABLED),
|
||||
'remark' => trim((string) ($data['remark'] ?? '')),
|
||||
];
|
||||
|
||||
$password = trim((string) ($data['password'] ?? ''));
|
||||
if ($password !== '') {
|
||||
$payload['password_hash'] = password_hash($password, PASSWORD_DEFAULT);
|
||||
} elseif (!$isUpdate) {
|
||||
$payload['password_hash'] = '';
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user