StoreCommunityController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace addons\Store\backend\controllers;
  3. use addons\Store\common\services\StoreCommunityService;
  4. use addons\Store\common\models\StoreCommunity as StoreCommunityModel;
  5. use common\models\common\Region;
  6. use addons\Store\common\enums\StoreEnum;
  7. use common\models\common\AddonsSetting;
  8. class StoreCommunityController extends BaseController
  9. {
  10. public function actionIndex()
  11. {
  12. if (!\Yii::$app->request->isAjax) {
  13. return $this->render('/store-community/index');
  14. }
  15. if (\Yii::$app->request->isGet) {
  16. $params = \Yii::$app->request->get();
  17. $model = new StoreCommunityService();
  18. $params['mall_id'] = $this->mall_id;
  19. $community_data = $model->getList($params);
  20. return $this->success('success', $community_data);
  21. }
  22. }
  23. public function actionAdd()
  24. {
  25. if (!\Yii::$app->request->isAjax) {
  26. return $this->render('/store-community/add');
  27. }
  28. if (\Yii::$app->request->isGet) {
  29. $regions = Region::find()
  30. ->select('id,name')
  31. ->where(['level' => 1])
  32. ->asArray()
  33. ->indexBy('id')
  34. ->all();
  35. return $this->success('success', ['provinces' => $regions]);
  36. }
  37. if (\Yii::$app->request->isPost) {
  38. $params = \Yii::$app->request->post();
  39. $params['mall_id'] = $this->mall_id;
  40. $model = new StoreCommunityService();
  41. if(!$model->addCommunity($params)){
  42. return $this->error('保存失败');
  43. }
  44. return $this->success('添加成功');
  45. }
  46. }
  47. public function actionEdit()
  48. {
  49. if (\Yii::$app->request->isPost) {
  50. $params = \Yii::$app->request->post()['form'];
  51. 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'])) {
  52. return $this->error('参数错误');
  53. }
  54. $communityExist = StoreCommunityModel::find()
  55. ->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']])
  56. ->asArray()->one();
  57. if(!empty($communityExist) && $communityExist['id'] != $params['community_id']) {
  58. return $this->error('抱歉,该街道已经存在该社区了');
  59. }
  60. $model = StoreCommunityModel::findOne($params['community_id']);
  61. $model->community_name = $params['community_name'];
  62. if(!$model->save()) {
  63. return $this->error('修改失败');
  64. }
  65. }
  66. return $this->success('修改成功');
  67. }
  68. public function actionDelete()
  69. {
  70. if (\Yii::$app->request->isPost) {
  71. $params = \Yii::$app->request->post();
  72. if(empty($params['community_id'])) {
  73. return $this->error('参数错误');
  74. }
  75. $model = StoreCommunityModel::findOne($params['community_id']);
  76. $model->status = 1;
  77. if(!$model->save()) {
  78. return $this->error('删除失败');
  79. }
  80. }
  81. return $this->success('删除成功');
  82. }
  83. public function actionSetting()
  84. {
  85. if (!\Yii::$app->request->isAjax) {
  86. return $this->render('/store-community/setting');
  87. }
  88. if(\Yii::$app->request->isGet){
  89. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
  90. $mall_info['community_status'] = $setting['community_status']??0;
  91. return $this->success('success', ['mall_info' => $mall_info]);
  92. }
  93. if(\Yii::$app->request->isPost)
  94. {
  95. $params = \Yii::$app->request->post();
  96. if(!isset($params['community_status'])){
  97. return $this->error('参数错误');
  98. }
  99. $params['community_status'] = $params['community_status'] == 'true' ? 1 : 0;
  100. try {
  101. //更新缓存
  102. $setting['community_status'] = $params['community_status'];
  103. $res = \Yii::$app->services->setting->addons->set($this->mall_id, StoreEnum::ADDONS_NAME, ['community_set'=>$setting]);
  104. if(!$res){
  105. return $this->error('保存失败');
  106. }
  107. }catch (\Exception $e){
  108. return $this->error('保存失败: ' . $e->getMessage());
  109. }
  110. return $this->success('保存成功');
  111. }
  112. }
  113. }