Active.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 活动
  8. */
  9. class Active extends Api
  10. {
  11. protected $noNeedLogin = ['lists'];
  12. protected $noNeedRight = ['*'];
  13. public function lists()
  14. {
  15. $where = ['is_show'=>1];
  16. $list = Db::name('active')->field('content',true)->where($where)->order('weigh desc')->autopage()->select();
  17. $list = list_domain_image($list,['images']);
  18. if(!empty($list)){
  19. foreach($list as $key => &$item){
  20. //第一个图
  21. $item['image'] = isset($item['images'][0]) ? $item['images'][0] : '';
  22. //状态
  23. $status_text = '进行中';
  24. $status = 2;
  25. if(time() < $item['activestarttime']){
  26. $status_text = '报名中';
  27. $status = 1;
  28. }
  29. if(time() > $item['activeendtime']){
  30. $status_text = '已结束';
  31. $status = 3;
  32. }
  33. $item['status_text'] = $status_text;
  34. $item['active_status'] = $status;
  35. }
  36. }
  37. $this->success(1,$list);
  38. }
  39. public function info(){
  40. $id = input('id',0);
  41. $info = Db::name('active')->where('is_show',1)->where('id',$id)->find();
  42. if(!$info){
  43. $this->error('不存在的活动');
  44. }
  45. $info = info_domain_image($info,['images']);
  46. $info['tags'] = !empty($info['tags']) ? explode(',',$info['tags']) : [];
  47. //几人已报名
  48. $info['payednumber'] = Db::name('order')->where('active_id',$id)->where('status',1)->count();
  49. $this->success(1,$info);
  50. }
  51. //报名
  52. public function join(){
  53. $remark = input('remark',0);
  54. $active_id = input('id',0);
  55. $info = Db::name('active')->where('is_show',1)->where('id',$active_id)->find();
  56. if(!$info){
  57. $this->error('不存在的活动');
  58. }
  59. $student_id = input('student_id',0);
  60. if(!$student_id){
  61. $this->error('请选择学生');
  62. }
  63. //匹配学生
  64. $check = Db::name('user_student')->where('id',$student_id)->where('user_id',$this->auth->id)->find();
  65. if(!$check){
  66. $this->error('不存在的学生');
  67. }
  68. //重复报名
  69. $map = [
  70. 'active_id' => $active_id,
  71. 'user_id' => $this->auth->id,
  72. 'student_id' => $student_id,
  73. 'status' => 1,
  74. ];
  75. $check = Db::name('order')->where($map)->find();
  76. if($check){
  77. $this->error('该学生已经报名本次活动');
  78. }
  79. Db::startTrans();
  80. //下单
  81. $data = [
  82. 'order_no' => createUniqueNo('A',$this->auth->id),
  83. 'active_id' => $active_id,
  84. 'user_id' => $this->auth->id,
  85. 'student_id' => $student_id,
  86. 'createtime' => time(),
  87. 'pay_fee' => $info['pay_fee'],
  88. 'status' => 0,
  89. 'remark' => $remark,
  90. ];
  91. $order_id = Db::name('order')->insertGetId($data);
  92. if(!$order_id){
  93. Db::rollback();
  94. $this->error('报名失败');
  95. }
  96. Db::commit();
  97. $this->success(1,$order_id);
  98. }
  99. }