AuthGroup.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\AuthService;
  4. use app\BaseController;
  5. use app\common\model\DepartmentModel;
  6. use think\App;
  7. use think\facade\View;
  8. class AuthGroup extends BaseController
  9. {
  10. private $authService;
  11. private $departmentModel;
  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->departmentModel = new DepartmentModel();
  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_department = $this->departmentModel->findAllDepartment();
  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. "data_rules" => isset($params["data_rules"]) ? join(',', $params['data_rules']) : null,
  48. "department_id" => format_string($params['department_id'] ?? null),
  49. 'describe' => format_string($params['describe'] ?? null),
  50. ]);
  51. return $this->ok($res);
  52. }
  53. View::assign([
  54. "reception_rules" => $this->reception_rules,
  55. "all_department" => recursion($all_department, 0)
  56. ]);
  57. return view();
  58. }
  59. /**
  60. * @return \think\response\Json|\think\response\View
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function edit() {
  66. $params = $this->request->param();
  67. $id = $params['id'];
  68. $group = $this->authService->authGroupModel->findById($id);
  69. $all_department = $this->departmentModel->findAllDepartment();
  70. if ($this->request->isAjax()) {
  71. $res = $this->authService->authGroupModel->where('id', $params['id'])->save([
  72. 'pid' => 0,
  73. 'name' => $params['name'] ?? $group->name,
  74. 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'],
  75. "reception_rules" => isset($params["reception_rules"]) ? join(',', $params['reception_rules']) : null,
  76. "data_rules" => isset($params["data_rules"]) ? join(',', $params['data_rules']) : null,
  77. "department_id" => format_string($params['department_id'] ?? null),
  78. 'describe' => $params['describe'] ?? $group->describe,
  79. 'update_time' => time()
  80. ]);
  81. return $this->ok($res);
  82. }
  83. $reception_rule_ids = $group->reception_rules ? explode(',',$group->reception_rules) : [];
  84. $reception_rules = array_merge([],$this->reception_rules);
  85. foreach ($reception_rules as &$item) $item['isChecked'] = in_array($item['id'], $reception_rule_ids);
  86. $data_rule_ids = $group->data_rules ? explode(',', $group->data_rules) : [];
  87. $data_rules = array_merge([], $this->reception_rules);
  88. foreach ($data_rules as &$item) $item['isChecked'] = in_array($item['id'], $data_rule_ids);
  89. View::assign([
  90. 'group' => $group,
  91. "reception_rules" => $reception_rules,
  92. "data_rules" => $data_rules,
  93. "all_department" => recursion($all_department, 0)
  94. ]);
  95. return view();
  96. }
  97. public function delete(\think\Request $request) {
  98. $params = $request->param();
  99. if(!isset($params['ids']))
  100. return $this->fail(lang("Please select the data you want to delete"));
  101. $this->authService->authGroupModel->deleteByIds(explode(',', $params['ids']));
  102. return $this->ok(true);
  103. }
  104. public function findAllAuthGroups() {
  105. $groups = $this->authService->authGroupModel->fetchAllGroups()->toArray();
  106. $format_groups = array_map(function ($data) {
  107. return [
  108. 'id' => $data['id'],
  109. 'title' => $data['name'],
  110. 'field' => $data['name'],
  111. 'spread' => true,
  112. 'checked' => false,
  113. 'disabled' => $data['id'] == 1
  114. ];
  115. }, $groups);
  116. return $this->ok($format_groups);
  117. }
  118. }