Active.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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,['image']);
  18. if(!empty($list)){
  19. foreach($list as $key => &$item){
  20. $status_text = '进行中';
  21. $status = 2;
  22. if(time() < $item['activestarttime']){
  23. $status_text = '报名中';
  24. $status = 1;
  25. }
  26. if(time() > $item['activeendtime']){
  27. $status_text = '已结束';
  28. $status = 3;
  29. }
  30. $item['status_text'] = $status_text;
  31. $item['status'] = $status;
  32. }
  33. }
  34. $this->success(1,$list);
  35. }
  36. public function info(){
  37. $id = input('id',0);
  38. $info = Db::name('active')->where('is_show',1)->where('id',$id)->find();
  39. if(!$info){
  40. $this->error('不存在的活动');
  41. }
  42. $info = info_domain_image($info,['image']);
  43. $this->success(1,$info);
  44. }
  45. //报名
  46. public function join(){
  47. $active_id = input('id',0);
  48. $info = Db::name('active')->where('is_show',1)->where('id',$active_id)->find();
  49. if(!$info){
  50. $this->error('不存在的活动');
  51. }
  52. $student_id = input('student_id',0);
  53. if(!$student_id){
  54. $this->error('请选择学生');
  55. }
  56. //匹配学生
  57. $check = Db::name('user_student')->where('id',$student_id)->where('user_id',$this->auth->id)->find();
  58. if(!$check){
  59. $this->error('不存在的学生');
  60. }
  61. //重复报名
  62. $map = [
  63. 'active_id' => $active_id,
  64. 'user_id' => $this->auth->id,
  65. 'student_id' => $student_id,
  66. ];
  67. $check = Db::name('order')->where($map)->find();
  68. if($check){
  69. if($check['status'] == 0){
  70. $this->error('您已经报名,请及时支付');
  71. }
  72. $this->error('您已经报名该活动');
  73. }
  74. Db::startTrans();
  75. //下单
  76. $data = [
  77. 'order_no' => createUniqueNo('A',$this->auth->id),
  78. 'active_id' => $active_id,
  79. 'user_id' => $this->auth->id,
  80. 'student_id' => $student_id,
  81. 'createtime' => time(),
  82. 'pay_fee' => $info['pay_fee'],
  83. 'status' => 0,
  84. ];
  85. $order_id = Db::name('order')->insertGetId($data);
  86. if(!$order_id){
  87. Db::rollback();
  88. $this->error('报名失败');
  89. }
  90. Db::commit();
  91. $this->success(1,$order_id);
  92. }
  93. }