| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace business\controllers;
- use biz\merchant\services\AdminService;
- use biz\merchant\services\MerchantExtendService;
- use biz\merchant\services\MerchantService;
- use common\components\httpUtil;
- use common\components\jwt;
- use Yii;
- use common\components\util;
- class BaseController extends PublicController
- {
- public $admin, $adminId, $adminAvatar, $account;
- public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantLogo, $signPackage;
- public $openMerchant, $openMerchantId;
- public $appId;
- public $isWx = false;
- public $isLogin = false;
- public $withoutLogin = [];//不需要登陆的方法名
- public $validate = true;
-
- public function beforeAction($action)
- {
- //token验证
- $adminId = jwt::getLoginId();
- if (empty($adminId)) {
- //本地环境请求接口没有登陆用户时设置一个默认管理员
- $env = getenv('YII_ENV', 'local');
- if (in_array($env, ['local', 'dev'])) {
- $adminId = 6;
- } else {
- $this->validate = false;
- }
- }
- //游客可以访问的方法
- if (in_array($action->id, $this->withoutLogin)) {
- $this->validate = true;
- }
-
- if ($this->validate == false) {
- util::notLogin();
- }
- if (httpUtil::isWx()) {
- //授权方式获取客户信息 xhAdmin userId存的是每个公众号店长的微信userId
- $admin = AdminService::getByCondition(['userId' => $adminId]);
- } else {
- $admin = AdminService::getById($adminId);
- }
- if (empty($admin)) {
- util::fail('没有找到管理员,但已经获取客户ID:' . $adminId);
- }
- $merchantId = $admin['merchantId'];
- if (empty($merchantId)) {
- util::fail('没有找到商家信息,但有管理员ID:' . $adminId);
- }
- $merchant = MerchantService::getMerchantById($merchantId);
- $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
- $this->admin = $admin;
- $this->adminId = $adminId;
- $this->merchantId = $merchantId;
- $this->merchant = $merchant;
- $this->merchantExtend = $merchantExtend;
- return parent::beforeAction($action);
- }
-
- public
- function actionList()
- {
- $numargs = func_num_args();
- $select = $numargs >= 1 ? func_get_arg(0) : '*';
- $where = $numargs >= 2 ? func_get_arg(1) : null;
- $order = $numargs >= 3 ? func_get_arg(2) : '';
- $with = $numargs >= 4 ? func_get_arg(3) : '';
-
- $service = $this->service;
- $list = $service::getList($select, $where, $order, $with);
- util::success($list);
- }
-
- public
- function actionAdd()
- {
- $data = Yii::$app->request->post();
- $service = $this->service;
- $re = $service::add($data);
- util::success($re);
- }
-
- public
- function actionDetail()
- {
- $id = Yii::$app->request->get('id');
- $service = $this->service;
- $data = $service::getById($id);
- if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
- util::fail('没有权限');
- }
- if (!empty($data)) {
- util::success($data);
- }
-
- util::fail('');
- }
-
- public
- function actionUpdate()
- {
- $data = Yii::$app->request->post();
- $id = $data['id'];
- $service = $this->service;
- $re = $service::updateById($id, $data);
- if ($re['status'] === true) {
- util::success($re);
- }
-
- util::fail();
- }
-
- public
- function actionDelete()
- {
- $id = Yii::$app->request->get('id');
- $service = $this->service;
- $service::deleteById($id);
- util::success([], '删除成功');
- }
- }
|