Admin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\system\controller;
  4. use app\model\system\AdminMenu;
  5. use app\model\system\AdminRole;
  6. use app\model\system\Admin as AdminModel;
  7. use app\Request;
  8. use app\system\controller\AuthController;
  9. use library\services\UtilService;
  10. use library\utils\IpLocation;
  11. use think\facade\Config;
  12. use think\facade\Cache;
  13. class Admin extends AuthController
  14. {
  15. /**
  16. * 退出登陆
  17. * @return mixed
  18. * @throws \Psr\SimpleCache\InvalidArgumentException
  19. */
  20. public function logout()
  21. {
  22. $key = trim(ltrim($this->request->header(Config::get('cookie.system_token_name')), ''));
  23. $res = Cache::store('redis')->delete($key);
  24. return app('json')->success("ok", []);
  25. }
  26. /**
  27. * 获取登录信息 [自己]
  28. * @param Request $request
  29. * @return mixed
  30. */
  31. public function info(Request $request) {
  32. [$id] = UtilService::getMore([
  33. ['id','','empty','参数错误']
  34. ],$request,true);
  35. if($id == $this->adminInfo['id']) {
  36. return app('json')->success([
  37. 'id' => $id,
  38. 'username' => $this->adminInfo['username'],
  39. 'nickname' => $this->adminInfo['nickname'],
  40. 'name' => $this->adminInfo['name'],
  41. 'avatar' => $this->adminInfo['avatar'],
  42. 'regTime' => empty($this->adminInfo['time']) ? '' : date('Y-m-d',$this->adminInfo['time']),
  43. 'lastTime' => empty($this->adminInfo['last_time']) ? '' : date('Y-m-d',$this->adminInfo['last_time']),
  44. 'ip' => $this->adminInfo['ip'],
  45. 'ipArea' => (new IpLocation())->getlocation($this->adminInfo['ip'])['country'],
  46. 'roleName' => (new AdminRole())->getRoleId($this->adminInfo['role_id'],'name')
  47. ]);
  48. }
  49. }
  50. /**
  51. * 修改信息 [自己]
  52. * @param Request $request
  53. * @return mixed
  54. */
  55. public function edit(Request $request) {
  56. [$name,$avatar] = UtilService::getMore([
  57. ['name','','empty','请输入管理姓名'],
  58. ['avatar','']
  59. ],$request,true);
  60. $bool = AdminModel::update(['name'=>$name,'avatar'=>$avatar],['id'=>$this->adminInfo['id']]);
  61. if($bool) {
  62. return app('json')->success("修改成功", []);
  63. }
  64. return app('json')->fail("数据更新失败");
  65. }
  66. /**
  67. * 修改密码 [自己]
  68. * @param Request $request
  69. * @return mixed
  70. */
  71. public function password(Request $request) {
  72. [$oldPass,$newPass] = UtilService::getMore([
  73. ['oldPass','','empty','请输入旧密码'],
  74. ['newPass','','empty','请输入新密码']
  75. ],$request,true);
  76. if(!AdminModel::checkPassowrd($this->adminInfo['id'],$oldPass)){
  77. return app('json')->fail("旧密码错误,请重新输入");
  78. }
  79. AdminModel::where('id',$this->adminInfo['id'])->save(['password'=>password_hash($newPass,PASSWORD_BCRYPT)]);
  80. return app('json')->success("密码修改成功", []);
  81. }
  82. /**
  83. * 获取管理列表 [筛选]
  84. * @param Request $request
  85. * @return mixed
  86. */
  87. public function list(Request $request)
  88. {
  89. $pageSize = 50;
  90. [$page, $roleId, $keyword] = UtilService::getMore(
  91. [
  92. ['page', 1],
  93. ['roleId', 0],
  94. ['keyword', ''],
  95. ], $request, true
  96. );
  97. list($pageCount, $data) = (new AdminModel)
  98. ->getList($page,
  99. ['role_id' => [$roleId, '!empty'], 'username' => [$keyword, '!empty']]
  100. , $pageSize, ['*',""],'id desc');
  101. $result = UtilService::getParam([
  102. 'id',
  103. 'username',
  104. 'name',
  105. 'avatar',
  106. 'nickname',
  107. 'status',
  108. ['regTime', 'time', 'date("Y-m-d",$1)'],
  109. ['lastTime', 'last_time', 'date("Y-m-d",$1)'],
  110. 'ip',
  111. ['ipArea', 'ip',
  112. function ($item) {
  113. return (new IpLocation())->getlocation($item)['country'];
  114. },
  115. ],
  116. ['roleName', 'role_id',
  117. function ($item) {
  118. return (new AdminRole())->getRoleId($item, 'name');
  119. },
  120. ],
  121. ], $data);
  122. return app('json')->success([
  123. 'list' => $result,
  124. 'pageCount' => $pageCount,
  125. 'pageSize' => $pageSize,
  126. 'page' => $page,
  127. ]);
  128. }
  129. /**
  130. * 获取管理数据 [某一管理]
  131. * @param Request $request
  132. * @return mixed
  133. */
  134. public function getInfo(Request $request) {
  135. [$id] = UtilService::getMore([
  136. ['id','','empty','参数错误']
  137. ],$request,true);
  138. $adminInfo = AdminModel::get(compact('id'));
  139. return app('json')->success([
  140. 'id' => $id,
  141. 'username' => $adminInfo['username'],
  142. 'nickname' => $adminInfo['nickname'],
  143. 'name' => $adminInfo['name'],
  144. 'avatar' => $adminInfo['avatar'],
  145. 'role_id' => $adminInfo['role_id'],
  146. 'status' => $adminInfo['status']
  147. ]);
  148. }
  149. /**
  150. * 保存数据 [某一个管理]
  151. * @param Request $request
  152. * @return mixed
  153. */
  154. public function save(Request $request) {
  155. $post = UtilService::getMore(
  156. [
  157. ['id','0'],
  158. ['name','','empty','请输入姓名'],
  159. ['username','','empty','请输入账户'],
  160. ['nickname',''],
  161. ['status','0'],
  162. ['role_id','','empty','请选择管理角色'],
  163. ['password',''],
  164. ['avatar',''],
  165. ],
  166. $request
  167. );
  168. $bool = AdminModel::saveAdmin($post);
  169. if($bool)
  170. return app('json')->success("操作成功", []);
  171. else
  172. return app('json')->fail(AdminModel::getErrorInfo());
  173. }
  174. /**
  175. * 删除数据 [删除管理员]
  176. * @param Request $request
  177. * @return mixed
  178. */
  179. public function del(Request $request) {
  180. [$id] = UtilService::getMore(
  181. [
  182. ['id','0'],
  183. ],
  184. $request,true
  185. );
  186. AdminModel::del($id);
  187. return app('json')->success("删除成功", []);
  188. }
  189. }