| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- namespace addons\Distribution\backend\controllers;
- use addons\Distribution\common\models\Distribution;
- use addons\Distribution\common\models\DistributionCommissionBasisLog;
- use addons\Distribution\common\models\DistributionLevelUpgradeLog;
- use addons\Distribution\common\models\DistributionLevel;
- use common\enums\AddonsEnum;
- use common\enums\DownloadEnum;
- use common\enums\LevelUpgradeLogEnum;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\models\user\User;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Distribution\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- public $from_type = ['Distribution'];
- /**
- * @desc:首页
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 18:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- * @throws \Exception
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = [];
- $user_id_arr = [];
- if (isset($get['level'])&&$get['level']!=='') $params['where'][] = ['=', 'level', $get['level']];
- $is_search = 0;
- if (!empty($get['user_id'])){
- $is_search = 1;
- $user_id_arr[]=$get['user_id'];
- }
- if (!empty($get['keyword'])) {
- $is_search = 1;
- $user_list = User::lists(['where'=>[['mall_id'=>$this->mall_id],[
- 'or',
- ['like', 'username', $get['keyword']],
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]],'select'=>'id']);
- $user_ids = array_column($user_list,'id');
- if(!empty($user_id_arr)){
- $user_id_arr = array_intersect($user_id_arr,$user_ids);
- }else{
- $user_id_arr = $user_ids;
- }
- }
- if($is_search)$params['where'][] = ['in', 'user_id', $user_id_arr];
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['order'] = 'id desc';
- $params['limit'] = 10;
- $pageData = Distribution::listPage($params, false);
- if ($pageData === false) return $this->error(Distribution::getStaticError());
- if(!empty($pageData['list'])){
- $user_id_arr = array_column($pageData['list'],'user_id');
- $user_list = $user_list = User::lists(['where'=>[['id'=>$user_id_arr]],'select'=>'id,nickname,avatar_url,mobile,parent_id']);
- $user_arr = $parent_arr = [];
- $parent_id_arr = [];
- foreach ($user_list as $item){
- $parent_id_arr[]=$item['parent_id'];
- $user_arr[$item['id']] = $item;
- }
- $user_parent_list = $user_list = User::lists(['where'=>[['id'=>$parent_id_arr]],'select'=>'id,nickname,avatar_url']);
- foreach ($user_parent_list as $item){
- $parent_arr[$item['id']] = $item;
- }
- foreach ($pageData['list'] as &$v) {
- $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
- if (isset($user_arr[$v['user_id']])) {
- $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
- $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
- $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
- $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
- if (isset($parent_arr[$v['parent_id']])) $v['parent_name'] = $parent_arr[$v['parent_id']]['nickname'];
- }
- }
- }
- $wheres = [];
- $wheres[] = ['mall_id' => $this->mall_id];
- $wheres[] = ['status' => StatusEnum::ENABLED];
- $level_list = DistributionLevel::getLevelList($wheres, [], true, 'id,level,name');
- $pageData['distributionLevelList'] = $level_list;
- $pageData['export_list'] = Distribution::exportFieldsList();
- return $this->success('操作成功', $pageData);
- }
- }
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $filename = '分销商列表-' . date('YmdHis').'_'.rand(10000,99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_DISTRIBUTION,Distribution::class,'exportHandle',$post);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- return $this->render('index', []);
- }
- /**
- * @desc:编辑
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 18:17
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id'], 'required'],
- [[ 'level'], 'required','message'=>'请选择等级'],
- [['user_id', 'level'], 'integer'],
- [['id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
- // if(isset($configs['is_default_level'])&&empty($configs['is_default_level'])) return $this->error('默认等级已经关闭');
- $wheres = [
- 'user_id' => $params['user_id'],
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ];
- $model = Distribution::findOne($wheres);
- if (!empty($model)) return $this->error('该会员已经是分销商,不能重复添加');
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::ENABLED;
- $res = Distribution::setData($params);
- if ($res === false) return $this->error(Distribution::getStaticError());
- //添加升级日志
- $upgrade_log_obj_clone = new DistributionLevelUpgradeLog();
- $upgrade_log_obj_clone->mall_id = $this->mall_id;
- $upgrade_log_obj_clone->user_id = $res['user_id'];
- $upgrade_log_obj_clone->before_level = 0;
- $upgrade_log_obj_clone->after_level = $res['level'];
- $upgrade_log_obj_clone->upgrade_type = LevelUpgradeLogEnum::MANUALLY_UPGRADE;
- $upgrade_log_obj_clone->order_id = 0;
- $upgrade_log_obj_clone->admin_id = $this->admin->id;
- $upgrade_log_obj_clone->upgrade_condition = [];
- $upgrade_log_obj_clone->save();
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:修改备注
- * @Author: hua
- * @Date: 2021/10/29
- * @Time: 10:34
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionRemarksEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['remarks'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = Distribution::setData($params);
- if ($res === false) return $this->error(Distribution::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:
- * @Author: hua
- * @Date: 2021/10/29
- * @Time: 10:37
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionSearchUser()
- {
- if (\Yii::$app->request->isAjax) {
- if (!\Yii::$app->request->isGet) return $this->error('请求方式错误');
- $keyword = \Yii::$app->request->get('keyword');
- $params = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- if (!empty($keyword)) $where[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword], ['like', 'id', $keyword]];
- $params['where'] = $where;
- $params['select'] = 'id,nickname,mobile,username,avatar_url';
- $params['limit'] = 20;
- $list = User::lists($params);
- if (!empty($list)) {
- foreach ($list as &$item) {
- if (empty($item['nickname'])) $item['nickname'] = $item['mobile'] . "(id:{$item['id']})";
- }
- }
- return $this->success('操作成功', ['list' => $list]);
- }
- }
- /**
- * @desc:
- * @Author: hua
- * @Date: 2021/11/12
- * @Time: 12:19
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLevelChange()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- $post = \Yii::$app->request->post();
- if ($post['level']) {
- $distributionLevel = DistributionLevel::findOne(['mall_id'=>$this->mall_id,'level'=>$post['level'],'status'=>StatusEnum::ENABLED]);
- if (empty($distributionLevel)) return $this->error('无效的分销商等级');
- } else {
- $post['level'] = 0;
- }
- $post['mall_id'] =$this->mall_id;
- $post['upgrade_status'] = Distribution::UPGRADE_STATUS_MANUAL;
- $detail = Distribution::getOne([['id' => $post['id']]], true);
- $res = Distribution::setData($post);
- if ($res==false) throw new \Exception(DistributionLevel::getStaticError());
- if ($detail['level'] != $post['level']) {
- $upgrade_log_obj_clone = new DistributionLevelUpgradeLog();
- $upgrade_log_obj_clone->mall_id = $this->mall_id;
- $upgrade_log_obj_clone->user_id = $detail['user_id'];
- $upgrade_log_obj_clone->before_level = $detail['level'];
- $upgrade_log_obj_clone->after_level = $post['level'];
- $upgrade_log_obj_clone->upgrade_type = $detail['level'] < $post['level'] ? LevelUpgradeLogEnum::MANUALLY_UPGRADE : LevelUpgradeLogEnum::DOWNGRADE_MANUALLY;
- $upgrade_log_obj_clone->order_id = 0;
- $upgrade_log_obj_clone->admin_id = $this->admin->id;
- $upgrade_log_obj_clone->upgrade_condition = [];
- $upgrade_log_obj_clone->save();
- }
- return $this->success('修改成功');
- }
- } catch (\Exception $exception) {
- return $this->error($exception->getMessage().$exception->getLine().$exception->getFile());
- }
- }
- /**
- * @desc:批量修改等级
- * @Author: hua
- * @Date: 2021/10/29
- * @Time: 10:41
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionBatchLevel()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['batch_ids', 'level'], 'required'],
- [['level'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::ENABLED;
- $res = Distribution::batchLevel($params);
- if ($res === false) return $this->error(Distribution::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- /**
- * @desc:
- * @Author: hua
- * @Date: 2021/10/29
- * @Time: 10:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelete()
- {
- if (\Yii::$app->request->isAjax) {
- if (!\Yii::$app->request->isPost) return $this->error('请求方式错误');
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['id'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = Distribution::setData($params);
- if ($res) return $this->success('删除成功');
- return $this->error(Distribution::getStaticError());
- }
- }
- public function actionEnableList()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $level = \Yii::$app->request->get('level');
- $where = [['mall_id'=>$this->mall_id],['=','status',StatusEnum::ENABLED]];
- if(!empty($level)) $where[]=['level'=>$level];
- $args['where'] = $where;
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $list['list'] = DistributionLevel::lists($args,true);
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic');
- if(!isset($configs['is_default_level'])||!empty($configs['is_default_level'])){
- $default_level_name=!empty($configs['default_level_name'])?$configs['default_level_name']:'默认等级';
- array_unshift($list['list'],['id'=>0, 'level'=> 0, 'name'=> $default_level_name]);
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->error();
- }
- public function actionCommissionBasicLog()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $get['mall_id'] = $this->mall_id;
- $params = [];
- if (!empty($get['source_table_id'])) {
- $params['where'][] = ['source_table_id'=> $get['source_table_id']];
- }
- $params['where'][] = ['=', 'mall_id',$this->mall_id];
- $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['order'] = 'id desc';
- $params['limit'] = 10;
- $pageData = DistributionCommissionBasisLog::listPage($params, false);
- if ($pageData === false) return $this->error(Distribution::getStaticError());
- if(!empty($pageData['list'])){
- foreach ($pageData['list'] as &$item){
- if($item['basic_config']) $item['basic_config'] = json_encode(json_decode($item['basic_config'],true),JSON_UNESCAPED_UNICODE);
- if($item['level_config']) $item['level_config'] = json_encode(json_decode($item['level_config'],true),JSON_UNESCAPED_UNICODE);
- if($item['goods_config']) $item['goods_config'] = json_encode(json_decode($item['goods_config'],true),JSON_UNESCAPED_UNICODE);
- }
- }
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('commission-basic-log', []);
- }
- /**
- * 处理
- * @Author: lun
- * @DateTime: 2022/5/14 0014 16:47
- * @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','is_height_commission'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!isset($params['status'])) unset($params['status']);
- if (!isset($params['is_height_commission'])) unset($params['is_height_commission']);
- $params['mall_id'] = $this->mall_id;
- $res = Distribution::setData($params);
- if (!$res) return $this->error('修改失败:msg=' . DoubleCopyLevel::getStaticError());
- return $this->success('修改成功');
- }
- }
- }
- /**
- * 处理
- * @Author: lun
- * @DateTime: 2022/5/14 0014 16:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionCommission()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id','height_commission'], 'required'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = Distribution::setData($params);
- if (!$res) return $this->error('设置失败:msg=' . Distribution::getStaticError());
- return $this->success('设置成功');
- }
- }
- }
- }
|