You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							200 lines
						
					
					
						
							5.4 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							200 lines
						
					
					
						
							5.4 KiB
						
					
					
				| <?php | |
| 
 | |
| namespace app\controller; | |
| 
 | |
| use app\BaseController; | |
| use app\model\{Article, Expert}; | |
| use think\facade\Db; | |
| 
 | |
| class ScriptController extends BaseController | |
| { | |
|      | |
|     public function getArticle(){ | |
|         //获取已就绪文章列表 | |
|         //$list = Article::where('status', 2)->field("*")->select()->toArray(); | |
|         $list = Article::where('status', 3)->field("*")->select()->toArray(); | |
|         if(empty($list)){ | |
|             echo 'none';die; | |
|         } | |
|          | |
|         //创建待处理目录 | |
|         $fileName = date('Y-m-d_H:i:s'); | |
|         $pathSrc = getcwd() . '/uploads/' . $fileName; | |
|         $zipFile = $pathSrc . '.zip'; | |
|         mkdir($pathSrc, 0777, true); | |
|          | |
|         foreach ($list as $value) { | |
|             if(empty($value['account']))continue; | |
|              | |
|             $uuid = strtoupper(md5($value['id'])); | |
| 
 | |
|             //创建文件夹 | |
|             $artSrc = $pathSrc . '/' . $uuid; | |
|             if (!is_dir($artSrc)) { //若目录不存在则创建之 | |
|                 mkdir($artSrc, 0777, true); | |
|             } | |
| 
 | |
|             //url图片保存本地 | |
|             if(empty($value['cover'])){ | |
|                 copy(getcwd() . '/static/empty.png', $artSrc . '/b_cover.jpg'); | |
|             }else{ | |
|                 $this->down_img($artSrc, $value['cover']); | |
|             } | |
|              | |
|             //文章内容 | |
|             $wordname = $artSrc . "/a_article.doc"; | |
| 
 | |
|             //html标签类文章转成word | |
|             $html = $value['content']; | |
|             $this->start(); | |
|             echo $html; | |
|             $this->save($wordname); | |
| 
 | |
|             //设置信息 | |
|             file_put_contents($artSrc . '/c_message.txt', $value['id'] . "\r\n" .$value['title']); | |
|              | |
|             //设置账号 | |
|              | |
|             file_put_contents($artSrc . '/d_account.txt', $this->getAccounts($value['account'])); | |
| 
 | |
|             //更改状态为已下载 | |
|             Article::where('id', $value['id'])->update([ | |
|                 'status' => 3, | |
|                 'download_time' => date('Y-m-d H:i:s') | |
|             ]); | |
|         } | |
|          | |
|         //打包 | |
|         $this->zip($pathSrc, $zipFile); | |
|          | |
|         //下载 | |
|         $this->downloadZip($zipFile); | |
|         | |
|         die;  | |
|     } | |
| 
 | |
| 
 | |
|     function start() | |
|     { | |
|         ob_start(); | |
|         echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" | |
| xmlns:w="urn:schemas-microsoft-com:office:word" | |
| xmlns="http://www.w3.org/TR/REC-html40">'; | |
|     } | |
| 
 | |
|     function save($path) | |
|     { | |
| 
 | |
|         echo "</html>"; | |
|         $data = ob_get_contents(); | |
|         ob_end_clean(); | |
| 
 | |
|         $this->wirtefile($path, $data); | |
|     } | |
| 
 | |
|     function wirtefile($fn, $data) | |
|     { | |
|         $fp = fopen($fn, "wb"); | |
|         fwrite($fp, $data); | |
|         fclose($fp); | |
|     } | |
| 
 | |
|     function down_img($path,$img_url){ | |
|         ob_start(); | |
|         readfile($img_url); | |
|         $img=ob_get_contents(); | |
|         ob_end_clean(); | |
|         $filename = '/b_cover.jpg'; | |
|         $fp2=@fopen($path . $filename,'a'); | |
|         fwrite($fp2,$img); | |
|         fclose($fp2); | |
|         unset($img,$url); | |
|     } | |
|      | |
|     private function getAccounts($accountIdStr){ | |
|         $content = []; | |
|         $accountIds = explode(',', $accountIdStr); | |
|         $accounts = Expert::where('id', 'in', $accountIds)->field("*")->select()->toArray(); | |
|          | |
|         foreach($accounts as $item){ | |
|             $content[] = $item['platform_code'] . '|' . $item['script_x'] . '|' . $item['script_y']; | |
|         } | |
|          | |
|         return implode("\r\n", $content); | |
|     } | |
|      | |
|      | |
|     //下载function | |
|     private function downloadZip($zipPath) | |
|     { | |
|         $zipPath = iconv("UTF-8", "GBK", $zipPath);//加这行中文文件夹也ok了 | |
|         header("Cache-Control: public"); | |
|         header("Content-Description: File Transfer"); | |
|         header('Content-disposition: attachment; filename=' . basename($zipPath)); //文件名 | |
|         header("Content-Type: application/zip"); //zip格式的 | |
|         header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 | |
|         header('Content-Length: ' . filesize($zipPath)); //告诉浏览器,文件大小 | |
|         @readfile($zipPath);//ob_end_clean(); | |
|         @unlink(app()->getRootPath().'public/'.$zipPath);//删除压缩包 | |
|     } | |
|      | |
|      | |
|     /** | |
| 	 * zip压缩 | |
| 	 * @param string $zipfile 压缩文件目录 | |
| 	 * @param string $unzipdir 解压目录 | |
| 	 */ | |
| 	public function zip($dirsrc, $zipfile){ | |
| 		if(!is_dir($dirsrc)){ | |
| 			return false; | |
| 		} | |
| 
 | |
| 		$aimDir = dirname($zipfile); | |
| 		$this->createDir($aimDir); | |
| 
 | |
| 		$zip = new \ZipArchive; | |
| 		$res = $zip->open($zipfile, \ZipArchive::CREATE); | |
| 		if($res !== true){ | |
| 			return false; | |
| 		} | |
| 
 | |
| 		$this->_zip($dirsrc, $zip); | |
| 
 | |
| 		$zip->close(); //关闭处理的zip文件 | |
| 		return true; | |
| 	} | |
| 
 | |
| 	//递归将文件添加至压缩包 | |
| 	private function _zip($path, $zip, $zip_path = ''){ | |
| 		$path = rtrim($path, '/'); | |
| 		$handler = opendir($path); //打开当前文件夹由$path指定。 | |
|  | |
| 		while(($filename = readdir($handler)) !== false){ | |
| 			if($filename == '.' || $filename == '..')continue; | |
| 
 | |
| 			$nowPath = $path . '/' . $filename; | |
| 			$thzip_path = $zip_path . $filename; | |
| 			if(is_dir($nowPath)){ | |
| 				// 如果读取的某个对象是文件夹,则递归 | |
| 				$this->_zip($nowPath, $zip, $thzip_path . '/'); | |
| 			}else{  | |
| 				//将文件加入zip对象 | |
| 				$zip->addFile($nowPath, $thzip_path); | |
| 			} | |
| 		} | |
| 
 | |
| 		@closedir($path); | |
| 	} | |
| 	 | |
| 	/** | |
| 	* 递归创建文件夹 | |
| 	* @param string $path 路径 | |
| 	*/ | |
| 	public function createDir($path){  | |
| 		if (!file_exists($path)){  | |
| 			$this->createDir(dirname($path)); | |
| 			mkdir($path, 0777);  | |
| 		}  | |
| 	}  | |
| 	 | |
| }
 |