| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\UserCoupon;
- use app\modules\client\behaviors\Auth;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\CouponForm;
- use app\utils\AutoSendCoupon;
- use yii\base\BaseObject;
- class CouponController extends BaseController
- {
- public function behaviors()
- {
- return parent::behaviors();
- }
- /**
- * 我的优惠券列表
- * @return \yii\web\Response
- */
- public function actionIndex()
- {
- $form = new CouponForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- return $this->asJson($form->search());
- }
- /**
- * 我的优惠券列表
- * @return \yii\web\Response
- */
- public function actionSaasIndex()
- {
- $form = new CouponForm();
- $form->attributes = get_params();
- $form->store_id = $this->store_id;
- $form->user_id = get_user_id();
- return $this->asJson($form->saasSearch());
- }
- /**
- * 分享页面送优惠券
- * @return \yii\web\Response
- */
- public function actionShareSend()
- {
- $form = new CouponForm();
- $form->store_id = $this->store_id;
- $form->user_id = get_user_id();
- return $this->asJson($form->saveCoupon());
- }
- /**
- * 领取优惠券
- * @return \yii\web\Response
- */
- public function actionReceive()
- {
- $form = new CouponForm();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- $form->id = get_params('id');
- $userK = [$form->id, get_user_id()];
- if(cache_lock($userK, 30)){
- debug_log('上个请求正在处理。。。', __CLASS__ . '.log');
- return $this->asJson([
- 'code' => 1,
- 'msg' => '上个请求正在处理。。。'
- ]);
- }
- $sendCoupon = $form->sendCoupon();
- cache_lock_del($userK);
- return $this->asJson($sendCoupon);
- }
- public function actionSubStock(){
- return $this->asJson(Coupon::subStock(input_params('id')));
- }
- /**
- * saas领取优惠券
- * @return \yii\web\Response
- */
- public function actionSaasReceive()
- {
- $form = new CouponForm();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- $form->id = get_params('id');
- return $this->asJson($form->sendSaasCoupon());
- }
- /**
- * saas分享领取优惠券
- * @return \yii\web\Response
- */
- public function actionSaasShareReceive()
- {
- $user_id = intval(get_params('user_id'));
- if ($user_id <= 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- $coupon_id = intval(get_params('coupon_id'));
- $res = UserCoupon::find()->where(['coupon_id' => $coupon_id, 'user_id' => $user_id, 'is_use' => 0, 'is_expire' => 0, 'is_delete' => 0])->one();
- if (!$res) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '优惠券不存在'
- ]);
- }
- $new_user_id = get_user_id();
- UserCoupon::updateAll(['user_id' => $new_user_id, 'type' => 4], ['user_id' => $user_id, 'coupon_id' => $coupon_id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '领取成功'
- ]);
- }
- /**
- * 优惠券详情
- * @return \yii\web\Response
- */
- public function actionDetail()
- {
- $form = new CouponForm();
- $form->store_id = $this->store_id;
- $form->user_id = get_user_id();
- $form->id = get_params('user_coupon_id');
- $form->coupon_id = get_params('coupon_id');
- return $this->asJson($form->detail());
- }
- /**
- * 优惠券详情
- * @return \yii\web\Response
- */
- public function actionSaasDetail()
- {
- $form = new CouponForm();
- $form->store_id = $this->store_id;
- $form->user_id = get_user_id();
- $form->id = get_params('user_conpon_id');
- $form->coupon_id = get_params('coupon_id');
- return $this->asJson($form->saasDetail());
- }
- /**
- * 分享获取优惠券
- */
- public function actionShare() {
- $user = get_user();
- \Yii::error([$user->id, UserCoupon::TYPE_AUTO, $user->store_id]);
- $res = AutoSendCoupon::send($user->id, CouponAutoSend::EVENT_SHARE, $user->store_id);
- return $this->asJson($res);
- }
- /**
- * 分享领取优惠券
- */
- public function actionShareReceive() {
- $user_id = get_user_id();
- $user_coupon_id = post_params('id');
- $user_coupon = UserCoupon::findOne(['id' => $user_coupon_id, 'is_delete' => 0, 'give_user_id' => 0]);
- if (!$user_coupon) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '优惠券已转赠或不存在'
- ]);
- }
- if ($user_id == $user_coupon->user_id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '不能领取自己的优惠券'
- ]);
- }
- $user_coupon->give_user_id = $user_coupon->user_id;
- $user_coupon->user_id = $user_id;
- if (!$user_coupon->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '领取失败'
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '领取成功'
- ]);
- }
- }
|