User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <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. 'school_id' => $userInfo['school_id'],
  56. 'parent_id' => $userInfo['parent_id'],
  57. 'user_type' => $userInfo['user_type'],
  58. 'is_login' => true,
  59. ], 86400 * 7);
  60. return $token;
  61. }
  62. /**
  63. * 清空登录状态
  64. * @return bool
  65. */
  66. public static function logout()
  67. {
  68. Cache::delete(self::getToken());
  69. return true;
  70. }
  71. /**
  72. * 更新登录信息
  73. * @param array $userInfo
  74. * @return mixed
  75. */
  76. public static function update(array $userInfo)
  77. {
  78. return Cache::set(self::getToken(), [
  79. 'user' => $userInfo,
  80. 'store_id' => $userInfo['store_id'],
  81. 'school_id' => $userInfo['school_id'],
  82. 'parent_id' => $userInfo['parent_id'],
  83. 'user_type' => $userInfo['user_type'],
  84. 'is_login' => true,
  85. ], 86400 * 7);
  86. }
  87. /**
  88. * 生成用户认证的token
  89. * @param int $userId
  90. * @return string
  91. */
  92. protected static function makeToken(int $userId)
  93. {
  94. // 生成一个不会重复的随机字符串
  95. $guid = get_guid_v4();
  96. // 当前时间戳 (精确到毫秒)
  97. $timeStamp = microtime(true);
  98. // 自定义一个盐
  99. $salt = self::TOKEN_SALT;
  100. return md5("{$timeStamp}_{$userId}_{$guid}_{$salt}");
  101. }
  102. /**
  103. * 获取用户认证Token
  104. * @return bool|string
  105. */
  106. private static function getToken()
  107. {
  108. // 获取请求中的token
  109. $token = request()->header('Access-Token');
  110. // 调试模式下可通过param
  111. if (empty($token) && is_debug()) {
  112. $token = request()->param('Access-Token');
  113. }
  114. // 不存在token报错
  115. if (empty($token)) {
  116. return false;
  117. }
  118. return $token;
  119. }
  120. }