Ver Fonte

feat:技师端- 发送手机验证码

刘学玺 há 4 meses atrás
pai
commit
79e24d9e13

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

@@ -15,6 +15,7 @@ use App\Http\Requests\Coach\SubmitBaseInfoRequest;
 use App\Http\Requests\Coach\SubmitRealNameRequest;
 use App\Http\Requests\Coach\SubmitQualificationRequest;
 use App\Http\Requests\Coach\UpdateBasicInfoRequest;
+use App\Http\Requests\Coach\SendVerifyCodeRequest;
 
 /**
  * @group 技师端
@@ -525,4 +526,34 @@ class AccountController extends Controller
             $this->service->updateBasicInfo(Auth::user()->coach, $request->validated())
         );
     }
+
+    /**
+     * [账户]发送验证码
+     *
+     * @description 发送手机验证码,用于修改手机号等操作
+     *
+     * @authenticated 需要技师身份认证
+     *
+     * @bodyParam mobile string required 手机号 Example: 13800138000
+     * @bodyParam type string required 验证码类型(update:修改手机号) Example: "update"
+     *
+     * @response {
+     *   "status": true,
+     *   "message": "验证码发送成功",
+     *   "data": {
+     *     "mobile": "138****8000",
+     *     "expire_time": 300
+     *   }
+     * }
+     *
+     * @response 422 {
+     *   "message": "该手机号已被使用"
+     * }
+     */
+    public function sendVerifyCode(SendVerifyCodeRequest $request)
+    {
+        return $this->success(
+            $this->service->sendVerifyCode($request->validated())
+        );
+    }
 }

+ 35 - 0
app/Http/Requests/Coach/SendVerifyCodeRequest.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Http\Requests\Coach;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * 发送验证码请求验证
+ */
+class SendVerifyCodeRequest extends FormRequest
+{
+    public function rules(): array
+    {
+        return [
+            'mobile' => [
+                'required',
+                'string',
+                'size:11',
+                'regex:/^1[3-9]\d{9}$/',
+            ],
+            'type' => 'required|string|in:update', // 验证码类型(update:修改手机号)
+        ];
+    }
+
+    public function messages(): array
+    {
+        return [
+            'mobile.required' => '手机号不能为空',
+            'mobile.size' => '手机号必须是11位',
+            'mobile.regex' => '手机号格式不正确',
+            'type.required' => '验证码类型不能为空',
+            'type.in' => '验证码类型无效',
+        ];
+    }
+}

+ 43 - 1
app/Services/Coach/AccountService.php

@@ -419,7 +419,7 @@ class AccountService
      */
     public function setLocation($coachId, $latitude, $longitude, $type = TechnicianLocationType::COMMON->value, array $locationInfo = [])
     {
-        // 使用事务确保一致性
+        // 使用事务确保��一致性
         return DB::transaction(function () use ($coachId, $latitude, $longitude, $type, $locationInfo) {
             // 验证经纬度的有效性(-90≤纬度≤90,-180≤经度≤180)
             $this->validateCoordinates($latitude, $longitude);
@@ -1283,4 +1283,46 @@ class AccountService
             ];
         });
     }
+
+    /**
+     * 发送验证码
+     *
+     * 业务流程:
+     * 1. 检查手机号是否已被使用
+     * 2. 生成验证码
+     * 3. 保存到 Redis
+     * 4. 发送验证码
+     *
+     * @param array $data 包含:
+     *        - mobile: string 手机号
+     *        - type: string 验证码类型
+     * @return array 返回结果
+     */
+    public function sendVerifyCode(array $data): array
+    {
+        // 检查手机号是否已被使用
+        $exists = MemberUser::where('mobile', $data['mobile'])
+            ->whereHas('coach')
+            ->exists();
+        abort_if($exists, 422, '该手机号已被使用');
+
+        // 生成6位随机验证码
+        $code = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
+
+        // 验证码有效期(5分钟)
+        $expireTime = 300;
+
+        // 缓存键
+        $cacheKey = "sms_code:{$data['type']}:{$data['mobile']}";
+
+        // 保存到Redis,设置5分钟过期
+        Redis::setex($cacheKey, $expireTime, $code);
+
+
+        return [
+            'code' => $code,
+            'mobile' => substr_replace($data['mobile'], '****', 3, 4),
+            'expire_time' => $expireTime
+        ];
+    }
 }

+ 5 - 0
routes/api.php

@@ -200,6 +200,11 @@ Route::middleware(['auth:sanctum', 'coach'])->prefix('coach')->group(function ()
         Route::post('update-basic-info', [App\Http\Controllers\Coach\AccountController::class, 'updateBasicInfo'])
             ->middleware(['throttle:6,1'])  // 添加频率限制,每分钟最多6次
             ->name('coach.account.update-basic-info');
+
+        // 发送验证码
+        Route::post('send-code', [App\Http\Controllers\Coach\AccountController::class, 'sendVerifyCode'])
+            ->middleware(['throttle:3,1'])  // 限制每分钟最多发送3次
+            ->name('coach.account.send-code');
     });
 
     // 订单相关路由