Coupon.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 优惠券
  7. */
  8. class Coupon extends Apic
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //优惠券管理
  13. public function lists(){
  14. $status = input('status',1);
  15. $where = [
  16. 'company_id' => $this->auth->id,
  17. 'type' => 1,
  18. ];
  19. if($status == 1){ //未开始
  20. $where['usetimestart'] = ['gt',time()];
  21. }elseif($status == 2){ //进行中
  22. $where['usetimestart'] = ['lt',time()];
  23. $where['usetimeend'] = ['gt',time()];
  24. }else{ //已结束
  25. $where['usetimeend'] = ['lt',time()];
  26. }
  27. $list = Db::name('coupons')->where($where)->order('id desc')->autopage()->select();
  28. $this->success(1,$list);
  29. }
  30. //新增
  31. public function add(){
  32. $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
  33. $data = request_post_hub($field);
  34. $data['company_id'] = $this->auth->id;
  35. $data['type'] = 1;
  36. $data['score'] = 0;
  37. $data['stock'] = $data['number'];
  38. $data['createtime'] = time();
  39. $data['updatetime'] = time();
  40. Db::name('coupons')->insertGetId($data);
  41. $this->success('添加成功');
  42. }
  43. public function info(){
  44. $id = input('id',0);
  45. $info = Db::name('coupons')->where('id',$id)->find();
  46. $this->success(1,$info);
  47. }
  48. //编辑
  49. public function edit(){
  50. $id = input('id','');
  51. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
  52. if(empty($check)){
  53. $this->error('不存在的优惠券');
  54. }
  55. if($check['usetimestart'] < time()){
  56. $this->error('优惠券已经进行中,不能修改');
  57. }
  58. //
  59. $field = ['enough','amount','number','limit','usetimestart','usetimeend'];
  60. $data = request_post_hub($field);
  61. $data['stock'] = $data['number'];
  62. $data['updatetime'] = time();
  63. Db::name('coupons')->where('id',$id)->update($data);
  64. $this->success('编辑成功');
  65. }
  66. //删除
  67. public function delete(){
  68. $id = input('id','');
  69. $check = Db::name('coupons')->where('id',$id)->where('company_id',$this->auth->id)->find();
  70. if(empty($check)){
  71. $this->error('不存在的优惠券');
  72. }
  73. if($check['usetimestart'] < time()){
  74. $this->error('优惠券已经进行中,不能删除');
  75. }
  76. Db::name('coupons')->where('id',$id)->delete();
  77. $this->success('删除成功');
  78. }
  79. }