Admin.php 8.1 KB

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