SettingController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\Seed\backend\controllers;
  3. use addons\Seed\common\enums\SeedEnum;
  4. use addons\Seed\common\models\SeedStoreSetting;
  5. use addons\Store\common\models\Store;
  6. use common\enums\StatusEnum;
  7. use common\models\mall\MallAddons;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * Class SettingController
  12. * @package addons\Seed\backend\controllers
  13. */
  14. class SettingController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * @return mixed|string|void
  19. */
  20. public function actionIndex()
  21. {
  22. if (Yii::$app->request->isAjax) {
  23. if(Yii::$app->request->isGet){
  24. $config = Yii::$app->services->setting->addons->get($this->mall_id, SeedEnum::ADDONS_NAME, 'basic');
  25. // 店铺列表获取
  26. $stores = [];
  27. if ($config['no_partake_stores'] && MallAddons::isInstall($this->mall_id, 'Store')) {
  28. $stores = Store::find()->select('id,store_name,logo_img')->where(['id' => Json::decode($config['no_partake_stores'], true)])->asArray()->all();
  29. }
  30. return $this->success('操作成功', compact('config', 'stores'));
  31. }
  32. if(Yii::$app->request->isPost){
  33. $post = Yii::$app->request->post('basic');
  34. $rules = [
  35. [['seed_status','collect_type'], 'integer'],
  36. [['multiple', 'pool_percent', 'ad_percent','basic_unit_price', 'release_cycle'], 'number'],
  37. [['quota_setting', 'no_partake_stores'], 'safe']
  38. ];
  39. $res = Yii::$app->services->validate->validate($post, $rules);
  40. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  41. if (is_array($post['quota_setting'])) $post['quota_setting'] = Json::encode($post['quota_setting']);
  42. if (is_array($post['no_partake_stores'])) $post['no_partake_stores'] = Json::encode($post['no_partake_stores']);
  43. Yii::$app->services->setting->addons->set($this->mall_id, SeedEnum::ADDONS_NAME, ['basic' => $post]);
  44. return $this->success('操作成功');
  45. }
  46. }
  47. return $this->render('index');
  48. }
  49. /**
  50. * 店铺设置
  51. * @Author: lun
  52. * @DateTime: 2022/8/23 0023 17:17
  53. * @Copyright: copyright (c) 2021 广东七件事集团
  54. * @return mixed|string|void
  55. */
  56. public function actionStore()
  57. {
  58. $retuest = Yii::$app->request;
  59. if ($retuest->isAjax) {
  60. if ($retuest->isGet) {
  61. $get = Yii::$app->request->get();
  62. // 检查提交的参数
  63. $res = Yii::$app->services->validate->validate($get,[
  64. [['store_id','page','limit'], 'integer'],
  65. ]);
  66. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  67. // 获取店铺统一设置
  68. $config = Yii::$app->services->setting->addons->get($this->mall_id, SeedEnum::ADDONS_NAME, 'store');
  69. $where = $list = [];
  70. $where[] = ['=', 'mall_id', $this->mall_id];
  71. !empty($get['store_id']) && $where[] = ['=', 'store_id', $get['store_id']];
  72. $where[] = ['=', 'status', StatusEnum::ENABLED];
  73. // 参数
  74. $params = array('where' => $where, 'order' => 'created_at desc', 'with' => ['store'], 'limit' => $get['limit']);
  75. $list = SeedStoreSetting::listPage($params, false, true);
  76. return $this->success('获取成功', compact('list','config'));
  77. } else {
  78. // 校验
  79. $post = Yii::$app->request->post();
  80. $rules = [
  81. [['type','store_id','status'], 'integer'],// 类型[1=统一设置,2=店铺独立设置]
  82. [['percent'], 'number'],
  83. ];
  84. $res = Yii::$app->services->validate->validate($post, $rules);
  85. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  86. $post = SeedStoreSetting::exceptNullVal($post);// 去除空值
  87. // 存入设置
  88. if ($post['type'] == 1) {
  89. Yii::$app->services->setting->addons->set($this->mall_id, SeedEnum::ADDONS_NAME, ['store' => ['percent' => $post['percent']]]);
  90. } else {
  91. $post['mall_id'] = $this->mall_id;
  92. $res = SeedStoreSetting::setData($post);
  93. if ($res === false) return $this->error('操作失败:'.SeedStoreSetting::getStaticError());
  94. }
  95. return $this->success('操作成功');
  96. }
  97. } else {
  98. return $this->render($this->action->id);
  99. }
  100. }
  101. }