brandModel = new EnterpriseSite(); } /** * 获取独立配置 * @return array */ public function setting(){ $form = $this->postForm('form'); if ($this->brandModel->addOne($form)){ return $this->renderSuccess(); } return $this->renderError('系统繁忙'); } /** * 其他配置批量设置 * @return array */ public function settingBatch(){ $form = $this->postForm('form'); if ($this->brandModel->addBatch($form)){ return $this->renderSuccess(); } return $this->renderError('系统繁忙'); } /** * 获取单个配置 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function gettting(){ $ctype = $this->request->get('ctype',1); $model = new EnterpriseSite(); $list = $model->getSetting($ctype); return $this->renderSuccess(compact('list')); } /** * 其他配置批量获取 * @return array */ public function getttingBatch(){ $model = new EnterpriseSite(); $list = $model->getSettingBacth(); return $this->renderSuccess(compact('list')); } /** * 获取目录下的所有文件路径 * @param $path * @return array */ private function getDir($path) { //判断目录是否为空 if(!file_exists($path)) { return []; } $files = scandir($path); $fileItem = []; foreach($files as $v) { $newPath = $path .DIRECTORY_SEPARATOR . $v; if(is_dir($newPath) && $v != '.' && $v != '..') { $fileItem = array_merge($fileItem, $this->getDir($newPath)); }else if(is_file($newPath)){ $fileItem[] = $newPath; } } return $fileItem; } /** * 获取当前目录下的所有子目录 * @param $path * @return array */ private function getDirPathes($path) { //判断目录是否为空 if(!file_exists($path)) { return []; } $files = scandir($path); $dirItem = []; foreach($files as $v) { $newPath = $path .DIRECTORY_SEPARATOR . $v; if(is_dir($newPath) && $v != '.' && $v != '..') { array_push($dirItem,$newPath); } } return $dirItem; } /** * 递归删除目录 * @param $dir * @return bool */ private function deleteDirFile($dir) { $flag = false; //默认没删除成功runtime目录 //判断是不是文件夹 if (is_dir($dir)) { // 打开目录流 成功返回一个资源类型 目录句柄 否则false if ($handle = opendir($dir)) { while (($file = readdir($handle)) !== false) { // 在php中删除一个文件夹的前提是该文件夹为空 if ($file != '.' && $file != '..') { if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) { // 子内容是目录 $this->deleteDirFile($dir . DIRECTORY_SEPARATOR . $file); } else { // 子内容是文件 unlink($dir . DIRECTORY_SEPARATOR . $file); } } } closedir($handle); $flag = true; if (rmdir($dir)) { $flag = true; } } } else { $flag = true; } return $flag; } /** * 删除目录下的文件 * @param $dir * @return bool */ private function deleteOneDirFile($dir) { $flag = false; //默认没删除成功runtime目录 //判断是不是文件夹 if (is_dir($dir)) { // 打开目录流 成功返回一个资源类型 目录句柄 否则false if ($handle = opendir($dir)) { while (($file = readdir($handle)) !== false) { // 在php中删除一个文件夹的前提是该文件夹为空 if (is_file($dir . DIRECTORY_SEPARATOR . $file) && $file != '.' && $file != '..' && $file != '.gitignore' && $file != 'header.html' && $file != 'footer.html') { if (is_file($dir . DIRECTORY_SEPARATOR . $file)) { // 子内容是文件 unlink($dir . DIRECTORY_SEPARATOR . $file); } } } closedir($handle); $flag = true; } } else { $flag = true; } return $flag; } /** * 一键生成官网纯静态页面 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function genAllPureHtml(){ //清空将要操作的目录 /* $clearAssetsDir = public_path('ysc_official/assets'); $flag0 = $this->deleteDirFile($clearAssetsDir); if ($flag0 == false){ return $this->renderError('系统繁忙'); }*/ //清空将要操作的目录 $clearOldDir = public_path('ysc_official'); $flag = $this->deleteOneDirFile($clearOldDir); if ($flag == false){ return $this->renderError('系统繁忙'); } //创建需要的目录 /* $newsDir = $clearOldDir.'assets/enterprise'; if (!is_dir($newsDir)){ mkdir($newsDir,0755,true); } //迁移等级目录和文件主要是静态文件 $path = public_path('assets')."enterprise"; $fileItems = $this->getDir($path); $dirItems = $this->getDirPathes($path); $search = public_path(); $replace = public_path('ysc_official'); foreach ($dirItems as $dir){ $newsDir = str_replace($search,$replace,$dir); if (!is_dir($newsDir)){ mkdir($newsDir,0755,true); } } foreach ($fileItems as $item){ $newFile = str_replace($search,$replace,$item); copy($item,$newFile); }*/ $replace = public_path('ysc_official'); $model = new EnterpriseSite(); //$ysc_official_path = public_path('ysc_official'); //首页 todo $newsModel = new EnterpriseNews(); $indexSeo = $model->getSetting(3); $newsTop = $newsModel->getItems(1); foreach ($newsTop as &$news){ $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义 } $html = View::fetch('index', [ 'news' => "'".json_encode($newsTop,JSON_UNESCAPED_UNICODE)."'" ,'title'=>$indexSeo['title'] ,'keywords'=>$indexSeo['keywords'] ,'desc'=>$indexSeo['desc'] ]); ob_start(); $path = $replace.'index.html'; file_put_contents($path,$html); ob_end_clean(); //品牌荣誉 $honor = $model->getSetting(1); $html = View::fetch('brand', ['honors' => "'".$honor['contents']."'"]); ob_start(); $path = $replace.'brand.html'; file_put_contents($path,$html); ob_end_clean(); //新闻动态页 $newsAll = $newsModel->getItems(); foreach ($newsAll as &$news){ $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义 } $html = View::fetch('notice', ['news' => "'".json_encode($newsAll)."'"]); ob_start(); $path = $replace.'notice.html'; file_put_contents($path,$html); ob_end_clean(); //新闻详情页 todo foreach ($newsAll as $i=>$item){ $html = View::fetch('noticeDetail', ['title' =>'"'.$item['title'].'"','time'=>'"'.$item['create_time'].'"', 'source'=>'"'.$item['sources'].'"','author'=>'"'.$item['author'].'"','contents'=>'"'.$item['contents'].'"']); ob_start(); $path = $replace.'noticeDetail'.$i .'.html'; if (file_exists($path)){ unlink($path); } file_put_contents($path,$html); ob_end_clean(); } //产品介绍 $goodsModel = new GoodsModel; $allGoods = $goodsModel->getListNoPage(); $goods = []; $productSeo = $model->getSetting(4); foreach ($allGoods as $good){ $temp['imgUrl'] = $good['images'][0]['file']['preview_url']; $temp['price'] = $good['goods_price_min']; $temp['title'] = $good['goods_name']; $goods[] = $temp; } $html = View::fetch('goods', ['goods' => "'".json_encode($goods,JSON_UNESCAPED_UNICODE)."'" ,'title'=>$productSeo['title'] ,'keywords'=>$productSeo['keywords'] ,'desc'=>$productSeo['desc'] ]); ob_start(); $path = $replace.'goods.html'; file_put_contents($path,$html); ob_end_clean(); //招商加盟 $honor = $model->getSetting(2); $html = View::fetch('bussiness', ['coopers' => "'".$honor['contents']."'"]); ob_start(); $path = $replace.'bussiness.html'; file_put_contents($path,$html); ob_end_clean(); //联系我们 $honor = $model->getSetting(5); $html = View::fetch('contact', ['coopers' => "'".$honor['contents']."'" ,'title'=>$honor['title'] ,'keywords'=>$honor['keywords'] ,'desc'=>$honor['desc'] ]); ob_start(); $path = $replace.'contact.html'; file_put_contents($path,$html); ob_end_clean(); //指定文件用户 @chgrp($replace,'web'); @chown($replace,'web'); // specify user by name //生成h5静态页面 $this->genAllPureH5(); return $this->renderSuccess(); } /** * 一键生成官网纯静态页面 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function genAllPureH5(){ /* //清空将要操作的目录 $clearAssetsDir = public_path('h5/assets'); $flag0 = $this->deleteDirFile($clearAssetsDir); if ($flag0 == false){ return $this->renderError('系统繁忙'); }*/ //清空将要操作的目录 $clearOldDir = public_path('h5'); $flag = $this->deleteOneDirFile($clearOldDir); if ($flag == false){ return $this->renderError('系统繁忙'); } //创建需要的目录 /* $newsDir = $clearOldDir.'assets/h5'; if (!is_dir($newsDir)){ mkdir($newsDir,0755,true); }*/ //迁移等级目录和文件主要是静态文件 /* $path = public_path('assets')."h5"; $fileItems = $this->getDir($path); $dirItems = $this->getDirPathes($path); $search = public_path(); $replace = public_path('h5'); foreach ($dirItems as $dir){ $newsDir = str_replace($search,$replace,$dir); if (!is_dir($newsDir)){ mkdir($newsDir,0755,true); } } foreach ($fileItems as $item){ $newFile = str_replace($search,$replace,$item); copy($item,$newFile); }*/ $replace = public_path('h5'); $model = new EnterpriseSite(); //$ysc_official_path = public_path('ysc_official'); //首页 todo $newsModel = new EnterpriseNews(); $indexSeo = $model->getSetting(3); $newsTop = $newsModel->getItems(1); foreach ($newsTop as &$news){ $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义 } $html = View::fetch('indexh5', [ 'news' => "'".json_encode($newsTop,JSON_UNESCAPED_UNICODE)."'" ,'title'=>$indexSeo['title'] ,'keywords'=>$indexSeo['keywords'] ,'desc'=>$indexSeo['desc'] ]); ob_start(); $path = $replace.'index.html'; file_put_contents($path,$html); ob_end_clean(); //品牌荣誉 $honor = $model->getSetting(1); $html = View::fetch('brandh5', ['honors' => "'".$honor['contents']."'"]); ob_start(); $path = $replace.'brand.html'; file_put_contents($path,$html); ob_end_clean(); //新闻动态页 $newsAll = $newsModel->getItems(); foreach ($newsAll as &$news){ $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义 } $html = View::fetch('noticeh5', ['news' => "'".json_encode($newsAll)."'"]); ob_start(); $path = $replace.'notice.html'; file_put_contents($path,$html); ob_end_clean(); //新闻详情页 todo foreach ($newsAll as $i=>$item){ $html = View::fetch('noticeDetailh5', ['title' =>'"'.$item['title'].'"','time'=>'"'.$item['create_time'].'"', 'source'=>'"'.$item['sources'].'"','author'=>'"'.$item['author'].'"','contents'=>'"'.$item['contents'].'"']); ob_start(); $path = $replace.'noticeDetail'.$i .'.html'; if (file_exists($path)){ unlink($path); } file_put_contents($path,$html); ob_end_clean(); } //产品介绍 $goodsModel = new GoodsModel; $allGoods = $goodsModel->getListNoPage(); $goods = []; $productSeo = $model->getSetting(4); foreach ($allGoods as $good){ $temp['imgUrl'] = $good['images'][0]['file']['preview_url']; $temp['price'] = $good['goods_price_min']; $temp['title'] = $good['goods_name']; $goods[] = $temp; } $html = View::fetch('goodsh5', ['goods' => "'".json_encode($goods,JSON_UNESCAPED_UNICODE)."'" ,'title'=>$productSeo['title'] ,'keywords'=>$productSeo['keywords'] ,'desc'=>$productSeo['desc'] ]); ob_start(); $path = $replace.'goods.html'; file_put_contents($path,$html); ob_end_clean(); //招商加盟 $honor = $model->getSetting(2); $html = View::fetch('bussinessh5', ['coopers' => "'".$honor['contents']."'"]); ob_start(); $path = $replace.'bussiness.html'; file_put_contents($path,$html); ob_end_clean(); //联系我们 $honor = $model->getSetting(5); $html = View::fetch('contacth5', ['coopers' => "'".$honor['contents']."'" ,'title'=>$honor['title'] ,'keywords'=>$honor['keywords'] ,'desc'=>$honor['desc'] ]); ob_start(); $path = $replace.'contact.html'; file_put_contents($path,$html); ob_end_clean(); //指定文件用户 @chgrp($replace,'web'); @chown($replace,'web'); // specify user by name return true; //return $this->renderSuccess(); } }