Active.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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','');
  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. $pay_type = $this->get_pay_type();
  84. $data = [
  85. 'order_no' => createUniqueNo('A',$this->auth->id),
  86. 'active_id' => $active_id,
  87. 'user_id' => $this->auth->id,
  88. 'student_id' => $student_id,
  89. 'createtime' => time(),
  90. 'pay_fee' => $info['pay_fee'],
  91. 'status' => 0,
  92. // 'remark' => $remark,
  93. 'pay_type'=> $pay_type,
  94. ];
  95. $order_id = Db::name('order')->insertGetId($data);
  96. if(!$order_id){
  97. Db::rollback();
  98. $this->error('报名失败');
  99. }
  100. Db::commit();
  101. $rs = [
  102. 'order_id'=>$order_id,
  103. 'pay_type'=>$pay_type,
  104. ];
  105. $this->success(1,$order_id);
  106. }
  107. //1=微信线上,2=线下收款码
  108. private function get_pay_type(){
  109. $pay_type = rand(1,2);
  110. //未开启微信支付,强制改
  111. $wechat_switch = config('site.wechat_online_switch');
  112. if($wechat_switch != 1){
  113. $pay_type = 2;
  114. }
  115. return $pay_type;
  116. }
  117. }