mirror of
https://gitee.com/technical-laohu/mpay_v2_webman.git
synced 2026-04-25 03:24:26 +08:00
更新后端基础
This commit is contained in:
@@ -1,207 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\common\base\BaseDao;
|
||||
|
||||
/**
|
||||
* 管理后台用户仓库(当前阶段使用内存数据模拟)
|
||||
*
|
||||
* 后续接入数据库时:
|
||||
* 1. 创建 AdminUserDao 继承 BaseDao
|
||||
* 2. 在构造函数中注入:public function __construct(AdminUserDao $dao) { parent::__construct($dao); }
|
||||
* 3. 将内存数据方法改为调用 $this->dao 的方法
|
||||
*/
|
||||
class AdminUserRepository extends BaseRepository
|
||||
{
|
||||
/**
|
||||
* 构造函数:支持注入 DAO(当前阶段为可选)
|
||||
*/
|
||||
public function __construct(?BaseDao $dao = null)
|
||||
{
|
||||
parent::__construct($dao);
|
||||
}
|
||||
/**
|
||||
* 模拟账户数据(对齐前端 mock accountData)
|
||||
*/
|
||||
protected function accounts(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'id' => 1,
|
||||
'deptId' => '100',
|
||||
'deptName' => '研发部门',
|
||||
'userName' => 'admin',
|
||||
'nickName' => '超级管理员',
|
||||
'email' => '2547096351@qq.com',
|
||||
'phone' => '15888888888',
|
||||
'sex' => 1,
|
||||
'avatar' => 'https://ooo.0x0.ooo/2025/04/10/O0dG7r.jpg',
|
||||
'status' => 1,
|
||||
'description' => '系统初始用户',
|
||||
'roles' => ['admin'],
|
||||
'loginIp' => '0:0:0:0:0:0:0:1',
|
||||
'loginDate' => '2025-03-31 10:30:59',
|
||||
'createBy' => 'admin',
|
||||
'createTime' => '2024-03-19 11:21:01',
|
||||
'updateBy' => null,
|
||||
'updateTime' => null,
|
||||
'admin' => true,
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'deptId' => '100010101',
|
||||
'deptName' => '研发部门',
|
||||
'userName' => 'common',
|
||||
'nickName' => '普通用户',
|
||||
'email' => '2547096351@qq.com',
|
||||
'phone' => '15222222222',
|
||||
'sex' => 0,
|
||||
'avatar' => 'https://ooo.0x0.ooo/2025/04/10/O0ddJI.jpg',
|
||||
'status' => 1,
|
||||
'description' => 'UI组用户',
|
||||
'roles' => ['common'],
|
||||
'loginIp' => '0:0:0:0:0:0:0:1',
|
||||
'loginDate' => '2025-03-31 10:30:59',
|
||||
'createBy' => 'admin',
|
||||
'createTime' => '2024-03-19 11:21:01',
|
||||
'updateBy' => null,
|
||||
'updateTime' => null,
|
||||
'admin' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟角色数据(对齐前端 mock roleData)
|
||||
*/
|
||||
protected function roles(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'id' => 1,
|
||||
'name' => '超级管理员',
|
||||
'code' => 'admin',
|
||||
'sort' => 1,
|
||||
'status' => 1,
|
||||
'admin' => true,
|
||||
'description' => '默认角色,超级管理员,上帝角色',
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'name' => '普通员工',
|
||||
'code' => 'common',
|
||||
'sort' => 2,
|
||||
'status' => 1,
|
||||
'admin' => false,
|
||||
'description' => '负责一些基础功能',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟权限数据(对齐前端 mock permissionData)
|
||||
*/
|
||||
protected function permissions(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin'],
|
||||
'permission' => 'sys:btn:add',
|
||||
],
|
||||
],
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin'],
|
||||
'permission' => 'sys:btn:edit',
|
||||
],
|
||||
],
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin'],
|
||||
'permission' => 'sys:btn:delete',
|
||||
],
|
||||
],
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin', 'common'],
|
||||
'permission' => 'common:btn:add',
|
||||
],
|
||||
],
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin', 'common'],
|
||||
'permission' => 'common:btn:edit',
|
||||
],
|
||||
],
|
||||
[
|
||||
'meta' => [
|
||||
'roles' => ['admin', 'common'],
|
||||
'permission' => 'common:btn:delete',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function findByUsername(string $username): ?array
|
||||
{
|
||||
foreach ($this->accounts() as $account) {
|
||||
if ($account['userName'] === $username) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
foreach ($this->accounts() as $account) {
|
||||
if ($account['id'] === $id) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRoleInfoByCodes(array $codes): array
|
||||
{
|
||||
if (!$codes) {
|
||||
return [];
|
||||
}
|
||||
$roles = [];
|
||||
foreach ($this->roles() as $role) {
|
||||
if (in_array($role['code'], $codes, true)) {
|
||||
$roles[] = $role;
|
||||
}
|
||||
}
|
||||
return $roles;
|
||||
}
|
||||
|
||||
public function getPermissionsByRoleCodes(array $codes): array
|
||||
{
|
||||
if (!$codes) {
|
||||
return [];
|
||||
}
|
||||
$permissions = [];
|
||||
foreach ($this->permissions() as $item) {
|
||||
$meta = $item['meta'] ?? [];
|
||||
$roles = $meta['roles'] ?? [];
|
||||
$permission = $meta['permission'] ?? null;
|
||||
if (!$permission) {
|
||||
continue;
|
||||
}
|
||||
foreach ($codes as $code) {
|
||||
if (in_array($code, $roles, true)) {
|
||||
$permissions[] = $permission;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 去重
|
||||
return array_values(array_unique($permissions));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/CronJobRepository.php
Normal file
19
app/repositories/CronJobRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\CronJob;
|
||||
|
||||
/**
|
||||
* 定时任务仓储
|
||||
*/
|
||||
class CronJobRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new CronJob());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/CronLogRepository.php
Normal file
19
app/repositories/CronLogRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\CronLog;
|
||||
|
||||
/**
|
||||
* 定时任务日志仓储
|
||||
*/
|
||||
class CronLogRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new CronLog());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/DepartmentRepository.php
Normal file
19
app/repositories/DepartmentRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\Department;
|
||||
|
||||
/**
|
||||
* 部门仓储
|
||||
*/
|
||||
class DepartmentRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new Department());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/DictGroupRepository.php
Normal file
19
app/repositories/DictGroupRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\DictGroup;
|
||||
|
||||
/**
|
||||
* 字典分组仓储
|
||||
*/
|
||||
class DictGroupRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new DictGroup());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/DictItemRepository.php
Normal file
19
app/repositories/DictItemRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\DictItem;
|
||||
|
||||
/**
|
||||
* 字典项仓储
|
||||
*/
|
||||
class DictItemRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new DictItem());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
54
app/repositories/MenuRepository.php
Normal file
54
app/repositories/MenuRepository.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\Menu;
|
||||
|
||||
/**
|
||||
* 菜单 / 权限仓储
|
||||
*/
|
||||
class MenuRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new Menu());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有启用的菜单(仅目录和菜单类型,排除按钮)
|
||||
*/
|
||||
public function getAllEnabledMenus(): array
|
||||
{
|
||||
return $this->model
|
||||
->newQuery()
|
||||
->whereIn('type', [1, 2]) // 1目录 2菜单,排除3按钮
|
||||
->where('status', 1) // 只获取启用的菜单
|
||||
->orderBy('sort', 'asc')
|
||||
->orderBy('id', 'asc')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据菜单ID列表获取启用的菜单(仅目录和菜单类型,排除按钮)
|
||||
*/
|
||||
public function getMenusByIds(array $menuIds): array
|
||||
{
|
||||
if (empty($menuIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->model
|
||||
->newQuery()
|
||||
->whereIn('id', $menuIds)
|
||||
->whereIn('type', [1, 2]) // 1目录 2菜单,排除3按钮
|
||||
->where('status', 1) // 只获取启用的菜单
|
||||
->orderBy('sort', 'asc')
|
||||
->orderBy('id', 'asc')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
app/repositories/RoleRepository.php
Normal file
19
app/repositories/RoleRepository.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\Role;
|
||||
|
||||
/**
|
||||
* 角色仓储
|
||||
*/
|
||||
class RoleRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new Role());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
47
app/repositories/UserRepository.php
Normal file
47
app/repositories/UserRepository.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace app\repositories;
|
||||
|
||||
use app\common\base\BaseRepository;
|
||||
use app\models\User;
|
||||
|
||||
/**
|
||||
* 用户仓储
|
||||
*/
|
||||
class UserRepository extends BaseRepository
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new User());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
*/
|
||||
public function findByUserName(string $userName): ?User
|
||||
{
|
||||
/** @var User|null $user */
|
||||
$user = $this->model
|
||||
->newQuery()
|
||||
->where('user_name', $userName)
|
||||
->first();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键查询并预加载角色
|
||||
*/
|
||||
public function findWithRoles(int $id): ?User
|
||||
{
|
||||
/** @var User|null $user */
|
||||
$user = $this->model
|
||||
->newQuery()
|
||||
->with('roles')
|
||||
->find($id);
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user