DefaultController.php 5.6 KB

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