User.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $userId = Session::get(self::USER_ID);
  38. if (empty($userId)){
  39. return null;
  40. }
  41. return intval($userId);
  42. }
  43. /**
  44. * 获取第三方用户信息
  45. * @param int $userId 用户ID
  46. * @param string $oauthType 第三方登陆类型
  47. * @return array|\think\Model|null
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public static function getOauth(int $userId, $oauthType = 'MP-WEIXIN')
  53. {
  54. return UserOauthModel::getOauth($userId, $oauthType);
  55. }
  56. /**
  57. * 获取登录用户
  58. * @return UserModel|array|false
  59. * @throws BaseException
  60. */
  61. public static function getCurrentLoginUser()
  62. {
  63. // 获取用户认证Token
  64. if (!$userId = self::getCurrentLoginUserId()) {
  65. return false;
  66. }
  67. // 获取用户信息
  68. if (!$user = UserModel::getUserById($userId)) {
  69. return false;
  70. }
  71. return $user;
  72. }
  73. /**
  74. * 验证是否已登录
  75. * @return bool
  76. */
  77. public static function isLogin(): bool
  78. {
  79. return !empty(Session::get(self::USER_ID));
  80. }
  81. }