UserService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\MemberUser;
  4. use App\Models\MemberSocialAccount;
  5. use App\Models\SysConfig;
  6. use App\Models\Wallet;
  7. use App\Models\CoachUser;
  8. use App\Models\WalletWithdrawRecord;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\DB;
  12. class UserService
  13. {
  14. /**
  15. * 获取用户信息
  16. */
  17. public function getUserInfo()
  18. {
  19. $user = Auth::user();
  20. $wallet = Wallet::where('owner_id', $user->id)
  21. ->where('owner_type', 'USER')
  22. ->first();
  23. return [
  24. 'user' => $user,
  25. 'wallet' => $wallet ? [
  26. 'id' => $wallet->id,
  27. 'balance' => $wallet->available_balance
  28. ] : null
  29. ];
  30. }
  31. /**
  32. * 修改用户信息
  33. */
  34. public function updateUserInfo(array $data)
  35. {
  36. $user = Auth::user();
  37. $user->update($data);
  38. return ['message' => '修改成功'];
  39. }
  40. /**
  41. * 获取用户钱包
  42. */
  43. public function getUserWallet()
  44. {
  45. $wallet = Wallet::where('owner_id', Auth::id())
  46. ->where('owner_type', MemberUser::class)
  47. ->first();
  48. if (!$wallet) {
  49. throw new \Exception('钱包不存在');
  50. }
  51. return $wallet;
  52. }
  53. /**
  54. * 用户提现
  55. */
  56. public function withdraw($amount)
  57. {
  58. // 获取当前用户
  59. $user = Auth::user();
  60. if (!$user || $user->state !== 'enable') {
  61. throw new \Exception('用户状态异常');
  62. }
  63. $config = SysConfig::where('key', 'withdraw_min_amount')->first();
  64. $configValue = json_decode($config->value, true);
  65. $minAmount = $configValue['minAmount'];
  66. $maxAmount = $configValue['maxAmount'];
  67. $fee = $configValue['fee'];
  68. if ($amount < $minAmount) {
  69. throw new \Exception("提现金额不能小于{$minAmount}元");
  70. }
  71. if ($amount > $maxAmount) {
  72. throw new \Exception("提现金额不能大于{$maxAmount}元");
  73. }
  74. // 创建提现记录
  75. DB::transaction(function() use ($user, $amount, $fee) {
  76. WalletWithdrawRecord::create([
  77. 'user_id' => $user->id,
  78. 'amount' => $amount,
  79. 'fee' => $fee,
  80. 'type' => MemberUser::class,
  81. 'state' => 'pending'
  82. ]);
  83. });
  84. return ['message' => '提现申请已提交'];
  85. }
  86. /**
  87. * 用户反馈
  88. */
  89. public function feedback($content)
  90. {
  91. $user = Auth::user();
  92. if (!$user || $user->state !== 'enable') {
  93. throw new \Exception('用户状态异常');
  94. }
  95. // UserFeedback::create([
  96. // 'owner_id' => $user->id,
  97. // 'owner_type' => MemberUser::class,
  98. // 'content' => $content
  99. // ]);
  100. return ['message' => '反馈已提交'];
  101. }
  102. /**
  103. * 申请成为技师
  104. */
  105. public function applyCoach()
  106. {
  107. // 获取当前用户
  108. $userId = Auth::id();
  109. $user = MemberUser::findOrFail($userId);
  110. // 检查用户状态
  111. if ($user->state !== 'enable') {
  112. throw new \Exception('用户状态异常');
  113. }
  114. // 检查是否已经是技师
  115. $exists = CoachUser::where('user_id', $userId)->exists();
  116. if ($exists) {
  117. throw new \Exception('您已经是技师了');
  118. }
  119. // 创建技师申请
  120. DB::transaction(function() use ($userId) {
  121. CoachUser::create([
  122. 'user_id' => $userId,
  123. 'state' => 'pending' // 待审核状态
  124. ]);
  125. });
  126. return ['message' => '申请已提交'];
  127. }
  128. }