WIN-2308041133\Administrator há 2 meses atrás
pai
commit
56001befdd
2 ficheiros alterados com 40 adições e 13 exclusões
  1. 3 1
      app/api/controller/Pub.php
  2. 37 12
      app/model/api/User.php

+ 3 - 1
app/api/controller/Pub.php

@@ -255,7 +255,9 @@ class Pub extends BaseController
             ['servicePrice', 0],
             ['is_china', ''],
             ['service_area', []],
-            ['timetype', '']
+            ['timetype', ''],
+            ['longitude', 0],
+            ['latitude', 0]
         ], $request);
         $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
 

+ 37 - 12
app/model/api/User.php

@@ -329,6 +329,10 @@ class User extends BaseModel
             || $post['is_china'] !== ''
             || (!empty($post["service_area"]) && is_array($post["service_area"]));
 
+        // 判断是否传了经纬度(用于按距离排序)
+        $hasLocation = !empty($post['longitude']) && !empty($post['latitude'])
+            && is_numeric($post['longitude']) && is_numeric($post['latitude']);
+
         $where = [];
         $where[] = ["u.work_type_id", ">", 0];
         $where[] = ["u.status", "=", 1];
@@ -336,6 +340,15 @@ class User extends BaseModel
             $where[] = ["u.work_type_id", "=", $post['work_type_id']];
         }
 
+        // 距离计算 SQL 字段
+        $distanceField = "";
+        if ($hasLocation) {
+            $lng = floatval($post['longitude']);
+            $lat = floatval($post['latitude']);
+            // 使用 MySQL 球面距离公式计算距离(单位:公里)
+            $distanceField = ", (6371 * acos(cos(radians({$lat})) * cos(radians(u.latitude)) * cos(radians(u.longitude) - radians({$lng})) + sin(radians({$lat})) * sin(radians(u.latitude)))) AS distance";
+        }
+
         if ($hasNewCondition) {
             // 使用新搜索条件的逻辑
             if ($post['servicePrice'] > 0) {
@@ -380,17 +393,23 @@ class User extends BaseModel
             if ($totalCount > 0) {
                 $dataModel = $this
                     ->alias("u")
-                    ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit,a.name,a.avatar,a.birthday,a.service_project")
+                    ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit,a.name,a.avatar,a.birthday,a.service_project" . $distanceField)
                     ->leftJoin("info_audit a", "u.uid=a.uid and a.status = 1 and a.is_show = 1")
                     ->leftJoin("user_show_template ut", "ut.uid  = u.uid and ut.is_default = 1")
                     ->where($where);
                 if ($serviceAreaWhere) {
                     $dataModel->where($serviceAreaWhere);
                 }
-                $data = $dataModel->order("u.show_temp_seq", "desc")
-                    ->order("u.uid", "desc")
-                    ->page($post["page"], $post["pageSize"])
-                    ->select();
+                if ($hasLocation) {
+                    $data = $dataModel->order("distance", "asc")
+                        ->page($post["page"], $post["pageSize"])
+                        ->select();
+                } else {
+                    $data = $dataModel->order("u.show_temp_seq", "desc")
+                        ->order("u.uid", "desc")
+                        ->page($post["page"], $post["pageSize"])
+                        ->select();
+                }
                 if (!empty($data)) {
                     $data = $data->toArray();
                     $infoAuditDb = new InfoAudit();
@@ -419,16 +438,22 @@ class User extends BaseModel
             $totalCount = $this->alias("u")->where($where)->count();
             $data = null;
             if ($totalCount > 0) {
-                $data = $this
+                $dataModel = $this
                     ->alias("u")
-                    ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit")
+                    ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit" . $distanceField)
                     ->leftJoin("info_audit a", "u.uid=a.uid")
                     ->leftJoin("user_show_template ut", "ut.uid  = u.uid and ut.is_default = 1")
-                    ->where($where)
-                    ->order("u.show_temp_seq", "desc")
-                    ->order("u.uid", "desc")
-                    ->page($post["page"], $post["pageSize"])
-                    ->select();
+                    ->where($where);
+                if ($hasLocation) {
+                    $data = $dataModel->order("distance", "asc")
+                        ->page($post["page"], $post["pageSize"])
+                        ->select();
+                } else {
+                    $data = $dataModel->order("u.show_temp_seq", "desc")
+                        ->order("u.uid", "desc")
+                        ->page($post["page"], $post["pageSize"])
+                        ->select();
+                }
                 if (!empty($data)) {
                     $data = $data->toArray();
                 }