--- a/application/p/logic/DateMenu.php +++ b/application/p/logic/DateMenu.php @@ -1322,6 +1322,7 @@ if (!empty($result)) { foreach ($result as $key => $value) { $result[$key]['file_path'] = getImgFullPath($value['file_path']); + $result[$key] = $this->normalizeDishesLookupResponse($result[$key]); } } } @@ -1672,12 +1673,46 @@ $order = 'b.category asc'; $info = $this->recipesModel->getMenuInof($where, $field, $order); - - + foreach ($info as &$dish) { + $dish = $this->normalizeRecipeInfoResponse($dish); + } + unset($dish); return ['code' => 0, 'message' => '获取信息成功', 'data' => $info]; } + /** Preserve only the proven MySQL narrow-DECIMAL/ROUND text fields. */ + private function normalizeDishesLookupResponse(array $dish): array + { + return $this->normalizeNarrowDecimalResponse($dish, [ + 'weight' => 2, 'power' => 0, 'carbohydrate' => 0, + 'protein' => 0, 'fat' => 0, + ]); + } + + private function normalizeRecipeInfoResponse(array $dish): array + { + $fields = ['weight', 'total_energy', 'energy', 'total_carbohydrate', + 'carbohydrate', 'total_protein', 'protein', 'total_fat', 'fat', + 'total_va', 'total_ve', 'total_dietary_fiber', 'total_retinol', + 'total_vb1', 'total_vb2', 'total_vpp', 'total_sodium', 'total_calcium', + 'total_iron', 'total_vc', 'total_tenieratene', 'total_potassium', + 'total_magnesium', 'total_manganese', 'total_zinc', 'total_cuprum', + 'total_phosphorus', 'total_selenium']; + return $this->normalizeNarrowDecimalResponse($dish, array_fill_keys($fields, 2)); + } + + /** DECIMAL(5,2)/(10,2) only; this does not repair wide driver precision loss. */ + private function normalizeNarrowDecimalResponse(array $row, array $scales): array + { + foreach ($scales as $field => $scale) { + if (isset($row[$field]) && (is_int($row[$field]) || is_float($row[$field]))) { + $row[$field] = number_format((float)$row[$field], $scale, '.', ''); + } + } + return $row; + } + /** * 获取推荐餐单 * @name 柳洪博