| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace addons\CircleDistribution\backend\controllers;
- use addons\CircleDistribution\common\models\Distribution;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\models\user\User;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\CircleDistribution\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']) && !empty($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];
- $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'],
- [['id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $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());
- 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();
- // $post['mall_id'] = $this->mall_id;
- // $post['upgrade_status'] = Distribution::UPGRADE_STATUS_MANUAL;
- // $res = Distribution::setData($post);
- // if ($res == false) throw new \Exception(Distribution::getStaticError());
- // return $this->success('修改成功');
- }
- } catch (\Exception $exception) {
- return $this->error($exception->getMessage() . $exception->getLine() . $exception->getFile());
- }
- }
- /**
- * @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());
- }
- }
- }
|