AuthGroup.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 $reception_rules = [
  13. ["id" => 10001, "zh" => "客户资料", "en" => "Customer information", "icon" => ""],
  14. ["id" => 10002, "zh" => "查询业绩", "en" => "Query performance", "icon" => ""],
  15. ["id" => 10003, "zh" => "历史订单", "en" => "Historical Orders", "icon" => ""],
  16. ["id" => 10004, "zh" => "定金订单", "en" => "Deposit order", "icon" => ""],
  17. ["id" => 10005, "zh" => "退货", "en" => "Return goods", "icon" => ""],
  18. ["id" => 10006, "zh" => "换货", "en" => "Exchange goods", "icon" => ""],
  19. ["id" => 10007, "zh" => "年费管理", "en" => "Annual fee management", "icon" => ""],
  20. ["id" => 10008, "zh" => "生成X-report", "en" => "生成X-report", "icon" => ""],
  21. ["id" => 10009, "zh" => "生成Z-report", "en" => "生成Z-report", "icon" => ""]
  22. ];
  23. public function __construct(App $app)
  24. {
  25. parent::__construct($app);
  26. $this->authService = new AuthService();
  27. $this->storeModel = new StoreModel();
  28. }
  29. public function index() {
  30. return view();
  31. }
  32. /**
  33. * @return \think\response\Json|\think\response\View
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function add() {
  39. $all_groups = $this->authService->authGroupModel->fetchAllGroups();
  40. if ($this->request->isAjax()) {
  41. $params = $this->request->param();
  42. $res = $this->authService->authGroupModel->save([
  43. 'pid' => 0,
  44. 'name' => format_string($params['name'] ?? null),
  45. 'rules' => format_string($params['rule_ids'] ?? null),
  46. "reception_rules" => isset($params["reception_rules"]) ? join(',', $params['reception_rules']) : null,
  47. 'describe' => format_string($params['describe'] ?? null),
  48. ]);
  49. return $this->ok($res);
  50. }
  51. View::assign([
  52. "reception_rules" => $this->reception_rules,
  53. "all_groups" => recursion($all_groups, 0)
  54. ]);
  55. return view();
  56. }
  57. /**
  58. * @return \think\response\Json|\think\response\View
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function edit() {
  64. $params = $this->request->param();
  65. $id = $params['id'];
  66. $all_groups = $this->authService->authGroupModel->fetchAllGroups();
  67. $group = $this->authService->authGroupModel->findById($id);
  68. if ($this->request->isAjax()) {
  69. $res = $this->authService->authGroupModel->where('id', $params['id'])->save([
  70. 'pid' => 0,
  71. 'name' => $params['name'] ?? $group->name,
  72. 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'],
  73. "reception_rules" => isset($params["reception_rules"]) ? join(',', $params['reception_rules']) : null,
  74. 'describe' => $params['describe'] ?? $group->describe,
  75. 'update_time' => time()
  76. ]);
  77. return $this->ok($res);
  78. }
  79. $reception_rules = $group->reception_rules ? explode(',',$group->reception_rules) : [];
  80. foreach ($this->reception_rules as &$item) $item['isChecked'] = in_array($item['id'], $reception_rules);
  81. View::assign([
  82. 'group' => $group,
  83. "all_groups" => recursion($all_groups, 0),
  84. "reception_rules" => $this->reception_rules,
  85. ]);
  86. return view();
  87. }
  88. public function delete(\think\Request $request) {
  89. $params = $request->param();
  90. if(!isset($params['ids']))
  91. return $this->fail(lang("Please select the data you want to delete"));
  92. $this->authService->authGroupModel->deleteByIds(explode(',', $params['ids']));
  93. return $this->ok(true);
  94. }
  95. public function findAllAuthGroups() {
  96. $groups = $this->authService->authGroupModel->fetchAllGroups()->toArray();
  97. $format_groups = array_map(function ($data) {
  98. return [
  99. 'id' => $data['id'],
  100. 'title' => $data['name'],
  101. 'field' => $data['name'],
  102. 'spread' => true,
  103. 'checked' => false,
  104. 'disabled' => $data['id'] == 1
  105. ];
  106. }, $groups);
  107. return $this->ok($format_groups);
  108. }
  109. }