model = new \app\admin\model\ps\Template; $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("sourceList", $this->model->getSourceList()); $this->view->assign("licenseTypeList", $this->model->getLicenseTypeList()); $this->view->assign('paperList', PaperSize::presets()); $this->view->assign('fieldCatalog', PrintPlanner::fieldCatalog()); $factories = []; try { $factories = (new \app\admin\model\ps\Factory()) ->where('status', 'normal') ->order('id desc') ->column('name', 'id'); } catch (\Exception $e) { $factories = []; } $this->view->assign('factories', $factories); } /** * 上传标准图 + 色块图并识版入库 */ public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } try { $result = TemplateIngest::ingest($params); } catch (\Exception $e) { $this->error($e->getMessage()); } $msg = '已入库,识别到 ' . $result['cells'] . ' 个填写格(' . $result['paper'] . ',图 ' . $result['image'] . ')'; if (!empty($result['unlabeled'])) { $msg .= ',其中 ' . $result['unlabeled'] . ' 个未读出色块文字,套打时这些格不会填值'; } $this->success($msg, null, $result); } /** * 识版结果:底图叠加格子,可重新识别 */ public function preview($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $versionId = (int)$row['current_version_id']; $schemaJson = ''; if ($versionId > 0) { $schemaJson = (string)Db::name('ps_template_version')->where('id', $versionId)->value('schema_json'); } $schema = PrintPlanner::decodeSchema($schemaJson, (string)$row['cover']); $raw = []; if (!empty($schema['elements']) && is_array($schema['elements'])) { $raw = $schema['elements']; } elseif (!empty($schema['cells']) && is_array($schema['cells'])) { $raw = $schema['cells']; } $cells = []; $unlabeled = 0; foreach ($raw as $i => $el) { if (!is_array($el)) { continue; } $key = trim((string)($el['key'] ?? '')); if ($key === '') { $unlabeled++; } $cells[] = [ 'n' => $i + 1, 'id' => (string)($el['id'] ?? ''), 'label' => (string)($el['label'] ?? $el['text'] ?? ''), 'key' => $key, 'x' => round((float)($el['x'] ?? 0), 2), 'y' => round((float)($el['y'] ?? 0), 2), 'w' => round((float)($el['w'] ?? 0), 2), 'h' => round((float)($el['h'] ?? 0), 2), 'unbound' => $key === '', ]; } $this->view->assign('row', $row); $this->view->assign('cells', $cells); $this->view->assign('cellCount', count($cells)); $this->view->assign('unlabeled', $unlabeled); $this->view->assign('cover', (string)$row['cover']); $this->view->assign('markCover', (string)$row['mark_cover']); $this->view->assign('paper', trim((string)$row['paper_size'] . ' ' . $row['page_w'] . '×' . $row['page_h'] . 'mm')); return $this->view->fetch(); } /** * 按当前标准图 / 色块图重新识版 */ public function rebuild($ids = null) { $ids = $ids ?: $this->request->param('ids'); if (!$ids) { $this->error(__('Parameter %s can not be empty', 'ids')); } try { $result = TemplateIngest::rebuild($ids); } catch (\Exception $e) { $this->error($e->getMessage()); } $msg = '已重新识别,共 ' . $result['cells'] . ' 个格'; if (!empty($result['unlabeled'])) { $msg .= ',其中 ' . $result['unlabeled'] . ' 个未绑定字段'; } $this->success($msg, null, $result); } /** * 查看 */ public function index() { $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['factory']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } }