Store.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. use Yii;
  11. use yii\base\InvalidValueException;
  12. /**
  13. * store service
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class Store extends Service
  18. {
  19. /**
  20. * init by config file.
  21. * all stores config . include : domain,language,theme,themePackage.
  22. */
  23. public $stores;
  24. public $store;
  25. /**
  26. * current store language,for example: en_US,fr_FR.
  27. */
  28. public $currentLang;
  29. /**
  30. * current store language name.
  31. */
  32. public $currentLangName;
  33. /**
  34. * current store theme package.
  35. */
  36. //public $currentThemePackage = 'default';
  37. /**
  38. * current store theme.
  39. */
  40. //public $currentTheme = 'default';
  41. /**
  42. * 当前store的key,也就是当前的store.
  43. */
  44. public $currentStore;
  45. /**
  46. * current language code example : fr es cn ru.
  47. */
  48. public $currentLangCode;
  49. public $thirdLogin;
  50. //public $https;
  51. public $serverLangs;
  52. public $apiAppNameArr = ['appserver','appapi'];
  53. public function init()
  54. {
  55. parent::init();
  56. $this->initCurrentStoreConfig();
  57. }
  58. // 是否是api入口
  59. public function isApiStore()
  60. {
  61. $appName = Yii::$app->params['appName'];
  62. if ($appName && in_array($appName, $this->apiAppNameArr)) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. public function isAppserver()
  69. {
  70. $appServerArr = ['appserver'];
  71. $currentAppName = $this->getCurrentAppName();
  72. if (in_array($currentAppName, $appServerArr)) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * 得到当前入口的名字
  79. * @return mixed
  80. */
  81. public function getCurrentAppName()
  82. {
  83. return Yii::$app->params['appName'];
  84. }
  85. // 初始化store配置
  86. public function initCurrentStoreConfig()
  87. {
  88. $currentAppName = $this->getCurrentAppName();
  89. if ($this->isAppserver()) {
  90. return $this->initAppserverCurrentStoreConfig();
  91. }
  92. $coll = Yii::$service->storeDomain->getCollByAppName($currentAppName);
  93. if (is_array($coll)) {
  94. foreach ($coll as $one) {
  95. $storeKey = $one['key'];
  96. $lang = $one['lang'];
  97. $lang_name = $one['lang_name'];
  98. $local_theme_dir = $one['local_theme_dir'];
  99. $third_theme_dir = $one['third_theme_dir'] ? explode(',',$one['third_theme_dir']) : '';
  100. $currency = $one['currency'];
  101. $mobile_enable = $one['mobile_enable'] == 1 ? true : false;
  102. $mobile_condition = $one['mobile_condition'] ? explode(',',$one['mobile_condition']) : '';
  103. $mobile_redirect_domain = $one['mobile_redirect_domain'];
  104. $mobile_https_enable = $one['mobile_https_enable'] == 1 ? true : false;
  105. $mobile_type = $one['mobile_type'];
  106. $facebook_login_app_id = $one['facebook_login_app_id'];
  107. $facebook_login_app_secret = $one['facebook_login_app_secret'];
  108. $google_login_client_id = $one['google_login_client_id'];
  109. $google_login_client_secret = $one['google_login_client_secret'];
  110. $https_enable = $one['https_enable'] == 1 ? true : false;
  111. $sitemap_dir = $one['sitemap_dir'];
  112. // set config stores
  113. $this->stores[$storeKey] = [
  114. 'language' => $lang, // 语言简码需要在@common/config/fecshop_local_services/FecshopLang.php 中定义。
  115. 'languageName' => $lang_name, // 语言简码对应的文字名称,将会出现在语言切换列表中显示。
  116. 'localThemeDir' => $local_theme_dir, // 设置当前store对应的模板路径。关于多模板的方面的知识,您可以参看fecshop多模板的知识。
  117. 'thirdThemeDir' => $third_theme_dir, // 第三方模板路径,数组,可以多个路径
  118. 'currency' => $currency, // 当前store的默认货币,这个货币简码,必须在货币配置中配置
  119. // 用于sitemap生成中域名。
  120. 'https' => $https_enable,
  121. // sitemap的路径。
  122. 'sitemapDir' => $sitemap_dir,
  123. ];
  124. if ($mobile_condition && $mobile_redirect_domain && $mobile_type) {
  125. $this->stores[$storeKey]['mobile'] = [
  126. 'enable' => $mobile_enable,
  127. 'condition' => $mobile_condition, // phone 代表手机,tablet代表平板,当都填写,代表手机和平板都会进行跳转
  128. 'redirectDomain' => $mobile_redirect_domain, // 如果是移动设备访问进行域名跳转,这里填写的值为store key
  129. 'https' => $mobile_https_enable, // 手机端url是否支持https,如果支持,设置https为true,如果不支持,设置为false
  130. 'type' => $mobile_type, // 填写值选择:[apphtml5, appserver],如果是 apphtml5 , 则表示跳转到html5入口,如果是appserver,则表示跳转到vue这种appserver对应的入口
  131. ];
  132. }
  133. // 第三方账号登录配置
  134. if ($facebook_login_app_id && $facebook_login_app_secret) {
  135. $this->stores[$storeKey]['thirdLogin']['facebook'] = [
  136. 'facebook_app_id' => $facebook_login_app_id,
  137. 'facebook_app_secret' => $facebook_login_app_secret,
  138. ];
  139. }
  140. // 第三方账号登录配置
  141. if ($google_login_client_id && $google_login_client_secret) {
  142. $this->stores[$storeKey]['thirdLogin']['google'] = [
  143. 'CLIENT_ID' => $google_login_client_id,
  144. 'CLIENT_SECRET' => $google_login_client_secret,
  145. ];
  146. }
  147. }
  148. }
  149. return true;
  150. }
  151. // 如果入口是appserver,那么通过这个函数初始化
  152. public function initAppserverCurrentStoreConfig()
  153. {
  154. $appserver_store_config = Yii::$app->store->get('appserver_store');
  155. $storeKey = $appserver_store_config['key'];
  156. $lang = $appserver_store_config['lang'];
  157. $lang_name = $appserver_store_config['lang_name'];
  158. $currency = $appserver_store_config['currency'];
  159. $facebook_login_app_id = $appserver_store_config['facebook_login_app_id'];
  160. $facebook_login_app_secret = $appserver_store_config['facebook_login_app_secret'];
  161. $google_login_client_id = $appserver_store_config['google_login_client_id'];
  162. $google_login_client_secret = $appserver_store_config['google_login_client_secret'];
  163. $https_enable = $appserver_store_config['https_enable'] == 1 ? true : false;
  164. // set config stores
  165. $this->stores[$storeKey] = [
  166. 'language' => $lang, // 语言简码需要在@common/config/fecshop_local_services/FecshopLang.php 中定义。
  167. 'languageName' => $lang_name, // 语言简码对应的文字名称,将会出现在语言切换列表中显示。
  168. 'currency' => $currency, // 当前store的默认货币,这个货币简码,必须在货币配置中配置
  169. // 用于sitemap生成中域名。
  170. 'https' => $https_enable,
  171. ];
  172. // 第三方账号登录配置
  173. if ($facebook_login_app_id && $facebook_login_app_secret) {
  174. $this->stores[$storeKey]['thirdLogin']['facebook'] = [
  175. 'facebook_app_id' => $facebook_login_app_id,
  176. 'facebook_app_secret' => $facebook_login_app_secret,
  177. ];
  178. }
  179. // 第三方账号登录配置
  180. if ($google_login_client_id && $google_login_client_secret) {
  181. $this->stores[$storeKey]['thirdLogin']['google'] = [
  182. 'CLIENT_ID' => $google_login_client_id,
  183. 'CLIENT_SECRET' => $google_login_client_secret,
  184. ];
  185. }
  186. // 初始化语言。
  187. $this->stores[$storeKey]['serverLangs'] = Yii::$app->store->get('appserver_store_lang');
  188. // 通过该方法,初始化货币services,直接从headers中取出来currency。进行set,这样currency就不会从session中读取(fecshop-2版本对于appserver已经抛弃session servcies)
  189. Yii::$service->page->currency->appserverSetCurrentCurrency();
  190. return true;
  191. }
  192. /**
  193. * Bootstrap:init website, class property $currentLang ,$currentTheme and $currentStore.
  194. * if you not config this ,default class property will be set.
  195. * if current store_code is not config , InvalidValueException will be throw.
  196. * class property $currentStore will be set value $store_code.
  197. * @param $app
  198. */
  199. protected function actionBootstrap($app)
  200. {
  201. $host = explode('//', $app->getHomeUrl());
  202. $stores = $this->stores;
  203. $init_complete = 0;
  204. if (is_array($stores) && !empty($stores)) {
  205. foreach ($stores as $store_code => $store) {
  206. if ($host[1] == $store_code) {
  207. $this->html5DeviceCheckAndRedirect($store_code, $store);
  208. Yii::$service->store->currentStore = $store_code;
  209. $this->store = $store;
  210. if (isset($store['language']) && !empty($store['language'])) {
  211. Yii::$service->store->currentLang = $store['language'];
  212. Yii::$service->store->currentLangCode = Yii::$service->fecshoplang->getLangCodeByLanguage($store['language']);
  213. Yii::$service->store->currentLangName = $store['languageName'];
  214. Yii::$service->page->translate->setLanguage($store['language']);
  215. }
  216. if (isset($store['theme']) && !empty($store['theme'])) {
  217. Yii::$service->store->currentTheme = $store['theme'];
  218. }
  219. // set local theme dir.
  220. if (isset($store['localThemeDir']) && $store['localThemeDir']) {
  221. //Yii::$service->page->theme->localThemeDir = $store['localThemeDir'];
  222. Yii::$service->page->theme->setLocalThemeDir($store['localThemeDir']);
  223. }
  224. // set third theme dir.
  225. if (isset($store['thirdThemeDir']) && $store['thirdThemeDir']) {
  226. //Yii::$service->page->theme->thirdThemeDir = $store['thirdThemeDir'];
  227. Yii::$service->page->theme->setThirdThemeDir($store['thirdThemeDir']);
  228. }
  229. // init store currency.
  230. if (isset($store['currency']) && !empty($store['currency'])) {
  231. $currency = $store['currency'];
  232. } else {
  233. $currency = '';
  234. }
  235. Yii::$service->page->currency->initCurrency($currency);
  236. /**
  237. * current domain is config is store config.
  238. */
  239. $init_complete = 1;
  240. $this->thirdLogin = $store['thirdLogin'];
  241. /**
  242. * appserver 部分
  243. */
  244. if (isset($store['serverLangs']) && !empty($store['serverLangs'])) {
  245. $this->serverLangs = $store['serverLangs'];
  246. }
  247. $headers = Yii::$app->request->getHeaders();
  248. if (isset($headers['fecshop-lang']) && $headers['fecshop-lang']) {
  249. $h_lang = $headers['fecshop-lang'];
  250. if (is_array($this->serverLangs)) {
  251. foreach ($this->serverLangs as $one) {
  252. if ($one['code'] == $h_lang) {
  253. Yii::$service->store->currentLangCode = $h_lang;
  254. Yii::$service->store->currentLang = $one['language'];
  255. Yii::$service->store->currentLangName = $one['languageName'];
  256. Yii::$service->page->translate->setLanguage($one['language']);
  257. }
  258. }
  259. }
  260. }
  261. if (isset($headers['fecshop-currency']) && $headers['fecshop-currency']) {
  262. $currentC = Yii::$service->page->currency->getCurrentCurrency();
  263. if ($currentC != $headers['fecshop-currency']) {
  264. Yii::$service->page->currency->setCurrentCurrency($headers['fecshop-currency']);
  265. }
  266. }
  267. break;
  268. }
  269. }
  270. }
  271. if (!$init_complete) {
  272. throw new InvalidValueException('this domain is not config in store component');
  273. }
  274. }
  275. /**
  276. * @param $store_code | String
  277. * @param $store | Array
  278. * mobile device url redirect.
  279. * pc端自动跳转到html5端的检测
  280. */
  281. protected function html5DeviceCheckAndRedirect($store_code, $store)
  282. {
  283. if (!isset($store['mobile'])) {
  284. return;
  285. }
  286. $enable = isset($store['mobile']['enable']) ? $store['mobile']['enable'] : false;
  287. if (!$enable) {
  288. return;
  289. }
  290. $condition = isset($store['mobile']['condition']) ? $store['mobile']['condition'] : false;
  291. $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false;
  292. $redirectType = isset($store['mobile']['type']) ? $store['mobile']['type'] : false;
  293. if (is_array($condition) && !empty($condition) && !empty($redirectDomain) && $redirectType === 'apphtml5') {
  294. $mobileDetect = Yii::$service->helper->mobileDetect;
  295. $mobile_https = (isset($store['mobile']['https']) && $store['mobile']['https']) ? true : false;
  296. if (in_array('phone', $condition) && in_array('tablet', $condition)) {
  297. if ($mobileDetect->isMobile()) {
  298. $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https);
  299. }
  300. } elseif (in_array('phone', $condition)) {
  301. if ($mobileDetect->isMobile() && !$mobileDetect->isTablet()) {
  302. $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https);
  303. }
  304. } elseif (in_array('tablet', $condition)) {
  305. if ($mobileDetect->isTablet()) {
  306. $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https);
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * @param $store_code | String
  313. * @param $redirectDomain | String
  314. * 检测,html5端跳转检测
  315. */
  316. protected function redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https)
  317. {
  318. $currentUrl = Yii::$service->url->getCurrentUrl();
  319. $redirectUrl = str_replace($store_code, $redirectDomain, $currentUrl);
  320. // pc端跳转到html5,可能一个是https,一个是http,因此需要下面的代码进行转换。
  321. if ($mobile_https) {
  322. if (strstr($redirectUrl, 'https://') || strstr($redirectUrl, 'http://')) {
  323. $redirectUrl = str_replace('http://', 'https://', $redirectUrl);
  324. } else {
  325. $redirectUrl = 'https:'.$redirectUrl;
  326. }
  327. } else {
  328. if (strstr($redirectUrl, 'https://') || strstr($redirectUrl, 'http://')) {
  329. $redirectUrl = str_replace('https://', 'http://', $redirectUrl);
  330. } else {
  331. $redirectUrl = 'http:'.$redirectUrl;
  332. }
  333. }
  334. header('Location:'.$redirectUrl);
  335. exit;
  336. }
  337. /**
  338. * @return boolean, 检测是否属于满足跳转到appserver的条件
  339. */
  340. public function isAppServerMobile()
  341. {
  342. $store = $this->store;
  343. $condition = isset($store['mobile']['condition']) ? $store['mobile']['condition'] : false;
  344. $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false;
  345. $redirectType = isset($store['mobile']['type']) ? $store['mobile']['type'] : false;
  346. if (is_array($condition) && !empty($condition) && !empty($redirectDomain) && $redirectType === 'appserver') {
  347. $mobileDetect = Yii::$service->helper->mobileDetect;
  348. if (in_array('phone', $condition) && in_array('tablet', $condition)) {
  349. if ($mobileDetect->isMobile()) {
  350. return true;
  351. }
  352. } elseif (in_array('phone', $condition)) {
  353. if ($mobileDetect->isMobile() && !$mobileDetect->isTablet()) {
  354. return true;
  355. }
  356. } elseif (in_array('tablet', $condition)) {
  357. if ($mobileDetect->isTablet()) {
  358. return true;
  359. }
  360. }
  361. }
  362. return false;
  363. }
  364. /**
  365. * @param $urlPath | String,跳转到vue端的url Path
  366. * @return boolean, 生成vue端的url,然后进行跳转。
  367. */
  368. public function redirectAppServerMobile($urlPath)
  369. {
  370. $store = $this->store;
  371. $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false;
  372. $mobile_https = (isset($store['mobile']['https']) && $store['mobile']['https']) ? 'https://' : 'http://';
  373. $host = $mobile_https.$redirectDomain.'/#/';
  374. $urlParam = $_SERVER["QUERY_STRING"];
  375. // 得到当前的语言
  376. if ($urlParam) {
  377. $urlParam .= '&lang='.$this->currentLangCode;
  378. } else {
  379. $urlParam .= 'lang='.$this->currentLangCode;
  380. }
  381. $redirectUrl = $host.$urlPath.'?'.$urlParam;
  382. header('Location:'.$redirectUrl);
  383. exit;
  384. }
  385. /**
  386. * @param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy']
  387. * @param $attrName|String, attribute name ,like: title ,description.
  388. * if object or array attribute is a language attribute, you can get current
  389. * language value by this function.
  390. * if lang attribute in current store language is empty , default language attribute will be return.
  391. * if attribute in default language value is empty, $attrVal will be return.
  392. */
  393. protected function actionGetStoreAttrVal($attrVal, $attrName)
  394. {
  395. $lang = $this->currentLangCode;
  396. return Yii::$service->fecshoplang->getLangAttrVal($attrVal, $attrName, $lang);
  397. }
  398. /**
  399. * @return array
  400. * get all store info, one item in array format is: ['storeCode' => 'store language'].
  401. */
  402. protected function actionGetStoresLang()
  403. {
  404. $stores = $this->stores;
  405. $topLang = [];
  406. foreach ($stores as $storeCode=> $store) {
  407. $languageName = $store['languageName'];
  408. $topLang[$storeCode] = $languageName;
  409. }
  410. return $topLang;
  411. }
  412. }