// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\index\service; use app\index\model\User as UserModel; use app\index\model\UserOauth as UserOauthModel; use app\common\service\BaseService; use cores\exception\BaseException; use think\facade\Session; use think\Response; /** * 用户服务类 * Class User * @package app\api\service */ class User extends BaseService { // 当前登录的会员信息 public const USER_TOKEN = 'access_token'; public const USER_ID = 'user_id'; /** * 获取当前登录的用户ID * getCurrentLoginUser方法的二次封装 * @return mixed * @throws BaseException */ public static function getCurrentLoginUserId() { return Session::get(self::USER_ID); } /** * 获取第三方用户信息 * @param int $userId 用户ID * @param string $oauthType 第三方登陆类型 * @return array|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getOauth(int $userId, $oauthType = 'MP-WEIXIN') { return UserOauthModel::getOauth($userId, $oauthType); } /** * 获取登录用户 * @return UserModel|array|false * @throws BaseException */ public static function getCurrentLoginUser() { // 获取用户认证Token if (!$userId = self::getCurrentLoginUserId()) { return false; } // 获取用户信息 if (!$user = UserModel::getUserById($userId)) { return false; } return $user; } /** * 验证是否已登录 * @param bool $isForce 是否强制验证登录, 如果未登录将抛错 * @return bool * @throws BaseException */ public static function isLogin() { return !empty(Session::get(self::USER_ID)); } }