Code.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\api\controller\coupon;
  3. use app\api\controller\Api;
  4. /**
  5. * 券码
  6. */
  7. class Code extends Api
  8. {
  9. protected $CouponCode = null;
  10. protected function init()
  11. {
  12. parent::init();
  13. $this->CouponCode = model('coupon.Code');
  14. }
  15. public function load()
  16. {
  17. $coupon_no = input('param.coupon_no');
  18. if (empty($coupon_no)) {
  19. $this->output(1, '券号不能为空');
  20. }
  21. $Coupon = model('Coupon');
  22. $coupon = $Coupon->where(['no' => $coupon_no])->find();
  23. if (!$coupon) {
  24. $this->output(1, '未找到券');
  25. }
  26. $quantity = input('param.quantity');
  27. $quantity = intval($quantity);
  28. if ($quantity <= 0) {
  29. $this->output(1, '数量不能小于0');
  30. }
  31. $member_id = input('param.userId');
  32. $list = $this->CouponCode->where(['coupon_id' => $coupon['id'], 'state' => 0])->limit($quantity)->select();
  33. if (count($list) < $quantity) {
  34. $this->output(1, '券码不足');
  35. }
  36. $data = [];
  37. $time = $this->request->time();
  38. $domain = $this->request->domain();
  39. foreach ($list as $key => $value) {
  40. $data[$key]['id'] = $value['id'];
  41. $data[$key]['cname'] = $coupon['cname'];
  42. $data[$key]['type'] = $coupon['type']['name'];
  43. $data[$key]['no'] = $value['no'];
  44. $data[$key]['secret'] = $value['secret'];
  45. if (!empty($value['qrcode'])) {
  46. $value['qrcode'] = $domain . $value['qrcode'];
  47. }
  48. $data[$key]['qrcode'] = $value['qrcode'];
  49. if (!empty($value['barcode'])) {
  50. $value['barcode'] = $domain . $value['barcode'];
  51. }
  52. $data[$key]['barcode'] = $value['barcode'];
  53. $data[$key]['url'] = $value['url'];
  54. $data[$key]['state'] = $value['state'];
  55. $expire_time = $value['expire_time'];
  56. list($startdate, $enddate) = explode('~', $expire_time);
  57. $data[$key]['start_time'] = strtotime($startdate);
  58. $data[$key]['end_time'] = strtotime($enddate);
  59. $this->CouponCode->where(['id' => $value['id']])->update(['state' => 1, 'sale_time' => $time, 'member_id' => $member_id]);
  60. }
  61. $this->output(0, '加载成功', $data);
  62. }
  63. public function check()
  64. {
  65. $coupon_no = input('param.coupon_no');
  66. if (empty($coupon_no)) {
  67. $this->output(1, '券号不能为空');
  68. }
  69. $Coupon = model('Coupon');
  70. $coupon = $Coupon->where(['no' => $coupon_no])->find();
  71. if (!$coupon) {
  72. $this->output(1, '未找到券');
  73. }
  74. $quantity = input('param.quantity');
  75. $quantity = intval($quantity);
  76. if ($quantity <= 0) {
  77. $this->output(1, '数量不能小于0');
  78. }
  79. $list = $this->CouponCode->where(['coupon_id' => $coupon['id'], 'state' => 0])->limit($quantity)->select();
  80. if (count($list) < $quantity) {
  81. $this->output(1, '券码不足');
  82. }
  83. $this->output(0, '券码充足');
  84. }
  85. }