User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\common\service\store;
  13. use think\facade\Cache;
  14. use app\common\service\BaseService;
  15. /**
  16. * 商家用户服务类
  17. * Class User
  18. */
  19. class User extends BaseService
  20. {
  21. // 用于生成token的自定义盐
  22. const TOKEN_SALT = '_store_user_salt_';
  23. /**
  24. * 获取当前登录用户的信息
  25. * @return array
  26. */
  27. public static function getLoginInfo(): array
  28. {
  29. if (($token = self::getToken()) !== false) {
  30. return Cache::get($token) ?: [];
  31. }
  32. return [];
  33. }
  34. /**
  35. * 获取当前登录用户的ID
  36. * @return int|null
  37. */
  38. public static function getLoginUserId(): ?int
  39. {
  40. $userInfo = static::getLoginInfo();
  41. if (empty($userInfo)) {
  42. return null;
  43. }
  44. return (int)$userInfo['user']['store_user_id'];
  45. }
  46. /**
  47. * 获取当前登录的商城ID
  48. * @return int|null
  49. */
  50. public static function getLoginStoreId(): ?int
  51. {
  52. $userInfo = static::getLoginInfo();
  53. if (empty($userInfo)) {
  54. return null;
  55. }
  56. return (int)$userInfo['store_id'];
  57. }
  58. /**
  59. * 记录登录信息
  60. * @param array $userInfo
  61. * @return string
  62. */
  63. public static function login(array $userInfo): string
  64. {
  65. // 生成token
  66. $token = self::makeToken((int)$userInfo['store_user_id']);
  67. // 记录缓存, 7天
  68. Cache::set($token, [
  69. 'user' => $userInfo,
  70. 'store_id' => $userInfo['store_id'],
  71. 'is_login' => true,
  72. ], 86400 * 7);
  73. return $token;
  74. }
  75. /**
  76. * 清空登录状态
  77. * @return bool
  78. */
  79. public static function logout(): bool
  80. {
  81. $token = self::getToken();
  82. !empty($token) && Cache::delete($token);
  83. return true;
  84. }
  85. /**
  86. * 更新登录信息
  87. * @param array $userInfo
  88. * @return bool
  89. */
  90. public static function update(array $userInfo): bool
  91. {
  92. return Cache::set(self::getToken(), [
  93. 'user' => $userInfo,
  94. 'store_id' => $userInfo['store_id'],
  95. 'is_login' => true,
  96. ], 86400 * 7);
  97. }
  98. /**
  99. * 生成用户认证的token
  100. * @param int $userId
  101. * @return string
  102. */
  103. protected static function makeToken(int $userId): string
  104. {
  105. // 生成一个不会重复的随机字符串
  106. $guid = get_guid_v4();
  107. // 当前时间戳 (精确到毫秒)
  108. $timeStamp = microtime(true);
  109. // 自定义一个盐
  110. $salt = self::TOKEN_SALT;
  111. return md5("{$timeStamp}_{$userId}_{$guid}_{$salt}");
  112. }
  113. /**
  114. * 获取用户认证Token
  115. * @return bool|string
  116. */
  117. private static function getToken()
  118. {
  119. // 获取请求中的token
  120. $token = request()->header('Access-Token');
  121. // 调试模式下可通过param
  122. if (empty($token) && is_debug()) {
  123. $token = request()->param('Access-Token');
  124. }
  125. // 不存在token报错
  126. if (empty($token)) {
  127. return false;
  128. }
  129. return $token;
  130. }
  131. }