cxfoot/modules/api/models/MapForm.php
2023-10-24 14:54:18 +08:00

186 lines
6.2 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\models\Order;
use app\models\OrderDetail;
use app\modules\api\components\Mqtt;
use function AlibabaCloud\Client\value;
use app\components\FlashStorage;
use app\components\SiteHelper;
use app\models\BallCart;
use app\models\BallMark;
use app\models\Coach;
use app\models\DeviceUniqueBindUser;
use app\models\Store;
use app\models\User;
use app\components\auth\AToken;
use app\components\EncryptHelper;
use app\modules\api\components\ApiHelper;
use app\modules\api\components\GetDistance;
use yii\data\Pagination;
class MapForm extends ApiModel
{
public $lat;
public $lng;
public $user_type;
public $user_id;
public function rules()
{
return [
[['lat','lng'], 'string'],
];
}
public function index()
{
$store = Store::find()->select('name,area')->where(['is_delete' => 0,'status' => 1])->asArray()->all();
if(empty($store)){
return $this->apiReturnSuccess();
}
$areas = [];
$dots = [];
foreach ($store as $index => $item){
if(empty($item['area'])){
continue;
}
$area = json_decode($item['area'],true);
$store_region = $area['store']['paths'];
$store_lats = array_column($store_region,'lat');
arsort($store_lats);
$store_region2 = [];
foreach ($store_region as &$item2){
array_push($store_region2,array_values($item2));
}
$store_centre = self::GetCenterFromDegrees($store_region2);
array_push($dots,['title' => $item['name'],'lat' => $store_lats[0],'lng' => $store_centre[1]]);
array_push($areas,$store_region);
}
$data['areas'] = $areas;
$data['dots'] = $dots;
return $this->apiReturnSuccess('ok',$data);
}
public function ball()
{
// $data = [
// ['id' => '0300','title' => '0300','latitude' => '24.468126','longitude' => '118.166156'],
// ['id' => '0200','title' => '0200','latitude' => '24.479394','longitude' => '118.17355'],
// ['id' => '0201','title' => '0201','latitude' => '24.508341','longitude' => '118.115605'],
// ['id' => '20221000860','title' => '11111','latitude' => '24.470787','longitude' => '118.109243'],
// ];
// return $this->apiReturnSuccess('ok',$data);
$ball_numbers = BallCart::find()->where(['is_delete' => 0])->andWhere([
'in','status',[0,1]
])->select('*')->asArray()->all();
if(empty($ball_numbers)){
return $this->apiReturnSuccess();
}
//门店管理员
if ($this->user_type == 2) {
$store_user_info = ApiHelper::findOneUserStoreId($this->user_id);
$user_store_id = isset($store_user_info['store_id']) ? $store_user_info['store_id'] : 0;
}
$data = [];
$redis = \Yii::$app->redis;
foreach ($ball_numbers as $item){
$ball_number = $item['ball_number'];
$key_detail = 'api:cxaibc:mqtt:data:'.$ball_number.'_Detail';
$detail = $redis->get($key_detail);
// TODO 测试
if(!$detail){
continue;
}
$detail = json_decode($detail, true);
$ELEC_PERCENT = json_decode($detail['data'], true)['ELEC_PERCENT'];
if(empty($ELEC_PERCENT)){
continue;
}
$key = 'api:cxaibc:mqtt:data:'.$ball_number.'_GPS';
if ($res = $redis->get($key)) {
$res_data = json_decode(json_decode($res, true)['data'], true);
$LAT = $res_data['LAT'];
$LON= $res_data['LON'];
if($LAT >90 || $LAT < -90){
$LAT = -1;
}
if($LON >180 || $LON < -180){
$LON = -1;
}
$arr = ['id' => (string) $ball_number,'title' => (string) $ball_number,'latitude' => (string) $LAT,'longitude' => (string) $LON];
if(intval($item['status']) === 1){
// 正在租赁中
$find_order = Order::find()->alias('o')
->join('inner join', ['od' => OrderDetail::tableName()], 'o.id=od.order_id')
->andWhere([
'od.goods_id' => $ball_number,
'o.is_delete' => 0,
'od.is_delete' => 0,
])->andWhere([
'in', 'o.status', [1, 2]
])->select('o.id,o.store_id,o.user_id')->asArray()->one();
// 能否进入控制台,是否为当前车辆的购买者,是否为当前车辆的管理者
if (!empty($find_order) && ($find_order['user_id'] == $this->user_id || $find_order['store_id'] == $user_store_id)) {
}else{
// 跳过,不让在地图上显示
continue;
}
}
array_push($data,$arr);
}
}
return $this->apiReturnSuccess('ok',$data);
}
public function GetCenterFromDegrees($data = [])
{
if (!is_array($data)) return false;
$num_coords = count($data);
$X = 0.0;
$Y = 0.0;
$Z = 0.0;
foreach ($data as $coord)
{
$lat = deg2rad($coord[0]) ;
$lon = deg2rad($coord[1]) ;
$a = cos($lat) * cos($lon);
$b = cos($lat) * sin($lon);
$c = sin($lat);
$X += $a;
$Y += $b;
$Z += $c;
}
$X /= $num_coords;
$Y /= $num_coords;
$Z /= $num_coords;
$lon = atan2($Y, $X);
$hyp = sqrt($X * $X + $Y * $Y);
$lat = atan2($Z, $hyp);
return array( rad2deg($lat) , rad2deg($lon));
}
}