Browse Source

feat:后台-修改技师基本信息

刘学玺 3 months ago
parent
commit
04dab8f588

+ 67 - 0
app/Admin/Controllers/CoachInfoRecordController.php

@@ -133,4 +133,71 @@ class CoachInfoRecordController extends AdminController
 
         return $this->autoResponse($result);
     }
+
+    /**
+     * [技师基本信息]修改技师基本信息
+     *
+     * @description 修改技师的基本信息,包括昵称、性别、手机号等基础字段
+     *
+     * @header x-xsrf-token required CSRF令牌 Example: your_csrf_token
+     *
+     * @bodyParam id integer required 记录ID Example: 1
+     * @bodyParam nickname string nullable 昵称(2-20个字符) Example: 张三
+     * @bodyParam avatar string nullable 头像图片 Example: path/to/avatar.jpg
+     * @bodyParam formal_photo string nullable 正装照片 Example: path/to/formal.jpg
+     * @bodyParam gender integer nullable 性别(1:男 2:女) Example: 1
+     * @bodyParam mobile string nullable 手机号 Example: 13800138000
+     * @bodyParam birthday date nullable 出生日期 Example: 1990-01-01
+     * @bodyParam work_years integer nullable 工作年限(0-99) Example: 5
+     * @bodyParam intention_city string nullable 意向城市 Example: 北京
+     * @bodyParam introduction string nullable 个人简介(最多255字符) Example: 专业按摩师,从业5年
+     * @bodyParam life_photos array nullable 生活照片数组
+     * @bodyParam life_photos.* string required 生活照片路径 Example: path/to/photo.jpg
+     *
+     * @response scenario=success {
+     *   "code": 200,
+     *   "message": "修改成功"
+     * }
+     *
+     * @response status=404 scenario="不存在" {
+     *   "message": "记录不存在"
+     * }
+     *
+     * @response status=422 scenario="验证失败" {
+     *   "message": "验证错误",
+     *   "errors": {
+     *     "nickname": ["昵称不能少于2个字符"],
+     *     "gender": ["性别只能是1(男)或2(女)"],
+     *     "mobile": ["手机号格式不正确"]
+     *   }
+     * }
+     */
+    public function updateInfo(Request $request)
+    {
+        $validated = $request->validate([
+            'id' => 'required|integer|exists:coach_info_records,id',
+            'nickname' => 'nullable|string|min:2|max:20',
+            'avatar' => 'nullable|string',
+            'formal_photo' => 'nullable|string',
+            'gender' => 'nullable|integer|in:1,2',
+            'mobile' => 'nullable|string|regex:/^1[3-9]\d{9}$/',
+            'birthday' => 'nullable|date',
+            'work_years' => 'nullable|integer|min:0|max:99',
+            'intention_city' => 'nullable|string|max:50',
+            'introduction' => 'nullable|string|max:255',
+            'life_photos' => 'nullable|array',
+            'life_photos.*' => 'required|string',
+        ], [
+            'nickname.min' => '昵称不能少于2个字符',
+            'nickname.max' => '昵称不能超过20个字符',
+            'gender.in' => '性别只能是1(男)或2(女)',
+            'mobile.regex' => '手机号格式不正确',
+            'work_years.min' => '工作年限不能小于0年',
+            'work_years.max' => '工作年限不能超过99年',
+        ]);
+
+        $result = $this->service->updateInfo($validated);
+
+        return $this->autoResponse($result);
+    }
 }

+ 80 - 0
app/Services/CoachInfoRecordService.php

@@ -8,6 +8,7 @@ use App\Models\CoachInfoRecord;
 use Illuminate\Support\Facades\DB;
 use App\Enums\TechnicianAuthStatus;
 use Slowlyo\OwlAdmin\Services\AdminService;
+use Illuminate\Support\Facades\Auth;
 
 /**
  * 技师信息记录
@@ -76,4 +77,83 @@ class CoachInfoRecordService extends AdminService
             return true;
         });
     }
+
+    /**
+     * 更新技师基本信息
+     *
+     * 业务流程:
+     * 1. 验证记录是否存在
+     * 2. 处理正装照片:
+     *    - 从数据中提取正装照片字段
+     *    - 如果有值则更新技师表
+     *    - 记录更新时间和操作人
+     * 3. 处理基本信息:
+     *    - 过滤空值字段
+     *    - 处理生活照片JSON转换
+     *    - 更新基本信息记录
+     * 4. 使用事务确保数据一致性
+     *
+     * 注意事项:
+     * - 所有字段都是可选的
+     * - 正装照片存储在技师表中
+     * - 生活照片以JSON数组格式存储
+     * - 只更新有值的字段
+     *
+     * @param array $data 更新数据,包含:
+     *        - id: int 记录ID
+     *        - nickname: ?string 昵称
+     *        - avatar: ?string 头像
+     *        - formal_photo: ?string 正装照片(存储在技师表)
+     *        - gender: ?int 性别(1:男 2:女)
+     *        - mobile: ?string 手机号
+     *        - birthday: ?string 出生日期
+     *        - work_years: ?int 工作年限
+     *        - intention_city: ?string 意向城市
+     *        - introduction: ?string 个人简介
+     *        - life_photos: ?array 生活照片数组
+     *
+     * @return array 更新结果
+     * @throws \Exception 当记录不存在时抛出异常
+     */
+    public function updateInfo(array $data): array
+    {
+        return DB::transaction(function () use ($data) {
+            $record = $this->getModel()::findOrFail($data['id']);
+            $coach = $record->coach;
+
+            // 提取正装照片字段
+            $formalPhoto = $data['formal_photo'] ?? null;
+            unset($data['formal_photo']);
+
+            // 移除 id 字段
+            unset($data['id']);
+
+            // 过滤掉空值字段
+            $updateData = array_filter($data, function ($value) {
+                return !is_null($value);
+            });
+
+            // 如果有生活照片,转换为JSON
+            if (isset($updateData['life_photos'])) {
+                $updateData['life_photos'] = json_encode(array_values($updateData['life_photos']));
+            }
+
+            // 更新记录
+            $record->update($updateData);
+
+            // 如果有正装照片,更新技师表
+            if ($formalPhoto !== null && $coach) {
+                $coach->update([
+                    'formal_photo' => $formalPhoto,
+                    'formal_photo_updated_at' => now(),
+                    'formal_photo_admin_id' => Auth::id(),
+                ]);
+            }
+
+            return [
+                'message' => '修改成功',
+                'data' => $record
+            ];
+        });
+    }
 }

+ 6 - 0
routes/web.php

@@ -127,6 +127,9 @@ Route::group(
         // 店铺余额冻结
         Route::post('shop/freeze-balance', [ShopInfoController::class, 'freezeBalance']);
 
+        // 修改技师基本信息
+        Route::post('coach_info_records/update_info', [\App\Admin\Controllers\CoachInfoRecordController::class, 'updateInfo']);
+
         // 上传
         Route::post('/upload', [UploadController::class, 'upload']);
     }
@@ -239,5 +242,8 @@ Route::group([
         Route::post('coach_real_records/audit', [\App\Admin\Controllers\CoachRealRecordController::class, 'audit']);
         // 审核技师资质认证记录
         Route::post('coach_qual_records/audit', [\App\Admin\Controllers\CoachQualRecordController::class, 'audit']);
+
+        // 修改技师基本信息
+        Route::post('coach_info_records/update_info', [\App\Admin\Controllers\CoachInfoRecordController::class, 'updateInfo']);
     });
 });