|
@@ -2,185 +2,167 @@
|
|
|
|
|
|
namespace App\Services\Client;
|
|
|
|
|
|
-use App\Models\CoachUser;
|
|
|
-use App\Models\MemberUser;
|
|
|
-use App\Models\SysConfig;
|
|
|
-use App\Models\Wallet;
|
|
|
-use App\Models\WalletWithdrawRecord;
|
|
|
+use App\Models\CoachApplication;
|
|
|
+use App\Models\Feedback;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
-use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
|
|
|
|
|
class UserService
|
|
|
{
|
|
|
/**
|
|
|
- * 获取用户信息
|
|
|
+ * 获取当前用户信息
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
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,
|
|
|
- ];
|
|
|
+ try {
|
|
|
+ // 获取当前登录用户
|
|
|
+ $user = Auth::user();
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'code' => 200,
|
|
|
+ 'message' => '获取成功',
|
|
|
+ 'data' => $user,
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取用户信息失败: '.$e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 修改用户信息
|
|
|
+ * 更新当前用户信息
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
public function updateUserInfo(array $data)
|
|
|
{
|
|
|
- $user = Auth::user();
|
|
|
- $user->update($data);
|
|
|
-
|
|
|
- return ['message' => '修改成功'];
|
|
|
+ try {
|
|
|
+ // 更新用户信息
|
|
|
+ $user = Auth::user();
|
|
|
+ $user->update($data);
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'code' => 200,
|
|
|
+ 'message' => '修改成功',
|
|
|
+ 'data' => null,
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('更新用户信息失败: '.$e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取用户钱包
|
|
|
+ * 提交用户反馈
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
- public function getUserWallet()
|
|
|
+ public function feedback(string $content)
|
|
|
{
|
|
|
- $user = Auth::user();
|
|
|
- $wallet = $user->wallet;
|
|
|
- if (! $wallet) {
|
|
|
- throw new \Exception('钱包不存在');
|
|
|
+ try {
|
|
|
+ // 保存用户反馈
|
|
|
+ // Feedback::create([
|
|
|
+ // 'user_id' => Auth::id(),
|
|
|
+ // 'content' => $content,
|
|
|
+ // ]);
|
|
|
+
|
|
|
+ // return response()->json([
|
|
|
+ // 'code' => 200,
|
|
|
+ // 'message' => '提交成功',
|
|
|
+ // 'data' => null,
|
|
|
+ // ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('提交反馈失败: '.$e->getMessage());
|
|
|
+ throw $e;
|
|
|
}
|
|
|
-
|
|
|
- return $wallet;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户提现
|
|
|
+ * 申请成为技师
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
- public function withdraw($amount, $type = 'wechat', $area_code = '')
|
|
|
+ public function applyCoach(string $mobile, string $gender, string $work_years, string $intention_city)
|
|
|
{
|
|
|
- // 获取当前用户
|
|
|
- $user = Auth::user();
|
|
|
- if (! $user || $user->state !== 'enable') {
|
|
|
- throw new \Exception('用户状态异常');
|
|
|
- }
|
|
|
-
|
|
|
- // 获取钱包并检查余额
|
|
|
- $wallet = $user->wallet;
|
|
|
- if ($wallet->state !== 'enable') {
|
|
|
- throw new \Exception('钱包已冻结');
|
|
|
- }
|
|
|
-
|
|
|
- if ($wallet->available_balance < $amount) {
|
|
|
- throw new \Exception('钱包余额不足');
|
|
|
- }
|
|
|
-
|
|
|
- $config = SysConfig::where('key', 'withdraw_min_amount')->first();
|
|
|
- $configValue = json_decode($config?->value, true);
|
|
|
-
|
|
|
- $minAmount = $configValue['minAmount'] ?? 1;
|
|
|
- $maxAmount = $configValue['maxAmount'] ?? 10000;
|
|
|
- $fee = $configValue['fee'] ?? 0.00;
|
|
|
-
|
|
|
- if ($amount < $minAmount) {
|
|
|
- throw new \Exception("提现金额不能小于{$minAmount}元");
|
|
|
- }
|
|
|
-
|
|
|
- if ($amount > $maxAmount) {
|
|
|
- throw new \Exception("提现金额不能大于{$maxAmount}元");
|
|
|
- }
|
|
|
-
|
|
|
- // 创建提现记录
|
|
|
- DB::transaction(function () use ($amount, $fee, $wallet, $type, $area_code) {
|
|
|
- WalletWithdrawRecord::create([
|
|
|
- 'wallet_id' => $wallet->id,
|
|
|
- 'amount' => $amount, // 提现金额
|
|
|
- 'withdraw_type' => $type, // 提现方式
|
|
|
- 'fee' => $fee, // 提现手续费
|
|
|
- 'area_code' => $area_code, // 行政区划代码
|
|
|
- 'state' => 'processing', // 状态
|
|
|
+ try {
|
|
|
+ // 创建技师申请记录
|
|
|
+ // CoachApplication::create([
|
|
|
+ // 'user_id' => Auth::id(),
|
|
|
+ // 'mobile' => $mobile,
|
|
|
+ // 'gender' => $gender,
|
|
|
+ // 'work_years' => $work_years,
|
|
|
+ // 'intention_city' => $intention_city,
|
|
|
+ // ]);
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'code' => 200,
|
|
|
+ 'message' => '申请成功',
|
|
|
+ 'data' => null,
|
|
|
]);
|
|
|
-
|
|
|
- // 扣除钱包余额
|
|
|
- $wallet->available_balance -= $amount;
|
|
|
- $wallet->total_balance -= $amount;
|
|
|
- $wallet->save();
|
|
|
- });
|
|
|
-
|
|
|
- return ['message' => '提现申请已提交'];
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('申请成为技师失败: '.$e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户反馈
|
|
|
+ * 生成用户邀请码
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
- public function feedback($content)
|
|
|
+ public function generateInviteCode()
|
|
|
{
|
|
|
- $user = Auth::user();
|
|
|
- if (! $user || $user->state !== 'enable') {
|
|
|
- throw new \Exception('用户状态异常');
|
|
|
- }
|
|
|
-
|
|
|
- // UserFeedback::create([
|
|
|
- // 'owner_id' => $user->id,
|
|
|
- // 'owner_type' => MemberUser::class,
|
|
|
- // 'content' => $content
|
|
|
- // ]);
|
|
|
+ try {
|
|
|
+ // 获取当前用户
|
|
|
+ $user = Auth::user();
|
|
|
|
|
|
- return ['message' => '反馈已提交'];
|
|
|
- }
|
|
|
+ // 生成邀请码
|
|
|
+ $inviteCode = strtoupper(substr(md5($user->id), 0, 6));
|
|
|
|
|
|
- /**
|
|
|
- * 申请成为技师
|
|
|
- */
|
|
|
- public function applyCoach($mobile = '', $gender = '', $work_years = '', $intention_city = '')
|
|
|
- {
|
|
|
- // 获取当前用户
|
|
|
- $user = Auth::user();
|
|
|
+ // 生成带参数的网页链接
|
|
|
+ $qrContent = config('app.url').'/invite?'.http_build_query([
|
|
|
+ 'user_id' => $user->id,
|
|
|
+ 'role' => 'user',
|
|
|
+ 'code' => $inviteCode,
|
|
|
+ ]);
|
|
|
|
|
|
- // 检查用户状态
|
|
|
- if ($user->state !== 'enable') {
|
|
|
- throw new \Exception('用户状态异常');
|
|
|
- }
|
|
|
+ // 使用QrCode库生成SVG格式的二维码
|
|
|
+ $qrImage = QrCode::format('svg')
|
|
|
+ ->size(200)
|
|
|
+ ->margin(2)
|
|
|
+ ->encoding('UTF-8')
|
|
|
+ ->generate($qrContent);
|
|
|
|
|
|
- // 检查是否已经申请过
|
|
|
- $coach = $user->coach;
|
|
|
- if ($coach) {
|
|
|
- if ($coach->state === 'pending') {
|
|
|
- throw new \Exception('您已提交申请,请等待审核');
|
|
|
- }
|
|
|
- if ($coach->state === 'enable') {
|
|
|
- throw new \Exception('您已经是技师了');
|
|
|
- }
|
|
|
- if ($coach->state === 'disable') {
|
|
|
- throw new \Exception('您的技师资格已被禁用');
|
|
|
- }
|
|
|
- }
|
|
|
+ // 将SVG转为base64
|
|
|
+ $qrBase64 = base64_encode($qrImage);
|
|
|
|
|
|
- // 创建技师申请
|
|
|
- DB::transaction(function () use ($user, $mobile, $gender, $work_years, $intention_city) {
|
|
|
- // 创建技师基本信息
|
|
|
- $coach = CoachUser::create([
|
|
|
+ // 记录生成日志
|
|
|
+ Log::info('用户生成邀请码:', [
|
|
|
'user_id' => $user->id,
|
|
|
- 'state' => 'pending', // 待审核状态
|
|
|
+ 'invite_code' => $inviteCode,
|
|
|
+ 'invite_url' => $qrContent, // 记录生成的邀请链接
|
|
|
]);
|
|
|
|
|
|
- $coach->infoRecords()->create([
|
|
|
- 'nickname' => $user->nickname ?? '', // 姓名
|
|
|
- 'mobile' => $mobile, // 服务电话
|
|
|
- 'avatar' => $user->avatar ?? '', // 头像
|
|
|
- 'gender' => $gender ?? 'unknown', // 性别
|
|
|
- 'work_years' => $work_years ?? '', // 从业年份
|
|
|
- 'intention_city' => $intention_city ?? '', // 意向城市
|
|
|
- 'state' => 'pending',
|
|
|
+ return response()->json([
|
|
|
+ 'code' => 200,
|
|
|
+ 'message' => '生成成功',
|
|
|
+ 'data' => [
|
|
|
+ 'invite_code' => $inviteCode,
|
|
|
+ 'invite_url' => $qrContent, // 返回邀请链接
|
|
|
+ 'qr_code' => 'data:image/svg+xml;base64,'.$qrBase64,
|
|
|
+ ],
|
|
|
]);
|
|
|
|
|
|
- });
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('生成邀请码失败: '.$e->getMessage());
|
|
|
|
|
|
- return [
|
|
|
- 'message' => '申请已提交,请等待审核',
|
|
|
- ];
|
|
|
+ return response()->json([
|
|
|
+ 'code' => 500,
|
|
|
+ 'msg' => '生成二维码失败,请稍后再试。',
|
|
|
+ ]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|