BackendController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace backend\controllers;
  3. use biz\auth\services\MenuService;
  4. use biz\goods\services\CategoryRelateService;
  5. use biz\goods\services\UsageRelationService;
  6. use biz\merchant\services\AdminService;
  7. use common\components\error;
  8. use common\components\stringUtil;
  9. use common\components\validate;
  10. use common\services\xhUserService;
  11. use Yii;
  12. use common\services\xhWxOpenService;
  13. use common\services\xhMerchantAssetService;
  14. use common\services\xhMerchantExtendService;
  15. use common\services\xhMerchantService;
  16. use common\services\xhAdminService;
  17. use common\components\util;
  18. use common\components\weixinUtil;
  19. use common\components\jsSDK;
  20. use common\services\xhAdminToMerchantService;
  21. use yii\web\Cookie;
  22. class BackendController extends PublicController
  23. {
  24. public $admin, $adminId, $adminAvatar, $account;
  25. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantAsset, $merchantLogo, $signPackage;
  26. public $openMerchant, $openMerchantId;
  27. public $appId;
  28. public $isWeixin = false;
  29. public $isLogin = false;
  30. public $withoutLogin = [];//不需要登陆的方法名
  31. public $ourself = [];
  32. public $error;
  33. public $menuList;
  34. public $subMenuList;
  35. public $usageList;
  36. public $categoryList;
  37. public function beforeAction($action)
  38. {
  39. //取一级菜单 二级菜单 shish 2019.8.17
  40. $controllerName = $action->controller->id;
  41. $actionName = $action->id;
  42. $this->menuList = MenuService::getMenu($controllerName);
  43. $this->subMenuList = MenuService::getSubMenu($controllerName, $actionName);
  44. $get = Yii::$app->request->get();
  45. //$this->isLogin = xhAdminService::isLogin();
  46. $this->isWeixin = util::isWeixin();
  47. $noNeedLogin = false;//需要登陆
  48. $account = isset($get['account']) ? $get['account'] : '';
  49. //后台默认的输出方式是网页
  50. Yii::$app->params['errorExportStyle'] = 'web';
  51. Yii::$app->params['appStyle'] = 'backend';
  52. $time = time();
  53. $urlSession = Yii::$app->request->get('SESSIONID', '');
  54. //通过url进行session id传输
  55. if (empty($urlSession)) {
  56. $sessionId = Yii::$app->request->cookies->getValue("SESSIONID");
  57. if (!empty($sessionId)) {
  58. $remainTime = Yii::$app->redis->executeCommand('TTL', [$sessionId]);
  59. //cookie过期时间不足2分钟时续期
  60. if ($remainTime < 120) {
  61. $expireTime = 10 * 60 + $time;
  62. Yii::$app->response->cookies->add(new Cookie([
  63. 'name' => 'SESSIONID',
  64. 'value' => $sessionId,
  65. 'domain' => $_SERVER['SERVER_NAME'],
  66. 'expire' => $expireTime
  67. ]));
  68. }
  69. $adminId = Yii::$app->redis->executeCommand('GET', [$sessionId]);
  70. if (!empty($adminId)) {
  71. $this->isLogin = true;
  72. }
  73. }
  74. }
  75. //绑定错误提示
  76. $this->error = error::instance();
  77. $merchant = [];
  78. if ($this->isLogin == true) {
  79. //$adminId = xhAdminService::getId();
  80. $admin = xhAdminService::getById($adminId);
  81. $noNeedLogin = true;
  82. if (!empty($admin) && isset($admin['openId'])) {
  83. $largeImg = $admin['avatar'];
  84. $this->adminId = $adminId;
  85. $this->admin = $admin;
  86. $this->adminAvatar = xhAdminService::getAvatarList($largeImg);
  87. $relation = xhAdminToMerchantService::getByAdminId($adminId);
  88. if (!empty($relation)) {
  89. $merchantId = $relation['merchantId'];
  90. $merchant = xhMerchantService::getById($merchantId);
  91. } else {
  92. //不是创建管理员过程,有非管理员想登陆后台
  93. if ($action->controller->id != 'admin' && $action->id != 'generate') {
  94. $noNeedLogin = false;
  95. xhAdminService::logout();
  96. util::stop('确认您已经加入花卉宝?请与管理员联系');
  97. }
  98. }
  99. } else {
  100. if ($action->controller->id != 'admin' && $action->id != 'generate') {
  101. $noNeedLogin = false;
  102. xhAdminService::logout();
  103. util::stop('确认您已经加入花卉宝?请与管理员联系…');
  104. } else {
  105. xhAdminService::logout();
  106. $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  107. $this->redirect($url);
  108. util::end();
  109. }
  110. }
  111. if (!empty($merchant)) {
  112. $merchantId = $merchant['id'];
  113. $this->merchantId = $merchantId;
  114. $this->merchant = $merchant;
  115. $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  116. $this->merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  117. $this->merchantName = $this->merchant['merchantName'];
  118. $this->merchantLogo = $this->merchant['logo'];
  119. $this->appId = $this->merchant['wxAppId'];
  120. }
  121. }
  122. if (!empty($account)) {
  123. $merchant = xhMerchantService::getByAccount($account);
  124. if (!empty($merchant)) {
  125. $merchantId = $merchant['id'];
  126. $this->merchant = $merchant;
  127. $this->merchantId = $merchantId;
  128. $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  129. $this->merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  130. $this->merchantName = $this->merchant['merchantName'];
  131. $this->merchantLogo = $this->merchant['logo'];
  132. $this->appId = $this->merchant['wxAppId'];
  133. }
  134. }
  135. if (!in_array($action->id, $this->withoutLogin) && $this->isLogin == false) {
  136. $this->redirect(['main/login']);
  137. util::end();
  138. }
  139. Yii::$app->params['merchantId'] = $this->merchantId;
  140. Yii::$app->params['merchant'] = $this->merchant;
  141. $this->ourself = ['uid' => $this->merchantId, 'userName' => $this->merchant['merchantName'], 'avatar' => $this->merchant['logo'], 'role' => 'merchant'];
  142. $this->usageList = UsageRelationService::getMyUsage();
  143. $this->categoryList = CategoryRelateService::getCategoryData();
  144. return parent::beforeAction($action);
  145. }
  146. /**
  147. * 商户数据权限检测
  148. */
  149. protected function merchantCheck($data, $msg = '')
  150. {
  151. if (empty($data)) {
  152. $this->error();
  153. }
  154. if ($data['merchantId'] != $this->merchantId) {
  155. $url = \yii\helpers\Url::to(['error/error-page']) . "?statusCode=illegalMerchantId";
  156. $this->redirect($url);
  157. Yii::$app->end();
  158. }
  159. return true;
  160. }
  161. /**
  162. * 后台友好错误提示页
  163. * @param string $errorType 错误类型(从configDict中取,或从备置文件config中)
  164. */
  165. protected function error($errorType = 'illegalData')
  166. {
  167. $url = \yii\helpers\Url::to(['error/error-page']) . "?statusCode=" . $errorType;
  168. $this->redirect($url);
  169. Yii::$app->end();
  170. }
  171. //参数验证方法 shish 2019.8.19
  172. public function validate($valid, $data)
  173. {
  174. return validate::check($valid, $data);
  175. }
  176. //方法里错误输出设置为json shish 2019.8.21
  177. public function errorExportJson()
  178. {
  179. Yii::$app->params['errorExportStyle'] = 'json';
  180. }
  181. }