Ver Fonte

fixed:技师端-优化获取技师位置

刘学玺 há 4 meses atrás
pai
commit
6666f9a848

+ 19 - 11
app/Http/Controllers/Coach/AccountController.php

@@ -247,26 +247,34 @@ class AccountController extends Controller
     /**
      * [账户]获取技师位置信息
      *
-     * @description 取技师的当前位置和常用位置信息
+     * @description 取技师的位置信息
      *
      * @authenticated
      *
+     * @queryParam type int 位置类型(1:当前位置 2:常用位置) Example: 2
+     *
      * @response {
      *   "data": {
-     *     "current": {
-     *       "address": "北京市朝阳区建国路93号万达广场"
-     *     },
-     *     "common": {
-     *       "address": "北京市海淀区中关村大街1号"
-     *     }
+     *     "province": "浙江省",
+     *     "city": "杭州市",
+     *     "district": "西湖区",
+     *     "address": "文三路478号",
+     *     "adcode": "330106",
+     *     "longitude": 120.12345,
+     *     "latitude": 30.12345,
+     *     "updated_at": "2024-03-22 10:00:00"
      *   }
-     *idid
+     * }
      */
-    public function getLocation()
+    public function getLocation(Request $request)
     {
-        $result = $this->service->getLocation(Auth::user()->id);
+        $validated = $request->validate([
+            'type' => 'required|integer|in:1,2'
+        ]);
 
-        return $this->success($result);
+        return $this->success(
+            $this->service->getLocation(Auth::user(), $validated['type'])
+        );
     }
 
     /**

+ 69 - 46
app/Services/Coach/AccountService.php

@@ -62,9 +62,7 @@ class AccountService
      */
     public function submitBaseInfo($user, array $data)
     {
-        // 开启数据库事务,确保数据一致性
-        DB::beginTransaction();
-        try {
+        return DB::transaction(function () use ($user, $data) {
             // 验证技师信息是否存在,不存在则抛出404异常
             abort_if(!$user->coach, 404, '技师信息不存在');
 
@@ -90,18 +88,8 @@ class AccountService
             // 避免用户获取到旧的缓存数据
             $this->clearCoachCache($user->coach->id);
 
-            // 提交事务,确保所有操作成功
-            DB::commit();
-
-            // 返回成功结果
             return ['message' => '基本信息提交成功'];
-        } catch (\Exception $e) {
-            // 发生异常时回滚事务,确保数据一致性
-            DB::rollBack();
-
-            // 向上抛出异常,由调用方处理
-            throw $e;
-        }
+        });
     }
 
     /**
@@ -431,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);
@@ -463,42 +451,77 @@ class AccountService
     /**
      * 获取技师位置信息
      *
-     * @param  int  $userId  用户ID
-     * @return array 位置信息
+     * 业务流程:
+     * 1. 获取指定类型的位置记录
+     * 2. 返回格式化的位置数据
+     *
+     * @param MemberUser $user 认证用户
+     * @param int $type 位置类型 (1:当前位置 2:常用位置)
+     * @return array 位置信息,包含:
+     *        - province: string 省份
+     *        - city: string 城市
+     *        - district: string 区县
+     *        - address: string 详细地址
+     *        - adcode: string 行政区划代码
+     *        - longitude: float 经度
+     *        - latitude: float 纬度
+     *        - updated_at: string 更新时间
      */
-    public function getLocation($userId)
+    public function getLocation(MemberUser $user, int $type): array
     {
-        try {
-            // 改进:直接使用 coach 模型
-            $user = MemberUser::find($userId);
-            abort_if(! $user, 404, '用户不存在');
-            abort_if(! $user->coach, 404, '技师信息不存在');
-
-            // 获取常用位置信息
-            $location = $user->coach->locations()
-                ->where('type', TechnicianLocationType::COMMON->value)
-                ->first();
+        // 获取指定类型的位置记录
+        $location = $this->getLocationByType($user->coach, $type);
 
-            $result = [
-                'address' => $location ? $location->location : null,
-            ];
+        // 返回格式化的位置数据
+        return $this->formatLocationResponse($location);
+    }
 
-            // 记录日志
-            Log::info('获取技师常用位置信息成功', [
-                'coach_id' => $user->coach->id,
-                'location' => $result,
-            ]);
+    /**
+     * 根据类型获取技师位置记录
+     *
+     * @param CoachUser $coach 技师对象
+     * @param int $type 位置类型 (1:当前位置 2:常用位置)
+     * @return CoachLocation 位置信息
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当位置信息不存在时抛出404异常
+     */
+    private function getLocationByType(CoachUser $coach, int $type): array
+    {
+        // 获取指定类型的位置记录
+        $location = $coach->locations()
+            ->where('type', $type)
+            ->first();
+        abort_if(!$location, 404, '位置信息不存在');
 
-            return $result;
-        } catch (\Exception $e) {
-            Log::error('获取技师常用位置信息异常', [
-                'coach_id' => $user->coach->id ?? null,
-                'error' => $e->getMessage(),
-                'file' => $e->getFile(),
-                'line' => $e->getLine(),
-            ]);
-            throw $e;
-        }
+        // 返回位置数据
+        return $location;
+    }
+
+    /**
+     * 格式化位置响应数据
+     *
+     * @param CoachLocation $location 位置记录
+     * @return array 格式化后的位置信息,包含:
+     *        - province: string 省份
+     *        - city: string 城市
+     *        - district: string 区县
+     *        - address: string 详细地址
+     *        - adcode: string 行政区划代码
+     *        - longitude: float 经度
+     *        - latitude: float 纬度
+     *        - updated_at: string 更新时间
+     */
+    private function formatLocationResponse(CoachLocation $location): array
+    {
+        return [
+            'province' => $location->province,    // 省份
+            'city' => $location->city,           // 城市
+            'district' => $location->district,    // 区县
+            'address' => $location->address,      // 详细地址
+            'adcode' => $location->adcode,        // 行政区划代码
+            'longitude' => $location->longitude,  // 经度
+            'latitude' => $location->latitude,    // 纬度
+            'updated_at' => $location->updated_at->toDateTimeString(), // 更新时间
+        ];
     }
 
     /**