suyuan/app/api/controller/IndexController.php

170 lines
6.5 KiB
PHP
Raw Normal View History

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