Преглед на файлове

fixed:技师端-优化基本信息

刘学玺 преди 4 месеца
родител
ревизия
a593e80ec2
променени са 3 файла, в които са добавени 70 реда и са изтрити 105 реда
  1. 6 7
      app/Http/Controllers/Coach/AccountController.php
  2. 13 20
      app/Http/Requests/Coach/SubmitBaseInfoRequest.php
  3. 51 78
      app/Services/Coach/AccountService.php

+ 6 - 7
app/Http/Controllers/Coach/AccountController.php

@@ -35,14 +35,14 @@ class AccountController extends Controller
      *
      * @authenticated
      *
-     * @bodyParam nickname string required 昵称(2-20个字符) Example: 张三
-     * @bodyParam avatar string required 头像URL Example: http://example.com/avatar.jpg
+     * @bodyParam nickname string nullable 昵称(2-20个字符) Example: 张三
+     * @bodyParam avatar string nullable 头像图片 Example: base64或其他格式的图片数据
      * @bodyParam gender string required 性别(1:男 2:女) Example: 1
      * @bodyParam mobile string required 手机号 Example: 13800138000
-     * @bodyParam birthday date required 出生日期(年龄需满18岁) Example: 1990-01-01
-     * @bodyParam work_years integer required 工作年限(0-99) Example: 5
-     * @bodyParam intention_city string required 意向城市 Example: 北京
-     * @bodyParam introduction string required 个人简介(10-255个字符) Example: 专业按摩师,从业5年
+     * @bodyParam birthday date nullable 出生日期(年龄需满18岁) Example: 1990-01-01
+     * @bodyParam work_years integer nullable 工作年限(0-99) Example: 5
+     * @bodyParam intention_city string nullable 意向城市 Example: 北京
+     * @bodyParam introduction string nullable 个人简介(10-255个字符) Example: 专业按摩师,从业5年
      *
      * @response {
      *  "message": "基本信息提交成功"
@@ -372,6 +372,5 @@ class AccountController extends Controller
         $schedule = $this->service->getSchedule(Auth::id());
 
         return $this->success($schedule);
-
     }
 }

+ 13 - 20
app/Http/Requests/Coach/SubmitBaseInfoRequest.php

@@ -14,43 +14,36 @@ class SubmitBaseInfoRequest extends FormRequest
     public function rules()
     {
         return [
-            'nickname' => 'required|string|min:2|max:20',
-            'avatar' => 'required|url|max:255',
-            'gender' => 'required|in:1,2',
-            'mobile' => ['required', 'string', 'regex:/^1[3-9]\d{9}$/'],
-            'birthday' => [
-                'required',
-                'date',
-                'before:'.now()->subYears(18)->format('Y-m-d'),
-            ],
-            'work_years' => 'required|integer|min:0|max:99',
-            'intention_city' => 'required|string|max:50',
-            'introduction' => 'required|string|min:10|max:255',
+            'nickname' => 'nullable|string|min:2|max:20',
+            'avatar' => 'nullable|string|max:2048',
+            'gender' => 'required|string|in:1,2',
+            'mobile' => 'required|string|size:11',
+            'birthday' => 'nullable|date|before:' . now()->subYears(18)->format('Y-m-d'),
+            'work_years' => 'nullable|integer|min:0|max:99',
+            'intention_city' => 'nullable|string|max:50',
+            'introduction' => 'nullable|string|min:10|max:255',
         ];
     }
 
     public function messages()
     {
         return [
-            'nickname.required' => '昵称不能为空',
+            'nickname.string' => '昵称必须是字符串',
             'nickname.min' => '昵称不能少于2个字符',
             'nickname.max' => '昵称不能超过20个字符',
-            'avatar.required' => '头像不能为空',
-            'avatar.url' => '头像必须是有效的URL地址',
+            'avatar.string' => '头像必须是有效的字符串',
+            'avatar.max' => '头像大小不能超过2048字节',
             'gender.required' => '性别不能为空',
             'gender.in' => '性别只能是1(男)或2(女)',
             'mobile.required' => '手机号不能为空',
-            'mobile.regex' => '手机号格式不正确',
-            'birthday.required' => '出生日期不能为空',
+            'mobile.string' => '手机号必须是有效的字符串',
+            'mobile.size' => '手机号长度必须是11位',
             'birthday.date' => '出生日期格式不正确',
             'birthday.before' => '年龄必须满18岁',
-            'work_years.required' => '工作年限不能为空',
             'work_years.integer' => '工作年限必须是整数',
             'work_years.min' => '工作年限不能小于0年',
             'work_years.max' => '工作年限不能超过99年',
-            'intention_city.required' => '意向城市不能为空',
             'intention_city.max' => '意向城市不能超过50个字符',
-            'introduction.required' => '个人简介不能为空',
             'introduction.min' => '个人简介不能少于10个字符',
             'introduction.max' => '个人简介不能超过255个字符',
         ];

+ 51 - 78
app/Services/Coach/AccountService.php

@@ -22,34 +22,61 @@ class AccountService
 
     /**
      * 提交技师基本信息
+     * 包括个人基础资料的提交和审核
+     *
+     * 业务流程:
+     * 1. 验证技师信息存在性
+     * 2. 检查是否有待审核的记录
+     * 3. 创建新的基本信息记录
+     * 4. 清除相关缓存
+     *
+     * 注意事项:
+     * - 同一时间只能有一条待审核记录
+     * - 审核不通过可以重新提交
+     * - 头像图片数据不限制格式
+     * - 除性别和手机号外,其他字段均为可选
+     * - 手机号会进行脱敏处理
+     *
+     * @param User $user 当前认证用户
+     * @param array $data 基本信息数据,包含:
+     *        - nickname: string|null 昵称(可选)
+     *        - avatar: string|null 头像图片(可选)
+     *        - gender: string 性别(1:男 2:女)
+     *        - mobile: string 手机号
+     *        - birthday: string|null 出生日期(可选)
+     *        - work_years: int|null 工作年限(可选)
+     *        - intention_city: string|null 意向城市(可选)
+     *        - introduction: string|null 个人简介(可选)
+     * @return array 返回结果
+     * @throws \Exception 当验证失败或保存失败时抛出异常
      */
     public function submitBaseInfo($user, array $data)
     {
         DB::beginTransaction();
         try {
+            // 验证技师信息是否存在
+            abort_if(!$user->coach, 404, '技师信息不存在');
 
-            abort_if(! $user->coach, 404, '技师信息不存在');
-
-            // 检查是否有待审核的记录
+            // 检查是否有待审核的记录,避免重复提交
             $pendingRecord = $this->hasPendingRecord($user->coach, 'info');
             abort_if($pendingRecord, 422, '已有待审核的基本信息记录');
 
             // 创建技师信息
-            $record = $user->coach->infoRecords()->create(array_merge($data, [
+            $user->coach->infoRecords()->create(array_merge($data, [
                 'state' => TechnicianAuthStatus::AUDITING->value,
             ]));
 
-            // 清除技师信息缓存
+            // 清除技师信息缓存,确保数据一致性
             $this->clearCoachCache($user->coach->id);
 
+            // 提交事务
             DB::commit();
 
-            $this->logInfo('技师提交基本信息成功', $user, $data);
-
+            // 返回成功结果
             return ['message' => '基本信息提交成功'];
         } catch (\Exception $e) {
+            // 发生异常时回滚事务
             DB::rollBack();
-            $this->logError('提交技师基本信息失败', $user, $data, $e);
             throw $e;
         }
     }
@@ -96,15 +123,11 @@ class AccountService
             abort_if($pendingRecord, 422, '已有待审核的资质信息记录');
 
             // 创建新的资质审核记录,设置为待审核状态
-            $record = $user->coach->qualRecords()->create([
-                'qual_type' => $data['qual_type'],
-                'qual_photo' => $data['qual_photo'],
-                'business_license' => $data['business_license'],
-                'health_cert' => $data['health_cert'],
-                'state' => TechnicianAuthStatus::AUDITING->value
-            ]);
+            $record = $user->coach->qualRecords()->create(array_merge($data, [
+                'state' => TechnicianAuthStatus::AUDITING->value,
+            ]));
 
-            // 清除技师信息缓存,确��数据一致性
+            // 清除技师信息缓存,确保数据一致性
             $this->clearCoachCache($user->coach->id);
 
             // 提交事务
@@ -166,7 +189,7 @@ class AccountService
             abort_if($pendingRecord, 422, '已有待审核的实名认证信息');
 
             // 创建新的实名认证记录,设置为待审核状态
-            $record = $user->coach->realRecords()->create(array_merge($data, [
+            $user->coach->realRecords()->create(array_merge($data, [
                 'state' => TechnicianAuthStatus::AUDITING->value,
             ]));
 
@@ -190,53 +213,18 @@ class AccountService
      */
     public function getCoachInfo($user)
     {
-        try {
-            abort_if(! $user, 404, '用户不存在');
-            abort_if(! $user->coach, 404, '技师信息不存在');
-
-            return Cache::remember(
-                self::CACHE_KEY_PREFIX . $user->coach->id,
-                self::CACHE_TTL,
-                function () use ($user) {
-                    return $this->fetchCoachInfo($user->coach);
-                }
-            );
-        } catch (\Exception $e) {
-            $this->logError('获取技师信息失败', $user, [], $e);
-            throw $e;
-        }
-    }
-
-    /**
-     * 记录信息日志
-     */
-    private function logInfo(string $message, $user, array $data)
-    {
-        Log::info($message, [
-            'user_id' => $user->id,
-            'coach_id' => $user->coach->id,
-            'data' => $data,
-            'ip' => request()->ip(),
-            'timestamp' => now()->toDateTimeString(),
-        ]);
+        abort_if(! $user, 404, '用户不存在');
+        abort_if(! $user->coach, 404, '技师信息不存在');
+
+        return Cache::remember(
+            self::CACHE_KEY_PREFIX . $user->coach->id,
+            self::CACHE_TTL,
+            function () use ($user) {
+                return $this->fetchCoachInfo($user->coach);
+            }
+        );
     }
 
-    /**
-     * 记录错误日志
-     */
-    private function logError(string $message, $user, array $data, \Exception $e)
-    {
-        Log::error($message, [
-            'user_id' => $user->id,
-            'coach_id' => $user->coach->id ?? null,
-            'data' => $data,
-            'error' => $e->getMessage(),
-            'file' => $e->getFile(),
-            'line' => $e->getLine(),
-            'ip' => request()->ip(),
-            'timestamp' => now()->toDateTimeString(),
-        ]);
-    }
 
     /**
      * 获取技师详细信息
@@ -326,21 +314,6 @@ class AccountService
         return substr_replace($idCard, '****', 6, 8);
     }
 
-    /**
-     * 敏感数据脱敏
-     */
-    private function maskSensitiveData(array $data)
-    {
-        if (isset($data['id_card'])) {
-            $data['id_card'] = $this->maskIdCard($data['id_card']);
-        }
-        if (isset($data['mobile'])) {
-            $data['mobile'] = $this->maskMobile($data['mobile']);
-        }
-
-        return $data;
-    }
-
     /**
      * 清除技师信息缓存
      */
@@ -845,7 +818,7 @@ class AccountService
 
             Redis::setex($cacheKey, 86400, json_encode($cacheData));
         } catch (\Exception $e) {
-            Log::error('更新工作状态缓存��败', [
+            Log::error('更新工作状态缓存败', [
                 'coach_id' => $coachId,
                 'error' => $e->getMessage(),
             ]);