DefaultController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace addons\ConsumeGift\backend\controllers;
  3. use addons\ConsumeGift\common\enums\GiftEnum;
  4. use common\enums\AddonsEnum;
  5. use common\logic\common\Gift;
  6. use common\models\mall\MallAddons;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * 默认控制器
  11. *
  12. * Class DefaultController
  13. * @package addons\ConsumeGift\backend\controllers
  14. */
  15. class DefaultController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. public $addonsName = AddonsEnum::ADDONS_NAME_CONSUMEGIFT;
  19. /**
  20. * 规则管理
  21. * @return string
  22. */
  23. public function actionIndex()
  24. {
  25. if (Yii::$app->request->isAjax) {
  26. if (Yii::$app->request->isGet) {
  27. $data = Gift::getGiftList($this->mall_id, $this->addonsName, (new GiftEnum()));
  28. $this->success('获取成功', $data);
  29. }
  30. } else {
  31. return $this->render($this->action->id);
  32. }
  33. }
  34. /**
  35. * 新增或编辑
  36. * @Author: lun
  37. * @DateTime: 2021/12/16 0016 14:51
  38. * @Copyright: copyright (c) 2021 广东七件事集团
  39. * @return mixed|string|void
  40. */
  41. public function actionEdit()
  42. {
  43. if (Yii::$app->request->isAjax) {
  44. if (Yii::$app->request->isGet) {
  45. $params = Yii::$app->request->get();
  46. // 检查提交的参数
  47. $res = Yii::$app->services->validate->validate($params,[
  48. [['gift_type'], 'required'],
  49. [['gift_type'], 'string'],
  50. ]);
  51. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  52. $data = Gift::giftEditByGet($this->mall_id, $this->addonsName, $params['gift_type'], (new GiftEnum()));
  53. $this->success('获取成功', $data);
  54. } else {
  55. $params = Yii::$app->request->post();
  56. // 检查提交的参数
  57. $res = Yii::$app->services->validate->validate($params,[
  58. [['gift_type','setting', 'status'], 'required'],
  59. [['gift_type'], 'string'],
  60. [['status'], 'integer'],
  61. ]);
  62. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  63. try {
  64. $res = Gift::giftEditByPost($this->mall_id, $this->addonsName, $params, (new GiftEnum()));
  65. if ($res == false) throw new Exception('保存失败');
  66. return $this->success('保存成功');
  67. } catch (\Exception $e) {
  68. return $this->error('保存失败:'. $e->getMessage());
  69. }
  70. }
  71. } else {
  72. // 判断优惠券插件是否安装
  73. $has_coupon = MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_COUPON);
  74. return $this->render($this->action->id,['has_coupon' => $has_coupon]);
  75. }
  76. }
  77. /**
  78. * 获取对应等级的详细内容
  79. * @Author: lun
  80. * @DateTime: 2021/12/16 0016 10:38
  81. * @Copyright: copyright (c) 2021 广东七件事集团
  82. * @return mixed|void
  83. */
  84. public function actionRuleDetail()
  85. {
  86. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  87. $params = Yii::$app->request->post();
  88. // 检查提交的参数
  89. $res = Yii::$app->services->validate->validate($params,[
  90. [['gift_type'], 'required'],
  91. [['gift_type'], 'string'],
  92. ]);
  93. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  94. try {
  95. $list = Gift::getRuleDetail($this->mall_id, $this->addonsName, $params['gift_type'], (new GiftEnum()));
  96. $this->success('获取成功', $list);
  97. } catch (\Exception $e) {
  98. $this->error('获取失败:' . $e->getMessage());
  99. }
  100. }
  101. }
  102. /**
  103. * 数据处理-已废除
  104. * @Author: lun
  105. * @DateTime: 2021/12/16 0016 14:24
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @return mixed|void
  108. */
  109. public function actionHandle()
  110. {
  111. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  112. $params = Yii::$app->request->post();
  113. // 检查提交的参数
  114. $res = Yii::$app->services->validate->validate($params,[
  115. [['id', 'type'], 'required'],
  116. [['id', 'status'], 'integer'],
  117. [['type'], 'string'],
  118. ]);
  119. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  120. try {
  121. $rule = UserGiftRule::getOne([['id' => $params, 'mall_id' => $this->mall_id]], true,[],'','id,gift_type,status');
  122. if (empty($rule)) throw new \Exception("查无此规则");
  123. if ($rule['status'] == $params['status']) throw new \Exception("规则状态已经为关闭状态");
  124. $rule_params = ['id' => $params['id'], 'gift_type' => $rule['gift_type'], 'status' => $params['status']];
  125. $res = UserGiftRule::setData($this->mall_id,$rule_params);
  126. if ($res == false) throw new \Exception(UserGiftRule::getStaticError());
  127. $this->success('修改成功');
  128. } catch (\Exception $e) {
  129. $this->error('修改失败:' . $e->getMessage());
  130. }
  131. }
  132. }
  133. }