123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/21
- * @time: 17:39
- */
- namespace app\controller\admin\system;
- use app\common\AdminBaseController;
- use app\Request;
- use app\services\system\admin\SystemMenusServices;
- use app\services\system\admin\SystemRoleServices;
- use qiniu\services\CacheService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- class SystemRoles extends AdminBaseController
- {
- public function __construct(Request $request, SystemRoleServices $service)
- {
- parent::__construct($request);
- $this->service = $service;
- $this->request->filter(['addslashes', 'trim']);
- $this->createParams = [
- ['role_name', ''],
- ['status', 0],
- ['checked_menus', []]
- ];
- $this->saveDeal = $this->updateDeal = function (&$data) {
- if (!$data['role_name']) throw new ValidateException('请输入身份名称');
- if (!is_array($data['checked_menus']) || !count($data['checked_menus']))
- throw new ValidateException('请选择最少一个权限');
- $data['rules'] = implode(',', $data['checked_menus']);
- };
- $this->searchable = [
- ['status', ''],
- ['role_name', ''],
- ];
- }
- public function index()
- {
- $where = $this->request->getMore($this->searchable);
- $where['type'] = 0;
- $where['level'] = $this->adminInfo['level'] + 1;
- return $this->success($this->service->getRoleList($where));
- }
- public function read($id)
- {
- $info = $this->service->get($id);
- if (!$info)
- return $this->error('数据不存在');
- /** @var SystemMenusServices $services */
- $services = app()->make(SystemMenusServices::class);
- $info['menus'] = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0);
- return $this->success('ok', $info->toArray());
- }
- public function save()
- {
- $data = $this->request->postMore($this->createParams);
- $data['level'] = $this->adminInfo['level'] + 1;
- $res = $this->service->create($data);
- if ($res) return $this->success('添加成功');
- return $this->error('添加失败');
- }
- /**
- * 修改状态
- * @param $id
- * @param $status
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function setStatus($id, $status)
- {
- if (!$id) {
- return $this->error('缺少参数');
- }
- $role = $this->service->get($id);
- if (!$role) {
- return $this->error('没有查到此身份');
- }
- $role->status = $status;
- if ($role->save()) {
- CacheService::clear();
- return $this->success('修改成功');
- } else {
- return $this->error('修改失败');
- }
- }
- }
|