Admin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\StoreModel;
  8. use think\App;
  9. use think\facade\View;
  10. use think\Request;
  11. class Admin extends BaseController
  12. {
  13. private $model;
  14. private $authGroupModel;
  15. private $authGroupAccessModel;
  16. private $storeModel;
  17. public $rules = [
  18. ["id" => 1, "name" => "后台管理员", "pid" => 0],
  19. ["id" => 2, "name" => "门店", "pid" => 0],
  20. ["id" => 3, "name" => "门店主管", "pid" => 2],
  21. ["id" => 4, "name" => "门店老师", "pid" => 2],
  22. ["id" => 5, "name" => "门店顾问", "pid" => 2],
  23. ["id" => 6, "name" => "门店促销员", "pid" => 2],
  24. ["id" => 7, "name" => "佛堂", "pid" => 0],
  25. ["id" => 8, "name" => "佛堂负责人", "pid" => 7],
  26. ["id" => 9, "name" => "佛堂前台", "pid" => 7],
  27. ["id" => 10, "name" => "佛堂道士", "pid" => 7],
  28. ["id" => 11, "name" => "佛堂和尚", "pid" => 7],
  29. ["id" => 12, "name" => "物流部", "pid" => 0],
  30. ["id" => 13, "name" => "物流负责人", "pid" => 12],
  31. ["id" => 14, "name" => "物流助理", "pid" => 12],
  32. ["id" => 15, "name" => "财务", "pid" => 0],
  33. ["id" => 16, "name" => "财务负责人", "pid" => 15],
  34. ["id" => 17, "name" => "门店经销商", "pid" => 2],
  35. ];
  36. public function __construct(App $app)
  37. {
  38. $this->model = new AdminModel();
  39. $this->authGroupModel = new AuthGroupModel();
  40. $this->authGroupAccessModel = new AuthGroupAccessModel();
  41. $this->storeModel = new StoreModel();
  42. parent::__construct($app);
  43. }
  44. /**
  45. * @param Request $request
  46. * @return \think\response\View
  47. * @throws \think\db\exception\DbException
  48. */
  49. public function index(Request $request)
  50. {
  51. $params = $request->param();
  52. $list = $this->model->findByPaginate();
  53. $all_groups = $this->authGroupModel->fetchAllGroups();
  54. $format_params = [
  55. 'group_id' => format_string($params['group_id'] ?? null),
  56. ];
  57. View::assign([
  58. 'rules' => $this->rules,
  59. 'list' => $list,
  60. 'all_groups' => recursion($all_groups, 0),
  61. 'params' => $format_params
  62. ]);
  63. return view();
  64. }
  65. /**
  66. * @return \think\response\Json|\think\response\View
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function add()
  72. {
  73. if ($this->request->isAjax()) {
  74. $params = $this->request->param();
  75. $admin = $this->model->doesItExist($params['account']);
  76. if($admin)
  77. return $this->fail(lang("The account already exists."));
  78. $res = $this->model->create([
  79. 'account' => $params['account'],
  80. 'nickname' => $params['nickname'],
  81. 'mobile' => $params['mobile'],
  82. 'password' => md5($params['password']),
  83. 'store_id' => isset($params['store_id']) && $params['store_id'] > 0 ? $params : null,
  84. 'is_login_backstage' => $params['is_login_backstage'],
  85. 'rule' => $params['rule']
  86. ]);
  87. $this->authGroupAccessModel->save([
  88. 'admin_id' => $res->id,
  89. 'group_id' => $params['group_id']
  90. ]);
  91. return $this->ok(true);
  92. }
  93. $all_groups = $this->authGroupModel->fetchAllGroups();
  94. View::assign([
  95. 'stores' => $this->storeModel->findAllStore(),
  96. 'rules' => recursion($this->rules,0),
  97. 'all_groups' => recursion($all_groups, 0)
  98. ]);
  99. return view();
  100. }
  101. /**
  102. * @return \think\response\Json|\think\response\View
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function edit() {
  108. $params = $this->request->param();
  109. if(!isset($params['id']))
  110. return $this->fail(lang('ID not exist'));
  111. if ($this->request->isAjax()) {
  112. $admin = $this->model->doesItExist($params['account']);
  113. if($admin && $admin->id != $params['id'])
  114. return $this->fail(lang("The account already exists."));
  115. $this->model->where('id',$params['id'])->update([
  116. 'account' => $params['account'],
  117. 'nickname' => $params['nickname'],
  118. 'mobile' => $params['mobile'],
  119. 'password' => isset($params['password']) && $params['password'] != $admin->password ? md5($params['password']) : $admin['password'],
  120. 'store_id' => isset($params['store_id']) && $params['store_id'] > 0 ? $params['store_id'] : null,
  121. 'is_login_backstage' => $params['is_login_backstage'],
  122. 'rule' => $params['rule'] > 0 ? $params['rule'] : null,
  123. 'update_time' => time()
  124. ]);
  125. $relation = $this->authGroupAccessModel->findByAdminId($admin->id);
  126. if($relation && $relation->group_id != $params['group_id']) {
  127. $relation->where('id', $relation->id)->update([
  128. 'group_id' => $params['group_id']
  129. ]);
  130. }
  131. return $this->ok(true);
  132. }
  133. View::assign([
  134. 'stores' => $this->storeModel->findAllStore(),
  135. 'all_groups' => recursion($this->authGroupModel->fetchAllGroups(), 0),
  136. 'rules' => recursion($this->rules,0),
  137. 'admin' => $this->model->findById($params['id']),
  138. ]);
  139. return view();
  140. }
  141. public function delete(\think\Request $request) {
  142. $params = $request->param();
  143. if(!isset($params['ids']))
  144. return $this->fail(lang("Please select the data you want to delete"));
  145. $this->model->deleteByIds(explode(',', $params['ids']));
  146. return $this->ok(true);
  147. }
  148. public function total_data() {
  149. $params = $this->request->param();
  150. if(!isset($params['id']))
  151. return $this->fail(lang('ID not exist'));
  152. View::assign([
  153. 'stores' => $this->storeModel->findAllStore(),
  154. 'all_groups' => recursion($this->authGroupModel->fetchAllGroups(), 0),
  155. 'rules' => recursion($this->rules,0),
  156. 'admin' => $this->model->findById($params['id']),
  157. ]);
  158. return view();
  159. }
  160. }