UserRepository.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\system\SystemAttachment;
  4. use app\models\system\SystemAdmin;
  5. use app\models\user\User;
  6. use app\models\user\UserToken;
  7. use crmeb\exceptions\AuthException;
  8. use crmeb\services\CacheService;
  9. use Firebase\JWT\ExpiredException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\db\exception\DataNotFoundException;
  12. use think\exception\DbException;
  13. use think\facade\Cache;
  14. /**
  15. * Class UserRepository
  16. * @package crmeb\repositories
  17. */
  18. class UserRepository
  19. {
  20. /**
  21. * 管理员后台给用户添加金额
  22. * @param $user
  23. * $user 用户信息
  24. * @param $money
  25. * $money 添加的金额
  26. */
  27. public static function adminAddMoney($user, $money)
  28. {
  29. }
  30. /**
  31. * 管理员后台给用户减少金额
  32. * @param $user
  33. * $user 用户信息
  34. * @param $money
  35. * $money 减少的金额
  36. */
  37. public static function adminSubMoney($user, $money)
  38. {
  39. }
  40. /**
  41. * 管理员后台给用户增加的积分
  42. * @param $user
  43. * $user 用户信息
  44. * @param $integral
  45. * $integral 增加的积分
  46. */
  47. public static function adminAddIntegral($user, $integral)
  48. {
  49. }
  50. /**
  51. * 管理员后台给用户减少的积分
  52. * @param $user
  53. * $user 用户信息
  54. * @param $integral
  55. * $integral 减少的积分
  56. */
  57. public static function adminSubIntegral($user, $integral)
  58. {
  59. }
  60. /**
  61. * 获取授权信息
  62. * @param string $token
  63. * @return array
  64. * @throws AuthException
  65. * @throws DataNotFoundException
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. */
  69. public static function parseToken($token): array
  70. {
  71. if (!$token || !$tokenData = CacheService::getTokenBucket($token))
  72. throw new AuthException('请登录', 410000);
  73. if (!is_array($tokenData) || empty($tokenData) || !isset($tokenData['uid'])) {
  74. throw new AuthException('请登录', 410000);
  75. }
  76. try {
  77. [$user, $type] = User::parseToken($token);
  78. } catch (\Throwable $e) {
  79. CacheService::clearToken($token);
  80. throw new AuthException('登录已过期,请重新登录', 410001);
  81. }
  82. if (!$user || $user->uid != $tokenData['uid']) {
  83. CacheService::clearToken($token);
  84. throw new AuthException('登录状态有误,请重新登录', 410002);
  85. }
  86. $tokenData['type'] = $type;
  87. return compact('user', 'tokenData');
  88. }
  89. }