Admin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. unset($params['username']);
  92. AdminModel::where('id', $id)->update($params);
  93. } else {
  94. $admin = AdminModel::create($params);
  95. //插入user
  96. $params['id'] = $admin->id;
  97. User::addCs($params);
  98. }
  99. Db::commit();
  100. } catch (\Exception $e) {
  101. Db::rollback();
  102. return $this->error($e->getMessage());
  103. }
  104. return $this->success([],'操作成功');
  105. }
  106. // 删除管理员
  107. public function delete()
  108. {
  109. try {
  110. $params = (new AdminValidate())->goCheck('del');
  111. // 只允许超级管理员配置
  112. if ($this->admin_id != 1) {
  113. return $this->error('只有超级管理员才能操作');
  114. }
  115. if ($params['id'] == 1) {
  116. return $this->error('超级管理员不能删除');
  117. }
  118. // TP 删除语法
  119. AdminModel::where('id', $params['id'])->delete();
  120. } catch (\Exception $e) {
  121. return $this->error('删除失败:'.$e->getMessage());
  122. }
  123. return $this->success([],'删除成功');
  124. }
  125. /**
  126. * @api {post} /setPassword 修改登录密码
  127. */
  128. public function setPassword()
  129. {
  130. Db::startTrans();
  131. try {
  132. $params = (new AdminValidate())->post()->goCheck('password');
  133. $user = AdminModel::find($this->admin_id);
  134. if (!$user) {
  135. throw new \Exception('用户不存在');
  136. }
  137. // 校验旧密码
  138. if (!password_verify($params['old_password'], $user->password)) {
  139. throw new \Exception('密码错误');
  140. }
  141. // 更新登录密码
  142. $user->password = password_hash($params['password'], PASSWORD_DEFAULT);
  143. $user->save();
  144. Db::commit();
  145. } catch (\Exception $e) {
  146. Db::rollback();
  147. return $this->error($e->getMessage());
  148. }
  149. }
  150. /**
  151. * @api {post} /login 管理员登录
  152. */
  153. public function login()
  154. {
  155. // 登录无需事务,移除多余的 Db::startTrans()
  156. try {
  157. $params = (new AdminValidate())->post()->goCheck('login');
  158. $admin = AdminModel::where('username', $params['username'])->find();
  159. if (!$admin) {
  160. throw new \Exception('账号不存在');
  161. }
  162. if (!password_verify($params['password'], $admin->password)) {
  163. throw new \Exception('密码错误');
  164. }
  165. // 获取角色权限(优化 TP 查询语法)
  166. if ($admin->role_id && $admin->id > 1) {
  167. $admin->role_menu = RoleMenu::alias('rm')
  168. ->join('menu m', 'rm.menu_id = m.id','left')
  169. ->where('rm.role_id', $admin->role_id)
  170. ->where('m.status', 1)
  171. ->field(['m.id', 'm.parent_id', 'm.type', 'm.name', 'm.perms', 'm.paths', 'm.component', 'm.params', 'm.is_cache', 'm.sort'])
  172. ->order('m.sort', 'asc')
  173. ->select()
  174. ->toArray();
  175. } else {
  176. $admin->role_menu = Menu::alias('m')
  177. ->where('m.status', 1)
  178. ->field(['m.id', 'm.parent_id', 'm.type', 'm.name', 'm.perms', 'm.paths', 'm.component', 'm.params', 'm.is_cache', 'm.sort'])
  179. ->order('m.sort', 'asc')
  180. ->select()
  181. ->toArray();
  182. }
  183. //如果登录信息中含有client——id则自动进行绑定
  184. $userInfo = User::where('account', $admin->username)->where('role', '>', 0)->find();
  185. if(!$userInfo){
  186. throw new \Exception('用户不存在');
  187. }
  188. if(!empty($params['client_id'])){
  189. $cid = $this->request->header('cid','');
  190. doBindUid($userInfo['user_id'],$params['client_id'],$cid,$this->request->isMobile());
  191. }
  192. $update=[
  193. 'last_login_time' => time(),
  194. 'last_login_ip' => $this->request->ip(),
  195. 'login_count' => Db::raw('login_count+1')
  196. ];
  197. User::where('user_id',$userInfo['user_id'])->update($update);
  198. $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']);
  199. unset($userInfo['password'],$userInfo['salt']);
  200. $userInfo['displayName']=$userInfo['realname'];
  201. $userInfo['id']=$userInfo['user_id'];
  202. $authToken=User::refreshToken($userInfo,$param['terminal'] ?? 'web', ['id' => $admin->id, 'role_id' => $admin->role_id]);
  203. $data=[
  204. 'sessionId'=>Session::getId(),
  205. 'token' => $authToken,
  206. 'userInfo'=>$userInfo,
  207. 'adminInfo' => $admin,
  208. ];
  209. //通知客服签到
  210. wsSendMsg($userInfo['id'],'sign',['id'=>$userInfo['id']]);
  211. } catch (\Exception $e) {
  212. return $this->error($e->getMessage());
  213. }
  214. return $this->success($data);
  215. }
  216. }