UserService.php 5.2 KB

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