--- a/application/p/logic/Equipment.php +++ b/application/p/logic/Equipment.php @@ -188,6 +188,7 @@ public function read($id) { $equipment = $this->model->readById($id); if($equipment) { + $equipment = $this->normalizeEquipmentReadResponse($equipment); $equipment['type_cn'] = empty($this->typeArr[$equipment['type']]) ? '' : $this->typeArr[$equipment['type']]; $equipment['status_cn'] = empty($this->statusArr[$equipment['status']]) ? '' : $this->statusArr[$equipment['status']]; $equipment = $this->appendReceiptPrinterFields($equipment, false); @@ -510,12 +511,34 @@ } public function getCode($type){ + // Source type is signed TINYINT: an outside-domain integer cannot match. + if (ltrim((string)\think\Config::get('database.type'), '\\') === \app\cores\database\Dm::class + && $this->isVersionTypeOutsideTinyInt($type)) { + return ['code' => 1]; + } $code = Db::name('equipment_version')->where(['type' => $type])->field('code')->order(['code'=>'desc'])->find(); if (empty($code)){ return ['code' => 1]; } $code['code'] += 1; return $code; + } + + private function isVersionTypeOutsideTinyInt($value): bool + { + if (!is_int($value) && !is_string($value)) { + return false; + } + $text = (string)$value; + if (!preg_match('/^[+-]?\d+$/D', $text)) { + return false; + } + $negative = isset($text[0]) && $text[0] === '-'; + $digits = ltrim(ltrim($text, '+-'), '0'); + if (strlen($digits) !== 3) { + return strlen($digits) > 3; + } + return strcmp($digits, $negative ? '128' : '127') > 0; } public function addVersion($params, $currentUser){ @@ -633,6 +656,7 @@ $dishess = array_column($dishess, null, 'uuid'); } foreach($equipments as $key=>$equipment) { + $equipments[$key] = $this->normalizeEquipmentReadResponse($equipment); $equipments[$key]['type_cn'] = empty($this->typeArr[$equipment['type']]) ? '' : $this->typeArr[$equipment['type']]; $equipments[$key]['status_cn'] = empty($this->statusArr[$equipment['status']]) ? '' : $this->statusArr[$equipment['status']]; //排菜信息 @@ -644,6 +668,28 @@ ]; } return $equipments; + } + + /** Only proven DECIMAL(10,2) and DATETIME(0) fields on these two reads. */ + private function normalizeEquipmentReadResponse(array $row): array + { + if (ltrim((string)\think\Config::get('database.type'), '\\') !== \app\cores\database\Dm::class) { + return $row; + } + foreach (['left_dishes_weight', 'left_dishes_weight_max', 'right_dishes_weight', 'right_dishes_weight_max'] as $field) { + if (isset($row[$field]) && (is_int($row[$field]) || is_float($row[$field]))) { + $row[$field] = number_format((float)$row[$field], 2, '.', ''); + } + } + foreach (['create_time', 'update_time', 'face_time', 'push_dishes_time'] as $field) { + if (isset($row[$field]) && is_string($row[$field]) + && preg_match('/^((\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}))\.0{1,6}$/D', $row[$field], $match) + && (int)$match[2] >= 1000 && checkdate((int)$match[3], (int)$match[4], (int)$match[2]) + && (int)$match[5] <= 23 && (int)$match[6] <= 59 && (int)$match[7] <= 59) { + $row[$field] = $match[1]; + } + } + return $row; } /** --- a/application/m/logic/Dishes.php +++ b/application/m/logic/Dishes.php @@ -499,10 +499,38 @@ $where['restaurant_id'] = $restaurant_id; $result = $this->recipeModel->getMenuInof($where,"*",'a.id desc'); foreach ($result as $key => $value){ + $result[$key] = $this->normalizeRecipeLookupResponse($value); $result[$key]['file_path'] = getImgFullPath($value['file_path']); $result[$key]['source_file_path'] = getImgFullPath($value['source_file_path']); } return ['code' => 0, 'message' => "获取成功", 'data' =>$result]; + } + + /** This endpoint's MySQL DECIMAL(5,2)/(10,2) and DATETIME(0) contract only. */ + private function normalizeRecipeLookupResponse(array $row): array + { + if (ltrim((string)\think\Config::get('database.type'), '\\') !== \app\cores\database\Dm::class) { + return $row; + } + $fields = ['weight', 'raw_flag_weight', 'energy', 'carbohydrate', 'protein', 'fat', + 'sfa', 'mufa', 'pufa', 'cholesterol', 'va', 'renieratene', 'retinol', 'vb1', 'vb2', + 'vpp', 'vc', 've', 'folic_acid', 'biotin', 'pantothenic_acid', 'choline', 'potassium', + 'sodium', 'calcium', 'magnesium', 'iron', 'zinc', 'selenium', 'gi', 'gl', 'purine', + 'dietary_fiber', 'moisture', 'ash', 'iodine', 'phosphorus', 'cuprum', 'manganese']; + foreach ($fields as $field) { + if (isset($row[$field]) && (is_int($row[$field]) || is_float($row[$field]))) { + $row[$field] = number_format((float)$row[$field], 2, '.', ''); + } + } + foreach (['create_time', 'create_times', 'update_time'] as $field) { + if (isset($row[$field]) && is_string($row[$field]) + && preg_match('/^((\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}))\.0{1,6}$/D', $row[$field], $match) + && (int)$match[2] >= 1000 && checkdate((int)$match[3], (int)$match[4], (int)$match[2]) + && (int)$match[5] <= 23 && (int)$match[6] <= 59 && (int)$match[7] <= 59) { + $row[$field] = $match[1]; + } + } + return $row; } /**