'app_account_number',//账号表 // 'juese'=>'app_user_data',//角色表 ]; protected $foodBarcodes = [ '8410600224517', // 西班牙食品格式示例 '8715700448235', // 荷兰食品格式示例 '0612345678952', // 美国UPC+校验位示例 '5410188034916', // 法国食品格式示例 '3017620424503', // 法国食品格式示例 '5060292330079', // 英国食品格式示例 '8017547352918', // 意大利食品格式示例 '5901234123457', // 波兰食品格式示例 '4005500318356', // 德国食品格式示例 '8076809514293', // 意大利食品格式示例 '7290100849635', // 以色列食品格式示例 '8720192037514', // 荷兰食品格式示例 '3345678901238', // 法国食品格式示例 '8431876543219', // 西班牙食品格式示例 '7612345678904', // 瑞士食品格式示例 '8851027304852', // 泰国食品格式示例 '5027612846195', // 英国食品格式示例 '6937268913452', // 中国(出口)食品示例 '4891234567895', // 香港食品格式示例 '8801234567898' // 韩国食品格式示例 ]; // 加 bcadd(,,20) // 减 bcsub(,,20) // 乘 bcmul(,,20) // 除 bcdiv(,,20) ################################################################接口################################################################ ################################################################接口################################################################ ################################################################接口################################################################ // 获取查询页页面导航食材列表 public function search_food_barcode($data=['barcode'=>'034000405978']){ // 尝试捕获异常 // try { // if(count(input('post.')) > 0){ // $data = input('post.'); // } $data = input(''); if(!array_key_exists('barcode', $data)){ return $this->msg(10001,'barcode is miss'); } if(!$this->verify_data_is_ok($data['barcode'],'intnum')){ return $this->msg(10005,'barcode type is error'); } $return_data = $this->search_food_barcode_action($data); // return $return_data; // } catch (\Exception $e) { // // 捕获异常 // $logContent["flie"] = $e->getFile(); // $logContent["line"] = $e->getLine(); // $logContent['all_content'] = "异常信息:\n"; // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; // // 记录日志 // $this->record_api_log($data, $logContent, null); // return json(['status' => 'error', 'message' => '系统错误']); // } } #######################################################################action####################################################################### #######################################################################action####################################################################### #######################################################################action####################################################################### public function search_food_barcode_action($data){ $food_data = $this->getOpenFoodFactsProduct($data['barcode']); // dump($food_data); // die; // return $this->msg($food_data); if($food_data['error'] == false){ $return_data = "
商品名字:".$food_data['data']['product_name']."
"; $return_data = $return_data."
商品大图:
"; if(array_key_exists('image_nutrition_url',$food_data['data'])){ $return_data = $return_data."
商品营养图:
"; }else if(array_key_exists('image_ingredients_url',$food_data['data'])){ $return_data = $return_data."
商品营养图:
"; } $return_data = $return_data."
商品营养元素:
"; foreach ($food_data['data']['nutriments'] as $key => $value) { if (strpos($key, '_unit') !== false || strpos($key, '_value') !== false) { $return_data = $return_data."
".$key.":".$value."
"; } // if($key == 'energy-kcal_unit'){ // $return_data = $return_data."
能量单位:".$food_data['data']['nutriments']['energy-kcal_unit']."
"; // }else if($key == 'energy-kcal_value'){ // $return_data = $return_data."
能量值:".$food_data['data']['nutriments']['energy-kcal_value']."
"; // }else if($key == 'carbohydrates_unit'){ // $return_data = $return_data."
碳水化合物单位:".$food_data['data']['nutriments']['carbohydrates_unit']."
"; // }else if($key == 'carbohydrates_value'){ // $return_data = $return_data."
碳水化合物值:".$food_data['data']['nutriments']['carbohydrates_value']."
"; // }else if($key == 'fat_unit'){ // $return_data = $return_data."
脂肪单位:".$food_data['data']['nutriments']['fat_unit']."
"; // }else if($key == 'fat_value'){ // $return_data = $return_data."
脂肪值:".$food_data['data']['nutriments']['fat_value']."
"; // }else if($key == 'energy-kcal_value'){ // $return_data = $return_data."
能量单位:".$food_data['data']['nutriments']['energy-kcal_value']."
"; // }else if($key == 'energy-kcal_value'){ // $return_data = $return_data."
能量单位:".$food_data['data']['nutriments']['energy-kcal_value']."
"; // }else if($key == 'energy-kcal_value'){ // $return_data = $return_data."
能量单位:".$food_data['data']['nutriments']['energy-kcal_value']."
"; // }else if($key == 'energy-kcal_value'){ // $return_data = $return_data."
能量单位:".$food_data['data']['nutriments']['energy-kcal_value']."
"; // } } echo $return_data; }else{ echo $food_data['message']; } // dump($food_data); // die; } #######################################################################小工具####################################################################### #######################################################################小工具####################################################################### #######################################################################小工具####################################################################### /** * 查询Open Food Facts API获取商品信息 * @param string $barcode 商品条码 * @param string $countryCode 国家代码,默认全球站(world),可选cn(中国站) * @param int $timeout 超时时间,默认10秒 * @return array */ public function getOpenFoodFactsProduct($barcode, $countryCode = 'world', $timeout = 10) { // 清理条码,只保留数字 $cleanBarcode = preg_replace('/[^0-9]/', '', $barcode); if (empty($cleanBarcode)) { return [ 'error' => true, 'message' => '无效的条码格式', 'http_code' => 0 ]; } // 构建API URL [1](@ref) $baseUrl = "https://{$countryCode}.openfoodfacts.org"; $apiUrl = "{$baseUrl}/api/v0/product/{$cleanBarcode}.json"; // 初始化cURL $ch = curl_init(); // 设置cURL选项 [2,4](@ref) curl_setopt_array($ch, [ CURLOPT_URL => $apiUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, // 设置User-Agent,这是Open Food Facts API的要求 [1](@ref) CURLOPT_USERAGENT => 'KitchenScaleApp/1.0 (PHP-cURL) - Contact: your-email@example.com', CURLOPT_HTTPHEADER => [ 'Accept: application/json', 'Accept-Charset: utf-8' ] ]); // 执行请求 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 错误处理 if ($response === false) { $errorMsg = curl_error($ch); curl_close($ch); return [ 'error' => true, 'message' => "cURL请求失败: {$errorMsg}", 'http_code' => $httpCode ]; } curl_close($ch); // 解析JSON响应 $data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { return [ 'error' => true, 'message' => 'JSON解析失败: ' . json_last_error_msg(), 'http_code' => $httpCode, 'raw_response' => $response ]; } // 检查API返回状态 [1](@ref) if (!isset($data['status']) || $data['status'] !== 1) { return [ 'error' => true, 'message' => '商品未找到或条码无效', 'http_code' => 404, 'api_status' => $data['status'] ?? 'unknown' ]; } // 返回成功结果 return [ 'error' => false, // 'data' => $this->formatProductData($data['product']), 'data' => $data['product'], 'http_code' => $httpCode, 'barcode' => $cleanBarcode ]; } }