<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:23
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace app\home\controller;

use app\BaseController;
use app\BaseViewController;
use app\model\api\Site;
use app\model\Api\SiteProduct;
use app\model\system\Sys;
use app\Request;
use library\services\UtilService;
use library\utils\AdvertUtils;
use library\utils\Icon;
use think\Cache;

class Index extends BaseViewController
{

    private $version = '1.0';

    public function index(Request $request){
        $domain = $request->domain();
        $redis = \think\facade\Cache::store('redis');
        $keyDomain = md5($domain . $this->version);
        $content = $redis->get($keyDomain);
        if(!empty($content)){
            return $content;
        } else {
            $strBody = '';
            $txt     = file_get_contents(app()->getRootPath() . 'public/index.html');
            preg_match('/\<link(.*?)<\/body>/is', $txt, $match);
            if (!empty($match[0])) {
                $strBody = $match[0];
            }

            $str = '<!DOCTYPE html><html><head>
                    <meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
                    <title>星选联盟</title>
                    <meta name="keywords" content="星选联盟">
                    <meta name="description" content="星选联盟">
                    <link rel="bookmark" type="image/x-icon" href="https://boofly.cn/favicon.ico" />
                    <link rel="shortcut icon" type="image/x-icon" href="https://boofly.cn/favicon.ico" />'. $strBody . '<script>
                      if (window.HTMLElement) {
                          if (Object.getOwnPropertyNames(HTMLElement.prototype).indexOf(\'dataset\') === -1) {
                              Object.defineProperty(HTMLElement.prototype, \'dataset\', {
                                  get: function () {
                                      var attributes = this.attributes // 获取节点的所有属性
                                      var name = []
                                      var value = [] // 定义两个数组保存属性名和属性值
                                      var obj = {} // 定义一个空对象
                                      for (var i = 0; i < attributes.length; i++) { // 遍历节点的所有属性
                                          if (attributes[i].nodeName.slice(0, 5) === \'data-\') { // 如果属性名的前面5个字符符合"data-"
                                              // 取出属性名的"data-"的后面的字符串放入name数组中
                                              name.push(attributes[i].nodeName.slice(5));
                                              // 取出对应的属性值放入value数组中
                                              value.push(attributes[i].nodeValue);
                                          }
                                      }
                                      for (var j = 0; j < name.length; j++) { // 遍历name和value数组
                                          obj[name[j]] = value[j]; // 将属性名和属性值保存到obj中
                                      }
                                      return obj // 返回对象
                                  }
                              })
                          }
                      }</script></html>';
            $redis->set($keyDomain, $str, 60 * 60 * 3);
        }
        echo $str;
    }
}
