<?php
 namespace TechSoft\Laravel\View; use Illuminate\Support\Facades\Cookie; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\View; use TechOnline\Laravel\Dao\ModelUtil; trait DomainCityTrait { protected $city = null; protected $cityDomain = null; protected $mainDomain = null; protected function cityAwareSetup() { $this->mainDomain = config('env.DOMAIN_MAIN'); if (empty($city)) { if (!empty($cityCookie = Cookie::get('city'))) { $city = ModelUtil::get('area', ['shortEnName' => $cityCookie]); } } if (empty($city)) { $city = ModelUtil::get('area', ['isDefault' => true, 'active' => true,]); } Cookie::queue('city', $city['shortEnName'], 365 * 24 * 60); $this->city = $city; $this->cityDomain = $city['shortEnName'] . '.' . $this->mainDomain; View::share('_city', $this->city); View::share('_cityDomain', $this->cityDomain); View::share('_mainDomain', $this->mainDomain); Session::set('_cityDomain', $this->cityDomain); } protected function citySetup() { $this->mainDomain = config('env.DOMAIN_MAIN'); $domain = Request::server('HTTP_HOST'); if ($domain != $this->mainDomain) { $cityEnName = substr($domain, 0, strlen($domain) - strlen($this->mainDomain) - 1); $city = ModelUtil::get('area', ['shortEnName' => $cityEnName]); if (empty($city)) { header('Location: http://' . $this->mainDomain); exit(); } } if (empty($city)) { if (!empty($cityCookie = Cookie::get('city'))) { $city = ModelUtil::get('area', ['shortEnName' => $cityCookie]); if (empty($city)) { header('Location: http://' . $this->mainDomain); exit(); } } } if (empty($city)) { $city = ModelUtil::get('area', ['isDefault' => true, 'active' => true,]); if (empty($city)) { exit('必须设定一个默认城市'); } if (empty($city)) { exit('默认城市必须为开启'); } } if (empty($city['active'])) { header('Location: http://' . $this->mainDomain); exit(); } Cookie::queue('city', $city['shortEnName'], 365 * 24 * 60); $this->city = $city; $this->cityDomain = $city['shortEnName'] . '.' . $this->mainDomain; View::share('_city', $this->city); View::share('_cityDomain', $this->cityDomain); View::share('_mainDomain', $this->mainDomain); Session::set('_cityDomain', $this->cityDomain); } protected function city() { return $this->city; } protected function cityId() { return $this->city['id']; } protected function cityDomain() { return $this->cityDomain; } }