| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace addons\Store\backend\controllers;
- use addons\Store\common\services\StoreCommunityService;
- use addons\Store\common\models\StoreCommunity as StoreCommunityModel;
- use common\models\common\Region;
- use addons\Store\common\enums\StoreEnum;
- use common\models\common\AddonsSetting;
- class StoreCommunityController extends BaseController
- {
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store-community/index');
- }
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $model = new StoreCommunityService();
- $params['mall_id'] = $this->mall_id;
- $community_data = $model->getList($params);
- return $this->success('success', $community_data);
- }
- }
- public function actionAdd()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store-community/add');
- }
- if (\Yii::$app->request->isGet) {
- $regions = Region::find()
- ->select('id,name')
- ->where(['level' => 1])
- ->asArray()
- ->indexBy('id')
- ->all();
- return $this->success('success', ['provinces' => $regions]);
- }
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $model = new StoreCommunityService();
- if(!$model->addCommunity($params)){
- return $this->error('保存失败');
- }
- return $this->success('添加成功');
- }
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post()['form'];
- if(empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id']) || empty($params['street_id']) || empty($params['community_id']) || empty($params['community_name'])) {
- return $this->error('参数错误');
- }
- $communityExist = StoreCommunityModel::find()
- ->where(['province_id'=>$params['province_id'],'city_id'=>$params['city_id'],'district_id'=>$params['district_id'],'street_id'=>$params['street_id'],'community_name'=>$params['community_name']])
- ->asArray()->one();
- if(!empty($communityExist) && $communityExist['id'] != $params['community_id']) {
- return $this->error('抱歉,该街道已经存在该社区了');
- }
- $model = StoreCommunityModel::findOne($params['community_id']);
- $model->community_name = $params['community_name'];
- if(!$model->save()) {
- return $this->error('修改失败');
- }
- }
- return $this->success('修改成功');
- }
- public function actionDelete()
- {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- if(empty($params['community_id'])) {
- return $this->error('参数错误');
- }
- $model = StoreCommunityModel::findOne($params['community_id']);
- $model->status = 1;
- if(!$model->save()) {
- return $this->error('删除失败');
- }
- }
- return $this->success('删除成功');
- }
- public function actionSetting()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store-community/setting');
- }
- if(\Yii::$app->request->isGet){
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
- $mall_info['community_status'] = $setting['community_status']??0;
- return $this->success('success', ['mall_info' => $mall_info]);
- }
- if(\Yii::$app->request->isPost)
- {
- $params = \Yii::$app->request->post();
- if(!isset($params['community_status'])){
- return $this->error('参数错误');
- }
- $params['community_status'] = $params['community_status'] == 'true' ? 1 : 0;
- try {
- //更新缓存
- $setting['community_status'] = $params['community_status'];
- $res = \Yii::$app->services->setting->addons->set($this->mall_id, StoreEnum::ADDONS_NAME, ['community_set'=>$setting]);
- if(!$res){
- return $this->error('保存失败');
- }
- }catch (\Exception $e){
- return $this->error('保存失败: ' . $e->getMessage());
- }
- return $this->success('保存成功');
- }
- }
- }
|