User.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\index\service;
  13. use app\index\model\User as UserModel;
  14. use app\index\model\UserOauth as UserOauthModel;
  15. use app\common\service\BaseService;
  16. use cores\exception\BaseException;
  17. use think\facade\Session;
  18. use think\Response;
  19. /**
  20. * 用户服务类
  21. * Class User
  22. * @package app\api\service
  23. */
  24. class User extends BaseService
  25. {
  26. // 当前登录的会员信息
  27. public const USER_TOKEN = 'access_token';
  28. public const USER_ID = 'user_id';
  29. /**
  30. * 获取当前登录的用户ID
  31. * getCurrentLoginUser方法的二次封装
  32. * @return mixed
  33. * @throws BaseException
  34. */
  35. public static function getCurrentLoginUserId()
  36. {
  37. return Session::get(self::USER_ID);
  38. }
  39. /**
  40. * 获取第三方用户信息
  41. * @param int $userId 用户ID
  42. * @param string $oauthType 第三方登陆类型
  43. * @return array|\think\Model|null
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function getOauth(int $userId, $oauthType = 'MP-WEIXIN')
  49. {
  50. return UserOauthModel::getOauth($userId, $oauthType);
  51. }
  52. /**
  53. * 获取登录用户
  54. * @return UserModel|array|false
  55. * @throws BaseException
  56. */
  57. public static function getCurrentLoginUser()
  58. {
  59. // 获取用户认证Token
  60. if (!$userId = self::getCurrentLoginUserId()) {
  61. return false;
  62. }
  63. // 获取用户信息
  64. if (!$user = UserModel::getUserById($userId)) {
  65. return false;
  66. }
  67. return $user;
  68. }
  69. /**
  70. * 验证是否已登录
  71. * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
  72. * @return bool
  73. * @throws BaseException
  74. */
  75. public static function isLogin()
  76. {
  77. return !empty(Session::get(self::USER_ID));
  78. }
  79. }