MyCoupon.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\model\coupon\CouponGenRedeemCodes;
  14. use app\api\model\coupon\CouponRedeemCodes as CouponRedeemCodesModel;
  15. use app\api\model\UserCoupon as UserCouponModel;
  16. use app\api\service\User as UserService;
  17. use app\common\exception\BaseException;
  18. use think\cache\driver\Redis;
  19. /**
  20. * 用户优惠券
  21. * Class Coupon
  22. * @package app\api\controller
  23. */
  24. class MyCoupon extends Controller
  25. {
  26. /**
  27. * 用户优惠券列表
  28. * @return mixed
  29. * @throws BaseException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function list()
  35. {
  36. $userId = UserService::getCurrentLoginUserId();
  37. $model = new UserCouponModel;
  38. $list = $model->getList($userId, $this->request->param());
  39. return $this->renderSuccess(compact('list'));
  40. }
  41. /**
  42. * 领取优惠券
  43. * @param int $couponId
  44. * @return array|\think\response\Json
  45. * @throws BaseException
  46. */
  47. public function receive(int $coupon_id)
  48. {
  49. $model = new UserCouponModel;
  50. if ($model->receive($coupon_id)) {
  51. return $this->renderSuccess([], '领取成功');
  52. }
  53. return $this->renderError($model->getError() ?: '领取失败');
  54. }
  55. /**
  56. * 兑换优惠券
  57. * @param string $redeemCode
  58. * @return array|\think\response\Json
  59. * @throws BaseException
  60. */
  61. public function exchange(string $redeemCode)
  62. {
  63. $CouponRedeemCodesModel = new CouponRedeemCodesModel;
  64. $genModel = new CouponGenRedeemCodes();
  65. $CouponRedeemCodes = $CouponRedeemCodesModel->with(['couponGenRedeemCodes'])->where('redeem_code',$redeemCode)->find();
  66. if(!$CouponRedeemCodes){
  67. return $this->renderError('错误,请重输入');
  68. }
  69. if ($CouponRedeemCodes['couponGenRedeemCodes']['amount'] - $CouponRedeemCodes['couponGenRedeemCodes']['used_amount']<=0){
  70. $CouponRedeemCodesModel->where('id', $CouponRedeemCodes->id)->update(['is_delete'=>1]);
  71. if($CouponRedeemCodes['couponGenRedeemCodes']['give_type'] == 1){
  72. return $this->renderError('领完啦');
  73. }else{
  74. return $this->renderError('兑换码已失效');
  75. }
  76. }
  77. $model = new UserCouponModel;
  78. if ($model->receive($CouponRedeemCodes['coupon_id'],true)) {
  79. if($CouponRedeemCodes['couponGenRedeemCodes']['give_type'] == 2){
  80. //更改激活码使用信息
  81. $CouponRedeemCodes->is_delete = 1;
  82. $CouponRedeemCodes->save();
  83. }
  84. //使用数量加一
  85. $genModel->where('id', $CouponRedeemCodes->coupon_gen_redeem_codes_id)
  86. ->where('used_amount','<',$CouponRedeemCodes['couponGenRedeemCodes']['amount'])
  87. ->inc('used_amount')
  88. ->update();
  89. return $this->renderSuccess([], '兑换成功');
  90. }
  91. return $this->renderError($model->getError() ?: '兑换失败');
  92. }
  93. }