| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace addons\WechatVideoShop\backend\controllers;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use addons\WechatVideoShop\common\event\WechatVideoShopLevelEvent;
- use addons\WechatVideoShop\common\models\WechatVideoShopLevel;
- use addons\WechatVideoShop\common\models\WechatVideoShopSharer;
- use addons\WechatVideoShop\common\models\WechatVideoShopUpgradeCondition;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- class LevelController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * @desc:首页
- * @Author: hua
- * @Date: 2021/10/20
- * @Time: 16:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- // 查询参数
- $get = \Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $where[] = ['or', ['like', 'name', $get['keyword']], ['like', 'level', $get['keyword']]];
- $params = array('where' => $where, 'limit' => $request->get('limit', 12));
- $params['order'] = 'level asc';
- $page_data = WechatVideoShopLevel::listPage($params, false);
- return $this->success('操作成功', $page_data);
- }
- return $this->render($this->action->id);
- }
- /**
- * @desc:修改状态
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 16:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- * @throws Exception
- */
- public function actionSwitchStatus()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['status'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = WechatVideoShopLevel::setData($params);
- if ($res === false) return $this->error(WechatVideoShopLevel::getStaticError());
- $event = new WechatVideoShopLevelEvent($this->mall_id);
- $data = [
- 'mall_id'=>$this->mall_id,
- 'level'=>$res['level'],
- 'level_name'=>$res['name'],
- 'status'=>$res['status'],
- 'id'=>$res['id'],
- 'type'=>'edit',
- ];
- \Yii::$app->services->events->dispatch($event, $data , $event->listeners);
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:删除等级
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 16:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- * @throws Exception
- */
- public function actionDelete()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = WechatVideoShopLevel::del($params);
- if ($res === false) return $this->error(WechatVideoShopLevel::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:设置升级条件
- * @Author: hua
- * @Date: 2021/10/20
- * @Time: 18:50
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionCondition()
- {
- if (\Yii::$app->request->isAjax) {
- try {
- if (\Yii::$app->request->isGet) {
- $data['upgrade_condition'] = WechatVideoShopUpgradeCondition::getData(WechatVideoShopUpgradeCondition::$wechat_video_shop_condition_ids);
- $agentUpgradeCondition = Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, 'upgrade_condition');
- $data['checked_condition_keys'] = !empty($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']) ?
- Json::decode($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']) : [];
- return $this->success('操作成功', $data);
- } else {
- $postData = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($postData, [
- [['checked_condition_keys'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $data['upgrade_condition'] = ['wechat_video_shop_level_upgrade_condition' => Json::encode($postData['checked_condition_keys'])];
- Yii::$app->services->setting->addons->set($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, $data);
- WechatVideoShopUpgradeCondition::clear($postData['checked_condition_keys'], WechatVideoShopLevel::class, $this->mall_id);
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- return $this->render('condition');
- }
- /**
- * @desc:编辑等级
- * @Author: hua
- * @Date: 2021/10/20
- * @Time: 15:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $mall_id = $this->mall_id;
- $data = [];
- if (!empty($id)) {
- $model = WechatVideoShopLevel::getOne([['id' => $id]], true);
- $model['checked_condition_values'] = Json::decode($model['checked_condition_values']) ?? [];
- if (empty($model['checked_condition_values'])) unset($model['checked_condition_values']);
- $model['checked_condition_keys'] = Json::decode($model['checked_condition_keys']) ?? [];
- $model['goods_ids'] = Json::decode($model['goods_ids']) ?? [];
- $model['goods_list'] = Json::decode($model['goods_list']) ?? [];
- if(!empty($model['goods_list'])){
- $goods_ids = array_column($model['goods_list'],'id');
- $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name','index'=>'id']);
- foreach ($model['goods_list'] as&$item){
- if(isset($goods_list[$item['id']])){
- $item['cover_pic'] = $goods_list[$item['id']]['cover_pic'];
- $item['name'] = $goods_list[$item['id']]['goods_name'];
- }
- }
- }
- unset($model['updated_at'], $model['created_at']);
- $model['score'] = [
- 'is_percent_team' => 0,
- 'is_percent_equal' => 0,
- 'wechat_video_shop_team_prize' => 0,
- 'wechat_video_shop_equal_prize' => 0,
- 'wechat_video_shop_over_prize' => 0,
- ];
- $model['commission'] = [
- 'is_percent_team' => 0,
- 'is_percent_equal' => 0,
- 'wechat_video_shop_team_prize' => 0,
- 'wechat_video_shop_equal_prize' => 0,
- 'wechat_video_shop_over_prize' => 0,
- ];
- $data['detail'] = $model;
- }
- $data['weights'] = WechatVideoShopLevel::getLevelWeights($this->mall_id);
- return $this->success('操作成功', $data);
- }
- if ($request->isPost) {
- $params = Yii::$app->request->post()['form'];
- $res = Yii::$app->services->validate->validate($params, [
- [['level', 'name'], 'required'],
- [['id', 'is_auto_upgrade', 'score', 'commission', 'checked_condition_values',
- 'checked_condition_keys', 'condition_type', 'upgrade_type_goods', 'goods_list', 'upgrade_type_condition', 'goods_ids', 'goods_type',
- 'buy_goods_type','explain','reward_commission_type','reward_commission', 'reward_score_type', 'reward_score'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if(!empty($params['id'])){
- $distribution_level = WechatVideoShopLevel::findOne(['id'=>$params['id'],'mall_id'=>$this->mall_id,'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]);
- if($distribution_level&&$distribution_level->level!=$params['level']){
- $res = WechatVideoShopSharer::findOne(['mall_id'=>$this->mall_id,
- 'level'=>$distribution_level->level,
- 'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]);
- if($res) return $this->error('该等级关联了经销商,等级不能修改');
- }
- }
- $params['mall_id'] = $this->mall_id;
- $res = WechatVideoShopLevel::setData($params);
- if ($res === false) throw new Exception(WechatVideoShopLevel::getStaticCodeError()['error']);
- $event = new WechatVideoShopLevelEvent($this->mall_id);
- $data = [
- 'mall_id'=>$this->mall_id,
- 'level'=>$params['level'],
- 'level_name'=>$params['name'],
- 'status'=>$res['status'],
- 'id'=>$res['id'],
- 'type'=>!empty($params['id'])?'edit':'add',
- ];
- \Yii::$app->services->events->dispatch($event,$data , $event->listeners);
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- $agentUpgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, 'upgrade_condition');
- $list = [];
- if (!empty($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition'])) {
- $ids = Json::decode($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']);
- $list = WechatVideoShopUpgradeCondition::getData($ids);
- }
- $upgrade_condition_data = WechatVideoShopUpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
- return $this->render('edit', $upgrade_condition_data);
- }
- /**
- * @desc:权重
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 15:56
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionSetting()
- {
- if (\Yii::$app->request->isAjax) {
- $weights = WechatVideoShopLevel::getLevelWeights($this->mall_id);
- $enable_level_list = [];
- foreach ($weights as $k) {
- if ($k['disabled']) {
- unset($k['disabled']);
- $enable_level_list[] = $k;
- }
- }
- return $this->success('操作成功', ['weights' => $weights, 'enable_level_list' => $enable_level_list]);
- }
- return $this->error();
- }
- }
|