0, 'phone' => $params['phone'], 'nickname' => '用户'.$params['phone'], 'password' => $userPassword, 'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'], 'age' => empty($params['age']) ? 0 : $params['age'], 'gender' => empty($params['gender']) ? 0 : $params['gender'], 'realname' => empty($params['realname']) ? '' : $params['realname'], 'id_card' => empty($params['id_card']) ? '' : $params['id_card'], 'province' => empty($params['province']) ? 0 : $params['province'], 'city' => empty($params['city']) ? 0 : $params['city'], 'area' => empty($params['area']) ? 0 : $params['area'], 'street' => empty($params['street']) ? 0 : $params['street'], 'village' => empty($params['village']) ? 0 : $params['village'], 'brigade' => empty($params['brigade']) ? 0 : $params['brigade'], 'address' => empty($params['address']) ? '' : $params['address'], ]); if(!empty($user['id'])){ Db::commit(); return true; }else{ Db::rollback(); return false; } } catch (\Exception $e) { Db::rollback(); Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } } // 账号/手机号登录,手机号验证码 public static function login($params): bool|array { try { $where = ['phone' => $params['account']]; $user = User::field(['id', 'phone'])->where($where)->findOrEmpty(); //更新登录信息 $user->last_login_time = time(); $user->last_login_ip = request()->ip(); $user->save(); //设置token $userInfo = UserTokenService::setToken($user->id, 0); return [ 'uid' => $userInfo['user_id'], 'phone' => $userInfo['phone'], 'token' => $userInfo['token'], ]; } catch (\Exception $e) { //记录日志 Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } } // 账号/手机号登录,手机号验证码 public static function loginJwt($params): bool|array { try { $where = ['phone' => $params['account']]; $user = User::field(['id', 'phone', 'avatar', 'nickname', 'need_reset'])->where($where)->findOrEmpty(); if ($user->need_reset == 1) { self::setError('系统已升级,请重置密码'); return false; } //更新登录信息 $user->last_login_time = time(); $user->last_login_ip = request()->ip(); $user->save(); //设置token $userInfo = JwtTokenService::createToken($user->id, $user->toArray()); return [ 'uid' => $userInfo['uid'], 'token' => $userInfo['token'] ]; } catch (\Exception $e) { //记录日志 Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } } // 退出登录 public static function logout($userInfo): bool { //token不存在,不注销 if (!isset($userInfo['token'])) { return false; } //设置token过期 return UserTokenService::expireToken($userInfo['token']); } // 验证token public static function verifyToken($params): bool|array { try { $userInfo = (new UserTokenCache())->getUserInfo($params['token']); if(empty($userInfo) || $userInfo['user_id'] != $params['uid']){ self::setError('token无效'); return false; } return [ 'uid' => $userInfo['user_id'], 'phone' => $userInfo['phone'], 'token' => $userInfo['token'], ]; } catch (\Exception $e) { //记录日志 Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } } // 验证token public static function verifyJwtToken($params): bool|array { try { $userInfo = JwtTokenService::parseToken($params['token']); return $userInfo; } catch (\Exception $e) { //记录日志 Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } } }