12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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));
- }
- }
|