zilong 4 years ago
parent
commit
c56f0f9a97

+ 51 - 0
app/Helpers/functions.php

@@ -8,6 +8,7 @@
 
 
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
 use App\Exceptions\ExitOutException;
 use App\Exceptions\ExitOutException;
+use App\Models\User;
 
 
 //统一输出格式话的json数据
 //统一输出格式话的json数据
 if (!function_exists('out')) {
 if (!function_exists('out')) {
@@ -121,3 +122,53 @@ if (!function_exists('birthday_to_age')){
         return $year_diff.$month_diff.$day_diff ;
         return $year_diff.$month_diff.$day_diff ;
     }
     }
 }
 }
+
+//计算经纬度两点之间距离(返回为米)
+if (!function_exists('get_distance')) {
+    function get_distance($lat1, $lng1, $lat2, $lng2)
+    {
+        if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
+            return '999999999';
+        }
+        $earthRadius = 6378138;
+        $lat1 = ($lat1 * pi()) / 180;
+        $lng1 = ($lng1 * pi()) / 180;
+        $lat2 = ($lat2 * pi()) / 180;
+        $lng2 = ($lng2 * pi()) / 180;
+        $calcLongitude = $lng2 - $lng1;
+        $calcLatitude = $lat2 - $lat1;
+        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
+        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
+        $calculatedDistance = $earthRadius * $stepTwo;
+
+        return number_format($calculatedDistance, 2, '.', '');
+    }
+}
+
+//获取用户坐标
+if (!function_exists('get_user_coordinate')) {
+    function get_user_coordinate($user)
+    {
+        $req = request()->post();
+        if (empty($req['latitude']) || empty($req['longitude'])) {
+            $latitude = $user['latitude'];
+            $longitude = $user['longitude'];
+        }
+        else {
+            $latitude = $req['latitude'];
+            $longitude = $req['longitude'];
+        }
+
+        return ['latitude' => $latitude, 'longitude' => $longitude];
+    }
+}
+
+//获取用户距离
+if (!function_exists('get_user_distance')) {
+    function get_user_distance($user, $lat, $lng)
+    {
+        $coordinate = get_user_coordinate($user);
+        $data = get_distance($coordinate['latitude'], $coordinate['longitude'], $lat, $lng);
+        return $data;
+    }
+}

+ 15 - 4
app/Http/Controllers/Api/V1/DocterController.php

@@ -32,9 +32,8 @@ class DocterController extends AuthController
         ]);
         ]);
         $user = $this->user;
         $user = $this->user;
 
 
-        $latitude = !empty($req['latitude']) ? $req['latitude'] : $user['latitude'];
-        $longitude = !empty($req['longitude']) ? $req['longitude'] : $user['longitude'];
-        $distance_field = get_distance_field($latitude, $longitude);
+        $coordinate = get_user_coordinate($user);
+        $distance_field = get_distance_field($coordinate['latitude'], $coordinate['longitude']);
 
 
         $builder = Docter::with('office', 'qualification')->select(['id', 'type', 'name', 'phone', 'sex', 'birthday', 'avatar', 'status', 'label', 'sign', 'intro', 'office_id', 'qualification_id', 'score', 'service_persons', 'eva_num', 'service_days', 'phone_minutes', 'chat_price', 'phone_price', 'appoint_price', 'is_chat', 'is_phone', 'is_appoint', 'latitude', 'longitude', DB::raw($distance_field)])->where('status', 1);
         $builder = Docter::with('office', 'qualification')->select(['id', 'type', 'name', 'phone', 'sex', 'birthday', 'avatar', 'status', 'label', 'sign', 'intro', 'office_id', 'qualification_id', 'score', 'service_persons', 'eva_num', 'service_days', 'phone_minutes', 'chat_price', 'phone_price', 'appoint_price', 'is_chat', 'is_phone', 'is_appoint', 'latitude', 'longitude', DB::raw($distance_field)])->where('status', 1);
         $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
         $list_type = !empty($req['list_type']) ? $req['list_type'] : 0;
@@ -116,9 +115,21 @@ class DocterController extends AuthController
         $this->validate(request(), [
         $this->validate(request(), [
             'docter_id' => 'required|integer',
             'docter_id' => 'required|integer',
             'per_page' => 'integer',
             'per_page' => 'integer',
+            'latitude' => 'numeric',
+            'longitude' => 'numeric',
         ]);
         ]);
+        $user = $this->user;
 
 
-        $data = Schedule::with('schedulePeriod.timePeriod')->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15);
+        $data = Schedule::with(['schedulePeriod.timePeriod', 'schedulePeriod.organization'])->where('docter_id', $req['docter_id'])->where('schedule_day', '>=', date('Ymd'))->paginate($req['per_page']??15)->toArray();
+        if (!empty($data)) {
+            foreach ($data['data'] as $k => &$v) {
+                foreach ($v['schedule_period'] as $k1 => &$v1) {
+                    if (!empty($v1['organization'])) {
+                        $v1['organization']['distance'] = get_user_distance($user, $v1['organization']['latitude'], $v1['organization']['longitude']);
+                    }
+                }
+            }
+        }
 
 
         return out($data);
         return out($data);
     }
     }

+ 5 - 0
app/Models/Schedule.php

@@ -14,4 +14,9 @@ class Schedule extends BaseModel
     {
     {
         return $this->hasMany(SchedulePeriod::class)->select(['id', 'schedule_id', 'organization_id', 'time_period_id', 'order_num']);
         return $this->hasMany(SchedulePeriod::class)->select(['id', 'schedule_id', 'organization_id', 'time_period_id', 'order_num']);
     }
     }
+
+    public function organization()
+    {
+        return $this->belongsTo(Organization::class)->select(['id', 'name', 'address', 'latitude', 'longitude']);
+    }
 }
 }

+ 1 - 1
app/Models/SchedulePeriod.php

@@ -12,7 +12,7 @@ class SchedulePeriod extends BaseModel
 {
 {
     public function organization()
     public function organization()
     {
     {
-        return $this->belongsTo(Organization::class);
+        return $this->belongsTo(Organization::class)->select(['id', 'name', 'address', 'latitude', 'longitude']);
     }
     }
 
 
     public function timePeriod()
     public function timePeriod()