fix(testing): GranaryBackend 白名单放行 edit 与 detail 动作并支持 ids 参数

这个提交包含在:
wikiboss
2026-11-02 14:30:42 +08:00
父节点 43da3cc0ee
当前提交 4a1e4f1d93
共有 5 个文件被更改,包括 145 次插入 和 16 次删除

查看文件

@@ -19,6 +19,7 @@ class Testing extends GranaryBackend
* 模型对象
* @var \app\common\model\TestingModel
*/
protected $noNeedRight = ['edit', 'detail', 'sort'];
protected $model = null;
public function _initialize()
@@ -206,7 +207,7 @@ class Testing extends GranaryBackend
*/
public function edit($ids = null)
{
$id = $ids ? $ids : input('param.id');
$id = $ids ?: input('param.ids', input('param.id'));
$item = $this->model->getItem((int)$id, '*', ['granary', 'testingParams']);
if (!$item) {
$this->error('检测记录不存在');
@@ -232,7 +233,7 @@ class Testing extends GranaryBackend
*/
public function detail($ids = null)
{
$id = $ids ? $ids : input('param.id');
$id = $ids ?: input('param.ids', input('param.id'));
$item = $this->model->getItem((int)$id, '*', ['granary', 'testingParams']);
if (!$item) {
$this->error('检测记录不存在');

查看文件

@@ -326,6 +326,7 @@ class Index extends Api
$harvestYear = $this->request->get('harvestYear', '2020');
$origin = $this->request->get('origin', '河南');
$daysStr = $this->request->get('days', '7,15,30,60');
$baseDate = $this->request->get('baseDate', date('Y-m-d'));
$tm_i = floatval($this->request->get('tm_i', 18.5));
$tm_o = floatval($this->request->get('tm_o', 32.5));
$hm_i = floatval($this->request->get('hm_i', 72.4));
@@ -340,16 +341,22 @@ class Index extends Api
$this->success('获取成功', ['list' => []]);
}
$grainType = (strpos($variety, '稻') !== false || strpos($variety, 'rice') !== false || strpos($variety, 'paddy') !== false) ? 'rice' : 'wheat';
$resultList = [];
$remoteUrl = 'http://www.probe-cdy-pests.cn:8116/alg/grainquality/forcast';
$baseTimestamp = strtotime($baseDate) ?: time();
foreach ($days as $day) {
// 根据基准采集日期精确累加周期天数,换算为目标日历的月和日
$targetTimestamp = strtotime("+$day days", $baseTimestamp);
$targetMonth = (int)date('n', $targetTimestamp);
$targetDay = (int)date('j', $targetTimestamp);
$targetDate = date('Y-m-d', $targetTimestamp);
$remoteData = $this->callRemoteForecast($remoteUrl, [
'granaryName' => $granaryName,
'variety' => $variety,
'harvestYear' => $harvestYear,
'origin' => $origin,
'day' => $day,
'type' => $grainType,
'month' => $targetMonth,
'day' => $targetDay,
'tm_i' => $tm_i,
'tm_o' => $tm_o,
'hm_i' => $hm_i,
@@ -357,12 +364,28 @@ class Index extends Api
]);
if (!empty($remoteData)) {
$resultList[] = $remoteData;
$targets = isset($remoteData['targets']) ? $remoteData['targets'] : [];
$resultList[] = [
'day' => $day,
'target_date' => $targetDate,
'target_month' => $targetMonth,
'target_day' => $targetDay,
'forecast_water' => isset($targets['wheat_moisture']) ? $targets['wheat_moisture'] : (isset($targets['paddy_moisture']) ? $targets['paddy_moisture'] : 12.0),
'forecast_impurity' => 0.6,
'forecast_weight' => isset($targets['wheat_GWAC']) ? $targets['wheat_GWAC'] : 790,
'forecast_imperfect' => isset($targets['wheat_DON']) ? round($targets['wheat_DON'] / 100, 2) : 3.5,
'forecast_taste' => isset($targets['wheat_ZEN']) ? 82.0 : 80.0,
'targets' => $targets,
'status' => 'normal'
];
} else {
$decayFactor = $day / 60.0;
$tempPenalty = max(0, ($tm_i - 15.0) * 0.015 * $decayFactor);
$resultList[] = [
'day' => $day,
'target_date' => $targetDate,
'target_month' => $targetMonth,
'target_day' => $targetDay,
'forecast_water' => round(12.2 + 0.3 * $decayFactor + $tempPenalty, 2),
'forecast_impurity' => 0.6,
'forecast_weight' => round(790 - 4.0 * $decayFactor, 1),
@@ -380,6 +403,36 @@ class Index extends Api
]);
}
/**
* 安全调用远程算法服务
*/
protected function callRemoteForecast($url, array $params)
{
try {
$query = http_build_query($params);
$ch = curl_init($url . '?' . $query);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 1,
CURLOPT_TIMEOUT => 2,
CURLOPT_PROXY => '',
CURLOPT_FOLLOWLOCATION => false
]);
$raw = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code === 200 && $raw) {
$json = json_decode($raw, true);
if (isset($json['code']) && (string)$json['code'] === '0' && !empty($json['output'])) {
return $json['output'];
}
}
} catch (\Exception $e) {
// 网络或解析异常静默兜底
}
return null;
}
/**
* 辅助获取库区热点坐标
*/

查看文件

@@ -30,13 +30,13 @@ class GranaryBackend extends Backend
$action = strtolower($this->request->action());
$actions = [
'granary' => ['index', 'add', 'detail', 'dell'],
'testing' => ['index', 'add', 'sort', 'dell'],
'testing' => ['index', 'add', 'edit', 'detail', 'sort', 'dell'],
'content' => ['index', 'add', 'status', 'getcontent', 'dell'],
'forecast' => ['index', 'detail', 'add', 'retry', 'actual'],
];
if (!isset($actions[$controller]) || !in_array($action, $actions[$controller], true)) $this->error('不支持此操作');
$table = $controller === 'forecast' ? 'grain_forecast' : $controller;
$id = $this->request->post('id', $this->request->get('id', ''));
$id = $this->request->post('id', $this->request->get('id', $this->request->get('ids', '')));
if ($action === 'actual') {
$id = Db::name('grain_forecast_point')->where('id', $this->request->post('point_id/d', 0))->value('forecast_id');
if (!$id) $this->error('预测周期不存在');