| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- namespace addons\Happiness\backend\controllers;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\models\HappinessAgent;
- use addons\Happiness\common\models\HappinessBoss;
- use addons\Happiness\common\models\HappinessBusiness;
- use common\enums\StatusEnum;
- use common\models\common\Region;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use Yii;
- /**
- * 默认控制器
- *
- * Class StoreAgentController
- * @package addons\Happiness\backend\controllers
- */
- class StoreAgentController extends BaseController
- {
- /**
- * 首页
- *
- * @return string
- * @throws \yii\web\NotFoundHttpException
- */
- public function actionIndex()
- {
- $request = \Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $get = \Yii::$app->request->get();
- // 检查提交的参数
- $res = \Yii::$app->services->validate->validate($get, [
- [['level'], 'integer'],
- [['user_keyword'], 'string'],
- [['parent_keyword'], 'string'],
- [['region'], 'string'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- if(!empty($get['user_keyword']))
- {
- $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
- 'or',
- ['=', 'id', $get['user_keyword']],
- ['like', 'nickname', $get['user_keyword'] . '%', false],
- ['like', 'mobile', $get['user_keyword'] . '%', false]
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- $where[]=['in', 'user_id', $user_ids];
- }
- if(!empty($get['parent_keyword']))
- {
- $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
- 'or',
- ['=', 'id', $get['parent_keyword']],
- ['like', 'nickname', $get['parent_keyword'] . '%', false],
- ['like', 'mobile', $get['parent_keyword'] . '%', false]
- ]], 'select' => 'id']);
- $parent_ids = array_column($user_list, 'id');
- $where[]=['in', 'parent_id', $parent_ids];
- }
- if(!empty($get['region']))
- {
- $where[]=['like', 'region_name', $get['region']];
- }
- $order = 'created_at desc';
- $select = '*';
- $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
- $params['with'] = ['user' => function ($query) {
- $query->select('id, nickname, mobile,avatar_url');
- }, 'parent' => function ($query) {
- $query->select('id, nickname, mobile,avatar_url');
- }];
- $list = HappinessAgent::listPage($params, false, true);
- if($list==false)
- {
- return $this->error(HappinessAgent::getStaticError(), []);
- }
- $config= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
- $agent_list=!empty($config['agent_list'])?json_decode($config['agent_list'],true):[];
- if(!empty($list['list']))
- {
- if(!empty($agent_list))
- {
- $agent_list=array_column($agent_list,null,'level');
- }
- foreach ($list['list'] as &$item)
- {
- $item['level_name']=isset($agent_list[$item['level']])?$agent_list[$item['level']]['name']:'';
- }
- }
- $list['agent_list'] = $agent_list;
- $list['regions'] = Region::find()
- ->select('id,name,lng,lat')
- ->where(['level' => 1])
- ->asArray()
- ->indexBy('id')
- ->all();
- $list['agent_login_url']= \Yii::$app->request->hostInfo . '?r=happiness/agent/auth/login&sign=' . $this->mall['mall_sign'];
- return $this->success('获取成功', $list);
- }
- if ($request->isPost) {
- $post = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($post, [
- [['level', 'user_id', 'province_id','role'], 'required'],
- [['level', 'user_id', 'parent_id', 'province_id', 'city_id', 'district_id', 'role'], 'integer'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $region_ids=[];
- if(!empty($post['province_id']))
- {
- $region_ids[]=$post['province_id'];
- }
- if(!empty($post['city_id']) && !empty($post['role']) && $post['role']>=HappinessEnum::AGENT_CITY)
- {
- $region_ids[]=$post['city_id'];
- }
- if(!empty($post['district_id']) && !empty($post['role']) && $post['role']==HappinessEnum::AGENT_DISTRICT)
- {
- $region_ids[]=$post['district_id'];
- }
- if(!$post['city_id'])
- {
- unset($post['city_id']);
- }
- if(!$post['district_id'])
- {
- unset($post['district_id']);
- }
- if(empty($post['parent_id']))
- {
- $post['parent_id']=0;
- }
- $region_name=Region::find()->where(['id'=>$region_ids])->select('name')->asArray()->all();
- $post['region_name']=implode('',array_column($region_name,'name'));
- $post['mall_id'] = $this->mall_id;
- $res=HappinessAgent::setData($post);
- if($res===false)
- {
- return $this->error(HappinessAgent::getStaticError());
- }
- return $this->success('操作成功');
- }
- }
- return $this->render('index');
- }
- /**
- * @name:
- * @msg: 根据地区id获取第一层子级地区信息列表
- * @param {*}
- * @return {*}
- */
- public function actionGetRegions()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $region_id = \Yii::$app->request->get('region_id');
- $level = \Yii::$app->request->get('level');
- if (empty($region_id)) {
- return $this->error('参数错误');
- }
- $region = Region::findOne($region_id);
- if (empty($region) && $level <= 3) {
- return $this->error('地区信息未找到');
- }
- if(isset($level) && $level < 3){
- $region_list = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $region->id])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- return $this->success('success', $region_list);
- }
- /**
- * @name:
- * @msg: 获取会员列表
- * @param {*}
- * @return {*}
- */
- public function actionGetUsers()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $wheres[] = ['=','mall_id', $this->mall_id];
- $wheres[] = ['status' => StatusEnum::ENABLED];
- if (!empty($user_id)) {
- $wheres[] = ['in','id',$user_id];
- }
- if (!empty($keyword)) {
- $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
- }
- $params = [
- 'select' => 'id,nickname,avatar_url,mobile,level,parent_id,mall_id',
- 'where' => $wheres,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $user_list = User::listPage($params);
- if (empty($user_list['list'])) {
- if (false === $user_list['list']) {
- return $this->error(User::getStaticError());
- }
- } else {
- $levels = UserLevel::find()
- ->select('id,level,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('level')
- ->all();
- $parent_ids = array_column($user_list['list'], 'parent_id');
- $parent_users = User::find()
- ->select('id,nickname,avatar_url,mobile,level,parent_id')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($user_list['list'] as &$user) {
- if (empty($user['avatar_url'])) {
- $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- }
- $level = $levels[$user['level']] ?? [];
- $parent_user = $parent_users[$user['parent_id']] ?? [];
- $user['level_name'] = $level['name'] ?? '';
- $user['parent_nickname'] = $parent_user['nickname'] ?? '';
- }
- }
- return $this->success('success', $user_list);
- }
- /**
- * @name:
- * @msg: 获取会员列表
- * @param {*}
- * @return {*}
- */
- public function actionGetAgentUsers()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $wheres[] = ['=','o.mall_id', $this->mall_id];
- $wheres[] = ['o.status' => StatusEnum::ENABLED];
- if (!empty($user_id)) {
- $wheres[] = ['in','id',$user_id];
- }
- if (!empty($keyword)) {
- $wheres[] = ['or', ['o.like', 'o.nickname', $keyword], ['like', 'o.mobile', $keyword]];
- }
- $params = [
- 'select' => 'u.*',
- 'where' => $wheres,
- 'alias' => 'o',
- 'order' => 'o.created_at desc',
- 'page' => $page,
- 'join' =>[['INNER JOIN', User::tableName() . ' u', 'u.id = o.user_id']],
- 'limit' => $limit,
- ];
- $user_list = HappinessBusiness::listPage($params);
- if (empty($user_list['list'])) {
- if (false === $user_list['list']) {
- return $this->error(User::getStaticError());
- }
- } else {
- $levels = UserLevel::find()
- ->select('id,level,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('level')
- ->all();
- $parent_ids = array_column($user_list['list'], 'parent_id');
- $parent_users = User::find()
- ->select('id,nickname,avatar_url,mobile,level,parent_id')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($user_list['list'] as &$user) {
- if (empty($user['avatar_url'])) {
- $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- }
- $level = $levels[$user['level']] ?? [];
- $parent_user = $parent_users[$user['parent_id']] ?? [];
- $user['level_name'] = $level['name'] ?? '';
- $user['parent_nickname'] = $parent_user['nickname'] ?? '';
- }
- }
- return $this->success('success', $user_list);
- }
- 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 = HappinessAgent::setData($params);
- if ($res === false) return $this->error(HappinessAgent::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- 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 = HappinessAgent::setData($params);
- if ($res === false) return $this->error(HappinessAgent::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- }
- if (\Yii::$app->request->isGet) {
- $id = \Yii::$app->request->get('id');
- $agent = HappinessAgent::find() ->with(['user'=>function ($query){
- $query->select('id,nickname,mobile,avatar_url');
- },'parent'=>function ($query) {
- $query->select('id,nickname,mobile,avatar_url');
- }])->where(['id' => $id, 'mall_id' =>
- $this->mall_id])
- ->asArray()
- ->one();
- if (empty($agent)) {
- return $this->error('数据未找到');
- }
- $citys = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $agent['province_id']])
- ->asArray()
- ->indexBy('id')
- ->all();
- $districts = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $agent['city_id']])
- ->asArray()
- ->indexBy('id')
- ->all();
- $agent['citys']= $citys;
- $agent['districts']= $districts;
- return $this->success('success', $agent);
- }
- }
- }
- }
|