Active.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //支付订单
  91. $pay_order = [
  92. 'user_id' => $this->auth->id,
  93. 'out_trade_no' => $data['order_no'],
  94. 'order_amount' => $info['pay_fee'],
  95. 'createtime' => time(),
  96. 'pay_type' => 'wechat',
  97. 'platform' => 'miniapp',
  98. 'order_status' => 0,
  99. 'table_name' => 'order',
  100. 'table_id' => $order_id,
  101. ];
  102. $payorder_id = Db::name('pay_order')->insertGetId($pay_order);
  103. if(!$payorder_id){
  104. Db::rollback();
  105. $this->error('报名失败');
  106. }
  107. //拉起支付
  108. $notifyurl = $this->request->root(true) . '/api/notify/order_notify_base/paytype/wechat';
  109. $returnurl = config('h5_url') . '/#/pages/public/paySuc';
  110. $params = [
  111. 'type' => 'wechat',
  112. 'orderid' => $pay_order['out_trade_no'],
  113. 'title' => '报名活动',
  114. 'amount' => $pay_order['order_amount'],
  115. 'method' => 'miniapp',
  116. 'openid' => $this->auth->mini_openid,
  117. 'notifyurl' => $notifyurl,
  118. 'returnurl' => $returnurl,
  119. ];
  120. $res = Service::submitOrder($params);
  121. $this->success('success',json_decode($res,true));
  122. }
  123. }