Coupon.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. /**
  5. * 首页接口
  6. */
  7. class Coupon extends Api
  8. {
  9. protected $noNeedLogin = [''];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 优惠券列表
  13. * @return void
  14. * @throws \think\Exception
  15. * @throws \think\db\exception\DataNotFoundException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. * @throws \think\exception\DbException
  18. */
  19. public function getList()
  20. {
  21. if (!$this->request->isPost()) {
  22. $this->error('请求方式异常');
  23. }
  24. $uid = $this->auth->id;
  25. $post = input('post.','','trim,strip_tags');
  26. if(isset($post['shop_id']) && $post['shop_id'] != '')
  27. {
  28. $where['shop_id'] = $post['shop_id'];
  29. }
  30. $where['is_check'] = 1;
  31. $where['type'] = ['<>',4];
  32. $where['starttime'] = ['<=',time()];
  33. $where['endtime'] = ['>=',time()];
  34. $list = model('app\api\model\service\Coupon')
  35. ->where($where)
  36. ->field('id,type,goods_id,shop_id,achieve,reduce,effective_day,starttime,endtime')
  37. ->order('id desc')
  38. ->select();
  39. foreach ($list as $key=>$value)
  40. {
  41. $exist = \app\api\model\service\UserCoupon::where(['user_id'=>$uid,'coupon_id'=>$value['id']])->field('id,state,exittime')->find();
  42. if($exist && $exist['exittime'] <=time())
  43. {
  44. unset($list[$key]);
  45. }
  46. $list[$key]['userState'] = $exist?1:0;
  47. $list[$key]['goodsName'] = $value['goods_id']?\app\api\model\service\Goods::where(['id'=>$value['goods_id']])->value('name'):'';
  48. $list[$key]['shopName'] = $value['shop_id']?\app\api\model\service\Shop::getName($value['shop_id']):'';
  49. }
  50. $list = array_values($list);
  51. $this->success('返回成功', $list);
  52. }
  53. /**
  54. * 领取优惠券
  55. * @return void
  56. * @throws \think\Exception
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @throws \think\exception\DbException
  60. */
  61. public function receive()
  62. {
  63. if (!$this->request->isPost()) {
  64. $this->error('请求方式异常');
  65. }
  66. $uid = $this->auth->id;
  67. $type = input('type/d','');
  68. if($type == 0){
  69. $coupon_id = input('coupon_id/d','');
  70. $where['id'] = $coupon_id;
  71. }elseif ($type == 1){
  72. $code = input('code','');
  73. $where['code'] = $code;
  74. }
  75. $where['starttime'] = ['<',time()];
  76. $where['endtime'] = ['>',time()];
  77. $couponInfo = model('app\api\model\service\Coupon')
  78. ->where($where)
  79. ->find();
  80. !$couponInfo && $this->error('优惠券信息异常');
  81. $receiveExist = model('app\api\model\service\UserCoupon')->where(['coupon_id'=>$couponInfo['id'],'user_id'=>$uid])->count();
  82. $receiveExist > 0 && $this->error('请勿重复领取');
  83. \app\api\model\service\UserCoupon::create(['user_id'=>$uid,'coupon_id'=>$couponInfo['id'],'type'=>$couponInfo['type'],'goods_id'=>$couponInfo['goods_id'],'shop_id'=>$couponInfo['shop_id'],'code'=>$couponInfo['code'],'achieve'=>$couponInfo['achieve'],'reduce'=>$couponInfo['reduce'],'exittime'=>86400*$couponInfo['effective_day']+time(),'state'=>0]);
  84. if($type == 1)
  85. {
  86. \app\api\model\service\Coupon::where('id',$couponInfo['id'])->update(['endtime'=>time()]);
  87. }
  88. $this->success('领取成功');
  89. }
  90. /**
  91. * 用户优惠券列表
  92. * @return void
  93. */
  94. public function userCoupon()
  95. {
  96. if (!$this->request->isPost()) {
  97. $this->error('请求方式异常');
  98. }
  99. $uid = $this->auth->id;
  100. $post = input('post.','','trim,strip_tags');
  101. $post['user_id'] = $uid;
  102. $list = \app\api\model\service\UserCoupon::getList($post);
  103. $this->success('信息返回成功',$list);
  104. }
  105. }