User.php 4.2 KB

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