BrandHonor.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace app\store\controller\enterprise;
  3. use app\store\controller\Controller;
  4. use app\store\model\enterprise\EnterpriseNews;
  5. use app\store\model\enterprise\EnterpriseSite;
  6. use app\store\model\Goods as GoodsModel;
  7. use think\facade\View;
  8. class BrandHonor extends Controller
  9. {
  10. private $brandModel;
  11. public function initialize()
  12. {
  13. parent::initialize(); // TODO: Change the autogenerated stub
  14. $this->brandModel = new EnterpriseSite();
  15. }
  16. /**
  17. * 获取独立配置
  18. * @return array
  19. */
  20. public function setting(){
  21. $form = $this->postForm('form');
  22. if ($this->brandModel->addOne($form)){
  23. return $this->renderSuccess();
  24. }
  25. return $this->renderError('系统繁忙');
  26. }
  27. /**
  28. * 其他配置批量设置
  29. * @return array
  30. */
  31. public function settingBatch(){
  32. $form = $this->postForm('form');
  33. if ($this->brandModel->addBatch($form)){
  34. return $this->renderSuccess();
  35. }
  36. return $this->renderError('系统繁忙');
  37. }
  38. /**
  39. * 获取单个配置
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function gettting(){
  46. $ctype = $this->request->get('ctype',1);
  47. $model = new EnterpriseSite();
  48. $list = $model->getSetting($ctype);
  49. return $this->renderSuccess(compact('list'));
  50. }
  51. /**
  52. * 其他配置批量获取
  53. * @return array
  54. */
  55. public function getttingBatch(){
  56. $model = new EnterpriseSite();
  57. $list = $model->getSettingBacth();
  58. return $this->renderSuccess(compact('list'));
  59. }
  60. /**
  61. * 获取目录下的所有文件路径
  62. * @param $path
  63. * @return array
  64. */
  65. private function getDir($path)
  66. {
  67. //判断目录是否为空
  68. if(!file_exists($path)) {
  69. return [];
  70. }
  71. $files = scandir($path);
  72. $fileItem = [];
  73. foreach($files as $v) {
  74. $newPath = $path .DIRECTORY_SEPARATOR . $v;
  75. if(is_dir($newPath) && $v != '.' && $v != '..') {
  76. $fileItem = array_merge($fileItem, $this->getDir($newPath));
  77. }else if(is_file($newPath)){
  78. $fileItem[] = $newPath;
  79. }
  80. }
  81. return $fileItem;
  82. }
  83. /**
  84. * 获取当前目录下的所有子目录
  85. * @param $path
  86. * @return array
  87. */
  88. private function getDirPathes($path)
  89. {
  90. //判断目录是否为空
  91. if(!file_exists($path)) {
  92. return [];
  93. }
  94. $files = scandir($path);
  95. $dirItem = [];
  96. foreach($files as $v) {
  97. $newPath = $path .DIRECTORY_SEPARATOR . $v;
  98. if(is_dir($newPath) && $v != '.' && $v != '..') {
  99. array_push($dirItem,$newPath);
  100. }
  101. }
  102. return $dirItem;
  103. }
  104. /**
  105. * 递归删除目录
  106. * @param $dir
  107. * @return bool
  108. */
  109. private function deleteDirFile($dir)
  110. {
  111. $flag = false; //默认没删除成功runtime目录
  112. //判断是不是文件夹
  113. if (is_dir($dir)) {
  114. // 打开目录流 成功返回一个资源类型 目录句柄 否则false
  115. if ($handle = opendir($dir)) {
  116. while (($file = readdir($handle)) !== false) {
  117. // 在php中删除一个文件夹的前提是该文件夹为空
  118. if ($file != '.' && $file != '..') {
  119. if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
  120. // 子内容是目录
  121. $this->deleteDirFile($dir . DIRECTORY_SEPARATOR . $file);
  122. } else {
  123. // 子内容是文件
  124. unlink($dir . DIRECTORY_SEPARATOR . $file);
  125. }
  126. }
  127. }
  128. closedir($handle);
  129. $flag = true;
  130. if (rmdir($dir)) {
  131. $flag = true;
  132. }
  133. }
  134. } else {
  135. $flag = true;
  136. }
  137. return $flag;
  138. }
  139. /**
  140. * 删除目录下的文件
  141. * @param $dir
  142. * @return bool
  143. */
  144. private function deleteOneDirFile($dir)
  145. {
  146. $flag = false; //默认没删除成功runtime目录
  147. //判断是不是文件夹
  148. if (is_dir($dir)) {
  149. // 打开目录流 成功返回一个资源类型 目录句柄 否则false
  150. if ($handle = opendir($dir)) {
  151. while (($file = readdir($handle)) !== false) {
  152. // 在php中删除一个文件夹的前提是该文件夹为空
  153. if (is_file($dir . DIRECTORY_SEPARATOR . $file) && $file != '.' && $file != '..' && $file != '.gitignore' && $file != 'header.html' && $file != 'footer.html') {
  154. if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
  155. // 子内容是文件
  156. unlink($dir . DIRECTORY_SEPARATOR . $file);
  157. }
  158. }
  159. }
  160. closedir($handle);
  161. $flag = true;
  162. }
  163. } else {
  164. $flag = true;
  165. }
  166. return $flag;
  167. }
  168. /**
  169. * 一键生成官网纯静态页面
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function genAllPureHtml(){
  175. //清空将要操作的目录
  176. /* $clearAssetsDir = public_path('ysc_official/assets');
  177. $flag0 = $this->deleteDirFile($clearAssetsDir);
  178. if ($flag0 == false){
  179. return $this->renderError('系统繁忙');
  180. }*/
  181. //清空将要操作的目录
  182. $clearOldDir = public_path('ysc_official');
  183. $flag = $this->deleteOneDirFile($clearOldDir);
  184. if ($flag == false){
  185. return $this->renderError('系统繁忙');
  186. }
  187. //创建需要的目录
  188. /* $newsDir = $clearOldDir.'assets/enterprise';
  189. if (!is_dir($newsDir)){
  190. mkdir($newsDir,0755,true);
  191. }
  192. //迁移等级目录和文件主要是静态文件
  193. $path = public_path('assets')."enterprise";
  194. $fileItems = $this->getDir($path);
  195. $dirItems = $this->getDirPathes($path);
  196. $search = public_path();
  197. $replace = public_path('ysc_official');
  198. foreach ($dirItems as $dir){
  199. $newsDir = str_replace($search,$replace,$dir);
  200. if (!is_dir($newsDir)){
  201. mkdir($newsDir,0755,true);
  202. }
  203. }
  204. foreach ($fileItems as $item){
  205. $newFile = str_replace($search,$replace,$item);
  206. copy($item,$newFile);
  207. }*/
  208. $replace = public_path('ysc_official');
  209. $model = new EnterpriseSite();
  210. //$ysc_official_path = public_path('ysc_official');
  211. //首页 todo
  212. $newsModel = new EnterpriseNews();
  213. $indexSeo = $model->getSetting(3);
  214. $newsTop = $newsModel->getItems(1);
  215. foreach ($newsTop as &$news){
  216. $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义
  217. }
  218. $html = View::fetch('index', [
  219. 'news' => "'".json_encode($newsTop,JSON_UNESCAPED_UNICODE)."'"
  220. ,'title'=>$indexSeo['title']
  221. ,'keywords'=>$indexSeo['keywords']
  222. ,'desc'=>$indexSeo['desc']
  223. ]);
  224. ob_start();
  225. $path = $replace.'index.html';
  226. file_put_contents($path,$html);
  227. ob_end_clean();
  228. //品牌荣誉
  229. $honor = $model->getSetting(1);
  230. $html = View::fetch('brand', ['honors' => "'".$honor['contents']."'"]);
  231. ob_start();
  232. $path = $replace.'brand.html';
  233. file_put_contents($path,$html);
  234. ob_end_clean();
  235. //新闻动态页
  236. $newsAll = $newsModel->getItems();
  237. foreach ($newsAll as &$news){
  238. $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义
  239. }
  240. $html = View::fetch('notice', ['news' => "'".json_encode($newsAll)."'"]);
  241. ob_start();
  242. $path = $replace.'notice.html';
  243. file_put_contents($path,$html);
  244. ob_end_clean();
  245. //新闻详情页 todo
  246. foreach ($newsAll as $i=>$item){
  247. $html = View::fetch('noticeDetail', ['title' =>'"'.$item['title'].'"','time'=>'"'.$item['create_time'].'"',
  248. 'source'=>'"'.$item['sources'].'"','author'=>'"'.$item['author'].'"','contents'=>'"'.$item['contents'].'"']);
  249. ob_start();
  250. $path = $replace.'noticeDetail'.$i .'.html';
  251. if (file_exists($path)){
  252. unlink($path);
  253. }
  254. file_put_contents($path,$html);
  255. ob_end_clean();
  256. }
  257. //产品介绍
  258. $goodsModel = new GoodsModel;
  259. $allGoods = $goodsModel->getListNoPage();
  260. $goods = [];
  261. $productSeo = $model->getSetting(4);
  262. foreach ($allGoods as $good){
  263. $temp['imgUrl'] = $good['images'][0]['file']['preview_url'];
  264. $temp['price'] = $good['goods_price_min'];
  265. $temp['title'] = $good['goods_name'];
  266. $goods[] = $temp;
  267. }
  268. $html = View::fetch('goods', ['goods' => "'".json_encode($goods,JSON_UNESCAPED_UNICODE)."'"
  269. ,'title'=>$productSeo['title']
  270. ,'keywords'=>$productSeo['keywords']
  271. ,'desc'=>$productSeo['desc']
  272. ]);
  273. ob_start();
  274. $path = $replace.'goods.html';
  275. file_put_contents($path,$html);
  276. ob_end_clean();
  277. //招商加盟
  278. $honor = $model->getSetting(2);
  279. $html = View::fetch('bussiness', ['coopers' => "'".$honor['contents']."'"]);
  280. ob_start();
  281. $path = $replace.'bussiness.html';
  282. file_put_contents($path,$html);
  283. ob_end_clean();
  284. //联系我们
  285. $honor = $model->getSetting(5);
  286. $html = View::fetch('contact', ['coopers' => "'".$honor['contents']."'"
  287. ,'title'=>$honor['title']
  288. ,'keywords'=>$honor['keywords']
  289. ,'desc'=>$honor['desc']
  290. ]);
  291. ob_start();
  292. $path = $replace.'contact.html';
  293. file_put_contents($path,$html);
  294. ob_end_clean();
  295. //指定文件用户
  296. @chgrp($replace,'web');
  297. @chown($replace,'web'); // specify user by name
  298. //生成h5静态页面
  299. $this->genAllPureH5();
  300. return $this->renderSuccess();
  301. }
  302. /**
  303. * 一键生成官网纯静态页面
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. private function genAllPureH5(){
  309. /* //清空将要操作的目录
  310. $clearAssetsDir = public_path('h5/assets');
  311. $flag0 = $this->deleteDirFile($clearAssetsDir);
  312. if ($flag0 == false){
  313. return $this->renderError('系统繁忙');
  314. }*/
  315. //清空将要操作的目录
  316. $clearOldDir = public_path('h5');
  317. $flag = $this->deleteOneDirFile($clearOldDir);
  318. if ($flag == false){
  319. return $this->renderError('系统繁忙');
  320. }
  321. //创建需要的目录
  322. /* $newsDir = $clearOldDir.'assets/h5';
  323. if (!is_dir($newsDir)){
  324. mkdir($newsDir,0755,true);
  325. }*/
  326. //迁移等级目录和文件主要是静态文件
  327. /* $path = public_path('assets')."h5";
  328. $fileItems = $this->getDir($path);
  329. $dirItems = $this->getDirPathes($path);
  330. $search = public_path();
  331. $replace = public_path('h5');
  332. foreach ($dirItems as $dir){
  333. $newsDir = str_replace($search,$replace,$dir);
  334. if (!is_dir($newsDir)){
  335. mkdir($newsDir,0755,true);
  336. }
  337. }
  338. foreach ($fileItems as $item){
  339. $newFile = str_replace($search,$replace,$item);
  340. copy($item,$newFile);
  341. }*/
  342. $replace = public_path('h5');
  343. $model = new EnterpriseSite();
  344. //$ysc_official_path = public_path('ysc_official');
  345. //首页 todo
  346. $newsModel = new EnterpriseNews();
  347. $indexSeo = $model->getSetting(3);
  348. $newsTop = $newsModel->getItems(1);
  349. foreach ($newsTop as &$news){
  350. $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义
  351. }
  352. $html = View::fetch('indexh5', [
  353. 'news' => "'".json_encode($newsTop,JSON_UNESCAPED_UNICODE)."'"
  354. ,'title'=>$indexSeo['title']
  355. ,'keywords'=>$indexSeo['keywords']
  356. ,'desc'=>$indexSeo['desc']
  357. ]);
  358. ob_start();
  359. $path = $replace.'index.html';
  360. file_put_contents($path,$html);
  361. ob_end_clean();
  362. //品牌荣誉
  363. $honor = $model->getSetting(1);
  364. $html = View::fetch('brandh5', ['honors' => "'".$honor['contents']."'"]);
  365. ob_start();
  366. $path = $replace.'brand.html';
  367. file_put_contents($path,$html);
  368. ob_end_clean();
  369. //新闻动态页
  370. $newsAll = $newsModel->getItems();
  371. foreach ($newsAll as &$news){
  372. $news['contents'] = addslashes($news['contents']);//富文本里面的url带有双引号,需转义
  373. }
  374. $html = View::fetch('noticeh5', ['news' => "'".json_encode($newsAll)."'"]);
  375. ob_start();
  376. $path = $replace.'notice.html';
  377. file_put_contents($path,$html);
  378. ob_end_clean();
  379. //新闻详情页 todo
  380. foreach ($newsAll as $i=>$item){
  381. $html = View::fetch('noticeDetailh5', ['title' =>'"'.$item['title'].'"','time'=>'"'.$item['create_time'].'"',
  382. 'source'=>'"'.$item['sources'].'"','author'=>'"'.$item['author'].'"','contents'=>'"'.$item['contents'].'"']);
  383. ob_start();
  384. $path = $replace.'noticeDetail'.$i .'.html';
  385. if (file_exists($path)){
  386. unlink($path);
  387. }
  388. file_put_contents($path,$html);
  389. ob_end_clean();
  390. }
  391. //产品介绍
  392. $goodsModel = new GoodsModel;
  393. $allGoods = $goodsModel->getListNoPage();
  394. $goods = [];
  395. $productSeo = $model->getSetting(4);
  396. foreach ($allGoods as $good){
  397. $temp['imgUrl'] = $good['images'][0]['file']['preview_url'];
  398. $temp['price'] = $good['goods_price_min'];
  399. $temp['title'] = $good['goods_name'];
  400. $goods[] = $temp;
  401. }
  402. $html = View::fetch('goodsh5', ['goods' => "'".json_encode($goods,JSON_UNESCAPED_UNICODE)."'"
  403. ,'title'=>$productSeo['title']
  404. ,'keywords'=>$productSeo['keywords']
  405. ,'desc'=>$productSeo['desc']
  406. ]);
  407. ob_start();
  408. $path = $replace.'goods.html';
  409. file_put_contents($path,$html);
  410. ob_end_clean();
  411. //招商加盟
  412. $honor = $model->getSetting(2);
  413. $html = View::fetch('bussinessh5', ['coopers' => "'".$honor['contents']."'"]);
  414. ob_start();
  415. $path = $replace.'bussiness.html';
  416. file_put_contents($path,$html);
  417. ob_end_clean();
  418. //联系我们
  419. $honor = $model->getSetting(5);
  420. $html = View::fetch('contacth5', ['coopers' => "'".$honor['contents']."'"
  421. ,'title'=>$honor['title']
  422. ,'keywords'=>$honor['keywords']
  423. ,'desc'=>$honor['desc']
  424. ]);
  425. ob_start();
  426. $path = $replace.'contact.html';
  427. file_put_contents($path,$html);
  428. ob_end_clean();
  429. //指定文件用户
  430. @chgrp($replace,'web');
  431. @chown($replace,'web'); // specify user by name
  432. return true;
  433. //return $this->renderSuccess();
  434. }
  435. }