User.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\api\service;
  13. use app\api\model\User as UserModel;
  14. use app\api\model\UserOauth as UserOauthModel;
  15. use app\common\service\BaseService;
  16. use app\common\exception\BaseException;
  17. /**
  18. * 用户服务类
  19. * Class User
  20. * @package app\api\service
  21. */
  22. class User extends BaseService
  23. {
  24. // 当前登录的会员信息
  25. private static $currentLoginUser;
  26. /**
  27. * 获取当前登录的用户信息 (快捷)
  28. * 可在api应用中的任意模块中调用此方法(controller model service)
  29. * 已登录情况下返回用户信息, 未登录返回false
  30. * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
  31. * @return false|UserModel
  32. * @throws BaseException
  33. */
  34. public static function getCurrentLoginUser(bool $isForce = false)
  35. {
  36. $service = new static;
  37. if (empty(static::$currentLoginUser)) {
  38. static::$currentLoginUser = $service->getLoginUser();
  39. if (empty(static::$currentLoginUser)) {
  40. $isForce && throwError($service->getError(), config('status.not_logged'));
  41. return false;
  42. }
  43. }
  44. return static::$currentLoginUser;
  45. }
  46. /**
  47. * 获取当前登录的用户ID
  48. * getCurrentLoginUser方法的二次封装
  49. * @return mixed
  50. * @throws BaseException
  51. */
  52. public static function getCurrentLoginUserId()
  53. {
  54. $userInfo = static::getCurrentLoginUser(true);
  55. return $userInfo['user_id'];
  56. }
  57. /**
  58. * 获取第三方用户信息
  59. * @param int $userId 用户ID
  60. * @param string $oauthType 第三方登陆类型
  61. * @return array|\think\Model|null
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function getOauth(int $userId, $oauthType = 'MP-WEIXIN')
  67. {
  68. return UserOauthModel::getOauth($userId, $oauthType);
  69. }
  70. /**
  71. * 验证是否已登录
  72. * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
  73. * @return bool
  74. * @throws BaseException
  75. */
  76. public static function isLogin(bool $isForce = false)
  77. {
  78. return !empty(static::getCurrentLoginUser($isForce));
  79. }
  80. /**
  81. * 获取当前登录的用户信息
  82. * @return UserModel|bool|null
  83. */
  84. private function getLoginUser()
  85. {
  86. // 获取用户认证Token
  87. $token = $this->getToken();
  88. if ($token === false) return false;
  89. // 获取用户信息
  90. if (!$user = UserModel::getUserByToken($token)) {
  91. $this->error = '没有找到用户信息';
  92. return false;
  93. }
  94. return $user;
  95. }
  96. /**
  97. * 获取用户认证Token
  98. * @return bool|string
  99. */
  100. protected function getToken()
  101. {
  102. // 获取请求中的token
  103. $token = $this->request->header('Access-Token');
  104. // 调试模式下可通过param
  105. if (empty($token) && is_debug()) {
  106. $token = $this->request->param('Access-Token');
  107. }
  108. // 不存在token报错
  109. if (empty($token)) {
  110. $this->error = '请前往个人中心登录';
  111. return false;
  112. }
  113. return $token;
  114. }
  115. }