CouponController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\models\Coupon;
  9. use app\models\CouponAutoSend;
  10. use app\models\UserCoupon;
  11. use app\modules\client\behaviors\Auth;
  12. use app\modules\client\controllers\BaseController;
  13. use app\modules\client\models\v1\CouponForm;
  14. use app\utils\AutoSendCoupon;
  15. use yii\base\BaseObject;
  16. class CouponController extends BaseController
  17. {
  18. public function behaviors()
  19. {
  20. return parent::behaviors();
  21. }
  22. /**
  23. * 我的优惠券列表
  24. * @return \yii\web\Response
  25. */
  26. public function actionIndex()
  27. {
  28. $form = new CouponForm();
  29. $form->attributes = get_params();
  30. $form->store_id = get_store_id();
  31. $form->user_id = get_user_id();
  32. return $this->asJson($form->search());
  33. }
  34. /**
  35. * 我的优惠券列表
  36. * @return \yii\web\Response
  37. */
  38. public function actionSaasIndex()
  39. {
  40. $form = new CouponForm();
  41. $form->attributes = get_params();
  42. $form->store_id = $this->store_id;
  43. $form->user_id = get_user_id();
  44. return $this->asJson($form->saasSearch());
  45. }
  46. /**
  47. * 分享页面送优惠券
  48. * @return \yii\web\Response
  49. */
  50. public function actionShareSend()
  51. {
  52. $form = new CouponForm();
  53. $form->store_id = $this->store_id;
  54. $form->user_id = get_user_id();
  55. return $this->asJson($form->saveCoupon());
  56. }
  57. /**
  58. * 领取优惠券
  59. * @return \yii\web\Response
  60. */
  61. public function actionReceive()
  62. {
  63. $form = new CouponForm();
  64. $form->store_id = get_store_id();
  65. $form->user_id = get_user_id();
  66. $form->id = get_params('id');
  67. $userK = [$form->id, get_user_id()];
  68. if(cache_lock($userK, 30)){
  69. debug_log('上个请求正在处理。。。', __CLASS__ . '.log');
  70. return $this->asJson([
  71. 'code' => 1,
  72. 'msg' => '上个请求正在处理。。。'
  73. ]);
  74. }
  75. $sendCoupon = $form->sendCoupon();
  76. cache_lock_del($userK);
  77. return $this->asJson($sendCoupon);
  78. }
  79. public function actionSubStock(){
  80. return $this->asJson(Coupon::subStock(input_params('id')));
  81. }
  82. /**
  83. * saas领取优惠券
  84. * @return \yii\web\Response
  85. */
  86. public function actionSaasReceive()
  87. {
  88. $form = new CouponForm();
  89. $form->store_id = get_store_id();
  90. $form->user_id = get_user_id();
  91. $form->id = get_params('id');
  92. return $this->asJson($form->sendSaasCoupon());
  93. }
  94. /**
  95. * saas分享领取优惠券
  96. * @return \yii\web\Response
  97. */
  98. public function actionSaasShareReceive()
  99. {
  100. $user_id = intval(get_params('user_id'));
  101. if ($user_id <= 0) {
  102. return $this->asJson([
  103. 'code' => 1,
  104. 'msg' => '参数有误'
  105. ]);
  106. }
  107. $coupon_id = intval(get_params('coupon_id'));
  108. $res = UserCoupon::find()->where(['coupon_id' => $coupon_id, 'user_id' => $user_id, 'is_use' => 0, 'is_expire' => 0, 'is_delete' => 0])->one();
  109. if (!$res) {
  110. return $this->asJson([
  111. 'code' => 1,
  112. 'msg' => '优惠券不存在'
  113. ]);
  114. }
  115. $new_user_id = get_user_id();
  116. UserCoupon::updateAll(['user_id' => $new_user_id, 'type' => 4], ['user_id' => $user_id, 'coupon_id' => $coupon_id]);
  117. return $this->asJson([
  118. 'code' => 0,
  119. 'msg' => '领取成功'
  120. ]);
  121. }
  122. /**
  123. * 优惠券详情
  124. * @return \yii\web\Response
  125. */
  126. public function actionDetail()
  127. {
  128. $form = new CouponForm();
  129. $form->store_id = $this->store_id;
  130. $form->user_id = get_user_id();
  131. $form->id = get_params('user_coupon_id');
  132. $form->coupon_id = get_params('coupon_id');
  133. return $this->asJson($form->detail());
  134. }
  135. /**
  136. * 优惠券详情
  137. * @return \yii\web\Response
  138. */
  139. public function actionSaasDetail()
  140. {
  141. $form = new CouponForm();
  142. $form->store_id = $this->store_id;
  143. $form->user_id = get_user_id();
  144. $form->id = get_params('user_conpon_id');
  145. $form->coupon_id = get_params('coupon_id');
  146. return $this->asJson($form->saasDetail());
  147. }
  148. /**
  149. * 分享获取优惠券
  150. */
  151. public function actionShare() {
  152. $user = get_user();
  153. \Yii::error([$user->id, UserCoupon::TYPE_AUTO, $user->store_id]);
  154. $res = AutoSendCoupon::send($user->id, CouponAutoSend::EVENT_SHARE, $user->store_id);
  155. return $this->asJson($res);
  156. }
  157. /**
  158. * 分享领取优惠券
  159. */
  160. public function actionShareReceive() {
  161. $user_id = get_user_id();
  162. $user_coupon_id = post_params('id');
  163. $user_coupon = UserCoupon::findOne(['id' => $user_coupon_id, 'is_delete' => 0, 'give_user_id' => 0]);
  164. if (!$user_coupon) {
  165. return $this->asJson([
  166. 'code' => 1,
  167. 'msg' => '优惠券已转赠或不存在'
  168. ]);
  169. }
  170. if ($user_id == $user_coupon->user_id) {
  171. return $this->asJson([
  172. 'code' => 1,
  173. 'msg' => '不能领取自己的优惠券'
  174. ]);
  175. }
  176. $user_coupon->give_user_id = $user_coupon->user_id;
  177. $user_coupon->user_id = $user_id;
  178. if (!$user_coupon->save()) {
  179. return $this->asJson([
  180. 'code' => 1,
  181. 'msg' => '领取失败'
  182. ]);
  183. }
  184. return $this->asJson([
  185. 'code' => 0,
  186. 'msg' => '领取成功'
  187. ]);
  188. }
  189. }