feat : section

This commit is contained in:
2024-11-07 16:17:57 +08:00
parent 7fd07c55d8
commit b6b2627129
11 changed files with 531 additions and 3 deletions

View File

@@ -42,4 +42,13 @@ class AdminRedisKey
{
return '__system:admin:menu:list:role_id:'.$roleId;
}
/**
* 部门集合
* @return string
*/
public static function adminSectionList()
{
return '__system:admin:section:list';
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Model\AdminSection;
use App\Service\ServiceTrait\Admin\SectionTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class SectionCache
{
use SectionTrait;
/**
* @var AdminSection
*/
#[Inject]
protected AdminSection $adminSectionModel;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* 菜单
* @var string
*/
protected string $key;
/**
* 构造函数注入 key
*/
public function __construct()
{
$this->key = AdminRedisKey::adminSectionList();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function getList(): array
{
if ($this->redis->exists($this->key,'system')) {
return json_decode($this->redis->get($this->key,'system'),true);
}
$allList = $this->adminSectionModel->getList();
$data = $this->getDbList($allList);
$this->redis->set($this->key,json_encode($data),'system');
return $data;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function delList()
{
$this->redis->delete($this->key,'system');
}
}

View File

@@ -28,4 +28,14 @@ class AuthCode
* 超级管理员
*/
const SUPERADMIN = 1;
/**
* 部门启用
*/
const SECTION_STATUS_ENABLE = 1;
/**
* 部门禁用
*/
const SECTION_STATUS_DISABLE = 2;
}

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\SectionRequest;
use App\Service\Admin\User\SectionService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
#[Controller(prefix: "admin/section")]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class SectionController
{
/**
* @param SectionRequest $request
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function add(SectionRequest $request)
{
return (new SectionService)->add();
}
/**
* @param SectionRequest $request
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function edit(SectionRequest $request)
{
return (new SectionService)->edit();
}
/**
* @param SectionRequest $request
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
#[RequestMapping(path: "del", methods: "GET")]
#[Scene(scene: "del")]
public function del(SectionRequest $request)
{
return (new SectionService)->del();
}
/**
* @param SectionRequest $request
* @return array
*/
#[RequestMapping(path: "info", methods: "GET")]
#[Scene(scene: "info")]
public function info(SectionRequest $request)
{
return (new SectionService)->info();
}
/**
* @param SectionRequest $request
* @return array
*/
#[RequestMapping(path: "list", methods: "GET")]
#[Scene(scene: "list")]
public function list(SectionRequest $request)
{
return (new SectionService)->handle();
}
/**
* @param SectionRequest $request
* @return array
*/
#[RequestMapping(path: "all_list", methods: "GET")]
#[Scene(scene: "all_list")]
public function allList(SectionRequest $request)
{
return (new SectionService)->handle();
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Admin\AuthCode;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $pid
* @property string $name
* @property int $city_id
* @property string $remark
* @property int $status
* @property string $create_time
* @property string $update_time
*/
class AdminSection extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'admin_section';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'city_id' => 'integer', 'status' => 'integer'];
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
/**
* @param int $id
* @return \Hyperf\Database\Model\Model|Builder|null
*/
public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('id',$id)->first(['id','pid','name','status','remark']);
}
/**
* @param int $pid
* @return Builder[]|Collection
*/
public function getInfoByPid(int $pid): Collection|array
{
return $this->where('pid',$pid)->get();
}
/**
* 获取所有
* @return array
*/
public function getList(): array
{
return $this
->where('status', AuthCode::SECTION_STATUS_ENABLE)
->get([['id','pid','name','status','remark']])
->toArray();
}
}

View File

@@ -16,11 +16,12 @@ use Hyperf\DbConnection\Model\Model;
* @property int $avatar
* @property string $chinese_name
* @property string $mobile
* @property int $status
* @property int $status
* @property string $last_login_ip
* @property string $last_login_time
* @property int $is_del
* @property int $role_id
* @property int $role_id
* @property int $section_id
* @property string $create_time
* @property string $update_time
*/

View File

@@ -22,7 +22,7 @@ class LoginRequest extends FormRequest
public function rules(): array
{
return [
'account' => 'required|digits:11',
'account' => 'required|string|digits:11',
'password' => 'required|string|min:6',
];
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class SectionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'id' => 'required|integer',
'name' => 'required|string',
'city_id' => 'exists:system_city,id',
'pid' => 'exists:admin_section,id',
'status' => 'in:1,2',
];
}
public function messages(): array
{
return [
];
}
protected array $scenes = [
'add' => ['name', 'city_id', 'status','pid'],
'edit' => ['name', 'city_id', 'status','pid','id'],
];
}

View File

@@ -0,0 +1,156 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Cache\Redis\Admin\SectionCache;
use App\Exception\AdminException;
use App\Model\AdminSection;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\SectionTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class SectionService extends BaseService
{
use SectionTrait;
/**
* 部门注入模型
* @var AdminSection $adminSectionModel
*/
#[Inject]
protected AdminSection $adminSectionModel;
/**
* @var AdminUser
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @var SectionCache
*/
#[Inject]
protected SectionCache $sectionCache;
/**
* @return array
*/
public function handle(): array
{
$all = $this->adminSectionModel->get(['id','pid','name','status','remark']);
if (empty($all)) return $this->return->success('success',['list' => []]);
$data = $this->getDbList($all->toArray());
return $this->return->success('success',['list' => $data]);
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function add(): array
{
$model = new AdminSection();
$pid = (int)$this->request->input('pid',0);
if ($pid > 0) $pidInfo = $this->adminSectionModel->getInfoById($pid);
$model->name = $this->request->input('name');
$model->pid = $pid;
$model->city_id = !empty($pidInfo) ? $pidInfo->city_id : $this->request->input('city_id',0);
$model->remark = $this->request->input('remark');
$model->status = $this->request->input('status',1);
if (!$model->save()) throw new AdminException('添加失败');
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
$info->name = $this->request->input('name');
$info->pid = $this->request->input('pid',0);
$info->city_id = $this->request->input('city_id',0);
$info->remark = $this->request->input('remark');
$info->status = $this->request->input('status',1);
if (!$info->save()) throw new AdminException('修改失败');
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
$children = $this->adminSectionModel->getInfoByPid($info->id);
if (empty($children)) throw new AdminException('请先删除下级数据');
$this->adminUserModel->where('section_id',$id)->update(['section_id'=>0]);
$info->delete();
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
*/
public function info(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
return $this->return->success('success',$info->toArray());
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function allList(): array
{
return $this->return->success('success',['list' => $this->sectionCache->getList()]);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Service\ServiceTrait\Admin;
trait SectionTrait
{
/**
* 递归生成合适的数据
* @param array $allMenuList
* @param int $parentId
* @return array
*/
protected function getDbList(array $allMenuList, int $parentId = 0): array
{
$menuList = [];
foreach ($allMenuList as $menu) {
if ($menu['pid'] == $parentId) {
$children = $this->getDbList($allMenuList, (int)$menu['id']);
if (!empty($children)) {
$menu['children'] = $children;
}
$menuList[] = $menu;
}
}
return $menuList;
}
}

View File

@@ -114,4 +114,39 @@ Authorization: Bearer {{admin_token}}
### ali sts 临时授权
GET {{host}}/admin/third/sts/accredit
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
### 部门列表
GET {{host}}/admin/section/list
Content-Type: application/json
Authorization: Bearer {{admin_token}}
### 部门添加
POST {{host}}/admin/section/add
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
name=总公司c&pid=1
### 部门修改
POST {{host}}/admin/section/edit
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
id=1&name=总公司啊
### 部门删除
GET {{host}}/admin/section/del?id=1
Content-Type: application/json
Authorization: Bearer {{admin_token}}
### 部门详情
GET {{host}}/admin/section/info?id=1
Content-Type: application/json
Authorization: Bearer {{admin_token}}
### 部门启用列表
GET {{host}}/admin/section/all_list
Content-Type: application/json
Authorization: Bearer {{admin_token}}