Admin.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Admin as AdminModel;
  5. use app\admin\model\RoleMenu;
  6. use app\admin\model\Menu;
  7. use think\exception\Exception;
  8. use think\facade\Db;
  9. use app\admin\validate\AdminValidate;
  10. use app\admin\model\User;
  11. use think\facade\Session;
  12. class Admin extends BaseController
  13. {
  14. protected $noNeedLogin = ['login'];
  15. /**
  16. * @api {get} /list 查询管理员列表
  17. */
  18. public function list()
  19. {
  20. try {
  21. $params = $this->request->param();
  22. $query = new AdminModel();
  23. if (!empty($params['username'])) {
  24. $query->where('username', 'like', "%{$params['username']}%");
  25. }
  26. if (!empty($params['phone'])) {
  27. $query->where('phone', 'like', "%{$params['phone']}%");
  28. }
  29. $page = isset($params['page']) ? intval($params['page']) : 1;
  30. $limit = isset($params['limit']) ? intval($params['limit']) : 15;
  31. $count = $query->count();
  32. $list = $query->with(['role', 'department'])
  33. ->order('id', 'desc')
  34. ->limit($limit)
  35. ->page($page)
  36. ->select();
  37. } catch (\Exception $e) {
  38. return $this->error('查询失败:'.$e->getMessage());
  39. }
  40. return $this->success(['count' => $count, 'list' => $list]);
  41. }
  42. /**
  43. * @api {post} /update 添加/修改管理员
  44. */
  45. public function update()
  46. {
  47. Db::startTrans();
  48. try {
  49. $params = (new AdminValidate())->post()->goCheck('add');
  50. $id = $this->request->param('id');
  51. // 只允许超级管理员配置
  52. if ($this->admin_id != 1) {
  53. return $this->error('只有超级管理员才能操作');
  54. }
  55. if (empty($id)) {
  56. // 新增校验
  57. if (empty($params['password'])) {
  58. return $this->error('密码不能为空');
  59. }
  60. if (empty($params['role_id'])) {
  61. return $this->error('角色ID不能为空');
  62. }
  63. if (empty($params['department_id'])) {
  64. return $this->error('部门ID不能为空');
  65. }
  66. } elseif ($id != 1) {
  67. if (empty($params['role_id'])) {
  68. return $this->error('角色ID不能为空');
  69. }
  70. if (empty($params['department_id'])) {
  71. return $this->error('部门ID不能为空');
  72. }
  73. }
  74. if (!empty($params['password'])) {
  75. $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
  76. } else {
  77. unset($params['password']);
  78. }
  79. if (!empty($params['payment_password'])) {
  80. $params['payment_password'] = password_hash($params['payment_password'], PASSWORD_DEFAULT);
  81. } else {
  82. unset($params['payment_password']);
  83. }
  84. $usernameCount = AdminModel::where('username', $params['username'])
  85. ->where('id', '<>', $id ?? 0)
  86. ->count();
  87. if ($usernameCount > 0) {
  88. return $this->error('用户名已存在');
  89. }
  90. if (!empty($id)) {
  91. AdminModel::where('id', $id)->update($params);
  92. } else {
  93. $admin = AdminModel::create($params);
  94. //插入user
  95. $params['id'] = $admin->id;
  96. User::addCs($params);
  97. }
  98. Db::commit();
  99. } catch (\Exception $e) {
  100. Db::rollback();
  101. return $this->error($e->getMessage());
  102. }
  103. return $this->success([],'操作成功');
  104. }
  105. // 删除管理员
  106. public function delete()
  107. {
  108. try {
  109. $params = (new AdminValidate())->goCheck('del');
  110. // 只允许超级管理员配置
  111. if ($this->admin_id != 1) {
  112. return $this->error('只有超级管理员才能操作');
  113. }
  114. if ($params['id'] == 1) {
  115. return $this->error('超级管理员不能删除');
  116. }
  117. // TP 删除语法
  118. AdminModel::where('id', $params['id'])->delete();
  119. } catch (\Exception $e) {
  120. return $this->error('删除失败:'.$e->getMessage());
  121. }
  122. return $this->success([],'删除成功');
  123. }
  124. /**
  125. * @api {post} /setPassword 修改登录密码
  126. */
  127. public function setPassword()
  128. {
  129. Db::startTrans();
  130. try {
  131. $params = (new AdminValidate())->post()->goCheck('password');
  132. $user = AdminModel::find($this->admin_id);
  133. if (!$user) {
  134. throw new \Exception('用户不存在');
  135. }
  136. // 校验旧密码
  137. if (!password_verify($params['old_password'], $user->password)) {
  138. throw new \Exception('密码错误');
  139. }
  140. // 更新登录密码
  141. $user->password = password_hash($params['password'], PASSWORD_DEFAULT);
  142. $user->save();
  143. Db::commit();
  144. } catch (\Exception $e) {
  145. Db::rollback();
  146. return $this->error($e->getMessage());
  147. }
  148. }
  149. /**
  150. * @api {post} /login 管理员登录
  151. */
  152. public function login()
  153. {
  154. // 登录无需事务,移除多余的 Db::startTrans()
  155. try {
  156. $params = (new AdminValidate())->post()->goCheck('login');
  157. $admin = AdminModel::where('username', $params['username'])->find();
  158. if (!$admin) {
  159. throw new \Exception('账号不存在');
  160. }
  161. if (!password_verify($params['password'], $admin->password)) {
  162. throw new \Exception('密码错误');
  163. }
  164. // 获取角色权限(优化 TP 查询语法)
  165. if ($admin->role_id && $admin->id > 1) {
  166. $admin->role_menu = RoleMenu::alias('rm')
  167. ->join('menu m', 'rm.menu_id = m.id','left')
  168. ->where('rm.role_id', $admin->role_id)
  169. ->where('m.status', 1)
  170. ->field(['m.id', 'm.parent_id', 'm.type', 'm.name', 'm.perms', 'm.paths', 'm.component', 'm.params', 'm.is_cache', 'm.sort'])
  171. ->order('m.sort', 'asc')
  172. ->select()
  173. ->toArray();
  174. } else {
  175. $admin->role_menu = Menu::alias('m')
  176. ->where('m.status', 1)
  177. ->field(['m.id', 'm.parent_id', 'm.type', 'm.name', 'm.perms', 'm.paths', 'm.component', 'm.params', 'm.is_cache', 'm.sort'])
  178. ->order('m.sort', 'asc')
  179. ->select()
  180. ->toArray();
  181. }
  182. //如果登录信息中含有client——id则自动进行绑定
  183. $userInfo = User::where('cs_uid', $admin->id)->find();
  184. if(!$userInfo){
  185. throw new \Exception('用户不存在');
  186. }
  187. if(!empty($params['client_id'])){
  188. $cid = $this->request->header('cid','');
  189. doBindUid($userInfo['user_id'],$params['client_id'],$cid,$this->request->isMobile());
  190. }
  191. $update=[
  192. 'last_login_time' => time(),
  193. 'last_login_ip' => $this->request->ip(),
  194. 'login_count' => Db::raw('login_count+1')
  195. ];
  196. User::where('user_id',$userInfo['user_id'])->update($update);
  197. $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']);
  198. unset($userInfo['password'],$userInfo['salt']);
  199. $userInfo['displayName']=$userInfo['realname'];
  200. $userInfo['id']=$userInfo['user_id'];
  201. $authToken=User::refreshToken($userInfo,$param['terminal'] ?? 'web', ['id' => $admin->id, 'role_id' => $admin->role_id]);
  202. $data=[
  203. 'sessionId'=>Session::getId(),
  204. 'token' => $authToken,
  205. 'userInfo'=>$userInfo,
  206. 'adminInfo' => $admin,
  207. ];
  208. } catch (\Exception $e) {
  209. return $this->error($e->getMessage());
  210. }
  211. return $this->success($data);
  212. }
  213. }