123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use addons\epay\library\Service;
- /**
- * 活动
- */
- class Active extends Api
- {
- protected $noNeedLogin = ['lists'];
- protected $noNeedRight = ['*'];
- public function lists()
- {
- $where = ['is_show'=>1];
- $list = Db::name('active')->field('content',true)->where($where)->order('weigh desc')->autopage()->select();
- $list = list_domain_image($list,['image']);
- if(!empty($list)){
- foreach($list as $key => &$item){
- $status_text = '进行中';
- $status = 2;
- if(time() < $item['activestarttime']){
- $status_text = '报名中';
- $status = 1;
- }
- if(time() > $item['activeendtime']){
- $status_text = '已结束';
- $status = 3;
- }
- $item['status_text'] = $status_text;
- $item['status'] = $status;
- }
- }
- $this->success(1,$list);
- }
- public function info(){
- $id = input('id',0);
- $info = Db::name('active')->where('is_show',1)->where('id',$id)->find();
- if(!$info){
- $this->error('不存在的活动');
- }
- $info = info_domain_image($info,['image']);
- $this->success(1,$info);
- }
- //报名
- public function join(){
- $active_id = input('id',0);
- $info = Db::name('active')->where('is_show',1)->where('id',$active_id)->find();
- if(!$info){
- $this->error('不存在的活动');
- }
- $student_id = input('student_id',0);
- if(!$student_id){
- $this->error('请选择学生');
- }
- //匹配学生
- $check = Db::name('user_student')->where('id',$student_id)->where('user_id',$this->auth->id)->find();
- if(!$check){
- $this->error('不存在的学生');
- }
- //重复报名
- $map = [
- 'active_id' => $active_id,
- 'user_id' => $this->auth->id,
- 'student_id' => $student_id,
- ];
- $check = Db::name('order')->where($map)->find();
- if($check){
- if($check['status'] == 0){
- $this->error('您已经报名,请及时支付');
- }
- $this->error('您已经报名该活动');
- }
- Db::startTrans();
- //下单
- $data = [
- 'order_no' => createUniqueNo('A',$this->auth->id),
- 'active_id' => $active_id,
- 'user_id' => $this->auth->id,
- 'student_id' => $student_id,
- 'createtime' => time(),
- 'pay_fee' => $info['pay_fee'],
- 'status' => 0,
- ];
- $order_id = Db::name('order')->insertGetId($data);
- if(!$order_id){
- Db::rollback();
- $this->error('报名失败');
- }
- //支付订单
- $pay_order = [
- 'user_id' => $this->auth->id,
- 'out_trade_no' => $data['order_no'],
- 'order_amount' => $info['pay_fee'],
- 'createtime' => time(),
- 'pay_type' => 'wechat',
- 'platform' => 'miniapp',
- 'order_status' => 0,
- 'table_name' => 'order',
- 'table_id' => $order_id,
- ];
- $payorder_id = Db::name('pay_order')->insertGetId($pay_order);
- if(!$payorder_id){
- Db::rollback();
- $this->error('报名失败');
- }
- //拉起支付
- $notifyurl = $this->request->root(true) . '/api/notify/order_notify_base/paytype/wechat';
- $returnurl = config('h5_url') . '/#/pages/public/paySuc';
- $params = [
- 'type' => 'wechat',
- 'orderid' => $pay_order['out_trade_no'],
- 'title' => '报名活动',
- 'amount' => $pay_order['order_amount'],
- 'method' => 'miniapp',
- 'openid' => $this->auth->mini_openid,
- 'notifyurl' => $notifyurl,
- 'returnurl' => $returnurl,
- ];
- $res = Service::submitOrder($params);
- $this->success('success',json_decode($res,true));
- }
- }
|