更新 app/api/controller/IndexController.php

修复数组判断
This commit is contained in:
mkm 2023-12-20 18:04:50 +08:00
parent 8f1b8ad26a
commit 8bf95a9a21
1 changed files with 171 additions and 166 deletions

View File

@ -1,167 +1,172 @@
<?php <?php
namespace app\api\controller; namespace app\api\controller;
use app\common\enum\notice\NoticeEnum; use app\common\enum\notice\NoticeEnum;
use app\common\model\action\Action; use app\common\model\action\Action;
use app\common\model\device\Device; use app\common\model\device\Device;
use app\common\model\device\MonitorThreshold; use app\common\model\device\MonitorThreshold;
use app\common\model\land\Land; use app\common\model\land\Land;
use app\common\model\land\LandProduct; use app\common\model\land\LandProduct;
use app\common\model\LandCollection; use app\common\model\LandCollection;
use app\common\model\monitor\MonitorData; use app\common\model\monitor\MonitorData;
use app\common\model\plant\Plant; use app\common\model\plant\Plant;
use app\common\model\product\ProductDevice; use app\common\model\product\ProductDevice;
use think\facade\Log; use think\facade\Log;
use think\response\Json; use think\response\Json;
/** /**
* index * index
* Class IndexController * Class IndexController
* @package app\api\controller * @package app\api\controller
*/ */
class IndexController extends BaseApiController class IndexController extends BaseApiController
{ {
public array $notNeedLogin = ['code','suYuan']; public array $notNeedLogin = ['code','suYuan'];
//溯源首页 //溯源首页
public function index(): Json public function index(): Json
{ {
$params = $this->request->get(['land_id']); $params = $this->request->get(['land_id']);
//获取土地信息 //获取土地信息
if(isset($params['land_id']) && $params['land_id'] !=''){ if(isset($params['land_id']) && $params['land_id'] !=''){
$data = Land::where('user_id',$this->userId)->where('id',$params['land_id'])->order('id desc')->findOrEmpty()->toArray(); $data = Land::where('user_id',$this->userId)->where('id',$params['land_id'])->order('id desc')->findOrEmpty()->toArray();
}else{ }else{
$data = Land::where('user_id',$this->userId)->order('id desc')->findOrEmpty()->toArray(); $data = Land::where('user_id',$this->userId)->order('id desc')->findOrEmpty()->toArray();
} }
if(empty($data)){ if(empty($data)){
return $this->success('请求成功',[]); return $this->success('请求成功',[]);
} }
$data['pic'] = json_decode($data['pic'],true); $data['pic'] = json_decode($data['pic'],true);
//获取绑定产品 //获取绑定产品
$landProduct = LandProduct::where('land_id',$data['id'])->findOrEmpty(); $landProduct = LandProduct::where('land_id',$data['id'])->findOrEmpty();
if($landProduct->isEmpty()){ if($landProduct->isEmpty()){
$data['monitor'] = []; $data['monitor'] = [];
}else{ }else{
$landCollection = LandCollection::where('land_id',$params['land_id'])->order('id desc')->findOrEmpty(); $landCollection = LandCollection::where('land_id',$params['land_id'])->order('id desc')->findOrEmpty();
if($landCollection->isEmpty()){ if($landCollection->isEmpty()){
$data['monitor'] = []; $data['monitor'] = [];
}else{ }else{
$data['monitor']['soil_monitor_data'] = [ $data['monitor']['soil_monitor_data'] = [
'soil_temperature'=>$landCollection['soil_temperature'], 'soil_temperature'=>$landCollection['soil_temperature'],
'soil_moisture'=>$landCollection['soil_moisture'], 'soil_moisture'=>$landCollection['soil_moisture'],
'conductivity'=>$landCollection['soil_conductivity'], 'conductivity'=>$landCollection['soil_conductivity'],
'ph'=>$landCollection['soil_PH'], 'ph'=>$landCollection['soil_PH'],
'n_content'=>$landCollection['soil_potassium_phosphate_nitrogen'], 'n_content'=>$landCollection['soil_potassium_phosphate_nitrogen'],
'p_content'=>$landCollection['soil_potassium_phosphate_phosphorus'], 'p_content'=>$landCollection['soil_potassium_phosphate_phosphorus'],
'k_content'=>$landCollection['soil_potassium_phosphate_potassium'], 'k_content'=>$landCollection['soil_potassium_phosphate_potassium'],
'create_time' => $landCollection['create_time'] 'create_time' => $landCollection['create_time']
]; ];
$data['monitor']['air_monitor_data'] = [ $data['monitor']['air_monitor_data'] = [
'wind_direction'=>$landCollection['wind_direction'], 'wind_direction'=>$landCollection['wind_direction'],
'wind_speed'=>$landCollection['wind_speed'], 'wind_speed'=>$landCollection['wind_speed'],
'air_temperature'=>$landCollection['ambient_temperature'], 'air_temperature'=>$landCollection['ambient_temperature'],
'air_moisture'=>$landCollection['ambient_humidity'], 'air_moisture'=>$landCollection['ambient_humidity'],
'co2_content'=>$landCollection['carbon_dioxide'], 'co2_content'=>$landCollection['carbon_dioxide'],
'pressure'=>$landCollection['ambient_air_pressure'], 'pressure'=>$landCollection['ambient_air_pressure'],
'rainfall'=>$landCollection['rainfall'], 'rainfall'=>$landCollection['rainfall'],
'light_intensity'=>$landCollection['ambient_lighting'], 'light_intensity'=>$landCollection['ambient_lighting'],
'create_time' => $landCollection['create_time'] 'create_time' => $landCollection['create_time']
]; ];
$monitorThreshold = MonitorThreshold::select()->toArray()[0]; $monitorThreshold = MonitorThreshold::select();
$monitorThreshold['ambient_air_pressure_max'] = 120; if($monitorThreshold){
$monitorThreshold['ambient_air_pressure_min'] = 10; $monitorThreshold=$monitorThreshold->toArray()[0]
$data['monitor']['threshold'] = $monitorThreshold; }else{
} $monitorThreshold=[];
} }
return $this->success('请求成功',$data); $monitorThreshold['ambient_air_pressure_max'] = 120;
} $monitorThreshold['ambient_air_pressure_min'] = 10;
$data['monitor']['threshold'] = $monitorThreshold;
//视频监控 }
public function video(): Json }
{ return $this->success('请求成功',$data);
$params = $this->request->get(['land_id']); }
if(empty($params['land_id'])){
return $this->fail('参数错误'); //视频监控
} public function video(): Json
//获取土地绑定的产品 {
$landProduct = LandProduct::where('land_id',$params['land_id'])->findOrEmpty(); $params = $this->request->get(['land_id']);
if($landProduct->isEmpty()){ if(empty($params['land_id'])){
return $this->fail('当前土地未绑定设备产品'); return $this->fail('参数错误');
} }
//获取监控设备 //获取土地绑定的产品
$productDevice = ProductDevice::where('product_id',$landProduct['product_id'])->where('device_type',3)->column('device_id'); $landProduct = LandProduct::where('land_id',$params['land_id'])->findOrEmpty();
if(empty($productDevice)){ if($landProduct->isEmpty()){
return $this->fail('当前土地未绑定监控设备'); return $this->fail('当前土地未绑定设备产品');
} }
//获取设备编号 //获取监控设备
$device = Device::where('id','in',$productDevice)->select(); $productDevice = ProductDevice::where('product_id',$landProduct['product_id'])->where('device_type',3)->column('device_id');
return $this->success('请求成功',$device->toArray()); if(empty($productDevice)){
} return $this->fail('当前土地未绑定监控设备');
}
// 获取短信验证码 //获取设备编号
public function code(): Json $device = Device::where('id','in',$productDevice)->select();
{ return $this->success('请求成功',$device->toArray());
//验证请求方式 }
if(!$this->request->isPost()){
return $this->fail('请求方式错误'); // 获取短信验证码
} public function code(): Json
//获取参数 {
$params = $this->request->post(['phone','scene']); //验证请求方式
if(empty($params['phone']) || empty($params['scene'])){ if(!$this->request->isPost()){
return $this->fail('缺少必要参数'); return $this->fail('请求方式错误');
} }
if(!in_array($params['scene'],[NoticeEnum::LOGIN_CAPTCHA,NoticeEnum::BIND_MOBILE_CAPTCHA,NoticeEnum::CHANGE_MOBILE_CAPTCHA,NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA])){ //获取参数
return $this->fail('短信场景错误'); $params = $this->request->post(['phone','scene']);
} if(empty($params['phone']) || empty($params['scene'])){
//发送短信 return $this->fail('缺少必要参数');
try { }
$result = event('Notice', [ if(!in_array($params['scene'],[NoticeEnum::LOGIN_CAPTCHA,NoticeEnum::BIND_MOBILE_CAPTCHA,NoticeEnum::CHANGE_MOBILE_CAPTCHA,NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA])){
'scene_id' => $params['scene'], return $this->fail('短信场景错误');
'params' => [ }
'mobile' => $params['phone'], //发送短信
'code' => mt_rand(100000, 999999), try {
] $result = event('Notice', [
]); 'scene_id' => $params['scene'],
return $this->success($result[0]); 'params' => [
}catch(\Exception $e){ 'mobile' => $params['phone'],
//记录日志 'code' => mt_rand(100000, 999999),
Log::error($e->getMessage()); ]
return $this->fail($e->getMessage()); ]);
} return $this->success($result[0]);
} }catch(\Exception $e){
//记录日志
public function suYuan(): Json Log::error($e->getMessage());
{ return $this->fail($e->getMessage());
$params = $this->request->get(['plant_id']); }
if(empty($params['plant_id'])){ }
return $this->fail('参数错误');
} public function suYuan(): Json
$plantInfo = Plant::where('id',$params['plant_id'])->findOrEmpty(); {
if($plantInfo->isEmpty()){ $params = $this->request->get(['plant_id']);
return $this->fail('种植信息错误'); if(empty($params['plant_id'])){
} return $this->fail('参数错误');
if($plantInfo['status'] != 2){ }
return $this->fail('种植信息状态错误'); $plantInfo = Plant::where('id',$params['plant_id'])->findOrEmpty();
} if($plantInfo->isEmpty()){
$landInfo = Land::where('id',$plantInfo['land_id'])->findOrEmpty(); return $this->fail('种植信息错误');
if($landInfo->isEmpty()){ }
return $this->fail('土地信息错误'); if($plantInfo['status'] != 2){
} return $this->fail('种植信息状态错误');
$plantInfo['pic'] = json_decode($plantInfo['pic'],true); }
$plantInfo['group_day'] = floor(($plantInfo['harvest_date'] - $plantInfo['plant_date']) / 86400); $landInfo = Land::where('id',$plantInfo['land_id'])->findOrEmpty();
$plantInfo['plant_date'] = date('Y-m-d',$plantInfo['plant_date']); if($landInfo->isEmpty()){
$plantInfo['harvest_date'] = date('Y-m-d',$plantInfo['harvest_date']); return $this->fail('土地信息错误');
$plantInfo['land_name'] = $landInfo['title']; }
$plantInfo['land_area'] = $landInfo['total_area']; $plantInfo['pic'] = json_decode($plantInfo['pic'],true);
$plantInfo['land_address'] = $landInfo['province_name'].$landInfo['city_name'].$landInfo['county_name'].$landInfo['town_name'].$landInfo['village_name'].$landInfo['group_name']; $plantInfo['group_day'] = floor(($plantInfo['harvest_date'] - $plantInfo['plant_date']) / 86400);
//获取操作 $plantInfo['plant_date'] = date('Y-m-d',$plantInfo['plant_date']);
$action = Action::field('type,type_text,detail,create_time')->where('plant_id',$params['plant_id'])->select()->each(function($item){ $plantInfo['harvest_date'] = date('Y-m-d',$plantInfo['harvest_date']);
$item['detail'] = json_decode($item['detail'],true); $plantInfo['land_name'] = $landInfo['title'];
return $item; $plantInfo['land_area'] = $landInfo['total_area'];
})->toArray(); $plantInfo['land_address'] = $landInfo['province_name'].$landInfo['city_name'].$landInfo['county_name'].$landInfo['town_name'].$landInfo['village_name'].$landInfo['group_name'];
$plantInfo['actions'] = $action; //获取操作
return $this->success('请求成功',$plantInfo->toArray()); $action = Action::field('type,type_text,detail,create_time')->where('plant_id',$params['plant_id'])->select()->each(function($item){
} $item['detail'] = json_decode($item['detail'],true);
return $item;
})->toArray();
$plantInfo['actions'] = $action;
return $this->success('请求成功',$plantInfo->toArray());
}
} }