Active.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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['image'] = isset($info['images'][0]) ? $info['images'][0] : '';
  49. //几人已报名
  50. $info['payednumber'] = Db::name('order')->where('active_id',$id)->where('status',1)->count();
  51. $this->success(1,$info);
  52. }
  53. //报名
  54. public function join(){
  55. $remark = input('remark',0);
  56. $active_id = input('id',0);
  57. $info = Db::name('active')->where('is_show',1)->where('id',$active_id)->find();
  58. if(!$info){
  59. $this->error('不存在的活动');
  60. }
  61. $student_id = input('student_id',0);
  62. if(!$student_id){
  63. $this->error('请选择学生');
  64. }
  65. //匹配学生
  66. $check = Db::name('user_student')->where('id',$student_id)->where('user_id',$this->auth->id)->find();
  67. if(!$check){
  68. $this->error('不存在的学生');
  69. }
  70. //重复报名
  71. $map = [
  72. 'active_id' => $active_id,
  73. 'user_id' => $this->auth->id,
  74. 'student_id' => $student_id,
  75. 'status' => 1,
  76. ];
  77. $check = Db::name('order')->where($map)->find();
  78. if($check){
  79. $this->error('该学生已经报名本次活动');
  80. }
  81. Db::startTrans();
  82. //下单
  83. $data = [
  84. 'order_no' => createUniqueNo('A',$this->auth->id),
  85. 'active_id' => $active_id,
  86. 'user_id' => $this->auth->id,
  87. 'student_id' => $student_id,
  88. 'createtime' => time(),
  89. 'pay_fee' => $info['pay_fee'],
  90. 'status' => 0,
  91. 'remark' => $remark,
  92. ];
  93. $order_id = Db::name('order')->insertGetId($data);
  94. if(!$order_id){
  95. Db::rollback();
  96. $this->error('报名失败');
  97. }
  98. Db::commit();
  99. $this->success(1,$order_id);
  100. }
  101. }