1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
public function format($data) { $table = []; $res = []; $items = $data['Cells']; foreach ($items as $item) { $table[$item['RowTl']][$item['ColTl']] = $item['Text']; } $data = [ 'sn' => ['序号', 'ID'], 'code' => ['编码', '商品编码', '零件编码', '零件号', '配件编码', '零件编号'], 'name' => ['零件名称', '名称', '零件中文名', '配件名称'], 'num' => ['出库数', '数量'], 'quality' => ['产地', '配件品质', '品质'], 'unit' => ['单位'], 'price' => ['售价', '单价'], 'total_price' => ['金额', '价格', '销售价', '配件价格'], ]; $init = 0; if (!isset($table[0])) { throw new BusinessException(CodeResponse::OCR_ERR); } else { $arr = array_values($table[0]); $intersection = array_intersect($arr, $data['name']); $user = Auth::guard('supplier')->user(); if (!empty($intersection)) { $header = $table[0]; $init = 1; $keys = array_keys($table[1]); $user->table_headers = json_encode($header); $user->save(); } else { $header = json_decode($user->table_headers); if (!$header) { Log::channel('ocr')->error('表头缺失'); throw new BusinessException(CodeResponse::OCR_ERR); } $keys = array_keys($table[0]); } } $initData = array_fill_keys(array_keys($data), ''); for ($i = $init; $i < count($table); $i++) { $row = $table[$i]; $rowData = []; foreach ($keys as $key) { if (isset($header[$key])) { $cellData = $row[$key] ?? ''; foreach ($data as $k => $v) { $header_k = str_replace(' ', '', $header[$key]); if (in_array($header_k, $v)) { $rowData[$k] = $cellData; break; } } } } $rowData = array_merge($initData, $rowData); if ($rowData['name']) { $rowData['sn'] = empty($rowData['sn']) ? count($res) + 1 : $rowData['sn']; $rowData["code"] = str_replace(' ', '', $rowData["code"]); $rowData["total_price"] = preg_replace("/[^0-9.]+/", "", $rowData["total_price"]); $res[] = $rowData; } } return $res; }
|