| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\admin\controller\ps;
- use app\common\controller\Backend;
- use app\common\library\ps\PaperSize;
- use app\common\library\ps\PrintPlanner;
- use app\common\library\ps\TemplateIngest;
- use think\Db;
- /**
- * 流程单-模板
- *
- * @icon fa fa-file-text-o
- */
- class Template extends Backend
- {
- /**
- * Template模型对象
- * @var \app\admin\model\ps\Template
- */
- protected $model = null;
- protected $relationSearch = true;
- protected $searchFields = 'id,name';
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|