UrsService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace ThirdParty\Urs\Services;
  3. use ThirdParty\Urs\Request\UrsGetUserInfoRequest;
  4. use ThirdParty\Urs\Request\UrsGetUserTeamRequest;
  5. use ThirdParty\Urs\Request\UrsGetUserLevelCountRequest;
  6. use ThirdParty\Urs\Request\UrsRegisterRequest;
  7. use ThirdParty\Urs\Request\UrsDepositRequest;
  8. use ThirdParty\Urs\Request\UrsWithdrawRequest;
  9. use ThirdParty\Urs\Request\UrsCheckBalanceRequest;
  10. /**
  11. * URS服务类
  12. *
  13. * 统一管理所有URS相关的请求操作,提供简洁的API接口
  14. * 每个方法对应一个专用的Request类,遵循单一职责原则
  15. */
  16. class UrsService
  17. {
  18. /**
  19. * 获取用户信息
  20. *
  21. * @param string $userKey 用户密钥
  22. * @return array
  23. * @throws \Exception
  24. */
  25. public static function getUserInfo(string $userKey): array
  26. {
  27. $request = new UrsGetUserInfoRequest();
  28. return $request->request(['userKey' => $userKey]);
  29. }
  30. /**
  31. * 获取用户团队关系
  32. *
  33. * @param int $userId 用户ID
  34. * @return array
  35. * @throws \Exception
  36. */
  37. public static function getUserTeam(int $userId): array
  38. {
  39. $request = new UrsGetUserTeamRequest();
  40. return $request->request(['userId' => $userId]);
  41. }
  42. /**
  43. * 获取用户下级统计
  44. *
  45. * @param int $userId 用户ID
  46. * @param int $level 统计级别(1或3)
  47. * @return array
  48. * @throws \Exception
  49. */
  50. public static function getUserLevelCount(int $userId, int $level): array
  51. {
  52. if (!in_array($level, [1, 3])) {
  53. throw new \InvalidArgumentException('level参数必须是1或3');
  54. }
  55. $request = new UrsGetUserLevelCountRequest();
  56. return $request->request([
  57. 'userId' => $userId,
  58. 'level' => $level
  59. ]);
  60. }
  61. /**
  62. * 用户注册
  63. *
  64. * @param int $userId 用户ID
  65. * @param string $username 用户名
  66. * @return array
  67. * @throws \Exception
  68. */
  69. public static function registerUser(int $userId, string $username): array
  70. {
  71. $request = new UrsRegisterRequest();
  72. return $request->request([
  73. 'user_id' => $userId,
  74. 'username' => $username
  75. ]);
  76. }
  77. /**
  78. * 充值操作
  79. *
  80. * @param int $userId 用户ID
  81. * @param float $amount 充值金额
  82. * @param string $orderId 订单ID
  83. * @return array
  84. * @throws \Exception
  85. */
  86. public static function deposit(int $userId, float $amount, string $orderId): array
  87. {
  88. $request = new UrsDepositRequest();
  89. return $request->request([
  90. 'user_id' => $userId,
  91. 'amount' => $amount,
  92. 'order_id' => $orderId
  93. ]);
  94. }
  95. /**
  96. * 提取操作
  97. *
  98. * @param int $userId 用户ID
  99. * @param float $amount 提取金额
  100. * @param string $orderId 订单ID
  101. * @return array
  102. * @throws \Exception
  103. */
  104. public static function withdraw(int $userId, float $amount, string $orderId): array
  105. {
  106. $request = new UrsWithdrawRequest();
  107. return $request->request([
  108. 'user_id' => $userId,
  109. 'amount' => $amount,
  110. 'order_id' => $orderId
  111. ]);
  112. }
  113. /**
  114. * 检查余额
  115. *
  116. * @param int $userId 用户ID
  117. * @return array
  118. * @throws \Exception
  119. */
  120. public static function checkBalance(int $userId): array
  121. {
  122. $request = new UrsCheckBalanceRequest();
  123. return $request->request(['user_id' => $userId]);
  124. }
  125. }