| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Exception;
- class Word extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- public function download($bach = null){
- try{
- $data= $this->getWords($bach);
- $destination = ROOT_PATH.'public'.DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx";
- $destination_path = DS."uploads".DS."words".DS."download".DS."卷烟包装材料安全卫生检测报告批次号-".$data['bach']."(浙江美浓世纪集团有限公司检测中心).docx";
- if(!file_exists($data['dist_fule_file'])){
- \exception("报告不存在");
- }
- if(file_exists($destination)){
- header("location:".cdnurl($destination_path,true));
- }else{
- if (copy($data['dist_fule_file'], $destination)) {
- header("location:".cdnurl($destination_path,true));
- }
- }
- }catch (Exception $e){
- $this->error($e->getMessage());
- }
- }
- public function preview($bach = null){
- header("Content-type:text/html; charset=utf-8");//设置当前页面编码为“utf-8”
- ini_set("display_errors","On");
- error_reporting(E_ALL);
- $data= $this->getWords($bach);
- header("location:http://vocs.7in6.com/word.php?doc=".$data['dist_file']);
- }
- protected function initData($bach=null){
- if (empty($bach)){
- $this->error('参数错误','',401);
- }
- $where = [];
- $where['bach'] = $bach;
- $where['delete_time'] = null;
- // $where['sample_no'] = array('like','%A%');
- $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();
- if ($res){
- $check = Db::name('item_judge_detail')->where('pid',$res['standard_id'])->select();
- foreach ($check as $key => $value){
- if ($value['params'] == '溶剂残留总量'){
- $res['dis_max'] = $value['max'];
- }
- if ($value['params'] == '溶剂杂质总量'){
- $res['dis_impurity_max'] = $value['max'];
- }
- if ($value['params'] == '苯系物总量'){
- $res['ben_total_max'] = $value['max'];
- }
- if ($value['params'] == '苯含量'){
- $res['ben_max'] = $value['max'];
- }
- }
- $res['create'] = date('Y',strtotime($res['create'])).'.'.date('m',strtotime($res['create'])).'.'.date('d',strtotime($res['create']));
- if ($res['judge'] == 1){
- $res['judge'] = '合格';
- }else{
- $res['judge'] = '不合格';
- }
- unset($res['standard_id']);
- return $res;
- }
- return null;
- }
- /**
- * @return string
- */
- protected function getWords($bach)
- {
- $data=$this->initData($bach);
- return $this->generateFile($data);
- }
- /**
- * @return string
- */
- protected function generateFile($data)
- {
- $data['dist_file']=DS."uploads".DS."words".DS.$data['bach'].".docx";
- $data['dist_fule_file']=ROOT_PATH.'public'.DS."uploads".DS."words".DS.$data['bach'].".docx";
- if(file_exists($data['dist_fule_file'])){
- return $data;
- }
- $temp_path=ROOT_PATH.'public'.config("site.ben");
- $exten=substr($temp_path, strrpos($temp_path, '.')+1);
- if($exten!='docx'){
- $this->error("模板文件请上传docx格式");
- }
- $templateProcessor=new \PhpOffice\PhpWord\TemplateProcessor($temp_path);
- $templateProcessor->setValue('entrust_no',$data['entrust_no']);//替换变量name
- $templateProcessor->setValue('name',$data['name']);//替换变量name
- $templateProcessor->setValue('bach',$data['bach']);//替换变量name
- $templateProcessor->setValue('create',$data['create']);//替换变量name
- $templateProcessor->setValue('dis_max',$data['dis_max']);//替换变量name
- $templateProcessor->setValue('dis',$data['dis']);//替换变量name
- $templateProcessor->setValue('dis_impurity_max',$data['dis_impurity_max']);//替换变量name
- $templateProcessor->setValue('dis_impurity',$data['dis_impurity']);//替换变量name
- $templateProcessor->setValue('ben_total_max',$data['ben_total_max']);//替换变量name
- $templateProcessor->setValue('ben_total',$data['ben_total']);//替换变量name
- $templateProcessor->setValue('ben_max',$data['ben_max']);//替换变量name
- $templateProcessor->setValue('ben',$data['ben']);//替换变量name
- $templateProcessor->setValue('judge',$data['judge']);//替换变量name
- $templateProcessor->saveAs($data['dist_fule_file']);//另存为
- return $data;
- }
- }
|