UserService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 sendVerifyCode(string $mobile)
  18. {
  19. // 生成验证码
  20. $code = mt_rand(100000, 999999);
  21. // 保存验证码到缓存
  22. Cache::put("verify_code:{$mobile}", $code, 300);
  23. // TODO: 调用短信服务发送验证码
  24. return ['code' => $code];
  25. }
  26. /**
  27. * 用户登录
  28. */
  29. public function login(string $mobile, string $code)
  30. {
  31. // 验证验证码
  32. $cacheCode = Cache::get("verify_code:{$mobile}");
  33. if (!$cacheCode || $cacheCode != $code) {
  34. throw new \Exception('验证码错误');
  35. }
  36. // 查找或创建用户
  37. $user = MemberUser::firstOrCreate(
  38. ['mobile' => $mobile],
  39. [
  40. 'state' => 'enable',
  41. 'register_area' => request()->header('area_code')
  42. ]
  43. );
  44. // 生成token
  45. $token = $user->createToken('auth-token')->plainTextToken;
  46. return [
  47. 'token' => $token,
  48. 'user' => $user
  49. ];
  50. }
  51. /**
  52. * 微信登录
  53. */
  54. public function wxLogin(string $openid)
  55. {
  56. // 查找或创建微信用户
  57. $socialAccount = MemberSocialAccount::firstOrCreate(
  58. [
  59. 'platform' => 'WECHAT',
  60. 'social_id' => $openid
  61. ]
  62. );
  63. $user = $socialAccount->user;
  64. if (!$user) {
  65. $user = MemberUser::create([
  66. 'state' => 'enable',
  67. 'register_area' => request()->header('area_code')
  68. ]);
  69. $socialAccount->update(['user_id' => $user->id]);
  70. }
  71. // 生成token
  72. $token = $user->createToken('auth-token')->plainTextToken;
  73. return [
  74. 'token' => $token,
  75. 'user' => $user
  76. ];
  77. }
  78. /**
  79. * 用户退出
  80. */
  81. public function logout()
  82. {
  83. Auth::user()->tokens()->delete();
  84. return ['message' => '退出成功'];
  85. }
  86. /**
  87. * 获取用户信息
  88. */
  89. public function getUserInfo()
  90. {
  91. $user = Auth::user();
  92. $wallet = Wallet::where('owner_id', $user->id)
  93. ->where('owner_type', 'USER')
  94. ->first();
  95. return [
  96. 'user' => $user,
  97. 'wallet' => $wallet ? [
  98. 'id' => $wallet->id,
  99. 'balance' => $wallet->available_balance
  100. ] : null
  101. ];
  102. }
  103. /**
  104. * 修改用户信息
  105. */
  106. public function updateUserInfo(array $data)
  107. {
  108. $user = Auth::user();
  109. $user->update($data);
  110. return ['message' => '修改成功'];
  111. }
  112. /**
  113. * 获取用户钱包
  114. */
  115. public function getUserWallet()
  116. {
  117. $wallet = Wallet::where('owner_id', Auth::id())
  118. ->where('owner_type', MemberUser::class)
  119. ->first();
  120. if (!$wallet) {
  121. throw new \Exception('钱包不存在');
  122. }
  123. return $wallet;
  124. }
  125. /**
  126. * 用户提现
  127. */
  128. public function withdraw($amount)
  129. {
  130. // 获取当前用户
  131. $user = Auth::user();
  132. if (!$user || $user->state !== 'enable') {
  133. throw new \Exception('用户状态异常');
  134. }
  135. $config = SysConfig::where('key', 'withdraw_min_amount')->first();
  136. $configValue = json_decode($config->value, true);
  137. $minAmount = $configValue['minAmount'];
  138. $maxAmount = $configValue['maxAmount'];
  139. $fee = $configValue['fee'];
  140. if ($amount < $minAmount) {
  141. throw new \Exception("提现金额不能小于{$minAmount}元");
  142. }
  143. if ($amount > $maxAmount) {
  144. throw new \Exception("提现金额不能大于{$maxAmount}元");
  145. }
  146. // 创建提现记录
  147. DB::transaction(function() use ($user, $amount, $fee) {
  148. WalletWithdrawRecord::create([
  149. 'user_id' => $user->id,
  150. 'amount' => $amount,
  151. 'fee' => $fee,
  152. 'type' => MemberUser::class,
  153. 'state' => 'pending'
  154. ]);
  155. });
  156. return ['message' => '提现申请已提交'];
  157. }
  158. /**
  159. * 用户注销
  160. */
  161. public function deleteAccount()
  162. {
  163. $user = Auth::user();
  164. if (!$user || $user->state !== 'enable') {
  165. throw new \Exception('用户状态异常');
  166. }
  167. $user->state = 'disable';
  168. $user->save();
  169. $user->delete();
  170. return ['message' => '账号已注销'];
  171. }
  172. /**
  173. * 用户反馈
  174. */
  175. public function feedback($content)
  176. {
  177. $user = Auth::user();
  178. if (!$user || $user->state !== 'enable') {
  179. throw new \Exception('用户状态异常');
  180. }
  181. // UserFeedback::create([
  182. // 'owner_id' => $user->id,
  183. // 'owner_type' => MemberUser::class,
  184. // 'content' => $content
  185. // ]);
  186. return ['message' => '反馈已提交'];
  187. }
  188. /**
  189. * 申请成为技师
  190. */
  191. public function applyCoach()
  192. {
  193. // 获取当前用户
  194. $userId = Auth::id();
  195. $user = MemberUser::findOrFail($userId);
  196. // 检查用户状态
  197. if ($user->state !== 'enable') {
  198. throw new \Exception('用户状态异常');
  199. }
  200. // 检查是否已经是技师
  201. $exists = CoachUser::where('user_id', $userId)->exists();
  202. if ($exists) {
  203. throw new \Exception('您已经是技师了');
  204. }
  205. // 创建技师申请
  206. DB::transaction(function() use ($userId) {
  207. CoachUser::create([
  208. 'user_id' => $userId,
  209. 'state' => 'pending' // 待审核状态
  210. ]);
  211. });
  212. return ['message' => '申请已提交'];
  213. }
  214. }