suyuan-breed/app/api/controller/IndexController.php

151 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2024-01-09 15:59:23 +08:00
<?php
namespace app\api\controller;
use app\common\enum\notice\NoticeEnum;
use app\common\logic\RemoteRequestLogic;
2024-01-09 15:59:23 +08:00
use app\common\model\action\Action;
2024-01-31 18:33:53 +08:00
use app\common\model\animal_info\AnimalInfo;
2024-01-09 15:59:23 +08:00
use app\common\model\device\Device;
use app\common\model\device\MonitorThreshold;
2024-01-17 11:59:25 +08:00
use app\common\model\farm\Farm;
use app\common\model\fence_house\FenceHouse;
2024-01-09 15:59:23 +08:00
use app\common\model\file\File;
use app\common\model\land\Land;
use app\common\model\land\LandProduct;
2024-01-17 11:59:25 +08:00
use app\common\model\land\Product;
2024-01-09 15:59:23 +08:00
use app\common\model\LandCollection;
use app\common\model\monitor\MonitorData;
use app\common\model\plant\Plant;
use app\common\model\product\ProductDevice;
2024-01-31 18:33:53 +08:00
use app\common\model\suyuan_operation\AnimalOperateAction;
2024-01-09 15:59:23 +08:00
use think\facade\Log;
use think\response\Json;
/**
* index
* Class IndexController
* @package app\api\controller
*/
class IndexController extends BaseApiController
{
public array $notNeedLogin = ['code','suYuan'];
2024-01-17 11:59:25 +08:00
//首页
2024-01-09 15:59:23 +08:00
public function index(): Json
{
2024-01-17 11:59:25 +08:00
$params = $this->request->get();
//获取栏舍信息
2024-01-25 17:35:07 +08:00
$data = FenceHouse::where('id',$params['fence_house_id'])->order('id desc')->findOrEmpty()->toArray();
2024-01-09 15:59:23 +08:00
if(empty($data)){
return $this->success('请求成功',[]);
}
2024-01-17 11:59:25 +08:00
$product = Product::where('fence_house_id', $data['id'])->findOrEmpty()->toArray();
2024-01-29 09:25:59 +08:00
if(empty($product)){
2024-01-30 09:35:19 +08:00
return $this->fail('栏舍暂未绑定设备',[]);
2024-01-29 09:25:59 +08:00
}
2024-01-17 11:59:25 +08:00
$deviceIds = ProductDevice::where('product_id', $product['id'])->column('device_id');
2024-01-25 17:35:07 +08:00
$device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty();
2024-01-17 11:59:25 +08:00
$data['video_url'] = $device['video_url'];
$data['device_id'] = $device['id'];
// 监控视频封面
$file = File::where('cid', $device['id'])->order('id', 'desc')->findOrEmpty();
$data['video_cover'] = isset($file['uri'])? env('project.project_url').'/'.$file['uri']: '';
//
$data['monitor']['datas'] = RemoteRequestLogic::getMonitorData($deviceIds);
2024-01-17 11:59:25 +08:00
2024-01-17 17:05:16 +08:00
$monitorThreshold = MonitorThreshold::where('fence_house_id', $params['fence_house_id'])->find();
2024-01-17 11:59:25 +08:00
$data['monitor']['threshold'] = $monitorThreshold;
2024-01-09 15:59:23 +08:00
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
{
2024-01-31 18:33:53 +08:00
$params = $this->request->get(['animal_sn']);
$pageNo = $this->request->get('page_no', 1);
$pageSize = $this->request->get('page_size', 5);
if(empty($params['animal_sn'])){
2024-01-09 15:59:23 +08:00
return $this->fail('参数错误');
}
2024-01-31 18:33:53 +08:00
$animalInfo = AnimalInfo::where('sn',$params['animal_sn'])->findOrEmpty();
if($animalInfo->isEmpty()){
return $this->fail('档案信息错误');
2024-01-09 15:59:23 +08:00
}
2024-01-31 18:33:53 +08:00
2024-01-31 18:34:13 +08:00
//获取suyaun
2024-01-31 18:33:53 +08:00
$action = AnimalOperateAction::field('type,type_text,detail,create_time')->where('fence_house_id',$animalInfo['fence_house_id'])->whereOr('animal_info_id', $animalInfo['id'])
->page($pageNo, $pageSize)
->select()->each(function($item){
2024-01-09 15:59:23 +08:00
$item['detail'] = json_decode($item['detail'],true);
return $item;
})->toArray();
2024-01-31 18:33:53 +08:00
$data = [
'lists' => $action,
'count' => AnimalOperateAction::where('fence_house_id',$animalInfo['fence_house_id'])->whereOr('animal_info_id', $animalInfo['id'])->count(),
'page_no' => $pageNo,
'page_size' => $pageSize,
];
return $this->success('请求成功', $data);
2024-01-09 15:59:23 +08:00
}
}