Template.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\admin\controller\ps;
  3. use app\common\controller\Backend;
  4. use app\common\library\ps\PaperSize;
  5. use app\common\library\ps\PrintPlanner;
  6. use app\common\library\ps\TemplateIngest;
  7. use think\Db;
  8. /**
  9. * 流程单-模板
  10. *
  11. * @icon fa fa-file-text-o
  12. */
  13. class Template extends Backend
  14. {
  15. /**
  16. * Template模型对象
  17. * @var \app\admin\model\ps\Template
  18. */
  19. protected $model = null;
  20. protected $relationSearch = true;
  21. protected $searchFields = 'id,name';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\ps\Template;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("sourceList", $this->model->getSourceList());
  28. $this->view->assign("licenseTypeList", $this->model->getLicenseTypeList());
  29. $this->view->assign('paperList', PaperSize::presets());
  30. $this->view->assign('fieldCatalog', PrintPlanner::fieldCatalog());
  31. $factories = [];
  32. try {
  33. $factories = (new \app\admin\model\ps\Factory())
  34. ->where('status', 'normal')
  35. ->order('id desc')
  36. ->column('name', 'id');
  37. } catch (\Exception $e) {
  38. $factories = [];
  39. }
  40. $this->view->assign('factories', $factories);
  41. }
  42. /**
  43. * 上传标准图 + 色块图并识版入库
  44. */
  45. public function add()
  46. {
  47. if (false === $this->request->isPost()) {
  48. return $this->view->fetch();
  49. }
  50. $params = $this->request->post('row/a');
  51. if (empty($params)) {
  52. $this->error(__('Parameter %s can not be empty', ''));
  53. }
  54. try {
  55. $result = TemplateIngest::ingest($params);
  56. } catch (\Exception $e) {
  57. $this->error($e->getMessage());
  58. }
  59. $msg = '已入库,识别到 ' . $result['cells'] . ' 个填写格(' . $result['paper'] . ',图 ' . $result['image'] . ')';
  60. if (!empty($result['unlabeled'])) {
  61. $msg .= ',其中 ' . $result['unlabeled'] . ' 个未读出色块文字,套打时这些格不会填值';
  62. }
  63. $this->success($msg, null, $result);
  64. }
  65. /**
  66. * 识版结果:底图叠加格子,可重新识别
  67. */
  68. public function preview($ids = null)
  69. {
  70. $row = $this->model->get($ids);
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $versionId = (int)$row['current_version_id'];
  75. $schemaJson = '';
  76. if ($versionId > 0) {
  77. $schemaJson = (string)Db::name('ps_template_version')->where('id', $versionId)->value('schema_json');
  78. }
  79. $schema = PrintPlanner::decodeSchema($schemaJson, (string)$row['cover']);
  80. $raw = [];
  81. if (!empty($schema['elements']) && is_array($schema['elements'])) {
  82. $raw = $schema['elements'];
  83. } elseif (!empty($schema['cells']) && is_array($schema['cells'])) {
  84. $raw = $schema['cells'];
  85. }
  86. $cells = [];
  87. $unlabeled = 0;
  88. foreach ($raw as $i => $el) {
  89. if (!is_array($el)) {
  90. continue;
  91. }
  92. $key = trim((string)($el['key'] ?? ''));
  93. if ($key === '') {
  94. $unlabeled++;
  95. }
  96. $cells[] = [
  97. 'n' => $i + 1,
  98. 'id' => (string)($el['id'] ?? ''),
  99. 'label' => (string)($el['label'] ?? $el['text'] ?? ''),
  100. 'key' => $key,
  101. 'x' => round((float)($el['x'] ?? 0), 2),
  102. 'y' => round((float)($el['y'] ?? 0), 2),
  103. 'w' => round((float)($el['w'] ?? 0), 2),
  104. 'h' => round((float)($el['h'] ?? 0), 2),
  105. 'unbound' => $key === '',
  106. ];
  107. }
  108. $this->view->assign('row', $row);
  109. $this->view->assign('cells', $cells);
  110. $this->view->assign('cellCount', count($cells));
  111. $this->view->assign('unlabeled', $unlabeled);
  112. $this->view->assign('cover', (string)$row['cover']);
  113. $this->view->assign('markCover', (string)$row['mark_cover']);
  114. $this->view->assign('paper', trim((string)$row['paper_size'] . ' ' . $row['page_w'] . '×' . $row['page_h'] . 'mm'));
  115. return $this->view->fetch();
  116. }
  117. /**
  118. * 按当前标准图 / 色块图重新识版
  119. */
  120. public function rebuild($ids = null)
  121. {
  122. $ids = $ids ?: $this->request->param('ids');
  123. if (!$ids) {
  124. $this->error(__('Parameter %s can not be empty', 'ids'));
  125. }
  126. try {
  127. $result = TemplateIngest::rebuild($ids);
  128. } catch (\Exception $e) {
  129. $this->error($e->getMessage());
  130. }
  131. $msg = '已重新识别,共 ' . $result['cells'] . ' 个格';
  132. if (!empty($result['unlabeled'])) {
  133. $msg .= ',其中 ' . $result['unlabeled'] . ' 个未绑定字段';
  134. }
  135. $this->success($msg, null, $result);
  136. }
  137. /**
  138. * 查看
  139. */
  140. public function index()
  141. {
  142. $this->request->filter(['strip_tags', 'trim']);
  143. if ($this->request->isAjax()) {
  144. if ($this->request->request('keyField')) {
  145. return $this->selectpage();
  146. }
  147. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  148. $list = $this->model
  149. ->with(['factory'])
  150. ->where($where)
  151. ->order($sort, $order)
  152. ->paginate($limit);
  153. $result = array("total" => $list->total(), "rows" => $list->items());
  154. return json($result);
  155. }
  156. return $this->view->fetch();
  157. }
  158. }