161 lines
5.4 KiB
PHP
161 lines
5.4 KiB
PHP
<?php
|
|
namespace app\api\controller;
|
|
|
|
use app\common\enum\notice\NoticeEnum;
|
|
use app\common\model\action\Action;
|
|
use app\common\model\device\Device;
|
|
use app\common\model\land\Land;
|
|
use app\common\model\land\LandProduct;
|
|
use app\common\model\monitor\MonitorData;
|
|
use app\common\model\plant\Plant;
|
|
use app\common\model\product\ProductDevice;
|
|
use think\facade\Log;
|
|
use think\response\Json;
|
|
|
|
|
|
/**
|
|
* index
|
|
* Class IndexController
|
|
* @package app\api\controller
|
|
*/
|
|
class IndexController extends BaseApiController
|
|
{
|
|
|
|
public array $notNeedLogin = ['code','suYuan'];
|
|
|
|
//溯源首页
|
|
public function index(): Json
|
|
{
|
|
$params = $this->request->get(['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();
|
|
}else{
|
|
$data = Land::where('user_id',$this->userId)->order('id desc')->findOrEmpty()->toArray();
|
|
}
|
|
if(empty($data)){
|
|
return $this->success('请求成功',[]);
|
|
}
|
|
$data['pic'] = json_decode($data['pic'],true);
|
|
//获取绑定产品
|
|
$landProduct = LandProduct::where('land_id',$data['id'])->findOrEmpty();
|
|
if($landProduct->isEmpty()){
|
|
$data['monitor'] = [];
|
|
}else{
|
|
$monitor = MonitorData::where('product_id',$landProduct['product_id'])->order('id desc')->findOrEmpty();
|
|
if($monitor->isEmpty()){
|
|
$data['monitor'] = [];
|
|
}else{
|
|
$data['monitor']['soil_monitor_data'] = [
|
|
'wind_direction'=>$monitor['wind_direction'],
|
|
'wind_speed'=>$monitor['wind_speed'],
|
|
'air_temperature'=>$monitor['air_temperature'],
|
|
'air_moisture'=>$monitor['air_moisture'],
|
|
'co2_content'=>$monitor['co2_content'],
|
|
'pressure'=>$monitor['pressure'],
|
|
'rainfall'=>$monitor['rainfall'],
|
|
'light_intensity'=>$monitor['light_intensity'],
|
|
'create_time' => $monitor['create_time']
|
|
];
|
|
$data['monitor']['air_monitor_data'] = [
|
|
'soil_temperature'=>$monitor['soil_temperature'],
|
|
'soil_moisture'=>$monitor['soil_moisture'],
|
|
'conductivity'=>$monitor['conductivity'],
|
|
'ph'=>$monitor['ph'],
|
|
'n_content'=>$monitor['n_content'],
|
|
'p_content'=>$monitor['p_content'],
|
|
'k_content'=>$monitor['k_content'],
|
|
'create_time' => $monitor['create_time']
|
|
];
|
|
}
|
|
}
|
|
return $this->success('请求成功',$data);
|
|
}
|
|
|
|
//视频监控
|
|
public function video(): Json
|
|
{
|
|
$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('当前土地未绑定设备产品');
|
|
}
|
|
//获取监控设备
|
|
$productDevice = ProductDevice::where('product_id',$landProduct['product_id'])->where('device_type',3)->column('device_id');
|
|
if(empty($productDevice)){
|
|
return $this->fail('当前土地未绑定监控设备');
|
|
}
|
|
//获取设备编号
|
|
$device = Device::where('id','in',$productDevice)->select();
|
|
return $this->success('请求成功',$device->toArray());
|
|
}
|
|
|
|
// 获取短信验证码
|
|
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());
|
|
}
|
|
}
|
|
|
|
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);
|
|
$plantInfo['group_day'] = floor(($plantInfo['harvest_date'] - $plantInfo['plant_date']) / 86400);
|
|
$plantInfo['plant_date'] = date('Y-m-d',$plantInfo['plant_date']);
|
|
$plantInfo['harvest_date'] = date('Y-m-d',$plantInfo['harvest_date']);
|
|
$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'];
|
|
//获取操作
|
|
$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());
|
|
}
|
|
} |