UserService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachUser;
  4. use App\Models\MemberUser;
  5. use App\Models\SysConfig;
  6. use App\Models\Wallet;
  7. use App\Models\WalletWithdrawRecord;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. class UserService
  11. {
  12. /**
  13. * 获取用户信息
  14. */
  15. public function getUserInfo()
  16. {
  17. $user = Auth::user();
  18. $wallet = Wallet::where('owner_id', $user->id)
  19. ->where('owner_type', 'USER')
  20. ->first();
  21. return [
  22. 'user' => $user,
  23. 'wallet' => $wallet ? [
  24. 'id' => $wallet->id,
  25. 'balance' => $wallet->available_balance,
  26. ] : null,
  27. ];
  28. }
  29. /**
  30. * 修改用户信息
  31. */
  32. public function updateUserInfo(array $data)
  33. {
  34. $user = Auth::user();
  35. $user->update($data);
  36. return ['message' => '修改成功'];
  37. }
  38. /**
  39. * 获取用户钱包
  40. */
  41. public function getUserWallet()
  42. {
  43. $user = Auth::user();
  44. $wallet = $user->wallet;
  45. if (! $wallet) {
  46. throw new \Exception('钱包不存在');
  47. }
  48. return $wallet;
  49. }
  50. /**
  51. * 用户提现
  52. */
  53. public function withdraw($amount, $type = 'wechat', $area_code = '')
  54. {
  55. // 获取当前用户
  56. $user = Auth::user();
  57. if (! $user || $user->state !== 'enable') {
  58. throw new \Exception('用户状态异常');
  59. }
  60. // 获取钱包并检查余额
  61. $wallet = $user->wallet;
  62. if ($wallet->state !== 'enable') {
  63. throw new \Exception('钱包已冻结');
  64. }
  65. if ($wallet->available_balance < $amount) {
  66. throw new \Exception('钱包余额不足');
  67. }
  68. $config = SysConfig::where('key', 'withdraw_min_amount')->first();
  69. $configValue = json_decode($config?->value, true);
  70. $minAmount = $configValue['minAmount'] ?? 1;
  71. $maxAmount = $configValue['maxAmount'] ?? 10000;
  72. $fee = $configValue['fee'] ?? 0.00;
  73. if ($amount < $minAmount) {
  74. throw new \Exception("提现金额不能小于{$minAmount}元");
  75. }
  76. if ($amount > $maxAmount) {
  77. throw new \Exception("提现金额不能大于{$maxAmount}元");
  78. }
  79. // 创建提现记录
  80. DB::transaction(function () use ($amount, $fee, $wallet, $type, $area_code) {
  81. WalletWithdrawRecord::create([
  82. 'wallet_id' => $wallet->id,
  83. 'amount' => $amount, // 提现金额
  84. 'withdraw_type' => $type, // 提现方式
  85. 'fee' => $fee, // 提现手续费
  86. 'area_code' => $area_code, // 行政区划代码
  87. 'state' => 'processing', // 状态
  88. ]);
  89. // 扣除钱包余额
  90. $wallet->available_balance -= $amount;
  91. $wallet->total_balance -= $amount;
  92. $wallet->save();
  93. });
  94. return ['message' => '提现申请已提交'];
  95. }
  96. /**
  97. * 用户反馈
  98. */
  99. public function feedback($content)
  100. {
  101. $user = Auth::user();
  102. if (! $user || $user->state !== 'enable') {
  103. throw new \Exception('用户状态异常');
  104. }
  105. // UserFeedback::create([
  106. // 'owner_id' => $user->id,
  107. // 'owner_type' => MemberUser::class,
  108. // 'content' => $content
  109. // ]);
  110. return ['message' => '反馈已提交'];
  111. }
  112. /**
  113. * 申请成为技师
  114. */
  115. public function applyCoach($mobile = '', $gender = '', $work_years = '', $intention_city = '')
  116. {
  117. // 获取当前用户
  118. $user = Auth::user();
  119. // 检查用户状态
  120. if ($user->state !== 'enable') {
  121. throw new \Exception('用户状态异常');
  122. }
  123. // 检查是否已经申请过
  124. $coach = $user->coach;
  125. if ($coach) {
  126. if ($coach->state === 'pending') {
  127. throw new \Exception('您已提交申请,请等待审核');
  128. }
  129. if ($coach->state === 'enable') {
  130. throw new \Exception('您已经是技师了');
  131. }
  132. if ($coach->state === 'disable') {
  133. throw new \Exception('您的技师资格已被禁用');
  134. }
  135. }
  136. // 创建技师申请
  137. DB::transaction(function () use ($user, $mobile, $gender, $work_years, $intention_city) {
  138. // 创建技师基本信息
  139. $coach = CoachUser::create([
  140. 'user_id' => $user->id,
  141. 'state' => 'pending', // 待审核状态
  142. ]);
  143. $coach->infoRecords()->create([
  144. 'nickname' => $user->nickname ?? '', // 姓名
  145. 'mobile' => $mobile, // 服务电话
  146. 'avatar' => $user->avatar ?? '', // 头像
  147. 'gender' => $gender ?? 'unknown', // 性别
  148. 'work_years' => $work_years ?? '', // 从业年份
  149. 'intention_city' => $intention_city ?? '', // 意向城市
  150. 'state' => 'pending',
  151. ]);
  152. });
  153. return [
  154. 'message' => '申请已提交,请等待审核',
  155. ];
  156. }
  157. }