Admin.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\AdminModel;
  5. use app\common\model\AuthGroupAccessModel;
  6. use app\common\model\AuthGroupModel;
  7. use app\common\model\DepartmentModel;
  8. use app\common\model\StoreModel;
  9. use think\App;
  10. use think\facade\View;
  11. use think\Request;
  12. class Admin extends BaseController
  13. {
  14. private $model;
  15. private $authGroupModel;
  16. private $authGroupAccessModel;
  17. private $storeModel;
  18. private $departmentModel;
  19. public function __construct(App $app)
  20. {
  21. $this->model = new AdminModel();
  22. $this->authGroupModel = new AuthGroupModel();
  23. $this->authGroupAccessModel = new AuthGroupAccessModel();
  24. $this->storeModel = new StoreModel();
  25. $this->departmentModel = new DepartmentModel();
  26. parent::__construct($app);
  27. }
  28. /**
  29. * @param Request $request
  30. * @return \think\response\View
  31. * @throws \think\db\exception\DbException
  32. */
  33. public function index(Request $request)
  34. {
  35. $params = $request->param();
  36. $format_params = [
  37. 'group_id' => format_string($params['group_id'] ?? null),
  38. 'department_id' => format_string($params['department_id'] ?? null),
  39. ];
  40. $list = $this->model->findByPaginate($format_params);
  41. $all_groups = $this->authGroupModel->fetchAllGroups();
  42. View::assign([
  43. 'all_department' => recursion($this->departmentModel->findAllDepartment()->toArray(),0),
  44. 'list' => $list,
  45. 'all_groups' => recursion($all_groups, 0),
  46. 'params' => $format_params
  47. ]);
  48. return view();
  49. }
  50. /**
  51. * @return \think\response\Json|\think\response\View
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function add()
  57. {
  58. if ($this->request->isAjax()) {
  59. $params = $this->request->param();
  60. $admin = $this->model->doesItExist($params['account']);
  61. if($admin)
  62. return $this->fail(lang("The account already exists."));
  63. $department = $this->departmentModel->findById($params['department_id']);
  64. if(!$department)
  65. return $this->fail('部门不存在!');
  66. $res = $this->model->create([
  67. 'account' => $params['account'],
  68. 'nickname' => $params['nickname'],
  69. 'mobile' => $params['mobile'],
  70. 'password' => md5($params['password']),
  71. 'department_id' => $params['department_id'],
  72. 'store_ids' => isset($params['store_ids']) ? join(',', $params['store_ids']) : $department->store_id,
  73. 'is_login_backstage' => $params['is_login_backstage'],
  74. ]);
  75. $this->authGroupAccessModel->save([
  76. 'admin_id' => $res->id,
  77. 'group_id' => $params['group_id']
  78. ]);
  79. return $this->ok(true);
  80. }
  81. $all_groups = $this->authGroupModel->fetchAllGroups();
  82. $all_department = recursion($this->departmentModel->findAllDepartment()->toArray(),0);
  83. View::assign([
  84. 'stores' => $this->storeModel->findAllStore()->toArray(),
  85. 'all_department' => $all_department,
  86. 'all_group' => recursion($all_groups, 0),
  87. 'now_group' => $this->authGroupModel->fetchByDepartmentId($all_department[0] ? $all_department[0]['id'] : -1 ),
  88. ]);
  89. return view();
  90. }
  91. /**
  92. * @return \think\response\Json|\think\response\View
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function edit() {
  98. $params = $this->request->param();
  99. if(!isset($params['id']))
  100. return $this->fail(lang('ID not exist'));
  101. $admin = $this->model->findById($params['id']);
  102. if ($this->request->isAjax()) {
  103. $admin = $this->model->doesItExist($params['account']);
  104. if($admin && $admin->id != $params['id'])
  105. return $this->fail(lang("The account already exists."));
  106. $department = $this->departmentModel->findById($params['department_id']);
  107. if(!$department)
  108. return $this->fail('部门不存在!');
  109. $this->model->where('id',$params['id'])->update([
  110. 'account' => $params['account'],
  111. 'nickname' => $params['nickname'],
  112. 'mobile' => $params['mobile'],
  113. 'password' => isset($params['password']) && $params['password'] != $admin->password ? md5($params['password']) : $admin['password'],
  114. 'department_id' => $params['department_id'],
  115. 'store_ids' => isset($params['store_ids']) ? join(',', $params['store_ids']) : $department->store_id,
  116. 'is_login_backstage' => $params['is_login_backstage'],
  117. 'update_time' => time()
  118. ]);
  119. $relation = $this->authGroupAccessModel->findByAdminId($admin->id);
  120. if($relation && $relation->group_id != $params['group_id']) {
  121. $relation->where('id', $relation->id)->update([
  122. 'group_id' => $params['group_id']
  123. ]);
  124. }
  125. return $this->ok(true);
  126. }
  127. $all_department = recursion($this->departmentModel->findAllDepartment()->toArray(),0);
  128. $stores = $this->storeModel->findAllStore();
  129. foreach ($stores as $store) $store->isChecked = in_array($store->id, explode(',', $admin->store_ids ?? ''));
  130. View::assign([
  131. 'stores' => $stores,
  132. 'all_department' => $all_department,
  133. 'admin' => $admin,
  134. 'now_group' => $this->authGroupModel->fetchByDepartmentId($admin->department_id ?? -1),
  135. ]);
  136. return view();
  137. }
  138. public function getBindRules($department_id = null) {
  139. return $this->ok(
  140. $this->authGroupModel
  141. ->fetchByDepartmentId($department_id > 0 ? $department_id : -1 )
  142. );
  143. }
  144. public function delete(\think\Request $request) {
  145. $params = $request->param();
  146. if(!isset($params['ids']))
  147. return $this->fail(lang("Please select the data you want to delete"));
  148. $this->model->deleteByIds(explode(',', $params['ids']));
  149. return $this->ok(true);
  150. }
  151. public function total_data() {
  152. $params = $this->request->param();
  153. if(!isset($params['id']))
  154. return $this->fail(lang('ID not exist'));
  155. View::assign([
  156. 'stores' => $this->storeModel->findAllStore(),
  157. 'all_groups' => recursion($this->authGroupModel->fetchAllGroups(), 0),
  158. 'admin' => $this->model->findById($params['id']),
  159. ]);
  160. return view();
  161. }
  162. public function findAdmins(Request $request) {
  163. $params = $request->param();
  164. if(!isset($params['text']))
  165. return $this->fail(lang('Data not exist'));
  166. return $this->ok($this->model->findAdmins($params['text']));
  167. }
  168. }