Vip.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\PayOrderModel;
  5. use app\common\model\Wallet;
  6. use app\utils\CurlUtil;
  7. use think\Db;
  8. use addons\epay\library\Service;
  9. /**
  10. * 充值配置与充值订单
  11. */
  12. class Vip extends Api
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = ['*'];
  16. // VIP抵扣券
  17. public function coupon()
  18. {
  19. $params = $this->request->param();
  20. $user_id = $this->auth->id;
  21. $query = Db::name('vip_coupon_user')
  22. ->field(['id', 'coupon_no', 'type', 'name', 'info', 'end_time', 'use_frequency_day', 'use_frequency_times', 'status', 'create_time'])
  23. ->where('user_id', $user_id);
  24. if (!empty($params['type'])) {
  25. $query->where('type', $params['type']);
  26. }
  27. if (!empty($params['status'])) {
  28. $params['status'] == 1 && $query->where('status', $params['status'])->where('end_time','>', time());
  29. $params['status'] == 2 && $query->where('status', $params['status']);
  30. $params['status'] == 3 && $query->where('status', 1)->where('end_time','<=', time());
  31. }else{
  32. $query->whereIn('status', [1, 2]);
  33. }
  34. $list = $query->order('id desc')
  35. ->autopage()
  36. ->select();
  37. foreach ($list as $key => $val) {
  38. $list[$key]['end_time_text'] = date('Y-m-d H:i', $val['end_time']);
  39. $list[$key]['create_time_text'] = date('Y-m-d H:i', $val['create_time']);
  40. }
  41. $this->success('success', $list);
  42. }
  43. // 使用抵扣券
  44. public function coupon_use()
  45. {
  46. $user_id = $this->auth->id;
  47. $params = $this->request->param();
  48. if (empty($params['coupon_id'])) {
  49. $this->error('请选择要使用的抵扣券');
  50. }
  51. $info = Db::name('vip_coupon_user')
  52. ->where('id', $params['coupon_id'])
  53. ->where('user_id', $user_id)
  54. ->whereIn('status', [1, 2])
  55. ->find();
  56. if (!$info) {
  57. $this->error('不存在的券');
  58. }
  59. if ($info['status'] != 1) {
  60. $this->error('券已使用');
  61. }
  62. if (!Db::name('vip_coupon_user')->where(['id' => $params['coupon_id'], 'status' => 1])->update(['status' => 2, 'use_time' => time()])) {
  63. $this->error('操作失败');
  64. }
  65. $this->success('使用成功');
  66. }
  67. }