Role.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\controller\store;
  10. use app\store\controller\Controller;
  11. use app\store\model\store\Role as RoleModel;
  12. use app\store\model\store\Access as AccessModel;
  13. /**
  14. * 商家用户角色控制器
  15. * Class StoreUser
  16. * @package app\store\controller
  17. */
  18. class Role extends Controller
  19. {
  20. /**
  21. * 角色列表
  22. * @return mixed
  23. * @throws \think\Exception
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public function index()
  29. {
  30. $model = new RoleModel;
  31. $list = $model->getList();
  32. return $this->fetch('index', compact('list'));
  33. }
  34. /**
  35. * 添加角色
  36. * @return array|mixed
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. * @throws \Exception
  41. */
  42. public function add()
  43. {
  44. $model = new RoleModel;
  45. if (!$this->request->isAjax()) {
  46. // 权限列表
  47. $accessList = (new AccessModel)->getJsTree();
  48. // 角色列表
  49. $roleList = $model->getList();
  50. return $this->fetch('add', compact('accessList', 'roleList'));
  51. }
  52. // 新增记录
  53. if ($model->add($this->postData('role'))) {
  54. return $this->renderSuccess('添加成功', url('store.role/index'));
  55. }
  56. return $this->renderError($model->getError() ?: '添加失败');
  57. }
  58. /**
  59. * 更新角色
  60. * @param $role_id
  61. * @return array|mixed
  62. * @throws \think\Exception
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. * @throws \think\exception\PDOException
  67. */
  68. public function edit($role_id)
  69. {
  70. // 角色详情
  71. $model = RoleModel::detail($role_id);
  72. if (!$this->request->isAjax()) {
  73. // 权限列表
  74. $accessList = (new AccessModel)->getJsTree($model['role_id']);
  75. // 角色列表
  76. $roleList = $model->getList();
  77. return $this->fetch('edit', compact('model', 'accessList', 'roleList'));
  78. }
  79. // 更新记录
  80. if ($model->edit($this->postData('role'))) {
  81. return $this->renderSuccess('更新成功', url('store.role/index'));
  82. }
  83. return $this->renderError($model->getError() ?: '更新失败');
  84. }
  85. /**
  86. * 删除角色
  87. * @param $role_id
  88. * @return array
  89. * @throws \think\exception\DbException
  90. */
  91. public function delete($role_id)
  92. {
  93. // 角色详情
  94. $model = RoleModel::detail($role_id);
  95. if (!$model->remove()) {
  96. return $this->renderError($model->getError() ?: '删除失败');
  97. }
  98. return $this->renderSuccess('删除成功');
  99. }
  100. }