<?php

namespace app\common\model;

use think\Model;
use think\Session;

class Sites extends Model
{
    // 表名
    protected $name = 'sites';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    
    // 追加属性
    protected $append = [

    ];


    public function config()
    {
        return $this->hasOne('SitesConfig', 'site_id','id')->field('seo,custom');
    }



    /**
     * 刷新路由缓存
     * @return array
     */
    public static function refreshRulesCache()
    {
        \think\Cache::rm('rules');

        $list = self::all(['status'=>'normal']);
        $rules = [];

        foreach ($list as $k => $v) {
            if (empty($v['domain']) && empty($v['url'])) continue;
            if($v['domain']=='www') continue;

            //只绑定一种语言,设置为默认语言
            if(!strpos(',', $v['lang'])) {
                $vars = '?user_site_id=' . $v['id'] . '&lang=' . $v['lang'];

                if (!empty($v['domain'])) {
                    $rules[] = ['s' => $v['domain'], 'p' => $vars];
                }
                if (!empty($v['url'])) {
                    $rules[] = ['s' => $v['url'], 'p' => $vars];
                }
            }
            else{
                $multilang = explode(',', $v['lang']);
                foreach ($multilang as $key => $lang) {
                    if ($lang != 'zh-cn') {
                        $vars = '?user_site_id=' . $v['id'] . '&lang=' . $lang;
                        if (!empty($v['domain'])) {
                            $rules[] = ['s' => $lang . '.' . $v['domain'], 'p' => $vars];
                        }
                        if (!empty($v['url'])) {
                            $rules[] = ['s' => $lang . '.' . $v['url'], 'p' => $vars];
                        }
                    }
                }
            }
            \think\Cache::set('rules', $rules, 86400);
            return $rules;
        }
    }



}
