Word.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Word extends Api
  7. {
  8. protected $noNeedLogin = '*';
  9. protected $noNeedRight = '*';
  10. public function download($bach = null){
  11. try{
  12. $data= $this->getWords($bach);
  13. $destination = ROOT_PATH.'public'.DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx";
  14. $destination_path = DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx";
  15. if(!file_exists($data['dist_fule_file'])){
  16. \exception("报告不存在");
  17. }
  18. if(file_exists($destination)){
  19. header("location:".cdnurl($destination_path,true));
  20. }else{
  21. if (copy($data['dist_fule_file'], $destination)) {
  22. header("location:".cdnurl($destination_path,true));
  23. }
  24. }
  25. }catch (Exception $e){
  26. $this->error($e->getMessage());
  27. }
  28. }
  29. public function preview($bach = null){
  30. header("Content-type:text/html; charset=utf-8");//设置当前页面编码为“utf-8”
  31. ini_set("display_errors","On");
  32. error_reporting(E_ALL);
  33. $data= $this->getWords($bach);
  34. header("location:http://vocs.7in6.com/word.php?doc=".$data['dist_file']);
  35. }
  36. protected function initData($bach=null){
  37. if (empty($bach)){
  38. $this->error('参数错误','',401);
  39. }
  40. $where = [];
  41. $where['bach'] = $bach;
  42. $where['delete_time'] = null;
  43. // $where['sample_no'] = array('like','%A%');
  44. $res = Db::name('res')->where($where)->field('entrust_no,name,bach,sell_bach,sample_no,dis,dis_impurity,ben_total,ben,judge,standard_id,create')->find();
  45. if ($res){
  46. $check = Db::name('item_judge_detail')->where('pid',$res['standard_id'])->select();
  47. foreach ($check as $key => $value){
  48. if ($value['params'] == '溶剂残留总量'){
  49. $res['dis_max'] = $value['max'];
  50. }
  51. if ($value['params'] == '溶剂杂质总量'){
  52. $res['dis_impurity_max'] = $value['max'];
  53. }
  54. if ($value['params'] == '苯系物总量'){
  55. $res['ben_total_max'] = $value['max'];
  56. }
  57. if ($value['params'] == '苯含量'){
  58. $res['ben_max'] = $value['max'];
  59. }
  60. }
  61. $res['create'] = date('Y',strtotime($res['create'])).'.'.date('m',strtotime($res['create'])).'.'.date('d',strtotime($res['create']));
  62. if ($res['judge'] == 1){
  63. $res['judge'] = '合格';
  64. }else{
  65. $res['judge'] = '不合格';
  66. }
  67. unset($res['standard_id']);
  68. return $res;
  69. }
  70. return null;
  71. }
  72. /**
  73. * @return string
  74. */
  75. protected function getWords($bach)
  76. {
  77. $data=$this->initData($bach);
  78. return $this->generateFile($data);
  79. }
  80. /**
  81. * @return string
  82. */
  83. protected function generateFile($data)
  84. {
  85. $data['dist_file']=DS."uploads".DS."words".DS.$data['bach'].".docx";
  86. $data['dist_fule_file']=ROOT_PATH.'public'.DS."uploads".DS."words".DS.$data['bach'].".docx";
  87. if(file_exists($data['dist_fule_file'])){
  88. return $data;
  89. }
  90. $temp_path=ROOT_PATH.'public'.config("site.ben");
  91. $exten=substr($temp_path, strrpos($temp_path, '.')+1);
  92. if($exten!='docx'){
  93. $this->error("模板文件请上传docx格式");
  94. }
  95. $templateProcessor=new \PhpOffice\PhpWord\TemplateProcessor($temp_path);
  96. $templateProcessor->setValue('entrust_no',$data['entrust_no']);//替换变量name
  97. $templateProcessor->setValue('name',$data['name']);//替换变量name
  98. $templateProcessor->setValue('bach',$data['bach']);//替换变量name
  99. $templateProcessor->setValue('create',$data['create']);//替换变量name
  100. $templateProcessor->setValue('dis_max',$data['dis_max']);//替换变量name
  101. $templateProcessor->setValue('dis',$data['dis']);//替换变量name
  102. $templateProcessor->setValue('dis_impurity_max',$data['dis_impurity_max']);//替换变量name
  103. $templateProcessor->setValue('dis_impurity',$data['dis_impurity']);//替换变量name
  104. $templateProcessor->setValue('ben_total_max',$data['ben_total_max']);//替换变量name
  105. $templateProcessor->setValue('ben_total',$data['ben_total']);//替换变量name
  106. $templateProcessor->setValue('ben_max',$data['ben_max']);//替换变量name
  107. $templateProcessor->setValue('ben',$data['ben']);//替换变量name
  108. $templateProcessor->setValue('judge',$data['judge']);//替换变量name
  109. $templateProcessor->saveAs($data['dist_fule_file']);//另存为
  110. return $data;
  111. }
  112. }