Admin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 think\App;
  8. use think\facade\View;
  9. use think\Request;
  10. class Admin extends BaseController
  11. {
  12. private $model;
  13. private $authGroupModel;
  14. private $authGroupAccessModel;
  15. public function __construct(App $app)
  16. {
  17. $this->model = new AdminModel();
  18. $this->authGroupModel = new AuthGroupModel();
  19. $this->authGroupAccessModel = new AuthGroupAccessModel();
  20. parent::__construct($app);
  21. }
  22. /**
  23. * @param Request $request
  24. * @return \think\response\View
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function index(Request $request)
  28. {
  29. $list = $this->model->findByPaginate();
  30. View::assign([
  31. 'list' => $list,
  32. ]);
  33. return view();
  34. }
  35. /**
  36. * @return \think\response\Json|\think\response\View
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function add()
  42. {
  43. if ($this->request->isAjax()) {
  44. $params = $this->request->param();
  45. $admin = $this->model->doesItExist($params['account']);
  46. if($admin)
  47. return $this->fail(lang("The account already exists."));
  48. $res = $this->model->create([
  49. 'account' => $params['account'],
  50. 'nickname' => $params['nickname'],
  51. 'password' => md5($params['password']),
  52. ]);
  53. $this->authGroupAccessModel->save([
  54. 'admin_id' => $res->id,
  55. 'group_id' => $params['group_id']
  56. ]);
  57. return $this->ok(true);
  58. }
  59. $groups = $this->authGroupModel->fetchAllGroups();
  60. View::assign([
  61. 'groups' => $groups
  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 edit() {
  72. $params = $this->request->param();
  73. if(!isset($params['id']))
  74. return $this->fail(lang('ID not exist'));
  75. if ($this->request->isAjax()) {
  76. $admin = $this->model->doesItExist($params['account']);
  77. if($admin && $admin->id != $params['id'])
  78. return $this->fail(lang("The account already exists."));
  79. $this->model->where('id',$params['id'])->update([
  80. 'account' => $params['account'],
  81. 'nickname' => $params['nickname'],
  82. 'password' => $params['password'] == $admin->password ? $admin->password : md5($params['password']),
  83. 'update_time' => time()
  84. ]);
  85. $relation = $this->authGroupAccessModel->findByAdminId($admin->id);
  86. if($relation && $relation->group_id != $params['group_id']) {
  87. $relation->where('id', $relation->id)->update([
  88. 'group_id' => $params['group_id']
  89. ]);
  90. }
  91. return $this->ok(true);
  92. }
  93. $groups = $this->authGroupModel->fetchAllGroups();
  94. View::assign([
  95. 'groups' => $groups,
  96. 'admin' => $this->model->findById($params['id']),
  97. ]);
  98. return view();
  99. }
  100. public function delete(\think\Request $request) {
  101. $params = $request->param();
  102. if(!isset($params['ids']))
  103. return $this->fail(lang("Please select the data you want to delete"));
  104. $this->model->deleteByIds(explode(',', $params['ids']));
  105. return $this->ok(true);
  106. }
  107. public function test() {
  108. echo "test";
  109. }
  110. }