| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- namespace backend\modules\admin\controllers;
- use backend\controllers\AdminController;
- use backend\modules\common\services\MallService;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\logic\common\MallCreateSync;
- use common\models\addons\AddonsCombo;
- use common\models\addons\MallAddonsComboRelation;
- use common\models\backend\Admin;
- use common\models\common\PlatformSetting;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\mall\MallHost;
- use Yii;
- /**
- * 商城控制器
- *
- * @Author bing
- * @DateTime 2021-08-13 15:29:41
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class MallController extends AdminController
- {
- public function actionIndex($keyword = null, $is_recycle = 0)
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $admin = Yii::$app->user->identity;
- $admin_type = $admin->admin_type;
- $params = $request->get();
- if (!in_array($admin_type, [Admin::ADMIN_TYPE_SUPER, Admin::ADMIN_TYPE_AGENT])) {
- return $this->error('管理员类型不符合条件');
- }
- // 查询参数
- $where[] = ['status' => StatusEnum::ENABLED, 'is_recycle' => $is_recycle];
- // ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $where[] = ['admin_id' => $admin->id];
- (!empty(trim($keyword))) && $where[] = ['LIKE', 'name', $keyword];
- if(!empty($params['mobile'])){
- $admin_list = Admin::lists([
- 'where'=>[
- ['like','username',trim($params['mobile'])],
- ['is_default'=>1],
- ['status'=>StatusEnum::ENABLED]
- ]
- ]);
- $mall_ids = array_column($admin_list,'mall_id');
- $where[] = ['id'=>$mall_ids];
- }
- if(!empty($params['date'])){
- $where[] = ['<=', 'expired_at', strtotime($params['date']) + 86400];
- $where[] = ['>', 'expired_at', 0];
- }
- $with = [
- 'admin' => function ($query) {
- $query->select('id,username');
- },
- 'adonsComboRelation',
- 'mallHost'
- ];
- $params = array('where' => $where, 'with' => $with, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
- $page_data = Mall::listPage($params, false);
- // 获取总页数
- $page_data['page_count'] = Mall::getMallTotalPage($params);
- $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
- $system = PlatformSetting::getConfByGroup('system', true);
- $h5_url = $system['h5_url']['value']??'';
- if(!empty($page_data['list']))$mall_ids = array_column($page_data['list'],'id');
- if(!empty($mall_ids)){
- $admin_list = Admin::findAll(['mall_id'=>$mall_ids,'is_default'=>1]);
- $admin_arr = array_column($admin_list,'username','mall_id');
- }
- $mall_ids = [];
- foreach ($page_data['list'] as &$mall) {
- $mall['logo'] = Yii::$app->services->setting->mall->get($mall['id'], 'basic.logo');
- if(empty($mall['logo'])) $mall['logo'] = \Yii::$app->request->hostInfo.'/resources/img/admin/mall-logo.png';
- $mall_sign = $mall['mall_sign'];
- $account = $admin_arr[$mall['id']]??'';
- $password=$account?substr($account,-6,6):'';
- $mall_info = "
- 后台网址:".$login_url."&mall_sign={$mall_sign}
- 账号:{$account}
- H5网址:{$h5_url}/#/?sign={$mall_sign}";
- $mall['mall_info'] = $mall_info;
- $mall_ids[] = $mall['id'];
- }
- $page_data['admin_type'] = $admin_type;
- // 获取付费套餐
- $page_data['addons_combo'] = AddonsCombo::getAddonsCombo([],[],true,"id,title");
- $page_data['addons_combo_data'] = array_column($page_data['addons_combo'],'title','id');
- return $this->success('操作成功', $page_data);
- }
- $has_subdomain = (isset(Yii::$app->params['has_subdomain']) && Yii::$app->params['has_subdomain']) ? 1 : 0;
- return $this->render($this->action->id, ['has_subdomain' => $has_subdomain]);
- }
- public function actionTest(){
- dd($_SERVER);
- $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
- var_dump($login_url);
- }
- /**
- * 管理员后台进入商城
- * @Author bing
- * @DateTime 2021-08-19 09:48:32
- * @return void
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public function actionEntry()
- {
- $mall_id = Yii::$app->request->post('mall_id');
- $admin = \Yii::$app->user->identity;
- $cond = ['id' => $mall_id, 'is_recycle' => 0, 'status' => StatusEnum::ENABLED];
- // ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $cond['admin_id'] = $admin->id;
- $mall = Mall::find()->where($cond)->asArray()->one();
- if(!empty($mall['expired_at'])&&time()>$mall['expired_at']) return $this->error('商城已过期');
- if (empty($mall)) return $this->error('商城不存在');
- Yii::$app->session->set('mall', $mall);
- $redirect_url = Yii::$app->urlManager->createUrl(['mall/overview/index']);
- $this->success('ok', compact('redirect_url'));
- }
- public function actionCreate()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name'], 'required'],
- [['id', 'logo', 'expired_at', 'password', 'mobile', 'addons_combo_id','is_show_copyright','install_addons','is_sync_mall_data',
- 'contact_tel','is_compliance'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if(empty($params['id'])){
- $res = Admin::findAll(['mobile'=>$params['mobile'],'status' => StatusEnum::ENABLED]);
- if($res)return $this->error('手机号码已存在');
- }
- Yii::$app->user->identity->getId();
- $admin = Yii::$app->user->identity;
- if($admin['mall_num']!=-1 && empty($params['id'])){
- $counts = Mall::find()->where(['admin_id'=>$admin['id'],'status' => StatusEnum::ENABLED])->count();
- if($counts>=$admin['mall_num']) return $this->error('您只能创建'.$admin['mall_num'].'个商城');
- }
- if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']);
- // 获取操作用户
- $params['operator_id'] = $this->admin->id;
- $params['operator_username'] = $this->admin->account;
- if(empty($params['is_show_copyright'])) $params['is_show_copyright'] = 0;
- if(empty($params['is_sync_mall_data'])) $params['is_sync_mall_data'] = 0;
- if(empty($params['contact_tel'])) $params['contact_tel'] = '';
- $res = Mall::setData($params);
- if ($res === false) return $this->error(Mall::getStaticError());
- $mall_id=$res['id'];
- // 批量安装插件
- if (!empty($params['id']) && $admin->admin_type == Admin::ADMIN_TYPE_SUPER) {
- $install_addons = $params['install_addons'] ?? [];
- if($install_addons){
- $res = MallAddons::batchInstall($params['id'], $install_addons);
- if ($res === false) return $this->error(MallAddons::getStaticError());
- }
- }
- if(!empty($params['is_sync_mall_data'])){
- \Yii::$app->services->rabbitMq->delay(10, ['self_mall_id'=>$mall_id], MallCreateSync::class, 'syncMallData');
- }
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- /**
- * @desc:删除/恢复商城
- * @Author: hua
- * @Date: 2021/12/13
- * @Time: 9:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionUpdate()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['is_recycle'], 'required'],
- [['id', ], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $post['id'] = $params['id'];
- $post['is_recycle'] = $params['is_recycle'];
- $post['status'] = $params['is_recycle'] == 1 ? StatusEnum::DELETE : StatusEnum::ENABLED;
- $post['is_delete'] = $params['is_recycle'] == 1 ? time() : 0;
- $post['is_disable'] = $params['is_recycle'] == 1 ? StatusEnum::ENABLED : StatusEnum::DISABLED;
- $res = Mall::setData($post);
- if ($res === false) return $this->error(Mall::getStaticError());
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- /**
- * 回收站
- * @Author: lun
- * @DateTime: 2023/4/27 0027 17:40
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param null $keyword
- * @return mixed|string|void
- */
- public function actionRecovery($keyword = null)
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $admin = Yii::$app->user->identity;
- $admin_type = $admin->admin_type;
- $params = $request->get();
- if (!in_array($admin_type, [Admin::ADMIN_TYPE_SUPER, Admin::ADMIN_TYPE_AGENT])) {
- return $this->error('管理员类型不符合条件');
- }
- // 查询参数
- $where[] = ['status' => StatusEnum::DELETE, 'is_recycle' => StatusEnum::ENABLED, 'is_delete' => StatusEnum::DISABLED];
- ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $where[] = ['admin_id' => $admin->id];
- (!empty(trim($keyword))) && $where[] = ['LIKE', 'name', $keyword];
- if(!empty($params['mobile'])){
- $admin_list = Admin::lists([
- 'where'=>[
- ['like','username',trim($params['mobile'])],
- ['is_default'=>1],
- ['status'=>StatusEnum::ENABLED]
- ]
- ]);
- $mall_ids = array_column($admin_list,'mall_id');
- $where[] = ['id'=>$mall_ids];
- }
- $with = [
- 'admin' => function ($query) {
- $query->select('id,username');
- },
- 'adonsComboRelation',
- ];
- $params = array('where' => $where, 'with' => $with, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
- $page_data = Mall::listPage($params, false);
- // 获取总页数
- $page_data['page_count'] = Mall::getMallTotalPage($params);
- foreach ($page_data['list'] as &$mall) {
- $mall['logo'] = Yii::$app->services->setting->mall->get($mall['id'], 'basic.logo');
- if(empty($mall['logo'])) $mall['logo'] = Yii::$app->request->hostInfo.'/resources/img/admin/mall-logo.png';
- }
- $page_data['admin_type'] = $admin_type;
- // 获取付费套餐
- $page_data['addons_combo'] = AddonsCombo::getAddonsCombo([],[],true,"id,title");
- $page_data['addons_combo_data'] = array_column($page_data['addons_combo'],'title','id');
- return $this->success('操作成功', $page_data);
- }
- return $this->render($this->action->id);
- }
- /**
- * 设置域名
- *
- * @return string
- */
- public function actionCustomHost(int $mall_id)
- {
- if ($this->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['backend_url', 'h5_url', 'api_url', 'static_url', 'is_custom_host', 'ssl_path'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $mall_id;
- $res = MallHost::setData($params);
- if ($res === false) return $this->error(Mall::getStaticError());
- // 执行
- if ($params['is_custom_host']) {
- $service = new MallService([]);
- $service->setDomain(
- $mall_id, $params['backend_url'], $params['h5_url'], $params['api_url'], $params['static_url'], $params['ssl_path']
- );
- }
- return $this->success('操作成功');
- }
- }
- public function actionWraparound()
- {
- return $this->render($this->action->id);
- }
- }
|