123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace App\Services\Client;
- use App\Models\MemberUser;
- use App\Models\MemberSocialAccount;
- use App\Models\SysConfig;
- use App\Models\Wallet;
- use App\Models\CoachUser;
- use App\Models\WalletWithdrawRecord;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- class UserService
- {
- /**
- * 发送验证码
- */
- public function sendVerifyCode(string $mobile)
- {
- // 生成验证码
- $code = mt_rand(100000, 999999);
-
- // 保存验证码到缓存
- Cache::put("verify_code:{$mobile}", $code, 300);
-
- // TODO: 调用短信服务发送验证码
-
- return ['code' => $code];
- }
- /**
- * 用户登录
- */
- public function login(string $mobile, string $code)
- {
- // 验证验证码
- $cacheCode = Cache::get("verify_code:{$mobile}");
- if (!$cacheCode || $cacheCode != $code) {
- throw new \Exception('验证码错误');
- }
- // 查找或创建用户
- $user = MemberUser::firstOrCreate(
- ['mobile' => $mobile],
- [
- 'state' => 'enable',
- 'register_area' => request()->header('area_code')
- ]
- );
- // 生成token
- $token = $user->createToken('auth-token')->plainTextToken;
- return [
- 'token' => $token,
- 'user' => $user
- ];
- }
- /**
- * 微信登录
- */
- public function wxLogin(string $openid)
- {
- // 查找或创建微信用户
- $socialAccount = MemberSocialAccount::firstOrCreate(
- [
- 'platform' => 'WECHAT',
- 'social_id' => $openid
- ]
- );
- $user = $socialAccount->user;
- if (!$user) {
- $user = MemberUser::create([
- 'state' => 'enable',
- 'register_area' => request()->header('area_code')
- ]);
- $socialAccount->update(['user_id' => $user->id]);
- }
- // 生成token
- $token = $user->createToken('auth-token')->plainTextToken;
- return [
- 'token' => $token,
- 'user' => $user
- ];
- }
- /**
- * 用户退出
- */
- public function logout()
- {
- Auth::user()->tokens()->delete();
- return ['message' => '退出成功'];
- }
- /**
- * 获取用户信息
- */
- public function getUserInfo()
- {
- $user = Auth::user();
- $wallet = Wallet::where('owner_id', $user->id)
- ->where('owner_type', 'USER')
- ->first();
- return [
- 'user' => $user,
- 'wallet' => $wallet ? [
- 'id' => $wallet->id,
- 'balance' => $wallet->available_balance
- ] : null
- ];
- }
- /**
- * 修改用户信息
- */
- public function updateUserInfo(array $data)
- {
- $user = Auth::user();
- $user->update($data);
- return ['message' => '修改成功'];
- }
- /**
- * 获取用户钱包
- */
- public function getUserWallet()
- {
- $wallet = Wallet::where('owner_id', Auth::id())
- ->where('owner_type', MemberUser::class)
- ->first();
- if (!$wallet) {
- throw new \Exception('钱包不存在');
- }
- return $wallet;
- }
- /**
- * 用户提现
- */
- public function withdraw($amount)
- {
- // 获取当前用户
- $user = Auth::user();
- if (!$user || $user->state !== 'enable') {
- throw new \Exception('用户状态异常');
- }
-
- $config = SysConfig::where('key', 'withdraw_min_amount')->first();
- $configValue = json_decode($config->value, true);
- $minAmount = $configValue['minAmount'];
- $maxAmount = $configValue['maxAmount'];
- $fee = $configValue['fee'];
-
- if ($amount < $minAmount) {
- throw new \Exception("提现金额不能小于{$minAmount}元");
- }
-
- if ($amount > $maxAmount) {
- throw new \Exception("提现金额不能大于{$maxAmount}元");
- }
- // 创建提现记录
- DB::transaction(function() use ($user, $amount, $fee) {
- WalletWithdrawRecord::create([
- 'user_id' => $user->id,
- 'amount' => $amount,
- 'fee' => $fee,
- 'type' => MemberUser::class,
- 'state' => 'pending'
- ]);
- });
- return ['message' => '提现申请已提交'];
- }
- /**
- * 用户注销
- */
- public function deleteAccount()
- {
- $user = Auth::user();
- if (!$user || $user->state !== 'enable') {
- throw new \Exception('用户状态异常');
- }
- $user->state = 'disable';
- $user->save();
- $user->delete();
- return ['message' => '账号已注销'];
- }
- /**
- * 用户反馈
- */
- public function feedback($content)
- {
- $user = Auth::user();
- if (!$user || $user->state !== 'enable') {
- throw new \Exception('用户状态异常');
- }
- // UserFeedback::create([
- // 'owner_id' => $user->id,
- // 'owner_type' => MemberUser::class,
- // 'content' => $content
- // ]);
- return ['message' => '反馈已提交'];
- }
- /**
- * 申请成为技师
- */
- public function applyCoach()
- {
- // 获取当前用户
- $userId = Auth::id();
- $user = MemberUser::findOrFail($userId);
-
- // 检查用户状态
- if ($user->state !== 'enable') {
- throw new \Exception('用户状态异常');
- }
-
- // 检查是否已经是技师
- $exists = CoachUser::where('user_id', $userId)->exists();
- if ($exists) {
- throw new \Exception('您已经是技师了');
- }
-
- // 创建技师申请
- DB::transaction(function() use ($userId) {
- CoachUser::create([
- 'user_id' => $userId,
- 'state' => 'pending' // 待审核状态
- ]);
- });
-
- return ['message' => '申请已提交'];
- }
- }
|