panda 5 months ago
parent
commit
b2ab39d5e9

+ 209 - 0
application/api/controller/UniversityCourse.php

@@ -0,0 +1,209 @@
+<?php
+
+namespace app\api\controller;
+
+use addons\epay\library\Service;
+use app\common\controller\Api;
+use app\common\model\PayOrderModel;
+use app\common\model\UniversityCourseModel;
+use app\common\model\UniversityEventModel;
+use app\common\model\Wallet;
+use app\utils\CurlUtil;
+use app\utils\Service\Tencent\TencentIm;
+use think\Db;
+
+/**
+ * 老年大学 活动板块
+ */
+class UniversityCourse extends Api
+{
+    protected $noNeedLogin = [''];
+    protected $noNeedRight = ['*'];
+
+    // 活动列表
+    public function list()
+    {
+        $user_id = $this->auth->id;
+        $list    = UniversityCourseModel::with([
+            'apply' => function ($query) use ($user_id) {
+                $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
+            }
+        ])
+            ->field('id,name,price,image,start_apply_time,end_apply_time,start_time,address')
+            ->where('status', 1)
+            ->order('id desc')
+            ->autopage()
+            ->select();
+        foreach ($list as $k => $v) {
+            $list[$k]['start_apply_time'] = date('Y-m-d H:i', $v['start_apply_time']);
+            $list[$k]['end_apply_time']   = date('Y-m-d H:i', $v['end_apply_time']);
+            $list[$k]['start_time']       = date('Y-m-d H:i', $v['start_time']);
+            $list[$k]['apply']            = !empty($v['apply']) ? 1 : 0;
+        }
+        return $this->success('success', $list);
+    }
+
+    // 活动详情
+    public function info()
+    {
+        $params = $this->request->param();
+        if (empty($params['event_id'])) {
+            return $this->error('参数缺失');
+        }
+        $user_id = $this->auth->id;
+
+        $info                     = UniversityEventModel::with([
+            'apply' => function ($query) use ($user_id) {
+                $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
+            }
+        ])
+            ->field('id,name,price,image,images,start_apply_time,end_apply_time,start_time,address,content')
+            ->where('id', $params['event_id'])
+            ->where('status', 1)
+            ->order('id desc')
+            ->find();
+        $info['start_apply_time'] = date('Y-m-d', $info['start_apply_time']);
+        $info['end_apply_time']   = date('Y-m-d', $info['end_apply_time']);
+        $info['start_time']       = date('Y-m-d', $info['start_time']);
+        $info['apply']            = !empty($info['apply']) ? 1 : 0;
+        return $this->success('success', $info);
+    }
+
+    public function apply()
+    {
+        $params = $this->request->param();
+        if (empty($params['event_id'])) {
+            return $this->error('参数缺失');
+        }
+        if (empty($params['pay_type']) || empty($params['platform'])) {
+            return $this->error('请选择支付方式');
+        }
+        if (empty($params['apply_list'])) {
+            return $this->error('请提交报名信息');
+        }
+        foreach ($params['apply_list'] as $k => $v) {
+            if (empty($v['name'])) {
+                return $this->error('报名信息姓名不能为空');
+            }
+            if (empty($v['phone'])) {
+                return $this->error('报名信息手机号不能为空');
+            }
+        }
+        $user_id = $this->auth->id;
+        $info    = UniversityEventModel::with([
+            'apply' => function ($query) use ($user_id) {
+                $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
+            }
+        ])
+            ->field('id,name,price,image,images,start_apply_time,end_apply_time,start_time,address,content')
+            ->where('id', $params['event_id'])
+            ->where('status', 1)
+            ->order('id desc')
+            ->find();
+        if (!$info) {
+            return $this->error('活动不存在');
+        }
+        if (!empty($info['apply'])) {
+            return $this->error('您已报过名了');
+        }
+        $nowTime = time();
+        if ($info['start_apply_time'] > $nowTime) {
+            return $this->error('报名未开始');
+        }
+        if ($info['end_apply_time'] < $nowTime) {
+            return $this->error('报名已结束');
+        }
+        // 开始报名
+        $num  = count($params['apply_list']);
+        $data = [
+            'user_id'     => $user_id,
+            'event_id'    => $params['event_id'],
+            'order_no'    => createUniqueNo('E', $user_id),
+            'pay_amount'  => bcmul($info['price'], $num, 2),
+            'create_time' => $nowTime
+        ];
+        Db::startTrans();
+        $apply_id = Db::name('university_event_apply')->insertGetId($data);
+        if (!$apply_id) {
+            Db::rollback();
+            return $this->error('订单创建失败');
+        }
+        $apply_info = [];
+        foreach ($params['apply_list'] as $k => $v) {
+            $apply_info[] = [
+                'user_id'  => $user_id,
+                'event_id' => $params['event_id'],
+                'order_id' => $apply_id,
+                'order_no' => $data['order_no'],
+                'name'     => $v['name'],
+                'phone'    => $v['phone']
+            ];
+        }
+        if (!Db::name('university_event_info')->insertAll($apply_info)) {
+            Db::rollback();
+            return $this->error('订单创建失败');
+        }
+        Db::commit();
+
+        // 创建支付订单
+        $remark    = '老年大学活动报名';
+        $orderData = [
+            'user_id'      => $user_id,
+            'out_trade_no' => $data['order_no'],
+            'order_amount' => $data['pay_amount'],
+            'pay_type'     => $params['pay_type'],
+            'platform'     => $params['platform'],
+            'table_name'   => 'university_event_apply',
+            'table_id'     => $apply_id,
+            'createtime'   => time(),
+            'args'         => json_encode([
+                'table_id' => $apply_id,
+                'remark'   => $remark
+            ], JSON_UNESCAPED_UNICODE),
+        ];
+        if (!Db::name('pay_order')->insert($orderData)) {
+            return $this->error('订单创建失败');
+        }
+
+        // 拉起支付 余额支付
+        if ($params['pay_type'] == 'wallet') {
+            Db::startTrans();
+            //钱包更新
+            $walletService = new Wallet();
+            if (!$walletService->change($user_id, -$orderData['order_amount'], 'money', 20, $remark, $orderData['table_name'], $orderData['table_id'])) {
+                Db::rollback();
+                return $this->error($walletService->getMessage());
+            }
+            // 支付成功,更改支付金额
+            [$res,$msg] = PayOrderModel::university_event($orderData['out_trade_no']);
+            if (!$res){
+                Db::rollback();
+                return $this->error($msg);
+            }
+            Db::commit();
+            return $this->success('支付成功');
+        }
+
+        // 第三方支付下单
+        $params = [
+            'type'      => $orderData['pay_type'],
+            'orderid'   => $orderData['out_trade_no'],
+            'title'     => $remark,
+            'amount'    => $orderData['order_amount'],
+            'method'    => $orderData['platform'],
+            'notifyurl' => CurlUtil::getHttp("/api/notify/university_event_{$params['pay_type']}"),
+            'returnurl' => '',
+        ];
+        // 如果是小程序则需要添加 openid
+        if ($params['pay_type'] == 'wechat' && $params['platform'] == 'miniapp') {
+            $params['openid'] = $this->auth->mini_openid;
+        }
+        $res = Service::submitOrder($params);
+        if ($params['pay_type'] == 'wechat') {
+            $this->success('success', json_decode($res, true));
+        } else {
+            $this->success('success', $res);
+        }
+    }
+
+}

+ 9 - 1
application/api/controller/UniversityEvent.php

@@ -87,6 +87,12 @@ class UniversityEvent extends Api
             if (empty($v['phone'])) {
             if (empty($v['phone'])) {
                 return $this->error('报名信息手机号不能为空');
                 return $this->error('报名信息手机号不能为空');
             }
             }
+            if (empty($v['sex'])) {
+                return $this->error('报名信息性别不能为空');
+            }
+            if (empty($v['age'])) {
+                return $this->error('报名信息年龄不能为空');
+            }
         }
         }
         $user_id = $this->auth->id;
         $user_id = $this->auth->id;
         $info    = UniversityEventModel::with([
         $info    = UniversityEventModel::with([
@@ -135,7 +141,9 @@ class UniversityEvent extends Api
                 'order_id' => $apply_id,
                 'order_id' => $apply_id,
                 'order_no' => $data['order_no'],
                 'order_no' => $data['order_no'],
                 'name'     => $v['name'],
                 'name'     => $v['name'],
-                'phone'    => $v['phone']
+                'phone'    => $v['phone'],
+                'sex'      => $v['sex'],
+                'age'      => $v['age'],
             ];
             ];
         }
         }
         if (!Db::name('university_event_info')->insertAll($apply_info)) {
         if (!Db::name('university_event_info')->insertAll($apply_info)) {

+ 27 - 0
application/common/model/UniversityCourseApplyModel.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class UniversityCourseApplyModel extends Model
+{
+    // 表名
+    protected $name = 'university_event_apply';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+
+    public function user()
+    {
+        return $this->hasOne(UserModel::class, 'id', 'user_id');
+    }
+}

+ 40 - 0
application/common/model/UniversityCourseModel.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 群组
+ */
+class UniversityCourseModel extends Model
+{
+    // 表名
+    protected $name = 'university_event';
+    // 开启自动写入时间戳字段
+    protected $autoWriteTimestamp = false;
+    // 定义时间戳字段名
+    protected $createTime = false;
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    public function getImageAttr($value, $data)
+    {
+        return cdnurl($value);
+    }
+
+    public function getImagesAttr($value, $data)
+    {
+        $value = explode(',',$value);
+        foreach ($value as &$v){
+            $v = cdnurl($v);
+        }
+        return $value;
+    }
+
+    public function apply()
+    {
+        return $this->hasOne(UniversityEventApplyModel::class, 'event_id', 'id');
+    }
+}

+ 0 - 8
public/.htaccess

@@ -1,8 +0,0 @@
-location ~* (runtime|application)/{
-	return 403;
-}
-location / {
-	if (!-e $request_filename){
-		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
-	}
-}