AuthGroup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\AuthService;
  4. use app\BaseController;
  5. use app\common\model\StoreModel;
  6. use think\App;
  7. use think\facade\View;
  8. class AuthGroup extends BaseController
  9. {
  10. private $authService;
  11. private $storeModel;
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->authService = new AuthService();
  16. $this->storeModel = new StoreModel();
  17. }
  18. public function index() {
  19. return view();
  20. }
  21. /**
  22. * @return \think\response\Json|\think\response\View
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function add() {
  28. $all_groups = $this->authService->authGroupModel->fetchAllGroups();
  29. if ($this->request->isAjax()) {
  30. $params = $this->request->param();
  31. $res = $this->authService->authGroupModel->save([
  32. 'pid' => $params['pid'] ?? 1,
  33. 'name' => format_string($params['name'] ?? null),
  34. 'rules' => format_string($params['rule_ids'] ?? null),
  35. "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null,
  36. 'describe' => format_string($params['describe'] ?? null),
  37. ]);
  38. return $this->ok($res);
  39. }
  40. $stores = $this->storeModel->findAllStore();
  41. View::assign([
  42. "stores" => $stores,
  43. "all_groups" => recursion($all_groups, 0)
  44. ]);
  45. return view();
  46. }
  47. /**
  48. * @return \think\response\Json|\think\response\View
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function edit() {
  54. $params = $this->request->param();
  55. $id = $params['id'];
  56. $all_groups = $this->authService->authGroupModel->fetchAllGroups();
  57. $group = $this->authService->authGroupModel->findById($id);
  58. if ($this->request->isAjax()) {
  59. $res = $this->authService->authGroupModel->where('id', $params['id'])->save([
  60. 'pid' => $params['pid'] ?? $group->pid,
  61. 'name' => $params['name'] ?? $group->name,
  62. 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'],
  63. "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null,
  64. 'describe' => $params['describe'] ?? $group->describe,
  65. 'update_time' => time()
  66. ]);
  67. return $this->ok($res);
  68. }
  69. $stores = $this->storeModel->findAllStore();
  70. $store_ids = $group->store_ids ? explode(",",$group->store_ids) : [];
  71. foreach ($stores as &$item) $item->isChecked = in_array($item->id, $store_ids);
  72. View::assign([
  73. 'group' => $group,
  74. "stores" => $stores,
  75. "all_groups" => recursion($all_groups, 0)
  76. ]);
  77. return view();
  78. }
  79. public function delete(\think\Request $request) {
  80. $params = $request->param();
  81. if(!isset($params['ids']))
  82. return $this->fail(lang("Please select the data you want to delete"));
  83. $this->authService->authGroupModel->deleteByIds(explode(',', $params['ids']));
  84. return $this->ok(true);
  85. }
  86. public function findAllAuthGroups() {
  87. $groups = $this->authService->authGroupModel->fetchAllGroups()->toArray();
  88. $format_groups = array_map(function ($data) {
  89. return [
  90. 'id' => $data['id'],
  91. 'title' => $data['name'],
  92. 'pid' => $data['pid'],
  93. 'field' => $data['name'],
  94. 'spread' => true,
  95. 'checked' => false,
  96. 'disabled' => $data['id'] == 1
  97. ];
  98. }, $groups);
  99. return $this->ok(recursion($format_groups));
  100. }
  101. }