Admin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\base\InvalidConfigException;
  11. use Yii;
  12. /**
  13. * Admin services.
  14. *
  15. * @property \fecshop\services\customer\UrlKey $urlKey
  16. * @property \fecshop\services\customer\RoleUrlKey $roleUrlKey
  17. * @property \fecshop\services\customer\Role $role
  18. * @property \fecshop\services\customer\Config $config
  19. * @property \fecshop\services\customer\UserRole $userRole
  20. * @property \fecshop\services\customer\SystemLog $systemLog
  21. * @property \fecshop\services\customer\Menu $menu
  22. *
  23. * @author Terry Zhao <2358269014@qq.com>
  24. * @since 1.0
  25. */
  26. class Admin extends Service
  27. {
  28. public $xhEditorUploadImgUrl = 'cms/xeditor/imageupload';
  29. public $xhEditorUploadImgForamt = 'jpg,jpeg,gif,png';
  30. public $xhEditorUploadFlashUrl = 'cms/xeditor/flashupload';
  31. public $xhEditorUploadFlashFormat = 'swf';
  32. public $xhEditorUploadLinkUrl = 'cms/xeditor/linkupload';
  33. public $xhEditorUploadLinkFormat = 'zip,rar,txt';
  34. public $xhEditorUploadMediaUrl = 'cms/xeditor/mediaupload';
  35. public $xhEditorUploadMediaFormat = 'avi';
  36. protected $_currentLangCode;
  37. const ADMIN_CURRENT_LANG_CODE = 'admin_current_lang_code';
  38. /**
  39. * @return string 得到编辑器上传图片upload url
  40. */
  41. public function getXhEditorUploadImgUrl(){
  42. return $this->xhEditorUploadImgUrl;
  43. }
  44. /**
  45. * @return string 得到编辑器上传图片允许的文件格式
  46. */
  47. public function getXhEditorUploadImgForamt(){
  48. return $this->xhEditorUploadImgForamt;
  49. }
  50. /**
  51. * @return string 得到编辑器上传Flash upload url
  52. */
  53. public function getXhEditorUploadFlashUrl(){
  54. return $this->xhEditorUploadFlashUrl;
  55. }
  56. /**
  57. * @return string 得到编辑器上传Flash允许的文件格式
  58. */
  59. public function getXhEditorUploadFlashFormat(){
  60. return $this->xhEditorUploadFlashFormat;
  61. }
  62. /**
  63. * @return string 得到编辑器上传Link upload url
  64. */
  65. public function getXhEditorUploadLinkUrl(){
  66. return $this->xhEditorUploadLinkUrl;
  67. }
  68. /**
  69. * @return string 得到编辑器上传Link允许的文件格式
  70. */
  71. public function getXhEditorUploadLinkFormat(){
  72. return $this->xhEditorUploadLinkFormat;
  73. }
  74. /**
  75. * @return string 得到编辑器上传Media upload url
  76. */
  77. public function getXhEditorUploadMediaUrl(){
  78. return $this->xhEditorUploadMediaUrl;
  79. }
  80. /**
  81. * @return string 得到编辑器上传Media允许的文件格式
  82. */
  83. public function getXhEditorUploadMediaFormat(){
  84. return $this->xhEditorUploadMediaFormat;
  85. }
  86. /**
  87. * @param $app
  88. * 在Yii2框架的初始化过程过程过程中执行的函数,将被组件(Yii2 components)Yii::$app->store->bootstrap() 调用
  89. * Yii::$app->store 组件, 就是文件:fecshop\components\Store.php
  90. * @throws InvalidConfigException
  91. */
  92. public function bootstrap($app){
  93. $this->initLangCode();
  94. }
  95. /**
  96. * 初始化后台多语言
  97. * @throws InvalidConfigException
  98. */
  99. protected function initLangCode(){
  100. if (!$this->_currentLangCode) {
  101. $currentLangCode = Yii::$service->session->get(self::ADMIN_CURRENT_LANG_CODE);
  102. if (!$currentLangCode) {
  103. $currentLangCode = Yii::$service->fecshoplang->defaultLangCode;
  104. }
  105. if (!$currentLangCode) {
  106. throw new InvalidConfigException('default lang code must config');
  107. }
  108. if ($this->setTranslateLang($currentLangCode)) {
  109. $this->_currentLangCode = $currentLangCode;
  110. } else {
  111. throw new InvalidConfigException('lang code: '.$currentLangCode.' can not find in fecshoplang service config, you should add this language config');
  112. }
  113. }
  114. }
  115. public function getCurrentLangCode(){
  116. if (!$this->_currentLangCode) {
  117. $this->initLangCode();
  118. }
  119. return $this->_currentLangCode;
  120. }
  121. public function setCurrentLangCode($code){
  122. $adminLangCode = Yii::$service->fecshoplang->getAdminLangCode();
  123. if (in_array($code, $adminLangCode)) {
  124. Yii::$service->session->set(self::ADMIN_CURRENT_LANG_CODE, $code);
  125. $this->_currentLangCode = $code;
  126. if ($this->setTranslateLang($code)) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. public function setTranslateLang($code){
  133. $langCodeArr = Yii::$service->fecshoplang->getLangAndCodeArr();
  134. if (isset($langCodeArr[$code]) && $langCodeArr[$code]) {
  135. Yii::$service->page->translate->setLanguage($langCodeArr[$code]);
  136. return true;
  137. }
  138. return false;
  139. }
  140. public function getLangArr(){
  141. $arr = [];
  142. $adminLangCode = Yii::$service->fecshoplang->adminLangCode;
  143. if (is_array($adminLangCode)) {
  144. foreach ($adminLangCode as $one) {
  145. $arr[$one['code']] = $one['name'];
  146. }
  147. }
  148. return $arr;
  149. }
  150. }