123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\controller\admin\v1\system;
- use app\controller\admin\AuthController;
- use app\services\system\SystemMenusServices;
- use app\services\system\SystemRoleServices;
- use think\facade\App;
- class SystemRole extends AuthController
- {
-
- public function __construct(App $app, SystemRoleServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
-
- public function index()
- {
- $where = $this->request->getMore([
- ['status', ''],
- ['role_name', ''],
- ]);
- $where['type'] = 0;
- $where['level'] = $this->adminInfo['level'] + 1;
- return $this->success($this->services->getRoleList($where));
- }
-
- public function create(SystemMenusServices $services)
- {
- $menus = $services->getmenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0);
- return $this->success(compact('menus'));
- }
-
- public function save($id)
- {
- $data = $this->request->postMore([
- 'role_name',
- ['status', 0],
- ['checked_menus', [], '', 'rules']
- ]);
- if (!$data['role_name']) return $this->fail('请输入身份名称');
- if (!is_array($data['rules']) || !count($data['rules']))
- return $this->fail('请选择最少一个权限');
- $data['rules'] = implode(',', $data['rules']);
- if ($id) {
- if (!$this->services->update($id, $data)) return $this->fail('修改失败!');
- \crmeb\services\CacheService::clear();
- return $this->success('修改成功!');
- } else {
- $data['level'] = $this->adminInfo['level'] + 1;
- if (!$this->services->save($data)) return $this->fail('添加身份失败!');
- \crmeb\services\CacheService::clear();
- return $this->success('添加身份成功!');
- }
- }
-
- public function edit(SystemMenusServices $services, $id)
- {
- $role = $this->services->get($id);
- if (!$role) {
- return $this->fail('修改的角色不存在');
- }
- $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0);
- return $this->success(['role' => $role->toArray(), 'menus' => $menus]);
- }
-
- public function delete($id)
- {
- if (!$this->services->delete($id))
- return $this->fail('删除失败,请稍候再试!');
- else {
- \crmeb\services\CacheService::clear();
- return $this->success('删除成功!');
- }
- }
-
- public function set_status($id, $status)
- {
- if (!$id) {
- return $this->fail('缺少参数');
- }
- $role = $this->services->get($id);
- if (!$role) {
- return $this->fail('没有查到此身份');
- }
- $role->status = $status;
- if ($role->save()) {
- \crmeb\services\CacheService::clear();
- return $this->success('修改成功');
- } else {
- return $this->fail('修改失败');
- }
- }
- }
|