DefaultController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace addons\UpgradeGift\backend\controllers;
  3. use addons\UpgradeGift\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\UpgradeGift\backend\controllers
  14. */
  15. class DefaultController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. public $addonsName = AddonsEnum::ADDONS_NAME_UPGRADEGIFT;
  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/24 0024 15:44
  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. // 只有会员升级才需要显示商品
  54. if ($params['gift_type'] !== GiftEnum::GIFT_TYPE_USER) {
  55. unset($data['setting_type_arr'][GiftEnum::SETTING_GOODS]);
  56. }
  57. $this->success('获取成功', $data);
  58. } else {
  59. $params = Yii::$app->request->post();
  60. // 检查提交的参数
  61. $res = Yii::$app->services->validate->validate($params,[
  62. [['gift_type','setting', 'status'], 'required'],
  63. [['gift_type'], 'string'],
  64. [['status'], 'integer'],
  65. ]);
  66. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  67. try {
  68. $res = Gift::giftEditByPost($this->mall_id, $this->addonsName, $params, (new GiftEnum()));
  69. if ($res == false) throw new Exception('保存失败');
  70. return $this->success('保存成功');
  71. } catch (\Exception $e) {
  72. return $this->error('保存失败:'. $e->getMessage());
  73. }
  74. }
  75. } else {
  76. // 判断优惠券插件是否安装
  77. $has_coupon = MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_COUPON);
  78. return $this->render($this->action->id,['has_coupon' => $has_coupon]);
  79. }
  80. }
  81. /**
  82. * 获取对应等级的详细内容
  83. * @Author: lun
  84. * @DateTime: 2021/12/16 0016 10:38
  85. * @Copyright: copyright (c) 2021 广东七件事集团
  86. * @return mixed|void
  87. */
  88. public function actionRuleDetail()
  89. {
  90. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  91. $params = Yii::$app->request->post();
  92. // 检查提交的参数
  93. $res = Yii::$app->services->validate->validate($params,[
  94. [['gift_type','level'], 'required'],
  95. [['gift_type'], 'string'],
  96. [['level'], 'integer'],
  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()), $params['level']);
  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. //
  126. // try {
  127. // $rule = UpgradeGiftRule::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true,[],'','id,gift_type,status');
  128. // if (empty($rule)) throw new \Exception("查无此规则");
  129. // if ($rule['status'] == $params['status']) throw new \Exception("规则状态已经为关闭状态");
  130. //
  131. // $rule_params = ['id' => $params['id'], 'gift_type' => $rule['gift_type'], 'status' => $params['status']];
  132. // $res = UpgradeGiftRule::setData($this->mall_id,$rule_params);
  133. // if ($res == false) throw new \Exception(UpgradeGiftRule::getStaticError());
  134. //
  135. // $this->success('修改成功');
  136. // } catch (\Exception $e) {
  137. // $this->error('修改失败:' . $e->getMessage());
  138. // }
  139. // }
  140. }
  141. }