123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace app\models\system;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- class SystemStore extends BaseModel
- {
- const EARTH_RADIUS = 6371;
- use ModelTrait;
-
- protected $pk = 'id';
-
- protected $name = 'system_store';
- public static function getLatlngAttr($value, $data)
- {
- return $data['latitude'] . ',' . $data['longitude'];
- }
- public static function verificWhere()
- {
- return self::where('is_show', 1)->where('is_del', 0);
- }
-
- public static function getStoreDispose($id = 0, $felid = '')
- {
- if ($id)
- $storeInfo = self::verificWhere()->where('id', $id)->find();
- else
- $storeInfo = self::verificWhere()->find();
- if ($storeInfo) {
- $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
- $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
- $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
- $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
- $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
- $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
- if ($felid) return $storeInfo[$felid] ?? '';
- }
- return $storeInfo;
- }
-
-
- public static function returnSquarePoint($lng, $lat, $distance = 200)
- {
- $dlng = 2 * asin(sin($distance / (2 * self::EARTH_RADIUS)) / cos(deg2rad($lat)));
- $dlng = rad2deg($dlng);
- $dlat = rad2deg($distance / self::EARTH_RADIUS);
- return [
- 'left_top' => [
- 'lat' => $lat + $dlat,
- 'lng' => $lng - $dlng,
- ],
- 'right_top' => [
- 'lat' => $lat + $dlat,
- 'lng' => $lng + $dlng
- ],
- 'left_bottom' => [
- 'lat' => $lat - $dlat,
- 'lng' => $lng - $dlng
- ],
- 'right_bottom' => [
- 'lat' => $lat - $dlat,
- 'lng' => $lng + $dlng
- ]
- ];
- }
-
- public static function nearbyWhere($model = null, $latitude = 0, $longitude = 0)
- {
- if (!is_object($model)) {
- $latitude = $model;
- $model = new self();
- $longitude = $latitude;
- }
- $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
- $model->field($field);
- return $model;
- }
-
- public static function distanceSql($latitude, $longitude)
- {
- $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
- return $field;
- }
-
- public static function lst($latitude, $longitude, $page, $limit)
- {
- $model = new self();
- $model = $model->where('is_del', 0);
- $model = $model->where('is_show',1);
- if ($latitude && $longitude) {
- $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
- }
- $list = $model->page((int)$page, (int)$limit)
- ->select()
- ->hidden(['is_show', 'is_del'])
- ->toArray();
- if ($latitude && $longitude) {
- foreach ($list as &$value) {
-
- $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
-
- $value['range'] = bcdiv($value['distance'], 1000, 1);
- }
- }
- return $list;
- }
- }
|