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