| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace backend\controllers;
- use biz\auth\services\MenuService;
- use biz\goods\services\CategoryRelateService;
- use biz\goods\services\UsageRelationService;
- use biz\merchant\services\AdminService;
- use common\components\error;
- use common\components\stringUtil;
- use common\components\validate;
- use common\services\xhUserService;
- use Yii;
- use common\services\xhWxOpenService;
- use common\services\xhMerchantAssetService;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use common\services\xhAdminService;
- use common\components\util;
- use common\components\weixinUtil;
- use common\components\jsSDK;
- use common\services\xhAdminToMerchantService;
- use yii\web\Cookie;
- class BackendController extends PublicController
- {
- public $admin, $adminId, $adminAvatar, $account;
- public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantAsset, $merchantLogo, $signPackage;
- public $openMerchant, $openMerchantId;
- public $appId;
- public $isWeixin = false;
- public $isLogin = false;
- public $withoutLogin = [];//不需要登陆的方法名
- public $ourself = [];
- public $error;
- public $menuList;
- public $subMenuList;
- public $usageList;
- public $categoryList;
-
- public function beforeAction($action)
- {
- //取一级菜单 二级菜单 shish 2019.8.17
- $controllerName = $action->controller->id;
- $actionName = $action->id;
- $this->menuList = MenuService::getMenu($controllerName);
- $this->subMenuList = MenuService::getSubMenu($controllerName, $actionName);
-
- $get = Yii::$app->request->get();
- //$this->isLogin = xhAdminService::isLogin();
- $this->isWeixin = util::isWeixin();
- $noNeedLogin = false;//需要登陆
- $account = isset($get['account']) ? $get['account'] : '';
-
- //后台默认的输出方式是网页
- Yii::$app->params['errorExportStyle'] = 'web';
- Yii::$app->params['appStyle'] = 'backend';
-
- $time = time();
- $urlSession = Yii::$app->request->get('SESSIONID', '');
- //通过url进行session id传输
- if (empty($urlSession)) {
- $sessionId = Yii::$app->request->cookies->getValue("SESSIONID");
- if (!empty($sessionId)) {
- $remainTime = Yii::$app->redis->executeCommand('TTL', [$sessionId]);
- //cookie过期时间不足2分钟时续期
- if ($remainTime < 120) {
- $expireTime = 10 * 60 + $time;
- Yii::$app->response->cookies->add(new Cookie([
- 'name' => 'SESSIONID',
- 'value' => $sessionId,
- 'domain' => $_SERVER['SERVER_NAME'],
- 'expire' => $expireTime
- ]));
- }
- $adminId = Yii::$app->redis->executeCommand('GET', [$sessionId]);
- if (!empty($adminId)) {
- $this->isLogin = true;
- }
- }
- }
-
- //绑定错误提示
- $this->error = error::instance();
-
- $merchant = [];
- if ($this->isLogin == true) {
- //$adminId = xhAdminService::getId();
- $admin = xhAdminService::getById($adminId);
- $noNeedLogin = true;
- if (!empty($admin) && isset($admin['openId'])) {
- $largeImg = $admin['avatar'];
- $this->adminId = $adminId;
- $this->admin = $admin;
- $this->adminAvatar = xhAdminService::getAvatarList($largeImg);
- $relation = xhAdminToMerchantService::getByAdminId($adminId);
- if (!empty($relation)) {
- $merchantId = $relation['merchantId'];
- $merchant = xhMerchantService::getById($merchantId);
- } else {
- //不是创建管理员过程,有非管理员想登陆后台
- if ($action->controller->id != 'admin' && $action->id != 'generate') {
- $noNeedLogin = false;
- xhAdminService::logout();
- util::stop('确认您已经加入花卉宝?请与管理员联系');
- }
- }
- } else {
- if ($action->controller->id != 'admin' && $action->id != 'generate') {
- $noNeedLogin = false;
- xhAdminService::logout();
- util::stop('确认您已经加入花卉宝?请与管理员联系…');
- } else {
- xhAdminService::logout();
- $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
- $this->redirect($url);
- util::end();
- }
- }
-
- if (!empty($merchant)) {
- $merchantId = $merchant['id'];
- $this->merchantId = $merchantId;
- $this->merchant = $merchant;
- $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $this->merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- $this->merchantName = $this->merchant['merchantName'];
- $this->merchantLogo = $this->merchant['logo'];
- $this->appId = $this->merchant['wxAppId'];
- }
- }
- if (!empty($account)) {
- $merchant = xhMerchantService::getByAccount($account);
- if (!empty($merchant)) {
- $merchantId = $merchant['id'];
- $this->merchant = $merchant;
- $this->merchantId = $merchantId;
- $this->merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $this->merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- $this->merchantName = $this->merchant['merchantName'];
- $this->merchantLogo = $this->merchant['logo'];
- $this->appId = $this->merchant['wxAppId'];
- }
- }
- if (!in_array($action->id, $this->withoutLogin) && $this->isLogin == false) {
- $this->redirect(['main/login']);
- util::end();
- }
- Yii::$app->params['merchantId'] = $this->merchantId;
- Yii::$app->params['merchant'] = $this->merchant;
- $this->ourself = ['uid' => $this->merchantId, 'userName' => $this->merchant['merchantName'], 'avatar' => $this->merchant['logo'], 'role' => 'merchant'];
-
- $this->usageList = UsageRelationService::getMyUsage();
- $this->categoryList = CategoryRelateService::getCategoryData();
-
- return parent::beforeAction($action);
- }
-
- /**
- * 商户数据权限检测
- */
- protected function merchantCheck($data, $msg = '')
- {
- if (empty($data)) {
- $this->error();
- }
- if ($data['merchantId'] != $this->merchantId) {
- $url = \yii\helpers\Url::to(['error/error-page']) . "?statusCode=illegalMerchantId";
- $this->redirect($url);
- Yii::$app->end();
- }
-
- return true;
- }
-
- /**
- * 后台友好错误提示页
- * @param string $errorType 错误类型(从configDict中取,或从备置文件config中)
- */
- protected function error($errorType = 'illegalData')
- {
- $url = \yii\helpers\Url::to(['error/error-page']) . "?statusCode=" . $errorType;
- $this->redirect($url);
- Yii::$app->end();
- }
-
- //参数验证方法 shish 2019.8.19
- public function validate($valid, $data)
- {
- return validate::check($valid, $data);
- }
-
- //方法里错误输出设置为json shish 2019.8.21
- public function errorExportJson()
- {
- Yii::$app->params['errorExportStyle'] = 'json';
- }
-
- }
|