Ver Fonte

feat:技师端-修改基本信息

刘学玺 há 4 meses atrás
pai
commit
1dd67bb57d

+ 28 - 0
app/Http/Controllers/Coach/AccountController.php

@@ -14,6 +14,7 @@ use App\Http\Requests\Coach\SetLocationRequest;
 use App\Http\Requests\Coach\SubmitBaseInfoRequest;
 use App\Http\Requests\Coach\SubmitRealNameRequest;
 use App\Http\Requests\Coach\SubmitQualificationRequest;
+use App\Http\Requests\Coach\UpdateBasicInfoRequest;
 
 /**
  * @group 技师端
@@ -497,4 +498,31 @@ class AccountController extends Controller
         // 返回成功响应
         return $this->success($data, '获取成功');
     }
+
+    /**
+     * [账户]更新基础信息
+     *
+     * @description 更新技师的基础信息,包括昵称、性别和手机号
+     *
+     * @authenticated 需要技师身份认证
+     *
+     * @bodyParam nickname string nullable 昵称(2-20个字符) Example: 张三
+     * @bodyParam gender integer nullable 性别(1:男 2:女) Example: 1
+     * @bodyParam mobile string nullable 手机号 Example: 13800138000
+     *
+     * @response {
+     *   "status": true,
+     *   "message": "基础信息修改申请已提交",
+     *   "data": {
+     *     "record_id": 1,
+     *     "updated_fields": ["nickname", "gender"]
+     *   }
+     * }
+     */
+    public function updateBasicInfo(UpdateBasicInfoRequest $request)
+    {
+        return $this->success(
+            $this->service->updateBasicInfo(Auth::user()->coach, $request->validated())
+        );
+    }
 }

+ 38 - 0
app/Http/Requests/Coach/UpdateBasicInfoRequest.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Http\Requests\Coach;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * 技师基础信息更新请求验证
+ */
+class UpdateBasicInfoRequest extends FormRequest
+{
+    public function rules(): array
+    {
+        return [
+            'nickname' => 'nullable|string|min:2|max:20',
+            'gender' => 'nullable|integer|in:1,2',
+            'mobile' => [
+                'nullable',
+                'string',
+                'size:11',
+                'regex:/^1[3-9]\d{9}$/',
+            ],
+        ];
+    }
+
+    public function messages(): array
+    {
+        return [
+            'nickname.string' => '昵称必须是字符串',
+            'nickname.min' => '昵称不能少于2个字符',
+            'nickname.max' => '昵称不能超过20个字符',
+            'gender.integer' => '性别必须是整数',
+            'gender.in' => '性别只能是1(男)或2(女)',
+            'mobile.size' => '手机号必须是11位',
+            'mobile.regex' => '手机号格式不正确',
+        ];
+    }
+}

+ 68 - 0
app/Services/Coach/AccountService.php

@@ -1215,4 +1215,72 @@ class AccountService
             'total_expense' => $coach->wallet->total_expense ?? 0,          // 累计支出
         ];
     }
+
+    /**
+     * 更新技师基础信息
+     *
+     * 业务流程:
+     * 1. 获取最新的非拒绝记录
+     * 2. 如果是待审核状态,直接更新字段
+     * 3. 如果是已通过状态,创建新记录并复制字段
+     *
+     * @param CoachUser $coach 技师对象
+     * @param array $data 待更新的数据,可包含:
+     *        - nickname: ?string 昵称
+     *        - gender: ?int 性别(1:男 2:女)
+     *        - mobile: ?string 手机号
+     * @return array 返回结果
+     */
+    public function updateBasicInfo(CoachUser $coach, array $data): array
+    {
+        return DB::transaction(function () use ($coach, $data) {
+            // 获取最新的非拒绝记录
+            $latestRecord = $coach->infoRecords()
+                ->where('state', '<>', TechnicianAuthStatus::REJECTED->value)
+                ->latest()
+                ->first();
+
+            abort_if(!$latestRecord, 404, '未找到有效的基础信息记录');
+
+            // 提取要更新的字段
+            $updateFields = array_intersect_key($data, [
+                'nickname' => '',
+                'gender' => '',
+                'mobile' => '',
+            ]);
+
+            // 如果没有要更新的字段,直接返回
+            abort_if(empty($updateFields), 422, '没有需要更新的字段');
+
+            // 如果最新记录是待审核状态,直接更新
+            if ($latestRecord->state === TechnicianAuthStatus::AUDITING->value) {
+                $latestRecord->update($updateFields);
+
+                return [
+                    'message' => '基础信息修改申请已更新',
+                    'record_id' => $latestRecord->id,
+                    'updated_fields' => array_keys($updateFields)
+                ];
+            }
+
+            // 创建新的审核记录,复制其他字段
+            $newRecord = $coach->infoRecords()->create(array_merge([
+                'avatar' => $latestRecord->avatar,
+                'life_photos' => $latestRecord->life_photos,
+                'gender' => $latestRecord->gender,
+                'mobile' => $latestRecord->mobile,
+                'birthday' => $latestRecord->birthday,
+                'work_years' => $latestRecord->work_years,
+                'intention_city' => $latestRecord->intention_city,
+                'introduction' => $latestRecord->introduction,
+                'state' => TechnicianAuthStatus::AUDITING->value,
+            ], $updateFields)); // 使用新的字段值覆盖
+
+            return [
+                'message' => '基础信息修改申请已提交',
+                'record_id' => $newRecord->id,
+                'updated_fields' => array_keys($updateFields)
+            ];
+        });
+    }
 }

+ 5 - 0
routes/api.php

@@ -195,6 +195,11 @@ Route::middleware(['auth:sanctum', 'coach'])->prefix('coach')->group(function ()
 
         // 获取技师详情
         Route::get('detail', [App\Http\Controllers\Coach\AccountController::class, 'detail'])->name('coach.detail');
+
+        // 更新基础信息
+        Route::post('update-basic-info', [App\Http\Controllers\Coach\AccountController::class, 'updateBasicInfo'])
+            ->middleware(['throttle:6,1'])  // 添加频率限制,每分钟最多6次
+            ->name('coach.account.update-basic-info');
     });
 
     // 订单相关路由