如果你使用FastAdminThinkPHP5开发的网站,以下方法可以实现全站一键变灰,无需修改任何视图文件,同时支持定时显示。

FastAdmin 实现方法

  1. 打开 application/tags.php 文件,找到 view_filter,并添加 'app\\common\\behavior\\Common'

    'view_filter' => [
        'app\\common\\behavior\\Common',
    ],
  2. 编辑或创建 application/common/behavior/Common.php 文件,添加以下方法:

    <?php
    
    namespace app\common\behavior;
    
    class Common
    {
        public function viewFilter(&$content)
        {
            // 设置具体生效的日期,请将 xxxx-xx-xx 替换成你的日期
            if (date("Y-m-d") == "xxxx-xx-xx") {
                $style = '<style>body{filter: grayscale(100%);-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%);-o-filter: grayscale(100%);filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);-webkit-filter: grayscale(1);}</style>';
                $content = preg_replace("/<\/head>/", $style . "</head>", $content, 1);
            }
        }
    }
  3. 使用插件市场:FastAdmin还可以通过插件市场安装网站变灰插件,访问 FastAdmin插件市场 进行安装。

ThinkPHP5 实现方法

  1. 创建 application/common/behavior/Common.php 文件,并写入以下内容:

    <?php
    
    namespace app\common\behavior;
    
    class Common
    {
        public function viewFilter(&$content)
        {
            // 设置具体生效的日期,请将 xxxx-xx-xx 替换成你的日期
            if (date("Y-m-d") == "xxxx-xx-xx") {
                $style = '<style>body{filter: grayscale(100%);-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%);-o-filter: grayscale(100%);filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);-webkit-filter: grayscale(1);}</style>';
                $content = preg_replace("/<\/head>/", $style . "</head>", $content, 1);
            }
        }
    }
  2. application/tags.php 文件中注册行为,找到 view_filter 配置,添加 'app\\common\\behavior\\Common'

    'view_filter' => [
        'app\\common\\behavior\\Common',
    ],

通过以上设置,你可以实现让网站在指定日期一键变灰的效果,无需对视图文件进行任何修改。

点赞(0)

微信扫一扫加关注

返回
顶部