PrintJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\FactoryContext;
  5. use app\common\library\ps\LodopPrinter;
  6. use app\common\library\ps\PaperSize;
  7. use app\common\library\ps\PrintPlanner;
  8. use app\common\library\ps\TemplateLicense;
  9. use app\common\model\ps\PrintJob as PrintJobModel;
  10. use app\common\model\ps\PrintSnapshot;
  11. use app\common\model\ps\Template;
  12. use fast\Random;
  13. use think\Db;
  14. use think\Exception;
  15. /**
  16. * 套打接口(测试环境只预览)
  17. */
  18. class PrintJob extends Api
  19. {
  20. protected $noNeedRight = '*';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. if (!FactoryContext::id()) {
  25. $this->error('账号未绑定工厂');
  26. }
  27. }
  28. /**
  29. * 打印历史
  30. */
  31. public function index()
  32. {
  33. $limit = $this->request->request('limit/d', 10);
  34. $list = (new PrintJobModel())->order('id desc')->paginate($limit);
  35. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  36. }
  37. /**
  38. * 预览套打数据:拆张、工序、填充字段。不锁定单次模板。
  39. */
  40. public function prepare()
  41. {
  42. $templateId = $this->request->param('template_id/d');
  43. $bizType = $this->request->param('biz_type', 'workorder');
  44. $bizId = $this->request->param('biz_id/d', 0);
  45. if (!$templateId) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. if ($bizId <= 0) {
  49. $no = trim((string)$this->request->param('no', $this->request->param('code', '')));
  50. $name = trim((string)$this->request->param('name', ''));
  51. if ($no === '' && $name === '') {
  52. $this->error('请填写工单或产品编号、名称');
  53. }
  54. }
  55. $template = Template::get($templateId);
  56. if (!$template) {
  57. $this->error(__('No Results were found'));
  58. }
  59. try {
  60. TemplateLicense::assertPrintable($template, FactoryContext::id());
  61. $plan = PrintPlanner::prepare($bizType, $bizId, $this->planOptions());
  62. } catch (Exception $e) {
  63. $this->error($e->getMessage());
  64. }
  65. $schemaJson = PrintPlanner::schemaJson($template);
  66. $schema = PrintPlanner::decodeSchema($schemaJson, $template['cover']);
  67. $htmlTpl = $this->request->param('html', '', null);
  68. if (trim((string)$htmlTpl) === '') {
  69. $htmlTpl = PrintPlanner::htmlFromSchema($schemaJson, $template['cover'], false);
  70. }
  71. $pages = PrintPlanner::renderPages($plan, $htmlTpl, $schema);
  72. $lodop = LodopPrinter::buildTask($pages[0]['html'] ?? '', $template['name'], $pages, 1, $this->printPage($template));
  73. $this->success('', [
  74. 'template_id' => (int)$template['id'],
  75. 'template_name' => $template['name'],
  76. 'template_cover' => $template['cover'] ? cdnurl($template['cover'], true) : '',
  77. 'template_version_id' => (int)$template['current_version_id'],
  78. 'schema_json' => PrintPlanner::schemaJson($template),
  79. 'qty_plan' => $plan['qty_plan'],
  80. 'sheet_qty' => $plan['sheet_qty'],
  81. 'plate_total' => $plan['plate_total'] ?? $plan['total_sheets'],
  82. 'sheet_from' => $plan['sheet_from'] ?? 1,
  83. 'sheet_to' => $plan['sheet_to'] ?? $plan['total_sheets'],
  84. 'total_sheets' => $plan['total_sheets'],
  85. 'processes' => $plan['processes'],
  86. 'fill' => $plan['fill'],
  87. 'sheets' => $plan['sheets'],
  88. 'pages' => $pages,
  89. 'lodop' => $lodop,
  90. ]);
  91. }
  92. /**
  93. * 确认套打:按拆张落快照,整单只锁定一次单次授权,返回 Lodop 预览任务
  94. */
  95. public function confirm()
  96. {
  97. $templateId = $this->request->param('template_id/d');
  98. $bizType = $this->request->param('biz_type', 'workorder');
  99. $bizId = $this->request->param('biz_id/d', 0);
  100. $copies = $this->request->param('copies/d', 1);
  101. $htmlTpl = $this->request->param('html', '', null);
  102. if (!$templateId) {
  103. $this->error(__('Invalid parameters'));
  104. }
  105. if ($bizId <= 0) {
  106. $no = trim((string)$this->request->param('no', $this->request->param('code', '')));
  107. $name = trim((string)$this->request->param('name', ''));
  108. if ($no === '' && $name === '') {
  109. $this->error('请填写工单或产品编号、名称');
  110. }
  111. }
  112. $factoryId = FactoryContext::id();
  113. $template = Template::get($templateId);
  114. if (!$template) {
  115. $this->error(__('No Results were found'));
  116. }
  117. $schemaJson = PrintPlanner::schemaJson($template);
  118. $schema = PrintPlanner::decodeSchema($schemaJson, $template['cover']);
  119. if (trim((string)$htmlTpl) === '') {
  120. $htmlTpl = PrintPlanner::htmlFromSchema($schemaJson, $template['cover'], false);
  121. }
  122. try {
  123. TemplateLicense::assertPrintable($template, $factoryId);
  124. $plan = PrintPlanner::prepare($bizType, $bizId, $this->planOptions());
  125. } catch (Exception $e) {
  126. $this->error($e->getMessage());
  127. }
  128. $postedPages = $this->request->param('pages/a');
  129. if (is_array($postedPages) && $postedPages) {
  130. $pages = [];
  131. foreach ($postedPages as $index => $item) {
  132. $html = is_array($item) ? (isset($item['html']) ? $item['html'] : '') : (string)$item;
  133. $sheet = $plan['sheets'][$index] ?? null;
  134. $fill = $sheet ? $sheet['fill'] : $plan['fill'];
  135. $pages[] = [
  136. 'no' => $sheet ? $sheet['no'] : ($index + 1),
  137. 'total' => $plan['total_sheets'],
  138. 'qty' => $sheet ? $sheet['qty'] : $plan['qty_plan'],
  139. 'title' => '流程单 ' . ($sheet ? $sheet['no_text'] : ($index + 1)),
  140. 'html' => $html,
  141. 'fill' => $fill,
  142. 'print_items' => PrintPlanner::isOverlay($schema)
  143. ? PrintPlanner::overlayPrintItems($schema, $fill, $plan['processes'])
  144. : [],
  145. ];
  146. }
  147. } else {
  148. $pages = PrintPlanner::renderPages($plan, $htmlTpl, $schema);
  149. }
  150. $lodop = LodopPrinter::buildTask($pages[0]['html'] ?? '', $template['name'], $pages, $copies, $this->printPage($template));
  151. $printNo = 'P' . date('YmdHis') . Random::numeric(4);
  152. Db::startTrans();
  153. try {
  154. $job = new PrintJobModel();
  155. $job->allowField(true)->save([
  156. 'operator_user_id' => $this->auth->id,
  157. 'print_no' => $printNo,
  158. 'biz_type' => $bizType,
  159. 'biz_id' => $bizId,
  160. 'template_id' => $templateId,
  161. 'template_version_id' => (int)$template['current_version_id'],
  162. 'copies' => $copies > 0 ? $copies : 1,
  163. 'lodop_mode' => $lodop['mode'],
  164. 'status' => 'previewed',
  165. ]);
  166. $snapshot = new PrintSnapshot();
  167. $snapshot->allowField(true)->save([
  168. 'print_job_id' => $job->id,
  169. 'fill_json' => json_encode([
  170. 'qty_plan' => $plan['qty_plan'],
  171. 'sheet_qty' => $plan['sheet_qty'],
  172. 'total_sheets' => $plan['total_sheets'],
  173. 'processes' => $plan['processes'],
  174. 'fill' => $plan['fill'],
  175. 'sheets' => $plan['sheets'],
  176. ], JSON_UNESCAPED_UNICODE),
  177. 'schema_json' => $schemaJson,
  178. 'lodop_html' => json_encode(array_column($pages, 'html'), JSON_UNESCAPED_UNICODE),
  179. ]);
  180. TemplateLicense::consumeOnConfirm($template, $factoryId);
  181. Db::commit();
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. $this->success('', [
  187. 'print_no' => $printNo,
  188. 'total_sheets' => $plan['total_sheets'],
  189. 'lodop' => $lodop,
  190. ]);
  191. }
  192. /**
  193. * 删除本厂打印记录
  194. */
  195. public function del()
  196. {
  197. $ids = $this->request->post('id');
  198. if (!$ids) {
  199. $this->error(__('Parameter %s can not be empty', 'id'));
  200. }
  201. $list = (new PrintJobModel())->where('id', 'in', $ids)->select();
  202. foreach ($list as $row) {
  203. PrintSnapshot::where('print_job_id', $row->id)->delete();
  204. $row->delete();
  205. }
  206. $this->success('已删除');
  207. }
  208. /**
  209. * @return array
  210. */
  211. protected function planOptions()
  212. {
  213. $extra = $this->request->param('extra/a');
  214. $options = [
  215. 'sheet_qty' => $this->request->param('sheet_qty', $this->request->param('tldx', 0)),
  216. 'no' => $this->request->param('no', ''),
  217. 'code' => $this->request->param('code', ''),
  218. 'name' => $this->request->param('name', ''),
  219. 'sheet_from' => $this->request->param('sheet_from/d', 0),
  220. 'sheet_to' => $this->request->param('sheet_to/d', 0),
  221. 'process_string' => $this->request->param('process_string', $this->request->param('string', '')),
  222. 'extra' => is_array($extra) ? $extra : [],
  223. ];
  224. $rawProcesses = $this->request->param('processes/a');
  225. if ($rawProcesses !== null) {
  226. $options['processes'] = is_array($rawProcesses) ? $rawProcesses : [];
  227. }
  228. $qty = $this->request->param('qty', null);
  229. if ($qty === null || $qty === '') {
  230. $qty = $this->request->param('num', null);
  231. }
  232. if ($qty !== null && $qty !== '') {
  233. $options['qty'] = $qty;
  234. }
  235. return $options;
  236. }
  237. /**
  238. * @param Template $template
  239. * @return array
  240. */
  241. protected function printPage($template)
  242. {
  243. $schema = PrintPlanner::decodeSchema(PrintPlanner::schemaJson($template), $template['cover']);
  244. $page = isset($schema['page']) && is_array($schema['page']) ? $schema['page'] : [];
  245. $resolved = PaperSize::resolve(
  246. (string)($template['paper_size'] ?: ($page['paper_size'] ?? 'A4')),
  247. (float)($template['page_w'] ?: ($page['w'] ?? 0)),
  248. (float)($template['page_h'] ?: ($page['h'] ?? 0))
  249. );
  250. $page['w'] = $resolved['w'];
  251. $page['h'] = $resolved['h'];
  252. $page['paper_size'] = $resolved['name'];
  253. $cover = (string)($schema['background'] ?? $template['cover'] ?? '');
  254. $page['background'] = $cover !== '' ? PrintPlanner::printImageSrc($cover) : '';
  255. return $page;
  256. }
  257. }