AppEndBehavior.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\admin\behavior;
  3. /**
  4. * 系统行为扩展:
  5. */
  6. class AppEndBehavior {
  7. protected static $actionName;
  8. protected static $controllerName;
  9. protected static $moduleName;
  10. protected static $method;
  11. /**
  12. * 构造方法
  13. * @param Request $request Request对象
  14. * @access public
  15. */
  16. public function __construct()
  17. {
  18. }
  19. // 行为扩展的执行入口必须是run
  20. public function run(&$params){
  21. self::$actionName = request()->action();
  22. self::$controllerName = request()->controller();
  23. self::$moduleName = request()->module();
  24. self::$method = request()->method();
  25. // file_put_contents ( DATA_PATH."log.txt", date ( "Y-m-d H:i:s" ) . " " . var_export('admin_CoreProgramBehavior',true) . "\r\n", FILE_APPEND );
  26. $this->_initialize();
  27. }
  28. private function _initialize() {
  29. $this->resetAuthor(); // 临时处理授权问题
  30. $this->clearHtmlCache(); // 变动数据之后,清除页面缓存和数据
  31. // $this->seodescriptionHandle(); // 发布或编辑时,截取SEO描述长度
  32. // $this->sitemap(); // 自动生成sitemap
  33. }
  34. /**
  35. * 自动生成sitemap
  36. * @access public
  37. */
  38. private function sitemap()
  39. {
  40. /*只有相应的控制器和操作名才执行,以便提高性能*/
  41. if ('POST' == self::$method) {
  42. $channeltype_row = \think\Cache::get("extra_global_channeltype");
  43. if (empty($channeltype_row)) {
  44. $ctlArr = \think\Db::name('channeltype')
  45. ->where('id','NOTIN', [6,8])
  46. ->column('ctl_name');
  47. } else {
  48. $ctlArr = array();
  49. foreach($channeltype_row as $key => $val){
  50. if (!in_array($val['id'], [6,8])) {
  51. $ctlArr[] = $val['ctl_name'];
  52. }
  53. }
  54. }
  55. $systemCtl= ['Arctype'];
  56. $ctlArr = array_merge($systemCtl, $ctlArr);
  57. $actArr = ['add','edit'];
  58. if (in_array(self::$controllerName, $ctlArr) && in_array(self::$actionName, $actArr)) {
  59. sitemap_auto();
  60. }
  61. }
  62. /*--end*/
  63. }
  64. /**
  65. * 临时处理授权问题
  66. */
  67. private function resetAuthor()
  68. {
  69. /*在以下相应的控制器和操作名里执行,以便提高性能*/
  70. $ctlActArr = array(
  71. 'Index@index',
  72. );
  73. $ctlActStr = self::$controllerName.'@'.self::$actionName;
  74. if (in_array($ctlActStr, $ctlActArr) && 'GET' == self::$method) {
  75. if(!empty($_SESSION['isset_resetAuthor']))
  76. return true;
  77. $_SESSION['isset_resetAuthor'] = 1;
  78. session('isset_author', null);
  79. }
  80. /*--end*/
  81. }
  82. /**
  83. * 数据变动之后,清理页面和数据缓存
  84. */
  85. private function clearHtmlCache()
  86. {
  87. /*在以下相应的控制器和操作名里执行,以便提高性能*/
  88. $actArr = ['add','edit','del','recovery','changeTableVal'];
  89. if ('POST' == self::$method) {
  90. foreach ($actArr as $key => $val) {
  91. if (preg_match('/^((.*)_)?('.$val.')$/i', self::$actionName)) {
  92. foreach ([HTML_ROOT,CACHE_PATH] as $k2 => $v2) {
  93. delFile($v2);
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * 发布或编辑时,对自定义模型的文档进行二次处理
  102. * @access private
  103. */
  104. // private function seodescriptionHandle()
  105. // {
  106. // /*只有相应的控制器和操作名才执行,以便提高性能*/
  107. // if ('POST' == self::$method) {
  108. // $ctlArr = \think\Db::name('channeltype')->field('id,ctl_name')
  109. // ->where('ifsystem',0)
  110. // ->getAllWithIndex('ctl_name');
  111. // $actArr = ['add','edit'];
  112. // if (!empty($ctlArr[self::$controllerName]) && in_array(self::$actionName, $actArr)) {
  113. // /*截取SEO描述长度*/
  114. // $contentField = \think\Db::name('channelfield')->where([
  115. // 'channel_id' => $ctlArr[self::$controllerName]['id'],
  116. // 'dtype' => 'htmltext',
  117. // ])->getField('name');
  118. // if (empty($_POST['seo_description']) && !empty($_POST['addonFieldExt'][$contentField])) {
  119. // $content = $_POST['addonFieldExt'][$contentField];
  120. // $seo_description = @msubstr(checkStrHtml($content), 0, \think\Config::get('global.arc_seo_description_length'), false);
  121. // $saveData['seo_description'] = $seo_description;
  122. // }
  123. // /*--end*/
  124. // /*SEO关键字替换中文逗号*/
  125. // if (!empty($_POST['seo_keywords'])) {
  126. // $saveData['seo_keywords'] = str_replace(',', ',', $_POST['seo_keywords']);
  127. // }
  128. // /*end*/
  129. // if (!empty($saveData)) {
  130. // $saveData['update_time'] = getTime();
  131. // \think\Db::name('archives')->where('aid', $_POST['aid'])->update($saveData);
  132. // }
  133. // }
  134. // /*--end*/
  135. // }
  136. // }
  137. }