Brak opisu
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.

Buildmhtml.php 82KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\home\controller;
  14. use think\Db;
  15. use think\template\driver\File;
  16. class Buildmhtml extends Base
  17. {
  18. public $php_sessid;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. @ini_set('memory_limit','-1');
  23. $this->php_sessid = !empty($_COOKIE['PHPSESSID']) ? $_COOKIE['PHPSESSID'] : '';
  24. if (!session('?admin_id')) {
  25. $this->error("只允许后台管理员操作!");
  26. }
  27. $this->makeminfo = \think\Db::name('weapp_makemhtml')->find(1);
  28. }
  29. /*
  30. * 清理缓存
  31. */
  32. private function clearCache()
  33. {
  34. cache("channel_page_total_serialize".$this->php_sessid, null);
  35. cache("channel_info_serialize".$this->php_sessid, null);
  36. cache("has_children_Row_serialize".$this->php_sessid, null);
  37. cache("aid_arr_serialize".$this->php_sessid, null);
  38. cache("channel_arr_serialize".$this->php_sessid, null);
  39. cache("article_info_serialize".$this->php_sessid, null);
  40. cache("article_page_total_serialize".$this->php_sessid, null);
  41. cache("article_tags_serialize".$this->php_sessid, null);
  42. cache("article_attr_info_serialize".$this->php_sessid, null);
  43. cache("article_children_row_serialize".$this->php_sessid, null);
  44. }
  45. /*
  46. * 获取全站生成时,需要生成的页面的个数
  47. */
  48. public function buildIndexAll()
  49. {
  50. \think\Session::pause(); // 暂停session,防止session阻塞机制
  51. $this->clearCache();
  52. $uphtmltype = input('param.uphtmltype/d');
  53. if (!empty($uphtmltype)) { // 指定文档后全部生成
  54. $this->buildAppointAll($uphtmltype);
  55. }
  56. else { // 更新全部
  57. $channelData = $this->getChannelData(0);
  58. $archivesArr = getAllArchivesAid(0, $this->home_lang);
  59. $articleData_pagetotal = count($archivesArr['aid_arr']);
  60. $allpagetotal = 1 + $channelData['pagetotal'] + $articleData_pagetotal;
  61. $msg = $this->handleBuildIndex();
  62. $data = [
  63. 'achievepage' => 1,
  64. 'channelpagetotal' => $channelData['pagetotal'],
  65. 'articlepagetotal' => $articleData_pagetotal,
  66. 'allpagetotal' => $allpagetotal,
  67. ];
  68. $this->success($msg, null, $data);
  69. }
  70. }
  71. /*
  72. * 指定文档生成全部(涉及栏目、首页)
  73. */
  74. private function buildAppointAll($uphtmltype = 0)
  75. {
  76. if (1 == $uphtmltype) { // 指定时间的文档更新
  77. $seo_start_time = $this->eyou['global']['seo_start_time'];
  78. $seo_start_time = !empty($seo_start_time) ? strtotime($seo_start_time) : 0;
  79. $startid = Db::name('archives')->where([
  80. 'add_time' => ['egt', $seo_start_time],
  81. ])->order('aid asc')->limit(1)->value('aid');
  82. if (empty($startid)) {
  83. $startid = Db::name('archives')->max('aid');
  84. $startid += 1;
  85. }
  86. }
  87. else if (2 == $uphtmltype) { // 指定ID文档的全部更新
  88. $startid = $this->eyou['global']['seo_startid2'];
  89. }
  90. $archivesArr = getAllArchivesAid(0, $this->home_lang, $startid);
  91. $articleData_pagetotal = count($archivesArr['aid_arr']);
  92. $channelpagetotal = 0;
  93. $typeid_arr = [];
  94. if (!empty($articleData_pagetotal)) {
  95. $typeid_arr = $archivesArr['typeid_arr'];
  96. foreach ($archivesArr['typeid_arr'] as $key => $val) {
  97. // 包含所有的上级栏目
  98. $allParentRow = model('Arctype')->getAllPid($val);
  99. $typeid_arr = array_merge($typeid_arr, get_arr_column($allParentRow, 'id'));
  100. }
  101. $typeid_arr = array_unique($typeid_arr);
  102. $channelData = $this->getChannelData($typeid_arr, false);
  103. $channelpagetotal = intval($channelData['pagetotal']);
  104. }
  105. $allpagetotal = 1 + $channelpagetotal + $articleData_pagetotal;
  106. $msg = $this->handleBuildIndex();
  107. $data = [
  108. 'achievepage' => 1,
  109. 'channelpagetotal' => $channelpagetotal,
  110. 'articlepagetotal' => $articleData_pagetotal,
  111. 'allpagetotal' => $allpagetotal,
  112. 'startid' => $startid,
  113. 'typeids' => implode(',', $typeid_arr),
  114. ];
  115. $this->success($msg, null, $data);
  116. }
  117. /*
  118. * 生成首页静态页面
  119. */
  120. public function buildIndex()
  121. {
  122. \think\Session::pause(); // 暂停session,防止session阻塞机制
  123. $msg = $this->handleBuildIndex();
  124. $this->success($msg);
  125. }
  126. /*
  127. * 处理生成首页
  128. */
  129. private function handleBuildIndex()
  130. {
  131. $msg = '';
  132. $indexurl = $this->request->domain().$this->root_dir;
  133. // 保存的文件名
  134. $seo_html_position_arr = explode('/', $this->eyou['global']['seo_html_position']);
  135. if (!empty($seo_html_position_arr)) {
  136. $savefilename = end($seo_html_position_arr);
  137. } else {
  138. $savefilename = 'index.html';
  139. }
  140. //获取手机路径
  141. $makeminfo = $this->makeminfo;
  142. if($makeminfo['web_url_model']==2){
  143. $seo_html_arcdir='';
  144. }else{
  145. $seo_html_arcdir=$makeminfo['value'];
  146. }
  147. if (!empty($this->eyou['global']['seo_showmod'])){
  148. $seo_html_position = !empty($this->eyou['global']['seo_html_position']) ? $this->eyou['global']['seo_html_position'] : '';
  149. if ($seo_html_position) {
  150. $seo_html_position = preg_replace('/^\.{1,}([\\\\\/]+)/i', '', $seo_html_position);
  151. $seo_html_position = ltrim($seo_html_position, '/');
  152. $seo_html_position = ROOT_PATH.$seo_html_position;
  153. $seo_html_position_path = dirname($seo_html_position);
  154. tp_mkdir($seo_html_position_path);
  155. clearstatcache(); // 清除文件夹权限缓存
  156. if (!is_writeable($seo_html_position_path)) {
  157. $msg = "目录 {$seo_html_position_path} 没有权限写入,生成失败";
  158. return $msg;
  159. }
  160. }
  161. $result['pageurl'] = $this->request->domain() . ROOT_DIR; // 获取当前页面URL
  162. // $result['pageurl_m'] = pc_to_mobile_url($result['pageurl']); // 获取当前页面对应的移动端URL
  163. // 移动端域名
  164. // $result['mobile_domain'] = '';
  165. // if (!empty($this->eyou['global']['web_mobile_domain_open']) && !empty($this->eyou['global']['web_mobile_domain'])) {
  166. // $result['mobile_domain'] = $this->eyou['global']['web_mobile_domain'] . '.' . $this->request->rootDomain();
  167. // }
  168. $eyou = array(
  169. 'field' => $result,
  170. );
  171. $this->eyou = array_merge($this->eyou, $eyou);
  172. $this->assign('eyou', $this->eyou);
  173. try {
  174. // $savepath = '.'.$seo_html_arcdir.'/'.$savefilename;
  175. // if($makeminfo['web_url_model']==2){
  176. // $savepath = '.'.$makeminfo['value'].'/'.$savefilename;
  177. // }
  178. $makeminfo =$this->makeminfo;
  179. //$seo_html_arcdir=$makeminfo['value'];
  180. $savepath = '.'.$makeminfo['value'].'/'.$savefilename;
  181. $tpl = 'index';
  182. $seo_html_templet = !empty($this->eyou['global']['seo_html_templet']) ? $this->eyou['global']['seo_html_templet'] : '';
  183. $seo_html_templet_arr = explode('.',$seo_html_templet);
  184. if(!empty($seo_html_templet_arr[0])){
  185. $tpl = $seo_html_templet_arr[0];
  186. }
  187. $this->request->get(['m' => 'Index']); // 首页焦点
  188. $this->filePutContents($savepath, $tpl, 'mobile', 0, '/', 0, 1, $result);
  189. if ($seo_html_position){
  190. //@copy($savepath, $seo_html_position);
  191. // @copy($savepath, './index.html');
  192. // $msg .= "成功更新主页HTML:<a href='".$indexurl."' target='_blank' onclick='layer.closeAll();'>浏览...</a><br />";
  193. }else{
  194. // $msg .= "成功更新主页HTML:".$savepath."<br /><a href='$savepath' target='_blank' onclick='layer.closeAll();'>浏览...</a><br />";
  195. }
  196. } catch (\Exception $e) {
  197. //$msg .= '<span>index.html生成失败!' . $e->getMessage() . '</span><br>';
  198. }
  199. }else{
  200. //@unlink('index.html');
  201. @unlink($savefilename);
  202. $msg .= "采用动态浏览模式:<a href='".$indexurl."' target='_blank' onclick='layer.closeAll();'>浏览...</a><br />";
  203. }
  204. return $msg;
  205. }
  206. /*
  207. * 写入静态页面
  208. */
  209. private function filePutContents($savepath, $tpl, $model = 'mobile', $pages = 0, $dir = '/', $tid = 0, $top = 1, $result = [])
  210. {
  211. ob_start();
  212. static $templateConfig = null;
  213. null === $templateConfig && $templateConfig = \think\Config::get('template');
  214. $templateConfig['view_path'] = "./template/".TPL_THEME."mobile/";
  215. $template = "./template/".TPL_THEME."{$model}/{$tpl}.{$templateConfig['view_suffix']}";
  216. $content = $this->fetch($template, [], [], $templateConfig);
  217. /*解决模板里没有设置编码的情况*/
  218. // if (!stristr($content, 'utf-8')) {
  219. // $content = str_ireplace('<head>', "<head>\n<meta charset='utf-8'>", $content);
  220. // }
  221. /*end*/
  222. /*解决手机端图片路径问题*/
  223. $url = tpCache('web.web_basehost');
  224. $pattern='#<img(.*?)src="(?!(https|http):)([^"]+)"#';
  225. $content = preg_replace($pattern,'<img $1 src="' .$url.'$3'.'"',$content);
  226. /*解决手机端动态页面问题*/
  227. $content = preg_replace('#"/index.php(.*?)#','"'.$url.'/index.php$1',$content);
  228. /*解决模板里有pc样式路径的情况*/
  229. if (stristr($content, '/template/pc/')) {
  230. $content = str_ireplace('/template/pc/', "/template/mobile/", $content);
  231. }
  232. /*解决模板里有pc样式路径的情况*/
  233. if (stristr($content, '/template/eyou/pc/')) {
  234. $content = str_ireplace('/template/eyou/pc/', "/template/eyou/mobile/", $content);
  235. }
  236. /*解决模板里有pc样式路径的情况*/
  237. if (stristr($content, '/template/default/pc/')) {
  238. $content = str_ireplace('/template/default/pc/', "/template/default/mobile/", $content);
  239. }
  240. if (stristr($content, '/tags/')) {
  241. $content = str_ireplace('/tags/', "/m/tags/", $content);
  242. }
  243. if ($pages > 0) {
  244. $page = "/<a(.*?)href(\s*)=(\s*)[\'|\"](.*?)page=([0-9]*)(.*?)data-ey_fc35fdc=[\'|\"]html[\'|\"](.*?)>/i";
  245. preg_match_all($page, $content, $matchpage);
  246. $dir = trim($dir, '.');
  247. $seo_html_listname = $this->eyou['global']['seo_html_listname'];
  248. foreach ($matchpage[0] as $key1 => $value1) {
  249. if ($matchpage[5][$key1] == 1) {
  250. if ($top == 1) {
  251. $url = $dir;
  252. } elseif ($top == 2) {
  253. $url = $dir . '/lists_' . $tid . '.html';
  254. } else {
  255. $url = $dir . '/lists_' . $tid . '.html';
  256. }
  257. } else {
  258. if ($seo_html_listname == 4) {
  259. if (!empty($result['rulelist'])) {
  260. if (!preg_match('/{page}/i', $result['rulelist'])) { // 没有分页变量的情况
  261. $rulelist_filename = '';
  262. } else {
  263. $rulelist = trim($result['rulelist'], '/');
  264. $rulelist_filename = preg_replace('/^((.*)\/)?([^\/]+)$/i', '${3}', $rulelist);
  265. $rulelist_filename = str_replace("{tid}", $tid, $rulelist_filename);
  266. $rulelist_filename = str_replace("{page}", $matchpage[5][$key1], $rulelist_filename);
  267. }
  268. $url = $dir;
  269. if (!empty($rulelist_filename)) {
  270. $url .= '/' . $rulelist_filename;
  271. }
  272. }else{
  273. $url = $dir . '/list_' . $tid . '_' . $matchpage[5][$key1] . '.html';
  274. }
  275. } else {
  276. $url = $dir . '/lists_' . $tid . '_' . $matchpage[5][$key1] . '.html';
  277. }
  278. }
  279. $url = ROOT_DIR . '/' . trim($url, '/');
  280. $value1_new = preg_replace('/href(\s*)=(\s*)[\'|\"]([^\'\"]*)[\'|\"]/i', '', $value1);
  281. $value1_new = str_replace('data-ey_fc35fdc=', " href=\"{$url}\" data-ey_fc35fdc=", $value1_new);
  282. $content = str_ireplace($value1, $value1_new, $content);
  283. }
  284. }
  285. // $content = $this->pc_to_mobile_js($content, $result); // 生成静态模式下,自动加上PC端跳转移动端的JS代码
  286. echo $content;
  287. $_cache = ob_get_contents();
  288. ob_end_clean();
  289. static $File = null;
  290. null === $File && $File = new File;
  291. $File->fwrite($savepath, $_cache);
  292. }
  293. /*
  294. * 生成文档静态页面
  295. */
  296. public function buildArticle()
  297. {
  298. function_exists('set_time_limit') && set_time_limit(0);
  299. \think\Session::pause(); // 暂停session,防止session阻塞机制
  300. $typeid = input("param.id/d", 0); // 栏目ID
  301. $findex = input("param.findex/d", 0);
  302. $achievepage = input("param.achieve/d", 0); // 已完成文档数
  303. if (empty($findex) && empty($achievepage)){
  304. $this->clearCache();
  305. }
  306. $data = $this->handelBuildArticleList($typeid, $findex, $achievepage,true,20);
  307. $this->success($data[0], null, $data[1]);
  308. }
  309. /*
  310. * 批量生成文档静态页面时候生成
  311. ** $typeid 栏目id
  312. * $aid 内容页id
  313. * $achievepage 已完成文档数
  314. * $batch 是否分批次执行,true:分批,false:不分批
  315. * limit 每次执行多少条数据
  316. * type 执行类型,0:aid指定的文档页,1:上一篇,2:下一篇
  317. */
  318. private function handelBuildArticleList($typeid, $nextid = 0, $achievepage = 0, $batch = true, $limit = 20)
  319. {
  320. !empty($this->eyou['global']['seo_pagesize']) && $limit = $this->eyou['global']['seo_pagesize'];
  321. $startid = '';
  322. $endid = '';
  323. $uphtmltype = input('param.uphtmltype/d');
  324. if (!empty($uphtmltype)) { // 更新选项/指定时间或者文档ID,最后都是转为以文档起始ID
  325. $startid = input('param.startid/d');
  326. empty($startid) && $startid = !empty($this->eyou['global']['seo_startid']) ? $this->eyou['global']['seo_startid'] : '';
  327. $endid = !empty($this->eyou['global']['seo_endid']) ? $this->eyou['global']['seo_endid'] : '';
  328. }
  329. $msg = "";
  330. $globalConfig = $this->eyou['global'];
  331. $result = $this->getArticleAidData($typeid,$startid,$endid);
  332. $aid_arr = $result['aid_arr'];
  333. $channel_arr = $result['channel_arr'];
  334. $allAttrInfo = getAllAttrInfo($channel_arr);
  335. $allTags = $result['allTags'];
  336. $has_children_Row = $result['has_children_Row'];
  337. $data['allpagetotal'] = $pagetotal = $result['pagetotal'];
  338. $data['achievepage'] = $achievepage;
  339. $data['pagetotal'] = 0;
  340. if ($batch && $pagetotal > $achievepage) {
  341. while ($limit && isset($aid_arr[$nextid])) {
  342. $archives = getAllArchives($this->home_lang, 0, $aid_arr[$nextid]);
  343. $row = $archives['info'][0];
  344. $arctypeRow = $archives['arctypeRow'];
  345. $attrInfo = getOneAttrInfo($allAttrInfo, $aid_arr[$nextid]);
  346. $msg .= $msg_temp = $this->createArticle($row, $globalConfig, $arctypeRow, $allTags, $has_children_Row, $attrInfo);
  347. $data['achievepage'] += 1;
  348. $limit--;
  349. $nextid++;
  350. }
  351. $data['findex'] = $nextid;
  352. } else if (!$batch) {
  353. foreach ($aid_arr as $key => $val) {
  354. $archives = getAllArchives($this->home_lang, 0, $val);
  355. $row = $archives['info'][0];
  356. $arctypeRow = $archives['arctypeRow'];
  357. $attrInfo = getOneAttrInfo($allAttrInfo, $val);
  358. $msg .= $msg_temp = $this->createArticle($row, $globalConfig, $arctypeRow, $allTags, $has_children_Row, $attrInfo);
  359. $data['achievepage'] += 1;
  360. $data['findex'] = $key;
  361. }
  362. }
  363. if ($data['allpagetotal'] == $data['achievepage']) { //生成完全部页面,删除缓存
  364. cache("aid_arr_serialize".$this->php_sessid, null);
  365. cache("channel_arr_serialize".$this->php_sessid, null);
  366. cache("article_page_total_serialize".$this->php_sessid, null);
  367. cache("article_tags_serialize".$this->php_sessid, null);
  368. cache("article_attr_info_serialize".$this->php_sessid, null);
  369. cache("article_children_row_serialize".$this->php_sessid, null);
  370. }
  371. return [$msg, $data];
  372. }
  373. /*
  374. * 获取所有需要生成静态的文档页面aid集合及相关信息
  375. * $typeid 栏目id,0:表示生成全部
  376. * $startid 起始ID(空或0表示从头开始)
  377. * $endid 结束ID(空或0表示直到结束ID)
  378. */
  379. private function getArticleAidData($typeid = 0,$startid = 0,$endid = 0){
  380. $aid_arr_serialize = cache("aid_arr_serialize".$this->php_sessid);
  381. if (empty($aid_arr_serialize)){
  382. $archivesArr = getAllArchivesAid($typeid, $this->home_lang,$startid,$endid);
  383. $aid_arr = $archivesArr['aid_arr'];
  384. $channel_arr = $archivesArr['channel_arr'];
  385. $pagetotal = count($aid_arr);
  386. $allTags = getAllTags();
  387. /*获取所有栏目是否有子栏目的数组*/
  388. $has_children_Row = Db::name('Arctype')->field('parent_id, count(id) AS total')->where([
  389. 'current_channel'=>['neq', 51], // 过滤问答模型
  390. 'is_del' => 0,
  391. ])->group('parent_id')->getAllWithIndex('parent_id');
  392. cache("aid_arr_serialize".$this->php_sessid, serialize($aid_arr), null, 'buildhtml');
  393. cache("channel_arr_serialize".$this->php_sessid, serialize($channel_arr), null, 'buildhtml');
  394. cache("article_page_total_serialize".$this->php_sessid, $pagetotal, null, 'buildhtml');
  395. cache("article_tags_serialize".$this->php_sessid, serialize($allTags), null, 'buildhtml');
  396. cache("article_children_row_serialize".$this->php_sessid, serialize($has_children_Row), null, 'buildhtml');
  397. }else{
  398. $aid_arr = unserialize($aid_arr_serialize);
  399. $channel_arr = cache("channel_arr_serialize".$this->php_sessid);
  400. $channel_arr = unserialize($channel_arr);
  401. $pagetotal = cache("article_page_total_serialize".$this->php_sessid);
  402. $allTags = unserialize(cache("article_tags_serialize".$this->php_sessid));
  403. $has_children_Row = unserialize(cache("article_children_row_serialize".$this->php_sessid));
  404. }
  405. return [
  406. 'aid_arr' => $aid_arr,
  407. 'channel_arr' => $channel_arr,
  408. 'pagetotal' => $pagetotal,
  409. 'allTags' => $allTags,
  410. 'has_children_Row' => $has_children_Row
  411. ];
  412. }
  413. /**
  414. * 获取所有详情页数据
  415. * $typeid 栏目id
  416. * $aid 文章id
  417. * $type 类型,0:aid指定的内容,1:上一篇,2:下一篇
  418. */
  419. private function getArticleData($typeid, $aid, $type = 0)
  420. {
  421. $info_serialize = cache("article_info_serialize".$this->php_sessid);
  422. if (empty($info_serialize)) {
  423. if ($type == 0) {
  424. $data = getAllArchives($this->home_lang, $typeid, $aid);
  425. } else if ($type == 1) {
  426. $data = getPreviousArchives($this->home_lang, $typeid, $aid);
  427. } else if ($type == 2) {
  428. $data = getNextArchives($this->home_lang, $typeid, $aid);
  429. }
  430. $info = $data['info'];
  431. $aid_arr = $typeid_arr = $channel_arr = [];
  432. foreach ($info as $key => $val) {
  433. $aid_arr[] = $val['aid'];
  434. $typeid_arr[] = $val['typeid'];
  435. $channel_arr[$val['channel']][] = $val['aid'];
  436. }
  437. $pagetotal = count($info);
  438. $allTags = getAllTags($aid_arr);
  439. $allAttrInfo = getAllAttrInfo($channel_arr);
  440. /*获取所有栏目是否有子栏目的数组*/
  441. $has_children_Row = model('Arctype')->hasChildren($typeid_arr);
  442. cache("article_info_serialize".$this->php_sessid, serialize($data), null, 'buildhtml');
  443. cache("article_page_total_serialize".$this->php_sessid, $pagetotal, null, 'buildhtml');
  444. cache("article_tags_serialize".$this->php_sessid, serialize($allTags), null, 'buildhtml');
  445. cache("article_attr_info_serialize".$this->php_sessid, serialize($allAttrInfo), null, 'buildhtml');
  446. cache("article_children_row_serialize".$this->php_sessid, serialize($has_children_Row), null, 'buildhtml');
  447. } else {
  448. $data = unserialize($info_serialize);
  449. $pagetotal = cache("article_page_total_serialize".$this->php_sessid);
  450. $allTags = unserialize(cache("article_tags_serialize".$this->php_sessid));
  451. $allAttrInfo = unserialize(cache("article_attr_info_serialize".$this->php_sessid));
  452. $has_children_Row = unserialize(cache("article_children_row_serialize".$this->php_sessid));
  453. }
  454. return ['data' => $data, 'pagetotal' => $pagetotal, 'allTags' => $allTags, 'allAttrInfo' => $allAttrInfo, 'has_children_Row' => $has_children_Row];
  455. }
  456. /**
  457. * 更新文档内容时候生成处理生成内容页
  458. * $typeid 栏目id
  459. * $aid 内容页id
  460. * $findex 下一次执行栏目id
  461. * $achievepage 已完成文档数
  462. * $batch 是否分批次执行,true:分批,false:不分批
  463. * limit 每次执行多少条数据
  464. * type 执行类型,0:aid指定的文档页,1:上一篇,2:下一篇
  465. *
  466. */
  467. private function handelBuildArticle($typeid, $aid = 0, $nextid = 0, $achievepage = 0, $batch = true, $limit = 20, $type = 0)
  468. {
  469. $msg = "";
  470. $globalConfig = $this->eyou['global'];
  471. $result = $this->getArticleData($typeid, $aid, $type);
  472. $info = $result['data']['info'];
  473. $arctypeRow = $result['data']['arctypeRow'];
  474. $allTags = $result['allTags'];
  475. $has_children_Row = $result['has_children_Row'];
  476. $allAttrInfo = $result['allAttrInfo'];
  477. $data['allpagetotal'] = $pagetotal = $result['pagetotal'];
  478. $data['achievepage'] = $achievepage;
  479. $data['pagetotal'] = 0;
  480. if ($batch && $pagetotal > $achievepage) {
  481. while ($limit && isset($info[$nextid])) {
  482. $row = $info[$nextid];
  483. $msg .= $msg_temp = $this->createArticle($row, $globalConfig, $arctypeRow, $allTags, $has_children_Row, $allAttrInfo);
  484. $data['achievepage'] += 1;
  485. $limit--;
  486. $nextid++;
  487. }
  488. $data['findex'] = $nextid;
  489. } else if (!$batch) {
  490. foreach ($info as $key => $row) {
  491. $msg .= $msg_temp = $this->createArticle($row, $globalConfig, $arctypeRow, $allTags, $has_children_Row, $allAttrInfo);
  492. $data['achievepage'] += 1;
  493. $data['findex'] = $key;
  494. }
  495. }
  496. if ($data['allpagetotal'] == $data['achievepage']) { //生成完全部页面,删除缓存
  497. cache("article_info_serialize".$this->php_sessid, null);
  498. cache("article_page_total_serialize".$this->php_sessid, null);
  499. cache("article_tags_serialize".$this->php_sessid, null);
  500. cache("article_attr_info_serialize".$this->php_sessid, null);
  501. cache("article_children_row_serialize".$this->php_sessid, null);
  502. }
  503. return [$msg, $data];
  504. }
  505. /*
  506. * 生成详情页静态页面
  507. */
  508. private function createArticle($result, $globalConfig, $arctypeRow, $allTags, $has_children_Row, $allAttrInfo)
  509. {
  510. $msg = "";
  511. $aid = $result['aid'];
  512. static $arc_seo_description_length = null;
  513. null === $arc_seo_description_length && $arc_seo_description_length = config('global.arc_seo_description_length');
  514. $this->request->get(['aid' => $aid]);
  515. $this->request->get(['tid' => $result['typeid']]);
  516. $arctypeInfo = $arctypeRow[$result['typeid']];
  517. /*排除文档模型与栏目模型对不上的文档 \ 问答模型 \ 外部链接跳转*/
  518. if (empty($result) || $arctypeInfo['current_channel'] != $result['channel'] || 51 == $result['channel']) {
  519. return false;
  520. }
  521. /*--end*/
  522. $arctypeInfo = model('Arctype')->parentAndTopInfo($result['typeid'], $arctypeInfo);
  523. /*自定义字段的数据格式处理*/
  524. $arctypeInfo = $this->fieldLogic->getTableFieldList($arctypeInfo, config('global.arctype_channel_id'));
  525. /*是否有子栏目,用于标记【全部】选中状态*/
  526. $arctypeInfo['has_children'] = !empty($has_children_Row[$result['typeid']]) ? 1 : 0;
  527. /*--end*/
  528. // 文档模板文件,不指定文档模板,默认以栏目设置的为主
  529. empty($result['tempview']) && $result['tempview'] = $arctypeInfo['tempview'];
  530. /*给没有type前缀的字段新增一个带前缀的字段,并赋予相同的值*/
  531. foreach ($arctypeInfo as $key => $val) {
  532. if (!preg_match('/^type/i', $key)) {
  533. $key_new = 'type' . $key;
  534. !array_key_exists($key_new, $arctypeInfo) && $arctypeInfo[$key_new] = $val;
  535. }
  536. }
  537. /*--end*/
  538. $result = array_merge($arctypeInfo, $result);
  539. //移动端详情
  540. if (!empty($result['content_ey_m'])){
  541. $result['content'] = $result['content_ey_m'];
  542. }
  543. // 获取当前页面URL
  544. $result['arcurl'] = $result['pageurl'] = $result['pageurl_m'] = '';
  545. if ($result['is_jump'] != 1) {
  546. $result['arcurl'] = $result['pageurl'] = arcurl('home/View/index', $result, true, true);
  547. $result['pageurl_m'] = pc_to_mobile_url($result['pageurl'], $result['typeid'], $result['aid']); // 获取当前页面对应的移动端URL
  548. }
  549. /*--end*/
  550. // 移动端域名
  551. $result['mobile_domain'] = '';
  552. if (!empty($this->eyou['global']['web_mobile_domain_open']) && !empty($this->eyou['global']['web_mobile_domain'])) {
  553. $result['mobile_domain'] = $this->eyou['global']['web_mobile_domain'] . '.' . $this->request->rootDomain();
  554. }
  555. $result['seo_title'] = set_arcseotitle($result['title'], $result['seo_title'], $result['typename'], $result['typeid']);
  556. $result['seo_description'] = checkStrHtml($result['seo_description']);
  557. $result['tags'] = empty($allTags[$aid]) ? '' : implode(',', $allTags[$aid]);
  558. $result['litpic'] = handle_subdir_pic($result['litpic']); // 支持子目录
  559. $result = view_logic($aid, $result['channel'], $result, $allAttrInfo); // 模型对应逻辑
  560. $result = $this->fieldLogic->getChannelFieldList($result, $result['channel']); // 自定义字段的数据格式处理
  561. if (!empty($result['users_id'])){
  562. $users_where['a.users_id'] = $result['users_id'];
  563. }elseif (!empty($result['admin_id'])){
  564. $users_where['a.admin_id'] = $result['admin_id'];
  565. }else {
  566. $users_where['a.admin_id'] = ['>',0];
  567. }
  568. $users = Db::name('users')->alias('a')->field('a.username,a.nickname,a.head_pic,b.level_name,b.level_value')->where($users_where)->join('users_level b','a.level = b.level_id','left')->find();
  569. if (!empty($users)) {
  570. $users['head_pic'] = get_default_pic($users['head_pic']);
  571. empty($users['nickname']) && $users['nickname'] = $users['username'];
  572. }
  573. $eyou = array(
  574. 'type' => $arctypeInfo,
  575. 'field' => $result,
  576. 'users' => $users,
  577. );
  578. $this->eyou = array_merge($this->eyou, $eyou);
  579. $this->assign('eyou', $this->eyou);
  580. // 模板文件
  581. $tpl = '';
  582. if (!empty($result['tempview']) && file_exists("./template/".TPL_THEME."mobile/{$result['tempview']}")) {
  583. $tpl = str_replace('.' . $this->view_suffix, '', $result['tempview']);
  584. } else {
  585. $tpl = 'view_' . $result['nid'];
  586. }
  587. $dir = $this->getArticleDir($result);
  588. if (!empty($result['htmlfilename'])) {
  589. $aid = $result['htmlfilename'];
  590. }
  591. if (4 == $this->eyou['global']['seo_html_pagename']) {
  592. if (!empty($result['ruleview'])) {
  593. $savepath = $dir;
  594. }else{
  595. $savepath = $dir . '/' . $aid . '.html';
  596. }
  597. } else {
  598. $savepath = $dir . '/' . $aid . '.html';
  599. }
  600. $makeminfo =$this->makeminfo;
  601. $seo_html_arcdir=$makeminfo['value'];
  602. $savepath = '.'.$seo_html_arcdir.trim($savepath, '.');
  603. try {
  604. $this->filePutContents($savepath, $tpl, 'mobile', 0, '/', 0, 1, $result);
  605. } catch (\Exception $e) {
  606. //$msg .= '<span>' . $savepath . '生成失败!' . $e->getMessage() . '</span><br>';
  607. }
  608. return $msg;
  609. }
  610. private function getArticleDir($row = [])
  611. {
  612. $dir = "";
  613. $seo_html_pagename = $this->eyou['global']['seo_html_pagename'];
  614. $makeminfo = $makem->info;
  615. $seo_html_arcdir=$makeminfo['value'];
  616. $dirpath = !empty($row['dirpath']) ? $row['dirpath'] : '';
  617. $aid = !empty($row['htmlfilename']) ? $row['htmlfilename'] : $row['aid'];
  618. if ($seo_html_pagename == 1) {//存放顶级目录
  619. $dirpath_arr = explode('/', $dirpath);
  620. if (count($dirpath_arr) > 2) {
  621. $dir = '.' . $seo_html_arcdir . '/' . $dirpath_arr[1];
  622. } else {
  623. $dir = '.' . $seo_html_arcdir . $dirpath;
  624. }
  625. } else if ($seo_html_pagename == 3) { //存放子级目录
  626. $dirpath_arr = explode('/', $dirpath);
  627. if (count($dirpath_arr) > 2) {
  628. $dir = '.' . $seo_html_arcdir . '/' . end($dirpath_arr);
  629. } else {
  630. $dir = '.' . $seo_html_arcdir . $dirpath;
  631. }
  632. } else if ($seo_html_pagename == 4) { //自定义存放目录
  633. $dir = '.' . $seo_html_arcdir;
  634. $diy_dirpath = !empty($row['diy_dirpath']) ? $row['diy_dirpath'] : '';
  635. if (!empty($row['ruleview'])) {
  636. $y = $m = $d = 1;
  637. if (!empty($row['add_time'])) {
  638. $y = date('Y', $row['add_time']);
  639. $m = date('m', $row['add_time']);
  640. $d = date('d', $row['add_time']);
  641. }
  642. $ruleview = ltrim($row['ruleview'], '/');
  643. $ruleview = str_ireplace("{aid}", $aid, $ruleview);
  644. $ruleview = str_ireplace("{Y}", $y, $ruleview);
  645. $ruleview = str_ireplace("{M}", $m, $ruleview);
  646. $ruleview = str_ireplace("{D}", $d, $ruleview);
  647. $ruleview = preg_replace('/{(栏目目录|typedir)}(\/?)/i', $diy_dirpath.'/', $ruleview);
  648. $ruleview = '/'.ltrim($ruleview, '/');
  649. $dir .= $ruleview;
  650. }else{
  651. $dir .= $diy_dirpath;
  652. }
  653. } else { //存放父级目录
  654. $dir = '.' . $seo_html_arcdir . $dirpath;
  655. }
  656. return $dir;
  657. }
  658. /*
  659. * 生成栏目静态页面
  660. * $id tpyeid 栏目id
  661. * $findex 下一次执行栏目id
  662. * $achievepage 已完成页数
  663. *$batch 是否分批次执行,true:分批,false:不分批
  664. *
  665. */
  666. public function buildChannel()
  667. {
  668. function_exists('set_time_limit') && set_time_limit(0);
  669. \think\Session::pause(); // 暂停session,防止session阻塞机制
  670. $id = input("param.id/d", 0); // 选中栏目ID(需要生成静态的栏目,需要生成全部时候为0)
  671. $findex = input("param.findex/d", 0); //栏目下标
  672. $index = input("param.index/d", 0); //本栏目本次执行下标
  673. $achievepage = input("param.achieve/d", 0);
  674. $parent = true;
  675. if (empty($findex) && empty($achievepage)){
  676. $this->clearCache();
  677. }
  678. // 指定文档后,生成的相关栏目
  679. $uphtmltype = input('param.uphtmltype/d');
  680. if (!empty($uphtmltype)) {
  681. $typeids = input("param.typeids/s");
  682. $id = explode(',', $typeids);
  683. $parent = false;
  684. }
  685. $data = $this->handleBuildChannelList($id, $findex, $achievepage,true,$parent,$index);
  686. $this->success($data[0], null, $data[1]);
  687. }
  688. /*
  689. * 获取栏目数据
  690. * $id 栏目id
  691. * $parent 是否获取下级栏目 true:获取,false:不获取
  692. */
  693. private function getChannelData($id, $parent = true, $aid = 0)
  694. {
  695. $info_serialize = cache("channel_info_serialize".$this->php_sessid);
  696. if (empty($info_serialize)) {
  697. $result = $this->getAllArctype($this->home_lang, $id, $this->view_suffix, $parent, $aid);
  698. $info = $result["info"];
  699. $pagetotal = intval($result["pagetotal"]);
  700. $has_children_Row = model('Arctype')->hasChildren(get_arr_column($info, 'typeid'));
  701. cache("channel_page_total_serialize".$this->php_sessid, $pagetotal, null, 'buildhtml');
  702. cache("channel_info_serialize".$this->php_sessid, serialize($info), null, 'buildhtml');
  703. cache("has_children_Row_serialize".$this->php_sessid, serialize($has_children_Row), null, 'buildhtml');
  704. } else {
  705. $info = unserialize($info_serialize);
  706. $pagetotal = cache("channel_page_total_serialize".$this->php_sessid);
  707. $has_children_Row = unserialize(cache("has_children_Row_serialize".$this->php_sessid));
  708. }
  709. return ['info' => $info, 'pagetotal' => $pagetotal, 'has_children_Row' => $has_children_Row];
  710. }
  711. /*
  712. * 处理生成栏目页
  713. * $id 栏目id
  714. * $findex 本次次执行栏目id
  715. * $achievepage 已完成页数
  716. * $batch 是否分批次执行,true:分批,false:不分批
  717. * $parent 是否获取下级栏目 true:获取,false:不获取
  718. * $index 本栏目本次执行第一条下标
  719. * $limit 单个栏目一次执行最多生成页数
  720. */
  721. private function handleBuildChannelList($id, $findex = 0, $achievepage = 0, $batch = true, $parent = true,$index = 0, $limit = 50){
  722. !empty($this->eyou['global']['seo_maxpagesize']) && $limit = $this->eyou['global']['seo_maxpagesize'];
  723. $msg = '';
  724. $result = $this->getChannelData($id, $parent);
  725. $info = $result['info'];
  726. $has_children_Row = $result['has_children_Row'];
  727. $data['allpagetotal'] = $pagetotal = $result['pagetotal'];
  728. $data['achievepage'] = $achievepage;
  729. $data['index'] = 0;
  730. $data['findex'] = $findex;
  731. $data['pagetotal'] = 1;
  732. $data['typeid'] = 0;
  733. $data['typename'] = "";
  734. $info = array_values($info);//重组数组
  735. if ($batch && $data['allpagetotal'] > $data['achievepage']) {
  736. $row = !empty($info[$findex]) ? $info[$findex] : [];
  737. // cache("buildhtml_channel_aidarr".$this->php_sessid, serialize($row['aid']), null, 'buildhtml');
  738. if (!empty($row)){
  739. list($msg_temp,$return_data) = $this->createChannelList($row, $has_children_Row,$index,$limit);
  740. $msg .= $msg_temp;
  741. $data['achievepage'] += !empty($return_data['achieve']) ? $return_data['achieve'] : 1;
  742. $data['index'] = !empty($return_data['index']) ? $return_data['index'] : 0;
  743. if (empty($return_data['index'])){
  744. $data['findex'] = $findex + 1;
  745. }else{
  746. $data['findex'] = $findex ;
  747. }
  748. }else{
  749. $data['findex'] = $findex + 1;
  750. }
  751. $data['pagetotal'] = !empty($row['pagetotal']) ? $row['pagetotal'] : 1;
  752. $data['typeid'] = !empty($row['typeid']) ? $row['typeid'] : 0;
  753. $data['typename'] = !empty($row['typename']) ? $row['typename'] :"";
  754. // cache("buildhtml_channel_aidarr".$this->php_sessid, null);
  755. } else if (!$batch) {
  756. foreach ($info as $key => $row) {
  757. $msg .= $this->createChannel($row, $has_children_Row);
  758. $data['pagetotal'] = $row['pagetotal'];
  759. $data['achievepage'] += $row['pagetotal'];
  760. $data['findex'] = $key;
  761. $data['typeid'] = !empty($row['typeid']) ? $row['typeid'] : 0;
  762. $data['typename'] = !empty($row['typename']) ? $row['typename'] :"";
  763. }
  764. }
  765. if ($data['allpagetotal'] == $data['achievepage']) { //生成完全部页面,删除缓存
  766. cache("channel_page_total_serialize".$this->php_sessid, null);
  767. cache("channel_info_serialize".$this->php_sessid, null);
  768. cache("has_children_Row_serialize".$this->php_sessid, null);
  769. }
  770. return [$msg, $data];
  771. }
  772. /*
  773. * 处理生成栏目页
  774. * $id typeid
  775. * $findex 下一次执行栏目id
  776. * $achievepage 已完成页数
  777. * $batch 是否分批次执行,true:分批,false:不分批
  778. * $parent 是否获取下级栏目 true:获取,false:不获取
  779. * $aid 文章页id,不等于0时,表示只获取文章页所在的列表页重新生成静态(在添加或者编辑文档内容时使用)
  780. */
  781. private function handleBuildChannel($id, $findex = 0, $achievepage = 0, $batch = true, $parent = true, $aid = 0)
  782. {
  783. $msg = '';
  784. $result = $this->getChannelData($id, $parent, $aid);
  785. $info = $result['info'];
  786. $has_children_Row = $result['has_children_Row'];
  787. $data['allpagetotal'] = $pagetotal = $result['pagetotal'];
  788. $data['achievepage'] = $achievepage;
  789. /***********2020 05 19 过滤并删除外部链接生成的静态页面 s*************/
  790. // foreach ($info as $k => $v) {
  791. // if ($v['is_part'] == 1 || $v['nid'] == 'ask') {//外部链接或问答模型
  792. // unset($info[$k]);//从数组里移除
  793. // $dir = ROOT_PATH . trim($v['dirpath'], '/');
  794. // if (!empty($v['dirpath']) && true == is_dir($dir)) {//判断是否生成过文件夹,文件夹存在则删除
  795. // $this->deldir($dir);
  796. // }
  797. // }
  798. // }
  799. // $info = array_values($info);//重组数组
  800. /***********2020 05 19 新增 e*************/
  801. if ($batch && $data['allpagetotal'] > $data['achievepage']) {
  802. $row = !empty($info[$findex]) ? $info[$findex] : [];
  803. !empty($row) && $msg .= $msg_temp = $this->createChannel($row, $has_children_Row);
  804. $data['pagetotal'] = !empty($row['pagetotal']) ? $row['pagetotal'] : 1;
  805. $data['achievepage'] += !empty($row['pagetotal']) ? $row['pagetotal'] : 1;
  806. $data['findex'] = $findex + 1;
  807. $data['typeid'] = !empty($row['typeid']) ? $row['typeid'] : 0;
  808. } else if (!$batch) {
  809. foreach ($info as $key => $row) {
  810. $msg .= $msg_temp = $this->createChannel($row, $has_children_Row, $aid);
  811. $data['pagetotal'] = $row['pagetotal'];
  812. $data['achievepage'] += $row['pagetotal'];
  813. $data['findex'] = $key;
  814. $data['typeid'] = $row['typeid'];
  815. }
  816. }
  817. if ($data['allpagetotal'] == $data['achievepage']) { //生成完全部页面,删除缓存
  818. cache("channel_page_total_serialize".$this->php_sessid, null);
  819. cache("channel_info_serialize".$this->php_sessid, null);
  820. cache("has_children_Row_serialize".$this->php_sessid, null);
  821. }
  822. return [$msg, $data];
  823. }
  824. /*
  825. * 分批生成栏目页面
  826. * $index 当前执行的页码下标
  827. * $limit 每次最多生成个数
  828. */
  829. private function createChannelList($row, $has_children_Row,$index = 0, $limit = 10)
  830. {
  831. $msg = "";
  832. $data = [
  833. 'achieve' => 0,
  834. 'index' => 0
  835. ];
  836. $seo_html_listname = $this->eyou['global']['seo_html_listname'];
  837. $seo_html_arcdir = '/';
  838. $tid = $row['typeid'];
  839. $this->request->get(['tid' => $tid]);
  840. $row = $this->lists_logic($row, $has_children_Row); // 模型对应逻辑
  841. $eyou = array(
  842. 'field' => $row,
  843. );
  844. $this->eyou = array_merge($this->eyou, $eyou);
  845. $this->assign('eyou', $this->eyou);
  846. // 模板文件
  847. $tpl = '';
  848. if (!empty($row['templist']) && file_exists("./template/".TPL_THEME."mobile/{$row['templist']}")) {
  849. $tpl = str_replace('.' . $this->view_suffix, '', $row['templist']);
  850. } else {
  851. $tpl = 'lists_' . $row['nid'];
  852. }
  853. if (in_array($row['current_channel'], [6, 8])) {
  854. //留言模型或单页模型,不存在多页
  855. $this->request->get(['page' => '']);
  856. $dirpath = explode('/', $eyou['field']['dirpath']);
  857. $dirpath_end = end($dirpath);
  858. if ($seo_html_listname == 1) { //存放顶级目录
  859. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath[1] . "/lists_" . $eyou['field']['typeid'] . ".html";
  860. } else if ($seo_html_listname == 3) { //存放子级目录
  861. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath_end . "/lists_" . $eyou['field']['typeid'] . ".html";
  862. } else if ($seo_html_listname == 4) { //自定义存放目录
  863. $savepath = '.' . $seo_html_arcdir;
  864. $diy_dirpath = !empty($eyou['field']['diy_dirpath']) ? $eyou['field']['diy_dirpath'] : '';
  865. if (!empty($eyou['field']['rulelist'])) {
  866. $rulelist = ltrim($eyou['field']['rulelist'], '/');
  867. $rulelist = str_replace("{tid}", $eyou['field']['typeid'], $rulelist);
  868. $rulelist = str_replace("{page}", '', $rulelist);
  869. $rulelist = preg_replace('/{(栏目目录|typedir)}(\/?)/i', $diy_dirpath.'/', $rulelist);
  870. $rulelist = '/'.ltrim($rulelist, '/');
  871. $rulelist = preg_replace('/([\/]+)/i', '/', $rulelist);
  872. $savepath .= $rulelist;
  873. }else{
  874. $eyou['field']['rulelist'] = '{栏目目录}/list_{tid}_{page}.html';
  875. $savepath .= $diy_dirpath . '/' . 'list_' . $eyou['field']['typeid'] . ".html";
  876. }
  877. } else {
  878. $savepath = '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/' . 'lists_' . $eyou['field']['typeid'] . ".html";
  879. }
  880. try {
  881. $makeminfo =$this->makeminfo;
  882. $seo_html_arcdir=$makeminfo['value'];
  883. $savepath = '.'.$seo_html_arcdir.trim($savepath, '.');
  884. // echo $savepath;
  885. // echo '----';
  886. // echo $seo_html_arcdir;
  887. // die;
  888. $this->filePutContents($savepath, $tpl, 'mobile', 0, '/', 0, 1, $row);
  889. if ($seo_html_listname == 3) {
  890. @copy($savepath, '.' . $seo_html_arcdir . '/' . $dirpath_end . '/index.html');
  891. @unlink($savepath);
  892. } else if ($seo_html_listname == 4) {
  893. if (preg_match('/^{(栏目目录|typedir)}\/list_{tid}_{page}\.html$/i', $eyou['field']['rulelist'])) {
  894. $dst_savepath = preg_replace('/\/([^\/]+)$/i', '/index.html', $savepath);
  895. @copy($savepath, $dst_savepath);
  896. @unlink($savepath);
  897. }
  898. } else if ($seo_html_listname == 2 || count($dirpath) < 3) {
  899. @copy($savepath, '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/index.html');
  900. @unlink($savepath);
  901. }
  902. } catch (\Exception $e) {
  903. //$msg .= '<span>' . $savepath . '生成失败!' . $e->getMessage() . '</span><br>';
  904. }
  905. $data['achieve'] += 1;
  906. }else { //多条信息的栏目
  907. $totalpage = $row['pagetotal'];
  908. $lastPage = cache("eyou-TagList-lastPage_{$tid}_{$this->php_sessid}"); //本栏目真实条数
  909. $differ = 0; //实际页数和预计页数的差集
  910. while ($limit && $totalpage > $index) {
  911. $msg .= $this->createMultipageChannel($index+1, $tid, $row, $has_children_Row, $seo_html_listname, $seo_html_arcdir, $tpl);
  912. $limit--;
  913. $index++;
  914. $data['achieve'] += 1;
  915. if (!empty($lastPage)) {
  916. if ($totalpage > $lastPage) {
  917. $differ = $totalpage - $lastPage;
  918. $totalpage = $lastPage;
  919. }
  920. }
  921. }
  922. if ($totalpage <= $index){ //已经执行完成本栏目
  923. $data['index'] = 0;
  924. $data['achieve'] += $differ;
  925. }else{
  926. $data['index'] = $index;
  927. }
  928. }
  929. return [$msg,$data];
  930. }
  931. /*
  932. * 生成栏目页面
  933. */
  934. private function createChannel($row, $has_children_Row, $aid = 0)
  935. {
  936. $msg = "";
  937. $seo_html_listname = $this->eyou['global']['seo_html_listname'];
  938. // $seo_html_arcdir = $this->eyou['global']['seo_html_arcdir'];
  939. $tid = $row['typeid'];
  940. $this->request->get(['tid' => $tid]);
  941. $seo_html_arcdir = '';
  942. $row = $this->lists_logic($row, $has_children_Row); // 模型对应逻辑
  943. $eyou = array(
  944. 'field' => $row,
  945. );
  946. $this->eyou = array_merge($this->eyou, $eyou);
  947. $this->assign('eyou', $this->eyou);
  948. // 模板文件
  949. $tpl = '';
  950. if (!empty($row['templist']) && file_exists("./template/".TPL_THEME."mobile/{$row['templist']}")) {
  951. $tpl = str_replace('.' . $this->view_suffix, '', $row['templist']);
  952. } else {
  953. $tpl = 'lists_' . $row['nid'];
  954. }
  955. if (in_array($row['current_channel'], [6, 8])) { //留言模型或单页模型,不存在多页
  956. $this->request->get(['page' => '']);
  957. $dirpath = explode('/', $eyou['field']['dirpath']);
  958. $dirpath_end = end($dirpath);
  959. if ($seo_html_listname == 1) { //存放顶级目录
  960. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath[1] . "/lists_" . $eyou['field']['typeid'] . ".html";
  961. } else if ($seo_html_listname == 3) { //存放子级目录
  962. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath_end . "/lists_" . $eyou['field']['typeid'] . ".html";
  963. } else if ($seo_html_listname == 4) { //自定义存放目录
  964. $savepath = '.' . $seo_html_arcdir;
  965. $diy_dirpath = !empty($eyou['field']['diy_dirpath']) ? $eyou['field']['diy_dirpath'] : '';
  966. if (!empty($eyou['field']['rulelist'])) {
  967. $rulelist = ltrim($eyou['field']['rulelist'], '/');
  968. $rulelist = str_replace("{tid}", $eyou['field']['typeid'], $rulelist);
  969. $rulelist = str_replace("{page}", '', $rulelist);
  970. $rulelist = preg_replace('/{(栏目目录|typedir)}(\/?)/i', $diy_dirpath.'/', $rulelist);
  971. $rulelist = '/'.ltrim($rulelist, '/');
  972. $rulelist = preg_replace('/([\/]+)/i', '/', $rulelist);
  973. $savepath .= $rulelist;
  974. }else{
  975. $eyou['field']['rulelist'] = '{栏目目录}/lists_{tid}_{page}.html';
  976. $savepath .= $diy_dirpath . '/' . 'list_' . $eyou['field']['typeid'] . ".html";
  977. }
  978. } else {
  979. $savepath = '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/' . 'lists_' . $eyou['field']['typeid'] . ".html";
  980. }
  981. try {
  982. $makeminfo =$this->makeminfo;
  983. $seo_html_arcdir=$makeminfo['value'];
  984. $savepath = '.'.$seo_html_arcdir.trim($savepath, '.');
  985. $this->filePutContents($savepath, $tpl, 'mobile', 0, '/', 0, 1, $row);
  986. if ($seo_html_listname == 3) {
  987. @copy($savepath, '.' . $seo_html_arcdir . '/' . $dirpath_end . '/index.html');
  988. @unlink($savepath);
  989. } else if ($seo_html_listname == 4) {
  990. if (preg_match('/^{(栏目目录|typedir)}\/list_{tid}_{page}\.html$/i', $eyou['field']['rulelist'])) {
  991. $dst_savepath = preg_replace('/\/([^\/]+)$/i', '/index.html', $savepath);
  992. @copy($savepath, $dst_savepath);
  993. @unlink($savepath);
  994. }
  995. } else if ($seo_html_listname == 2 || count($dirpath) < 3) {
  996. @copy($savepath, '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/index.html');
  997. @unlink($savepath);
  998. }
  999. } catch (\Exception $e) {
  1000. //$msg .= '<span>' . $savepath . '生成失败!' . $e->getMessage() . '</span><br>';
  1001. }
  1002. } else if (!empty($aid)) { //只更新aid所在的栏目页码
  1003. $orderby = getOrderBy($row['orderby'], $row['ordermode']);
  1004. $limit = getLocationPages($tid, $aid, $orderby);
  1005. $i = !empty($limit) ? ceil($limit / $row['pagesize']) : 1;
  1006. $msg .= $this->createMultipageChannel($i, $tid, $row, $has_children_Row, $seo_html_listname, $seo_html_arcdir, $tpl);
  1007. } else { //多条信息的栏目
  1008. $totalpage = $row['pagetotal'];
  1009. for ($i = 1; $i <= $totalpage; $i++) {
  1010. $msg .= $this->createMultipageChannel($i, $tid, $row, $has_children_Row, $seo_html_listname, $seo_html_arcdir, $tpl);
  1011. $lastPage = cache("eyou-TagList-lastPage_{$tid}_{$this->php_sessid}");
  1012. if (!empty($lastPage)) {
  1013. $totalpage = $lastPage;
  1014. }
  1015. }
  1016. }
  1017. return $msg;
  1018. }
  1019. /*
  1020. * 创建有文档列表模型的静态栏目页面
  1021. */
  1022. private function createMultipageChannel($i, $tid, $row, $has_children_Row, $seo_html_listname, $seo_html_arcdir, $tpl)
  1023. {
  1024. $makeminfo =$this->makeminfo;
  1025. // 二级目录模式
  1026. if($makeminfo['web_url_model'] !=2){
  1027. $seo_html_arcdir = $makeminfo['value'];
  1028. }
  1029. $msg = "";
  1030. $this->request->get(['page' => $i]);
  1031. $row['seo_title'] = set_typeseotitle($row['typename'], $row['seo_title_tmp']);
  1032. // $row = $this->lists_logic($row, $has_children_Row); // 模型对应逻辑
  1033. $eyou = array(
  1034. 'field' => $row,
  1035. );
  1036. $this->eyou = array_merge($this->eyou, $eyou);
  1037. $this->assign('eyou', $this->eyou);
  1038. $dirpath = explode('/', $eyou['field']['dirpath']);
  1039. $dirpath_end = end($dirpath);
  1040. if ($seo_html_listname == 1) { //存放顶级目录
  1041. $dir = '.' . $seo_html_arcdir . '/' . $dirpath[1];
  1042. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath[1] . "/lists_" . $eyou['field']['typeid'];
  1043. } else if ($seo_html_listname == 3) { //存放子级目录
  1044. $dir = '.' . $seo_html_arcdir . '/' . $dirpath_end;
  1045. $savepath = '.' . $seo_html_arcdir . '/' . $dirpath_end . "/lists_" . $eyou['field']['typeid'];
  1046. } else if ($seo_html_listname == 4) { //自定义存放目录
  1047. $dir = $savepath = '.' . $seo_html_arcdir;
  1048. $diy_dirpath = !empty($eyou['field']['diy_dirpath']) ? $eyou['field']['diy_dirpath'] : '';
  1049. if (!empty($eyou['field']['rulelist'])) {
  1050. $rulelist = ltrim($eyou['field']['rulelist'], '/');
  1051. $rulelist = str_replace("{tid}", $eyou['field']['typeid'], $rulelist);
  1052. $rulelist = str_replace("{page}", $i, $rulelist);
  1053. $rulelist = preg_replace('/{(栏目目录|typedir)}(\/?)/i', $diy_dirpath.'/', $rulelist);
  1054. $rulelist = '/'.ltrim($rulelist, '/');
  1055. $dir .= preg_replace('/\/([\/]*)([^\/]*)$/i', '', $rulelist);
  1056. $savepath .= $rulelist;
  1057. }else{
  1058. $dir .= $diy_dirpath;
  1059. $savepath .= $diy_dirpath . '/' . 'list_' . $eyou['field']['typeid'];
  1060. }
  1061. } else {
  1062. $dir = '.' . $seo_html_arcdir . $eyou['field']['dirpath'];
  1063. $savepath = '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/' . 'lists_' . $eyou['field']['typeid'];
  1064. }
  1065. if ($seo_html_listname != 4 || empty($eyou['field']['rulelist'])) {
  1066. if ($i > 1) {
  1067. $savepath .= '_' . $i . '.html';
  1068. } else {
  1069. $savepath .= '.html';
  1070. }
  1071. }
  1072. $top = 1;
  1073. if ($i > 1 && $seo_html_listname == 1 && count($dirpath) > 2) {
  1074. $top = 2;
  1075. } else if ($i > 1 && $seo_html_listname == 3) {
  1076. $top = 1;
  1077. } else if ($i > 1 && $seo_html_listname == 4) {
  1078. $top = 1;
  1079. }
  1080. // $seo_html_arcdir ='/m';
  1081. // $savepath = './m'.trim($savepath, '.');
  1082. //获取手机路径
  1083. if($makeminfo['web_url_model'] ==2){
  1084. $seo_html_arcdir=$makeminfo['value'];
  1085. $savepath = '.'.$seo_html_arcdir.trim($savepath, '.');
  1086. }else{
  1087. $savepath = '.'.trim($savepath, '.');
  1088. }
  1089. // echo $seo_html_arcdir;
  1090. // echo '----';
  1091. // echo $savepath;
  1092. // die;
  1093. try {
  1094. $this->filePutContents($savepath, $tpl, 'mobile', $i, $dir, $tid, $top, $row);
  1095. if ($i == 1 && $seo_html_listname == 3) {
  1096. @copy($savepath, '.' . $seo_html_arcdir . '/' . $dirpath_end . '/index.html');
  1097. @unlink($savepath);
  1098. } else if ($seo_html_listname == 4) {
  1099. if ($i == 1) {
  1100. $dst_savepath = preg_replace('/\/([^\/]+)$/i', '/index.html', $savepath);
  1101. @copy($savepath, $dst_savepath);
  1102. @unlink($savepath);
  1103. } else if ($i > 1) {
  1104. if (!empty($eyou['field']['rulelist']) && !preg_match('/{page}/i', $eyou['field']['rulelist'])) { // 没有分页变量的情况
  1105. @unlink($savepath);
  1106. }
  1107. }
  1108. } else if ($i == 1 && ($seo_html_listname == 2 || count($dirpath) < 3)) {
  1109. @copy($savepath, '.' . $seo_html_arcdir . $eyou['field']['dirpath'] . '/index.html');
  1110. @unlink($savepath);
  1111. }
  1112. } catch (\Exception $e) {
  1113. //$msg .= '<span>' . $savepath . '生成失败!' . $e->getMessage() . '</span><br>';
  1114. }
  1115. return $msg;
  1116. }
  1117. /**
  1118. * 更新静态生成页
  1119. * @param int $aid 文章id
  1120. * @param int $typeid 栏目id
  1121. * @return boolean
  1122. * $del_ids 删除的文章数组
  1123. */
  1124. public function upHtml()
  1125. {
  1126. \think\Session::pause(); // 暂停session,防止session阻塞机制
  1127. $aid = input("param.aid/d");
  1128. $typeid = input("param.typeid/d");
  1129. $del_ids = input('param.del_ids/a');
  1130. $type = input('param.type/s');
  1131. $lang = input("param.lang/s", 'cn');
  1132. $seo_uphtml_after_pernext = input("param.seo_uphtml_after_pernext/d");
  1133. $param = input('param.');
  1134. $this->php_sessid .= 'upHtml'.json_encode($param);
  1135. /*由于全站共用删除JS代码,这里排除不能发布文档的模型的控制器*/
  1136. if ('index' != $type) {
  1137. $ctl_name = input("param.ctl_name/s");
  1138. $channeltypeRow = Db::name('channeltype')
  1139. ->where('nid', 'NOT IN', ['guestbook', 'single'])
  1140. ->column('ctl_name');
  1141. array_push($channeltypeRow, 'Archives', 'Arctype', 'Custom');
  1142. if (!in_array($ctl_name, $channeltypeRow)) {
  1143. $this->error("排除非发布文档的模型");
  1144. }
  1145. }
  1146. /*end*/
  1147. $seo_pseudo = $this->eyou['global']['seo_pseudo'];
  1148. $seo_html_pagename = $this->eyou['global']['seo_html_pagename'];
  1149. $this->clearCache();
  1150. if ($seo_pseudo != 2) {
  1151. $this->error("当前非静态模式,不做静态处理");
  1152. }
  1153. if (!empty($del_ids)) { //删除文章页面
  1154. $info = Db::name('archives')->field('b.dirpath,b.diy_dirpath,b.rulelist,b.ruleview,a.*')
  1155. ->alias('a')
  1156. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  1157. ->where([
  1158. 'a.aid' => ['in', $del_ids],
  1159. 'a.lang' => $lang,
  1160. ])
  1161. ->select();
  1162. foreach ($info as $key => $row) {
  1163. $filename = $row['aid'];
  1164. if (!empty($row['htmlfilename'])) {
  1165. $filename = $row['htmlfilename'];
  1166. }
  1167. $dir = $this->getArticleDir($row);
  1168. if (4 == $seo_html_pagename) {
  1169. if (!empty($row['ruleview'])) {
  1170. $path = $dir;
  1171. }else{
  1172. $path = $dir . "/" . $filename . ".html";
  1173. }
  1174. } else {
  1175. $path = $dir . "/" . $filename . ".html";
  1176. }
  1177. if (file_exists($path)) @unlink($path);
  1178. }
  1179. } else if (!empty($aid) && !empty($typeid)) { //变更文档信息,更新文档页及相关的栏目页
  1180. if ('view' == $type) {
  1181. $this->handelBuildArticle($typeid, $aid, 0, 0, false, 1, 0);
  1182. if (1 == $seo_uphtml_after_pernext) {
  1183. $this->handelBuildArticle($typeid, $aid, 0, 0, false, 1, 1); // 更新上篇
  1184. $this->handelBuildArticle($typeid, $aid, 0, 0, false, 1, 2); // 更新下篇
  1185. }
  1186. } else if ('lists' == $type) {
  1187. $data = $this->handleBuildChannel($typeid, 0, 0, false, false, $aid);
  1188. } else {
  1189. $this->handleBuildChannel($typeid, 0, 0, false, false, $aid);
  1190. $this->handelBuildArticle($typeid, $aid, 0, 0, false);
  1191. }
  1192. } else if (!empty($typeid)) { //变更栏目信息,更新栏目页
  1193. $this->handleBuildIndex();
  1194. $data = $this->handleBuildChannel($typeid, 0, 0, false, false);
  1195. } else if ('index' == $type) {
  1196. $this->handleBuildIndex();
  1197. }
  1198. $this->success("静态页面生成完成");
  1199. }
  1200. /*
  1201. * 拓展页面相关信息
  1202. */
  1203. private function lists_logic($result = [], $has_children_Row = [])
  1204. {
  1205. if (empty($result)) {
  1206. return [];
  1207. }
  1208. $tid = $result['typeid'];
  1209. switch ($result['current_channel']) {
  1210. case '6': // 单页模型
  1211. {
  1212. $arctype_info = model('Arctype')->parentAndTopInfo($tid, $result);
  1213. if ($arctype_info) {
  1214. // 读取当前栏目的内容,否则读取每一级第一个子栏目的内容,直到有内容或者最后一级栏目为止。
  1215. $archivesModel = new \app\home\model\Archives();
  1216. $result_new = $archivesModel->readContentFirst($tid);
  1217. // 阅读权限 或 外部链接跳转
  1218. if ($result_new['arcrank'] == -1 || $result_new['is_part'] == 1) {
  1219. return false;
  1220. }
  1221. /*自定义字段的数据格式处理*/
  1222. $result_new = $this->fieldLogic->getChannelFieldList($result_new, $result_new['current_channel']);
  1223. /*--end*/
  1224. $result = array_merge($arctype_info, $result_new);
  1225. $result['templist'] = !empty($arctype_info['templist']) ? $arctype_info['templist'] : 'lists_' . $arctype_info['nid'];
  1226. $result['dirpath'] = $arctype_info['dirpath'];
  1227. $result['diy_dirpath'] = $arctype_info['diy_dirpath'];
  1228. $result['typeid'] = $arctype_info['typeid'];
  1229. $result['rulelist'] = $arctype_info['rulelist'];
  1230. }
  1231. break;
  1232. }
  1233. default:
  1234. {
  1235. $result = model('Arctype')->parentAndTopInfo($tid, $result);
  1236. break;
  1237. }
  1238. }
  1239. if (!empty($result)) {
  1240. /*自定义字段的数据格式处理*/
  1241. $result = $this->fieldLogic->getTableFieldList($result, config('global.arctype_channel_id'));
  1242. /*--end*/
  1243. }
  1244. /*是否有子栏目,用于标记【全部】选中状态*/
  1245. $result['has_children'] = !empty($has_children_Row[$tid]) ? 1 : 0;
  1246. /*--end*/
  1247. // seo
  1248. if (!isset($result['seo_title_tmp'])) {
  1249. $result['seo_title_tmp'] = $result['seo_title'];
  1250. }
  1251. $result['seo_title'] = set_typeseotitle($result['typename'], $result['seo_title_tmp']);
  1252. $result['pageurl'] = $result['typeurl']; // 获取当前页面URL
  1253. $result['pageurl_m'] = pc_to_mobile_url($result['pageurl'], $result['typeid']); // 获取当前页面对应的移动端URL
  1254. // 移动端域名
  1255. $result['mobile_domain'] = '';
  1256. if (!empty($this->eyou['global']['web_mobile_domain_open']) && !empty($this->eyou['global']['web_mobile_domain'])) {
  1257. $result['mobile_domain'] = $this->eyou['global']['web_mobile_domain'] . '.' . $this->request->rootDomain();
  1258. }
  1259. /*给没有type前缀的字段新增一个带前缀的字段,并赋予相同的值*/
  1260. foreach ($result as $key => $val) {
  1261. if (!preg_match('/^type/i', $key)) {
  1262. $key_new = 'type' . $key;
  1263. !array_key_exists($key_new, $result) && $result[$key_new] = $val;
  1264. }
  1265. }
  1266. /*--end*/
  1267. return $result;
  1268. }
  1269. /**
  1270. * 生成静态模式下且PC和移动端模板分离,就自动给PC端加上跳转移动端的JS代码
  1271. * @access public
  1272. */
  1273. private function pc_to_mobile_js($html = '', $result = [])
  1274. {
  1275. static $other_pcwapjs = null;
  1276. null === $other_pcwapjs && $other_pcwapjs = tpCache('other.other_pcwapjs');
  1277. if (!empty($other_pcwapjs)) {
  1278. return $html;
  1279. }
  1280. if (file_exists('./template/'.TPL_THEME.'mobile')) { // 分离式模板
  1281. /*是否开启手机站域名,并且配置*/
  1282. if (!empty($this->eyou['global']['web_mobile_domain_open']) && !empty($this->eyou['global']['web_mobile_domain'])) {
  1283. $domain = $this->eyou['global']['web_mobile_domain'] . '.' . $this->request->rootDomain();
  1284. } else {
  1285. $domain = true;
  1286. }
  1287. /*end*/
  1288. $aid = input('param.aid/d');
  1289. $tid = input('param.tid/d');
  1290. if (!empty($aid)) { // 内容页
  1291. $url = url('home/View/index', ['aid' => $aid], true, $domain, 1, 1, 0);
  1292. } else if (!empty($tid)) { // 列表页
  1293. $url = url('home/Lists/index', ['tid' => $tid], true, $domain, 1, 1, 0);
  1294. } else { // 首页
  1295. $url = $this->request->scheme().'://'. $this->request->host(true) . ROOT_DIR . '/index.php';
  1296. }
  1297. $jsStr = <<<EOF
  1298. <meta http-equiv="mobile-agent" content="format=xhtml;url={$url}">
  1299. <script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else{if(/applewebkit.*mobile/i.test(navigator.userAgent.toLowerCase()) || (/midp|symbianos|nokia|samsung|lg|nec|tcl|alcatel|bird|dbtel|dopod|philips|haier|lenovo|mot-|nokia|sonyericsson|sie-|amoi|zte/.test(navigator.userAgent.toLowerCase()))){try{if(/android|windows phone|webos|iphone|ipod|blackberry/i.test(navigator.userAgent.toLowerCase())){window.location.href="{$url}";}else if(/ipad/i.test(navigator.userAgent.toLowerCase())){}else{}}catch(e){}}}</script>
  1300. EOF;
  1301. $html = str_ireplace('</head>', $jsStr . "\n</head>", $html);
  1302. } else { // 响应式模板
  1303. // 开启手机站域名,且配置
  1304. if (!empty($this->eyou['global']['web_mobile_domain_open']) && !empty($this->eyou['global']['web_mobile_domain'])) {
  1305. if (empty($result['pageurl'])) {
  1306. $url = $this->request->subDomain($this->eyou['global']['web_mobile_domain']) . ROOT_DIR . '/index.php';
  1307. } else {
  1308. $url = !preg_match('/^(http(s?):)?\/\/(.*)$/i', $result['pageurl']) ? $this->request->domain() . $result['pageurl'] : $result['pageurl'];
  1309. $url = preg_replace('/^(.*)(\/\/)([^\/]*)(\.?)(' . $this->request->rootDomain() . ')(.*)$/i', '${1}${2}' . $this->eyou['global']['web_mobile_domain'] . '.${5}${6}', $url);
  1310. }
  1311. $mobileDomain = $this->eyou['global']['web_mobile_domain'] . '.' . $this->request->rootDomain();
  1312. $jsStr = <<<EOF
  1313. <meta http-equiv="mobile-agent" content="format=xhtml;url={$url}">
  1314. <script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else{if(/applewebkit.*mobile/i.test(navigator.userAgent.toLowerCase()) || (/midp|symbianos|nokia|samsung|lg|nec|tcl|alcatel|bird|dbtel|dopod|philips|haier|lenovo|mot-|nokia|sonyericsson|sie-|amoi|zte/.test(navigator.userAgent.toLowerCase()))){try{if(/android|windows phone|webos|iphone|ipod|blackberry/i.test(navigator.userAgent.toLowerCase())){if(window.location.toString().indexOf('{$mobileDomain}') == -1){window.location.href="{$url}";}}else if(/ipad/i.test(navigator.userAgent.toLowerCase())){}else{}}catch(e){}}}</script>
  1315. EOF;
  1316. $html = str_ireplace('</head>', $jsStr . "\n</head>", $html);
  1317. }
  1318. }
  1319. return $html;
  1320. }
  1321. /**
  1322. * 删除文件夹
  1323. * @param $dir
  1324. * @return bool
  1325. */
  1326. private function deldir($dir)
  1327. {
  1328. //先删除目录下的文件:
  1329. $fileArr = glob($dir.'/*.html');
  1330. if (!empty($fileArr)) {
  1331. foreach ($fileArr as $key => $val) {
  1332. !empty($val) && @unlink($val);
  1333. }
  1334. }
  1335. $fileArr = glob($dir.'/*');
  1336. if(empty($fileArr)){ //目录为空
  1337. rmdir($dir); // 删除空目录
  1338. }
  1339. return true;
  1340. }
  1341. /*getAllArctype*/
  1342. function getAllArctype($home_lang,$id,$view_suffix,$parent = true,$aid = 0){
  1343. $map = [];
  1344. if (!empty($id)){
  1345. if (is_array($id)) {
  1346. $map['a.id'] = ['IN', $id];
  1347. } else {
  1348. $map['a.id'] = $id;
  1349. }
  1350. }
  1351. $map['a.lang'] = $home_lang;
  1352. $map['a.is_del'] = 0;
  1353. $map['a.status'] = 1;
  1354. $info = \think\Db::name('arctype')->field('c.*, a.*, a.id as typeid')
  1355. ->alias('a')
  1356. ->join('__CHANNELTYPE__ c', 'c.id = a.current_channel', 'LEFT')
  1357. ->where($map)
  1358. ->order("a.grade desc")
  1359. ->cache(true,EYOUCMS_CACHE_TIME,"arctype")
  1360. ->select();
  1361. if (!empty($id) && $parent && $aid == 0) {
  1362. // $aid > 0 表示栏目生成不生成子栏目
  1363. $this->getAllChildArctype($info,$id);
  1364. }
  1365. $info = $this->getAllArctypeCount($home_lang,$info,$id,$view_suffix,$aid);
  1366. return $info;
  1367. }
  1368. //递归查询所有的子类
  1369. function getAllChildArctype(&$arctype_child_all,$id){
  1370. $where['a.is_del'] = 0;
  1371. $where['a.status'] = 1;
  1372. $where['a.parent_id'] = $id;
  1373. $arctype_child = \think\Db::name('arctype')->field('c.*, a.*, a.id as typeid')
  1374. ->alias('a')
  1375. ->join('__CHANNELTYPE__ c', 'c.id = a.current_channel', 'LEFT')
  1376. ->where($where)
  1377. ->select();
  1378. if(!empty($arctype_child)){
  1379. $arctype_child_all = array_merge($arctype_child,$arctype_child_all);
  1380. for($i=0;$i<count($arctype_child);$i++){
  1381. $this->getAllChildArctype($arctype_child_all,$arctype_child[$i]['typeid']);
  1382. }
  1383. }
  1384. }
  1385. function getAllArctypeCount($home_lang,$info,$id = 0,$view_suffix = ".htm",$aid = 0)
  1386. {
  1387. /**
  1388. * 这里统计每个栏目的文档数有两种方法
  1389. * 1、当文档数量少于10W时,执行第一种方法,在循环外部查询一条sql统计出栏目的文档数
  1390. * 2、当文档数量大于10W时,执行第二种方法,在循环里面每次执行一个统计当前栏目的文档数sql
  1391. * @var integer
  1392. */
  1393. $method_mode = 1; // 默认是第一种方法
  1394. $max_aid = \think\Db::name('archives')->max('aid'); // 取出文档的最大数量,已最大文档ID来大概计算
  1395. if ($max_aid > 100000) {
  1396. $method_mode = 2;
  1397. }
  1398. $map_arc = [];
  1399. // 是否更新子栏目
  1400. $seo_upnext = tpCache('seo.seo_upnext');
  1401. $web_stypeid_open = tpCache('web.web_stypeid_open'); // 是否开启副栏目
  1402. if (1 == $method_mode && $id) {
  1403. if (empty($web_stypeid_open)) {
  1404. $map_arc['typeid'] = array('in',get_arr_column($info,'typeid'));
  1405. } else {
  1406. $typeids_tmp = get_arr_column($info,'typeid');
  1407. $typeids_tmp = implode(',', $typeids_tmp);
  1408. $map_arc[] = \think\Db::raw(" ( typeid IN ({$typeids_tmp}) OR CONCAT(',', stypeid, ',') LIKE '%,{$id},%' ) ");
  1409. }
  1410. }
  1411. // 可发布文档列表的频道模型
  1412. static $new_channel = null;
  1413. if (null === $new_channel) {
  1414. $allow_release_channel = config('global.allow_release_channel');
  1415. $arctypeRow = \think\Db::name('arctype')->field('channeltype,current_channel')->select();
  1416. foreach ($arctypeRow as $key => $val) {
  1417. if (in_array($val['channeltype'], $allow_release_channel)) {
  1418. $new_channel[] = $val['channeltype'];
  1419. }
  1420. if (in_array($val['current_channel'], $allow_release_channel)) {
  1421. $new_channel[] = $val['current_channel'];
  1422. }
  1423. }
  1424. $new_channel = array_unique($new_channel);
  1425. }
  1426. !empty($new_channel) && $map_arc['a.channel'] = ['IN', $new_channel];
  1427. $map_arc['a.arcrank'] = ['egt', 0];
  1428. $map_arc['a.status'] = 1;
  1429. $map_arc['a.is_del'] = 0;
  1430. $map_arc['a.lang'] = $home_lang;
  1431. /*定时文档显示插件*/
  1432. if (is_dir('./weapp/TimingTask/')) {
  1433. $TimingTaskRow = model('Weapp')->getWeappList('TimingTask');
  1434. if (!empty($TimingTaskRow['status']) && 1 == $TimingTaskRow['status']) {
  1435. $map_arc['a.add_time'] = array('elt', getTime()); // 只显当天或之前的文档
  1436. }
  1437. }
  1438. /*end*/
  1439. if (1 == $method_mode) { // 方法1
  1440. $count_type = [];
  1441. $archivesList = \think\Db::name('archives')->alias('a')->field("typeid,stypeid")->where($map_arc)->order('typeid asc')->select();
  1442. foreach ($archivesList as $key => $val) {
  1443. if (!isset($count_type[$val['typeid']])) {
  1444. $count_type[$val['typeid']] = [
  1445. 'typeid' => $val['typeid'],
  1446. 'count' => 1,
  1447. ];
  1448. } else {
  1449. $count_type[$val['typeid']]['count']++;
  1450. }
  1451. // 开启副栏目
  1452. if (!empty($web_stypeid_open) && !empty($val['stypeid'])) {
  1453. $stypeids = explode(',', $val['stypeid']);
  1454. $arr_index = array_search($val['typeid'], $stypeids);
  1455. if (is_numeric($arr_index) && 0 <= $arr_index) {
  1456. unset($stypeids[$arr_index]);
  1457. }
  1458. foreach ($stypeids as $_k => $_v) {
  1459. if (!isset($count_type[$_v])) {
  1460. $count_type[$_v] = [
  1461. 'typeid' => $_v,
  1462. 'count' => 1,
  1463. ];
  1464. } else {
  1465. $count_type[$_v]['count']++;
  1466. }
  1467. }
  1468. }
  1469. }
  1470. }
  1471. $db = new \think\Db;
  1472. $pagetotal = 0;
  1473. $arctypeAllSub = arctypeAllSub(); // 获取所有栏目,并每个栏目都包含所有子栏目,以及自己
  1474. $info2 = $tplData = [];
  1475. $info = convert_arr_key($info,'typeid');
  1476. foreach ($info as $k => $v) {
  1477. //外链
  1478. if ($v['is_part'] == 1 || 'ask' == $v['nid']) {
  1479. $dir = ROOT_PATH . trim($v['dirpath'], '/');
  1480. if (!empty($v['dirpath']) && true == is_dir($dir)) {//判断是否生成过文件夹,文件夹存在则删除
  1481. deldir_html($dir);
  1482. }
  1483. continue;
  1484. }
  1485. if (1 == $method_mode) { // 方法1
  1486. if (!isset($info[$v['typeid']]['count'])){ //判断当前栏目的count是否已经存在
  1487. $v['count'] = 0;
  1488. }else{
  1489. $v['count'] = intval($info[$v['typeid']]['count']);
  1490. }
  1491. if (isset($count_type[$v['typeid']])){ //存在当前栏目个数
  1492. $v['count'] += $count_type[$v['typeid']]['count'];
  1493. }
  1494. //判断是否存在上级目录,且当前栏目和上级栏目都不是单页,且当前栏目和上级栏目是相同模型,则,把当前栏目的aid和count赋值给父栏目
  1495. if ($v['parent_id'] && !in_array($v['current_channel'], [6,8]) && isset($info[$v['parent_id']]) && $v['current_channel'] == $info[$v['parent_id']]['current_channel']){
  1496. if (isset($info[$v['parent_id']]['count'])) {
  1497. $info[$v['parent_id']]['count'] += intval($v['count']);
  1498. }else{
  1499. $info[$v['parent_id']]['count'] = intval($v['count']);
  1500. }
  1501. }
  1502. }
  1503. /**
  1504. * 判断是否需要更新子栏目
  1505. * 1、更新子栏目,正常处理
  1506. * 2、不更新子栏目
  1507. * (1)、选择指定栏目的情况下,判断当前栏目是否为选择栏目,如果是,走正常流程,否则,去掉当前栏目
  1508. * (2)、不选择指定栏目的情况下,判断当前栏目是否为顶级栏目,如果是,走正常流程,否则,去掉当前栏目
  1509. */
  1510. if (empty($seo_upnext) && ( (!empty($id) && $v['typeid'] != $id) || (empty($id) && !empty($v['parent_id'])) ) ){
  1511. continue;
  1512. }
  1513. $tag_attr_arr = [];
  1514. if (!isset($tplData[$v['templist']])) {
  1515. $tpl = !empty($v['templist']) ? str_replace('.'.$view_suffix, '',$v['templist']) : 'lists_'. $v['nid'];
  1516. $template_html = "./template/".TPL_THEME."mobile/".$tpl.".htm";
  1517. $content = file_get_contents($template_html);
  1518. if ($content) {
  1519. preg_match_all('/\{eyou:list(\s+)?(.*)\}/i', $content, $matchs);
  1520. if (!empty($matchs[0][0])) {
  1521. $tag_attr = !empty($matchs[2][0]) ? $matchs[2][0] : '';
  1522. if (!empty($tag_attr)) {
  1523. $tag_attr = preg_replace('/([a-z]+)(\s*)=(\s*)([\'|\"]?)([^ \f\n\r\t\v\'\"]+)([\'|\"]?)/i', '${1}=\'${5}\'', $tag_attr); // 属性引导统一设置单引号
  1524. preg_match_all('/([0-9a-z_-]+)=\'([^\']+)\'/i', $tag_attr, $attr_matchs);
  1525. $attr_keys = !empty($attr_matchs[1]) ? $attr_matchs[1] : [];
  1526. $attr_vals = !empty($attr_matchs[2]) ? $attr_matchs[2] : [];
  1527. if (!empty($attr_keys)) {
  1528. foreach ($attr_keys as $_ak => $_av) {
  1529. $tag_attr_arr[$_av] = $attr_vals[$_ak];
  1530. }
  1531. // 每页条数
  1532. if (!empty($tag_attr_arr['loop'])) $tag_attr_arr['pagesize'] = intval($tag_attr_arr['loop']);
  1533. $tag_attr_arr['pagesize'] = !empty($tag_attr_arr['pagesize']) ? intval($tag_attr_arr['pagesize']) : 10;
  1534. // 模型ID
  1535. if (!empty($tag_attr_arr['modelid'])) $tag_attr_arr['channelid'] = intval($tag_attr_arr['modelid']);
  1536. // 排序
  1537. if (empty($tag_attr_arr['ordermode'])) {
  1538. if (!empty($tag['orderWay'])) {
  1539. $tag_attr_arr['ordermode'] = $tag_attr_arr['orderWay'];
  1540. } else {
  1541. $tag_attr_arr['ordermode'] = !empty($tag_attr_arr['orderway']) ? $tag_attr_arr['orderway'] : 'desc';
  1542. }
  1543. }
  1544. }
  1545. }
  1546. $tag_attr_arr['orderby'] = !empty($tag_attr_arr['orderby']) ? $tag_attr_arr['orderby'] : "";
  1547. $tag_attr_arr['ordermode'] = !empty($tag_attr_arr['ordermode']) ? $tag_attr_arr['ordermode'] : "desc";
  1548. $tplData[$v['templist']] = $tag_attr_arr;
  1549. } else {
  1550. $tplData[$v['templist']]['count'] = -1;
  1551. }
  1552. }
  1553. }
  1554. $tplDataInfo = !empty($tplData[$v['templist']]) ? $tplData[$v['templist']] : [];
  1555. if (2 == $method_mode) { // 方法2
  1556. $map_arc2 = $map_arc;
  1557. if (empty($web_stypeid_open)) { // 没开启副栏目
  1558. $map_arc2['a.typeid'] = array('in', $arctypeAllSub[$v['typeid']]);
  1559. } else { // 开启副栏目
  1560. $stypeid_where = "";
  1561. $typeid_str = implode(',', $arctypeAllSub[$v['typeid']]);
  1562. foreach ($arctypeAllSub[$v['typeid']] as $_k => $_v) {
  1563. $stypeid_where .= " OR CONCAT(',', a.stypeid, ',') LIKE '%,{$_v},%' ";
  1564. }
  1565. $map_arc2[] = $db::raw(" (a.typeid IN ({$typeid_str}) {$stypeid_where}) ");
  1566. }
  1567. $v['count'] = 0;
  1568. if (!in_array($v['current_channel'], [6,8])) {
  1569. $v['count'] = $db::name('archives')->alias('a')->where($map_arc2)->count('aid');
  1570. }
  1571. }
  1572. if (in_array($v['current_channel'], [6,8])){
  1573. $v['pagesize'] = 1;
  1574. $v['pagetotal'] = 1;
  1575. $pagetotal += $v['pagetotal'];
  1576. }else{
  1577. if (!empty($tplDataInfo)) {
  1578. $count = !empty($tplDataInfo['count']) ? $tplDataInfo['count'] : 0;
  1579. if (-1 == $count) {
  1580. $v['count'] = 1;
  1581. } else {
  1582. $pagesize = !empty($tplDataInfo['pagesize']) ? $tplDataInfo['pagesize'] : 0;
  1583. $channelid = !empty($tplDataInfo['channelid']) ? $tplDataInfo['channelid'] : 0;
  1584. if (!empty($channelid)) {
  1585. $map_arc['a.channel'] = $channelid;
  1586. if (isset($map_arc['a.typeid'])) {
  1587. unset($map_arc['a.typeid']);
  1588. }
  1589. if (isset($map_arc[0])) {
  1590. foreach ($map_arc as $_k => $_v) {
  1591. if (is_numeric($_k) && stristr($_v, 'stypeid')) {
  1592. unset($map_arc[$_k]);
  1593. }
  1594. }
  1595. }
  1596. $v['count'] = $db::name('archives')->alias('a')->where($map_arc)->count();
  1597. }
  1598. if ($aid) {
  1599. $orderby = !empty($tplDataInfo['orderby']) ? $tplDataInfo['orderby'] : '';
  1600. $ordermode = !empty($tplDataInfo['ordermode']) ? $tplDataInfo['ordermode'] : 'desc';
  1601. }
  1602. }
  1603. }
  1604. $v['pagesize'] = !empty($pagesize) ? $pagesize : 10;
  1605. $v['pagetotal'] = !empty($v['count']) ? (int)ceil($v['count'] / $v['pagesize']) : 1;
  1606. $pagetotal += $v['pagetotal'];
  1607. }
  1608. $v['orderby'] = !empty($orderby) ? $orderby : "";
  1609. $v['ordermode'] = !empty($ordermode) ? $ordermode : "desc";
  1610. $info2[] = $v;
  1611. }
  1612. // file_put_contents ( ROOT_PATH."/log.txt", var_export($info2,true) . "\r\n", FILE_APPEND );
  1613. // exit;
  1614. return ["info"=>$info2, "pagetotal"=>$pagetotal];
  1615. }
  1616. }