123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * 模板打印控制器
  4. *
  5. * @author qifan
  6. * @date 2020-12-15
  7. */
  8. namespace app\crm\controller;
  9. use app\admin\controller\ApiCommon;
  10. use app\crm\logic\PrintingLogic;
  11. use PhpOffice\PhpWord\IOFactory;
  12. use PhpOffice\PhpWord\PhpWord;
  13. use PhpOffice\PhpWord\Shared\Html;
  14. use think\Controller;
  15. use think\Hook;
  16. use think\Request;
  17. class Printing extends ApiCommon
  18. {
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>['previewData'],
  23. 'allow'=>['printingdata', 'template', 'setrecord', 'getrecord', 'preview', 'down']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. }
  32. /**
  33. * 获取打印的数据
  34. *
  35. * @param PrintingLogic $printingLogic
  36. * @return \think\response\Json
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function printingData(PrintingLogic $printingLogic)
  42. {
  43. $actionId = $this->param['action_id'];
  44. $templateId = $this->param['template_id'];
  45. $type = $this->param['type'];
  46. $recordId = $this->param['record_id'];
  47. $data = $printingLogic->getPrintingData($type, $actionId, $templateId, $recordId);
  48. return resultArray(['data' => $data]);
  49. }
  50. /**
  51. * 获取打印模板列表
  52. *
  53. * @param PrintingLogic $printingLogic
  54. * @return \think\response\Json
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \think\exception\DbException
  58. */
  59. public function template(PrintingLogic $printingLogic)
  60. {
  61. if (empty($this->param['type'])) return resultArray(['error' => '请选择打印的类型!']);
  62. $data = $printingLogic->getTemplateList($this->param['type']);
  63. return resultArray(['data' => $data]);
  64. }
  65. /**
  66. * 创建模板打印记录
  67. *
  68. * @param PrintingLogic $printingLogic
  69. * @return \think\response\Json
  70. */
  71. public function setRecord(PrintingLogic $printingLogic)
  72. {
  73. if (empty($this->param['type'])) return resultArray(['error' => '请选择模块!']);
  74. if (empty($this->param['action_id'])) return resultArray(['error' => '缺少数据ID!']);
  75. if (empty($this->param['template_id'])) return resultArray(['error' => '缺少模板ID!']);
  76. if (empty($this->param['recordContent'])) return resultArray(['error' => '缺少打印内容!']);
  77. $userId = $this->userInfo['id'];
  78. if (!$printingLogic->setRecord($userId, $this->param)) return resultArray(['error' => '操作失败!']);
  79. return resultArray(['data' => '操作成功!']);
  80. }
  81. /**
  82. * 获取打印记录
  83. *
  84. * @param PrintingLogic $printingLogic
  85. * @return \think\response\Json
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function getRecord(PrintingLogic $printingLogic)
  91. {
  92. if (empty($this->param['crmType'])) return resultArray(['error' => '请选择模块!']);
  93. if (empty($this->param['typeId'])) return resultArray(['error' => '缺少数据ID']);
  94. $data = $printingLogic->getRecord($this->param, $this->userInfo['id']);
  95. return resultArray(['data' => $data]);
  96. }
  97. /**
  98. * 保存打印内容
  99. *
  100. * @param user_id 用户id
  101. * @param type 类型(work,pdf)
  102. * @param content 打印内容
  103. * @author fanqi
  104. * @date 2021-03-25
  105. * @return \think\response\Json
  106. */
  107. public function preview(PrintingLogic $printingLogic)
  108. {
  109. if (empty($this->param['type'])) return resultArray(['error' => '缺少类型参数!']);
  110. if (empty($this->param['content'])) return resultArray(['error' => '缺少打印内容!']);
  111. $userInfo = $this->userInfo;
  112. $this->param['user_id'] = $userInfo['id'];
  113. $key = $printingLogic->preview($this->param);
  114. return resultArray(['data' => $key]);
  115. }
  116. /**
  117. * 打下打印文件
  118. * @param string key 打印数据的唯一key
  119. * @author fanqi
  120. * @date 2021-03-26
  121. * @return \think\response\Json
  122. */
  123. public function down()
  124. {
  125. if (empty($this->param['key'])) return resultArray(['error' => '参数错误!']);
  126. $data = db('admin_printing_data')->field(['type', 'content'])->where('key', $this->param['key'])->find();
  127. $type = $data['type'];
  128. $contentArray = json_decode($data['content'], true);
  129. $content = $contentArray['data'];
  130. if ($type == 'pdf') {
  131. require_once(EXTEND_PATH.'tcpdf'.DS.'config'.DS.'tcpdf_config.php');
  132. require_once(EXTEND_PATH.'tcpdf'.DS.'tcpdf.php');
  133. $tcpdf = new \TCPDF();
  134. // 设置PDF页面边距:LEFT,TOP,RIGHT
  135. $tcpdf->SetMargins(10, 10, 10);
  136. // 设置字体,防止中文乱码
  137. $tcpdf->SetFont('simsun', '', 10);
  138. // 设置文件信息
  139. $tcpdf->SetCreator(TITLE_NAME);
  140. $tcpdf->SetAuthor(TITLE_NAME);
  141. $tcpdf->SetTitle("打印文件");
  142. // 删除预定义的打印 页眉/页尾
  143. $tcpdf->setPrintHeader(false);
  144. // 设置文档对齐,间距,字体,图片
  145. $tcpdf->SetCreator(PDF_CREATOR);
  146. $tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  147. $tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  148. // 自动分页
  149. $tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  150. $tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  151. $tcpdf->setFontSubsetting(true);
  152. $tcpdf->setPageMark();
  153. $tcpdf->AddPage();
  154. $html = $content;
  155. $tcpdf->writeHTML($html, true, false, true, true, '');
  156. $tcpdf->lastPage();
  157. $tcpdf->Output('print.PDF','I');
  158. }
  159. if ($type == 'word') {
  160. $fileName = 'print.docx';
  161. header("Cache-Control: no-cache, must-revalidate");
  162. header("Pragma: no-cache");
  163. header("Content-Type: application/octet-stream");
  164. header("Content-Disposition: attachment; filename=$fileName");
  165. header('Transfer-Encoding: chunked');
  166. $html = '<html xmlns:v="urn:schemas-microsoft-com:vml"
  167. xmlns:o="urn:schemas-microsoft-com:office:office"
  168. xmlns:w="urn:schemas-microsoft-com:office:word"
  169. xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
  170. xmlns="http://www.w3.org/TR/REC-html40">';
  171. $html .= '<head><meta charset="UTF-8" /></head>';
  172. echo $html . '<body>'.$content .'</body></html>';
  173. }
  174. }
  175. }