| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace addons\Community\backend\controllers;
- use addons\Community\common\enums\CommunityEnum;
- use addons\Community\common\models\CommunityGoods;
- use addons\Community\common\models\CommunityHead;
- use addons\Community\common\models\CommunityHeadApply;
- use addons\Community\common\service\SettingService;
- use common\enums\StatusEnum;
- use common\helpers\RegularHelper;
- use common\models\common\Region;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use yii\helpers\Json;
- /**
- * 默认控制器
- *
- * Class CommunityHeadController
- * @package addons\Community\backend\controllers
- */
- class CommunityHeadController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render($this->action->id);
- }
- if (\Yii::$app->request->isGet) {
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $name = \Yii::$app->request->get('name');
- $address = \Yii::$app->request->get('address');
- $user_id = \Yii::$app->request->get('user_id');
- $start_at = \Yii::$app->request->get('start_at');
- $end_at = \Yii::$app->request->get('end_at');
- $nickname = \Yii::$app->request->get('nickname');
- $mobile = \Yii::$app->request->get('mobile');
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $where[] = ['check_status' => CommunityEnum::CHECK_ADOPT];
- if (!empty($name)) $where[] = ['like', 'name', $name];
- if (!empty($address)) $where[] = ['like', 'address', $address];
- if (!empty($user_id)) $where[] = ['user_id' => $user_id];
- if (!empty($start_at)) $where[] = ['>=', 'created_at', strtotime($start_at)];
- if (!empty($end_at)) $where[] = ['<=', 'created_at', strtotime($end_at)];
- if (!empty($mobile)) $where[] = ['mobile' => $mobile];
- if (!empty($nickname)) {
- $n_where = [
- 'or', ['like', 'nickname', $nickname]
- ];
- $users = User::find()
- ->select('id')
- ->where($n_where)
- ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('id')
- ->all();
- $user_ids = array_column($users, 'id');
- $where[] = ['user_id' => $user_ids];
- }
- $params = [
- 'select' => 'id,mall_id,user_id,mobile,name,province_id,check_status,city_id,district_id,address,logo,idcard_img,license_img,status,created_at',
- 'where' => $where,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- 'with' => ['user' => function ($query) {
- return $query->select('avatar_url,id,nickname');
- }]
- ];
- $store_list = CommunityHead::listPage($params);
- if (false === $store_list) {
- return $this->error(CommunityHead::getStaticError());
- }
- if (!empty($store_list['list'])) {
- foreach ($store_list['list'] as &$item) {
- $item['idcard_img'] = Json::decode($item['idcard_img']);
- }
- }
- return $this->success('success', $store_list);
- }
- }
- public function actionDelete()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $param = \Yii::$app->request->post();
- $t = \Yii::$app->db->beginTransaction();
- try {
- $one = CommunityHead::findOne(['mall_id' => $this->mall_id, 'id' => $param['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (!$one) throw new \Exception('数据id错误');
- $one->status = StatusEnum::DELETE;
- if (!$one->save()) throw new \Exception($one->getErrorMessage());
- $apply = CommunityHeadApply::findOne(['mall_id' => $this->mall_id, 'user_id' => $one->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (!empty($apply)) {
- $apply->status = StatusEnum::DELETE;
- if (!$apply->save()) throw new \Exception($one->getErrorMessage());
- }
- // 删除商品
- CommunityGoods::find()->createCommand()->update(CommunityGoods::tableName(), [
- 'status' => StatusEnum::DELETE
- ], ['and', ['mall_id' => $this->mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['community_id' => $one->id]])->execute();
- $t->commit();
- return $this->success('删除成功');
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->error($e->getMessage());
- }
- }
- }
- }
- public function actionStorage()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render($this->action->id);
- }
- if (\Yii::$app->request->isGet) {
- $param = \Yii::$app->request->get();
- $setting_service = new SettingService();
- $area = $setting_service->getFieldSetting($this->mall_id, 'area', true);
- $area = $area ? json_decode($area, true) : $area;
- $regions = Region::find()
- ->select('id,name,lng,lat')
- ->where(['level' => 1])
- ->andWhere(['id' => $area])
- ->asArray()
- ->indexBy('id')
- ->all();
- $community_head = [];
- $id = $param['id'] ?? 0;
- $provinceInfo = [];
- $cityInfo = [];
- $districtInfo = [];
- $citys = [];
- $districts = [];
- if ($id > 0) {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['id' => $param['id']]
- ];
- $community_head = CommunityHead::getOne($where, true, ['user']);
- $city_regions = [
- 'where' => [['level' => 2], ['id' => $area], ['parent_id' => $community_head['province_id']]],
- 'select' => 'id,name,lng,lat',
- 'index' => 'id',
- ];
- $district_regions = [
- 'where' => [['level' => 3], ['id' => $area], ['parent_id' => $community_head['city_id']]],
- 'select' => 'id,name,lng,lat',
- 'index' => 'id',
- ];
- if (!empty($community_head)) {
- $community_head['idcard_img'] = Json::decode($community_head['idcard_img']);
- }
- if (!empty($community_head['setting'])) {
- $community_head['setting'] = json_decode($community_head['setting'], true);
- } else {
- $community_head['setting'] = [
- "is_enable_reward" => 0,
- "settlement_type" => 1,
- "calculate_type" => 0,
- "head_commission_type" => 0,
- "commission" => 0,
- ];
- }
- $citys = Region::lists($city_regions, true);
- $districts = Region::lists($district_regions, true);
- $provinceInfo = $regions[$community_head['province_id']] ?? [];
- $cityInfo = $citys[$community_head['city_id']] ?? [];
- $districtInfo = $districts[$community_head['district_id']] ?? [];
- }
- return $this->success('success', ['provinces' => $regions, 'citys' => $citys, 'districts' => $districts, 'community_head' => $community_head, 'provinceInfo' => $provinceInfo, 'cityInfo' => $cityInfo, 'districtInfo' => $districtInfo]);
- } else {
- $post = \Yii::$app->request->post();
- $rules = [
- [['user_id', 'mobile', 'contacts', 'name', 'logo', 'license_img', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude'], 'required'],
- [['user_id', 'mobile', 'province_id', 'city_id', 'district_id', 'id', 'is_home_delivery'], 'integer'],
- [['longitude', 'latitude'], 'number'],
- [['contacts', 'name', 'desc', 'logo', 'license_img', 'address'], 'string', 'max' => 255],
- ['mobile', function ($attribute, $params) {
- if (!RegularHelper::verify('mobile', $this->$attribute)) {
- $this->addError($attribute, '手机号格式错误');
- }
- }],
- ['desc', 'default', 'value' => ''],
- [['desc', 'idcard_img', 'setting'], 'safe']
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules, [
- 'province_id' => '省份',
- 'city_id' => '城市',
- 'district_id' => '地区',
- 'mobile' => '手机号码',
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if (!empty($post['setting'])) {
- $post['setting'] = json_encode($post['setting']);
- }
- $post['mall_id'] = $this->mall_id;
- $post['check_status'] = CommunityEnum::CHECK_ADOPT;
- $post['idcard_img'] = Json::encode($post['idcard_img']);
- $res = CommunityHead::setData($post, $this->admin->id);
- if (false === $res) {
- return $this->error(CommunityHead::getStaticError());
- }
- return $this->success('添加成功');
- }
- }
- /**
- * @Description: 根据地区id获取第一层子级地区信息列表
- * @Author: zhngsan
- * @DateTime 2023-02-08 11:22:11
- * @return mixed
- */
- public function actionGetRegions()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $region_id = \Yii::$app->request->get('region_id');
- if (empty($region_id)) {
- return $this->error('参数错误');
- }
- // 获取基础设置中包含的地区
- $setting_service = new SettingService();
- $area = $setting_service->getFieldSetting($this->mall_id, 'area', true);
- $area = $area ? json_decode($area, true) : $area;
- $region = Region::findOne($region_id);
- if (empty($region)) {
- return $this->error('地区信息未找到');
- }
- $region_list = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $region->id, 'id' => $area])
- ->asArray()
- ->all();
- return $this->success('success', $region_list);
- }
- /**
- * @Description: 更改状态
- * @Author: zhngsan
- * @DateTime 2023-02-08 15:43:13
- * @return mixed
- */
- public function actionSwitchStatus()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- try {
- $param = \Yii::$app->request->post();
- $one = CommunityHead::findOne(['mall_id' => $this->mall_id, 'id' => $param['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (!$one) throw new \Exception('数据id错误');
- $one->status = $param['status'];
- if (!$one->save()) throw new \Exception($one->getErrorMessage());
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- }
- /**
- * @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);
- $store_user_ids = CommunityHead::find()->select('user_id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- $wheres[] = ['not in', 'id', $store_user_ids];
- if (!empty($user_id)) {
- $wheres[] = ['id' => $user_id];
- }
- if (!empty($keyword)) {
- $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
- }
- $params = [
- 'select' => 'id,nickname,avatar_url,mobile,level,parent_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);
- }
- }
|