标签搜索
PHP

倾斜缸角度API接口

wehg489
2025-08-08 / 0 评论 / 2 阅读 / 正在检测是否收录...

API接口

<?php
require '../vendor/autoload.php';
header('Content-Type: application/json');

use PhpOffice\PhpSpreadsheet\IOFactory;

// 获取JSON格式的输入参数
$json = file_get_contents('php://input');
$requestData = json_decode($json, true);

$input1 = $requestData['input1'] ?? '';
$input2 = $requestData['input2'] ?? '';
$combined = $input1 . $input2;

// 验证必填参数
if (empty($input1) || empty($input2)) {
    http_response_code(400);
    echo json_encode(['error' => 'input1和input2参数不能为空']);
    exit;
}

try {
    // 加载Excel文件
    $spreadsheet = IOFactory::load('1.xlsx');
    $sheet = $spreadsheet->getSheetByName('Sheet1');
    $data = $sheet->toArray();
    
    // 移除标题行
    array_shift($data);
    
    // 解析数据
    $excelData = [];
    foreach ($data as $row) {
        if (count($row) >= 4) {
            $excelData[] = [
                'model' => $row[0],
                'spec' => $row[1],
                'single' => $row[2],
                'double' => $row[3]
            ];
        }
    }
    
    // 匹配逻辑
    $hasDouble = (strpos($combined, '双胎') !== false);
    $matched = false;
    $matchDetails = [];
    $bz = '5吨以下默认单胎,除非规格描述里包含双胎配置';
    
    foreach ($excelData as $row) {
        // 解析车型信息
        preg_match('/([A-Za-z]+)(\\d+)(?:-(\\d+))?/', $row['model'], $modelParts);
        $powerType = $modelParts[1] ?? '';
        $minTon = isset($modelParts[3]) ? (int)$modelParts[2] : (int)$modelParts[2];
        $maxTon = isset($modelParts[3]) ? (int)$modelParts[3] : (int)$modelParts[2];
        
        // 检查动力类型匹配
        $powerMatch = (stripos($combined, $powerType) !== false;
        
        // 提取吨位数字
        preg_match_all('/\\d+/', $combined, $tonMatches);
        $tonInRange = false;
        foreach ($tonMatches[0] as $ton) {
            $ton = (int)$ton;
            if ($ton >= $minTon && $ton <= $maxTon) {
                $tonInRange = true;
                if($ton>50){
                    $hasDouble = true;
                    $bz = "监测到吨位大于5.0,默认配置双胎";
                }
                else if($ton == 50 && strpos($combined, 'TF') !== false){
                    $hasDouble = false;
                    $bz = "监测到50TF,默认配置单胎";
                }
                else if($ton == 50 && strpos($combined, 'TF') === false) {  // 修正此处逻辑
                    $hasDouble = true;
                    $bz = "监测到吨位等于5.0,且不含TF,默认配置双胎";
                }
                break;
            }
        }
        
        // 检查门架规格匹配
        $specMatch = (stripos($combined, $row['spec']) !== false);
        
        // 完整匹配条件
        if ($powerMatch && $tonInRange && $specMatch) {
            $matched = true;
            $matchDetails = [
                'model' => $row['model'],
                'spec' => $row['spec'],
                'angle' => $hasDouble ? $row['double'] : $row['single'],
                'powerType' => $powerType,
                'tonRange' => $minTon == $maxTon ? $minTon : "{$minTon}-{$maxTon}",
                'powerMatch' => $powerMatch,
                'tonMatch' => $tonInRange,
                'specMatch' => $specMatch,
                'hasDouble' => $hasDouble,
                'note' => $bz
            ];
            break;
        }
    }
    
    if ($matched) {
        echo json_encode([
            'status' => 'success',
            'data' => $matchDetails
        ]);
    } else {
        http_response_code(404);
        echo json_encode([
            'status' => 'error',
            'message' => '未找到匹配数据'
        ]);
    }
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'status' => 'error',
        'message' => '处理Excel文件时出错: ' . $e->getMessage()
    ]);
}
?>

调用方式

document.addEventListener('DOMContentLoaded', function() {
    const submitBtn = document.getElementById('matchBtn');
    if (!submitBtn) {
        console.error('Submit button not found');
        return;
    }

    submitBtn.addEventListener('click', function() {
        const input1 = document.getElementById('productDesc')?.value.trim();
        const input2 = document.getElementById('spec')?.value.trim();
        
        if (!input1 || !input2) {
            alert('请填写完整表单内容');
            return;
        }

        fetch('./api/tiltapi.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ input1, input2 })
        })
        .then(response => {
            if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
            return response.json();
        })
        .then(data => console.log(data))
        .catch(error => console.error('Error:', error));
    });
});
0

评论

博主关闭了当前页面的评论
歌曲封面
0:00