LevelController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace addons\OperationCenter\backend\controllers;
  3. use Yii;
  4. use Exception;
  5. use yii\helpers\Json;
  6. use common\models\common\UpgradeCondition;
  7. use addons\OperationCenter\common\models\Level;
  8. use addons\OperationCenter\common\services\LevelService;
  9. use addons\OperationCenter\common\enums\OperationCenterEnum;
  10. /**
  11. * 等级控制器
  12. *
  13. * Class LevelController
  14. * @package addons\OperationCenter\backend\controllers
  15. */
  16. class LevelController extends BaseController
  17. {
  18. /**
  19. * @var boolean
  20. */
  21. public $enableCsrfValidation = false;
  22. /**
  23. * @var LevelService
  24. */
  25. protected $service;
  26. public function init()
  27. {
  28. parent::init();
  29. $this->service = new LevelService(['mall_id' => $this->mall_id]);
  30. }
  31. /**
  32. * 首页
  33. *
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. if ($this->request->isAjax) {
  39. $params = $this->request->get();
  40. return $this->success('ok', $this->service->getPageList($params));
  41. }
  42. return $this->render($this->action->id);
  43. }
  44. /**
  45. * 升级条件
  46. *
  47. * @return string
  48. */
  49. public function actionCondition()
  50. {
  51. if (Yii::$app->request->isAjax) {
  52. try {
  53. if (Yii::$app->request->isGet) {
  54. $data['upgrade_condition'] = UpgradeCondition::getData(UpgradeCondition::$operation_center_condition_ids);
  55. $agentUpgradeCondition = Yii::$app->services->setting->addons->get($this->mall_id, OperationCenterEnum::OPERATION_CENTER, 'upgrade_condition');
  56. $data['checked_condition_keys'] = !empty($agentUpgradeCondition['level_upgrade_condition']) ? json_decode($agentUpgradeCondition['level_upgrade_condition'], true) : [];
  57. return $this->success('操作成功', $data);
  58. } else {
  59. $postData = Yii::$app->request->post();
  60. $res = Yii::$app->services->validate->validate($postData, [
  61. [['checked_condition_keys'], 'required'],
  62. ]);
  63. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  64. $data['upgrade_condition'] = ['level_upgrade_condition' => json_encode($postData['checked_condition_keys'])];
  65. Yii::$app->services->setting->addons->set($this->mall_id, OperationCenterEnum::OPERATION_CENTER, $data);
  66. UpgradeCondition::clear($postData['checked_condition_keys'], Level::class, $this->mall_id);
  67. return $this->success('操作成功');
  68. }
  69. } catch (Exception $e) {
  70. return $this->error($e->getMessage());
  71. }
  72. }
  73. return $this->render($this->action->id);
  74. }
  75. /**
  76. * 编辑等级
  77. *
  78. * @return string
  79. */
  80. public function actionEdit()
  81. {
  82. if ($this->request->isAjax) {
  83. // ID
  84. $id = $this->request->get('id');
  85. if ($this->request->isGet) {
  86. return $this->success('ok', $this->service->getLevelData($id));
  87. }
  88. if ($this->request->isPost) {
  89. $data = $this->request->post();
  90. unset($data['_csrf']);
  91. // $validate = Yii::$app->services->validate->validate($data, [
  92. // [['level', 'name'], 'required'],
  93. // [[
  94. // 'is_auto_upgrade', 'checked_condition_values',
  95. // 'checked_condition_keys', 'condition_type', 'upgrade_type_goods',
  96. // 'goods_list', 'upgrade_type_condition', 'goods_ids', 'goods_type',
  97. // 'buy_goods_type', 'explain', 'status'
  98. // ], 'safe']
  99. // ]);
  100. // if ($validate === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  101. return $this->service->setLevelData($id, $data)
  102. ? $this->success('ok')
  103. : $this->error($this->service->getError());
  104. }
  105. }
  106. // 获取等级升级的条件
  107. // $upgradeCondition = Yii::$app->services->setting->addons->get(
  108. // $this->mall_id, OperationCenterEnum::OPERATION_CENTER, 'upgrade_condition'
  109. // );
  110. // $list = [];
  111. // if(!empty($upgradeCondition['level_upgrade_condition'])) {
  112. // $ids = Json::decode($upgradeCondition['level_upgrade_condition'], true);
  113. // $list = UpgradeCondition::getData($ids);
  114. // }
  115. // $upgrade_condition_data = UpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
  116. return $this->render($this->action->id/* , $upgrade_condition_data */);
  117. }
  118. /**
  119. * 等级列表
  120. *
  121. * @return string
  122. */
  123. public function actionList()
  124. {
  125. return $this->success('ok', $this->service->getLevelList());
  126. }
  127. /**
  128. * 切换状态
  129. *
  130. * @return string
  131. */
  132. public function actionSwitchStatus()
  133. {
  134. $id = $this->request->get('id');
  135. $status = $this->request->get('status');
  136. return $this->service->switchStatus($id, $status)
  137. ? $this->success('ok')
  138. : $this->error($this->service->getError());
  139. }
  140. /**
  141. * 删除等级
  142. *
  143. * @return string
  144. */
  145. public function actionDelLevel()
  146. {
  147. $id = $this->request->get('id');
  148. return $this->service->delLevel($id)
  149. ? $this->success('ok')
  150. : $this->error($this->service->getError());
  151. }
  152. }