| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace addons\BossWeight\backend\controllers;
- use addons\BossWeight\common\enums\BossEnum;
- use addons\BossWeight\common\logic\RewardLog;
- use addons\BossWeight\common\models\AddonsBoss;
- use addons\BossWeight\common\models\AddonsBossLevel;
- use common\enums\StatusEnum;
- use common\models\common\UpgradeCondition;
- use common\models\order\Order;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- use common\models\goods\Goods;
- use common\models\user\UserLevel;
- /**
- * 股东等级控制器
- *
- * Class LevelController
- * @package addons\BossWeight\backend\controllers
- */
- class LevelController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'integer'],
- [['id','keyword'], 'safe'],
- [['keyword'], 'string'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['<>', 'status', StatusEnum::DELETE];
- isset($params['keyword']) && $where[] = ['like', 'name', $params['keyword']];
- $with = ['setting' => function($query) {
- $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- }];
- $order = 'level asc';
- $params = array('where' => $where, 'with' => $with, 'order'=>$order, 'limit' => $params['limit']);
- $list = AddonsBossLevel::listPage($params, false, true);
- if (empty($list)) $this->error('获取失败');
- $this->success('获取成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 等级数据处理
- * @Author: lun
- * @DateTime: 2021/10/20 0020 18:03
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionHandle()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'required'],
- [['id','status'], 'integer'],
- [['setting','status'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!isset($params['setting'])) unset($params['setting']);
- if (!isset($params['status'])) unset($params['status']);
- $params['mall_id'] = $this->mall_id;
- $res = AddonsBossLevel::setData($params);
- if (!$res) return $this->error('修改失败:msg=' . AddonsBossLevel::getStaticError());
- $this->success('获取成功');
- }
- }
- }
- /**
- * 等级编辑
- * @Author: lun
- * @DateTime: 2021/10/21 0021 11:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionEdit()
- {
- $upgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, BossEnum::ADDONS_NAME, 'upgrade_condition');
- // 获取等级升级的条件
- $list = [];
- if(!empty($upgradeCondition['level_upgrade_condition'])){
- $ids = Json::decode($upgradeCondition['level_upgrade_condition'],true);
- if(!empty($ids)) {
- $list = UpgradeCondition::getData($ids);
- }
- }
- $upgrade_condition_data = UpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- // 查看等级详情
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $data = [];
- if(!empty($id)){
- // 获取等级信息
- $model = AddonsBossLevel::getOne([['id' => $id, 'mall_id' => $this->mall_id]], true, ['setting' => function($query) {
- $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- }]);
- // 解析选中的条件跟条件值
- $model['checked_condition_values'] = Json::decode($model['checked_condition_values'],true) ?? [];
- $model['checked_condition_keys'] = Json::decode($model['checked_condition_keys'],true) ?? [];
- //处理key可能不存在 或者 数组为 null 的情况
- $key_arr = json_decode($upgrade_condition_data['key_arr'],true);
- foreach ($key_arr as $key=>$arr){
- if(isset($model['checked_condition_values'][$key]) == false){
- $model['checked_condition_values'][$key] = $arr;
- }
- }
- foreach ($model['checked_condition_values'] as $key=>&$value){
- if($value['key'] == null){
- $value = $key_arr[$key];
- }
- }
- unset($value);
- // 解析被选中的商品
- $model['goods_ids'] = Json::decode($model['goods_ids'],true) ?? [];
- $model['goods_list'] = Json::decode($model['goods_list'],true) ?? [];
- unset($model['updated_at'], $model['created_at']);
- $data['detail'] = $model;
- $data['goods'] = [];
- if (!empty($data['detail']['setting'])){
- unset($data['detail']['setting']['level']);// 删除配置的等级防止修改等级报错
- if($data['detail']['setting']['checked_weight_values']){
- $data['detail']['setting']['checked_weight_values'] = json_decode($data['detail']['setting']['checked_weight_values'],true);
- }
- if($data['detail']['setting']['checked_weight_values'][0]['value']['val1']){
- $data['goods'] = Goods::find()->andWhere(['in','id',$data['detail']['setting']['checked_weight_values'][0]['value']['val1']])->asArray()->all();
- }
- }
- }
- $data['weights'] = AddonsBossLevel::getLevelWeights($this->mall_id);
- $data['memberLevels'] = UserLevel::find()->select('name,level')->where(['mall_id' => $this->mall_id,'status' => 1])->asArray()->all();
- return $this->success('操作成功', $data);
- }
- // 提交等级内容
- if ($request->isPost) {
- $params = Yii::$app->request->post()['form'];
- $res = Yii::$app->services->validate->validate($params, [
- [['level', 'name', 'explain', 'status'], 'required'],
- [['id', 'is_auto_upgrade', 'checked_condition_values', 'setting',
- 'checked_condition_keys', 'condition_type', 'upgrade_type_goods', 'goods_list', 'upgrade_type_condition', 'goods_ids', 'goods_type',
- 'buy_goods_type', 'extra_limit_price','goods_list'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- // 将数组转为json字符串
- foreach ($params as &$item) {
- if (is_array($item)) $item = Json::encode($item);
- }
- $res = AddonsBossLevel::setData($params);
- if ($res === false) throw new Exception(AddonsBossLevel::getStaticCodeError()['error']);
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render('edit', $upgrade_condition_data);
- }
- /**
- * 设置升级条件
- * @Author: lun
- * @DateTime: 2021/10/21 0021 11:28
- * @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'] = UpgradeCondition::getData(UpgradeCondition::$boss_weight_condition_ids);
- $upgradeCondition = Yii::$app->services->setting->addons->get($this->mall_id, BossEnum::ADDONS_NAME, 'upgrade_condition');
- $data['checked_condition_keys'] = !empty($upgradeCondition['level_upgrade_condition']) ? Json::decode($upgradeCondition['level_upgrade_condition'], true) : [];
- 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'] = ['level_upgrade_condition' => Json::encode($postData['checked_condition_keys'])];
- Yii::$app->services->setting->addons->set($this->mall_id, BossEnum::ADDONS_NAME, $data);
- UpgradeCondition::clear($postData['checked_condition_keys'],'addons\BossWeight\common\models\AddonsBossLevel',$this->mall_id);
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- return $this->render('condition');
- }
- }
|