Browse Source

商家端课时表与签到

lizhen_gitee 1 year ago
parent
commit
081d4592a9

+ 1 - 1
application/api/controller/Lesson.php

@@ -38,7 +38,7 @@ class Lesson extends Api
         $coach_id = input('coach_id',0);
 
         $where = [
-            'slot.starttime' => ['BETWEEN',[$date,$date+864010]],
+            'slot.starttime' => ['BETWEEN',[$date,$date+86399]],
         ];
         if($lesson_id){
             $where['slot.lesson_id'] = $lesson_id;

+ 167 - 0
application/api/controller/coach/Lesson.php

@@ -0,0 +1,167 @@
+<?php
+
+namespace app\api\controller\coach;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 售课
+ */
+class Lesson extends Api
+{
+    // 无需登录的接口,*表示全部
+    protected $noNeedLogin = [];
+    // 无需鉴权的接口,*表示全部
+    protected $noNeedRight = ['*'];
+
+    //课时首页
+    public function slot_list(){
+
+        $date = input('date',date('Y-m-d'),'strtotime');
+        $where = [
+            'slot.starttime' => ['BETWEEN',[$date,$date+86399]],
+        ];
+
+        //课时
+        $list = Db::name('lesson_slot')->alias('slot')
+            ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
+            ->join('lesson','slot.lesson_id = lesson.id','LEFT')
+            ->where($where);
+
+        $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
+
+        $list = $list->order('slot.starttime asc')->select();
+        if(empty($list)){
+            $this->success(1,[]);
+        }
+        $list = list_domain_image($list,['image']);
+        $list = $this->list_lang($list,['name']);
+
+
+        foreach($list as $key => &$slot){
+
+            //已报名数量,(包含已支付,已点名,没有冲突)
+            $pay_number = Db::name('lesson_order')->alias('order')
+                ->field('order.id,user.avatar,order.usernumber')
+                ->join('user','order.user_id = user.id','LEFT')
+                ->where('order.slot_id',$slot['id'])
+                ->where('order.order_status','IN',[10,20])
+                ->select();
+            $pay_number = list_domain_image($pay_number,['avatar']);
+
+            $slot['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
+            $slot['useravatar'] = array_column($pay_number,'avatar');
+        }
+
+        $this->success(1,$list);
+    }
+
+    //课时详情
+    public function slot_info(){
+        $slot_id = input('slot_id',0);
+
+        //课时
+        $info = Db::name('lesson_slot')->alias('slot')
+            ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
+            ->join('lesson','slot.lesson_id = lesson.id','LEFT')
+            ->where('slot.id',$slot_id)->find();
+
+        $info = info_domain_image($info,['image']);
+        $info = $this->info_lang($info,['name']);
+
+        //已报名数量,(包含已支付,已点名,没有冲突)
+        $pay_number = Db::name('lesson_order')->alias('order')
+            ->field('order.id,user.avatar,order.usernumber,order.usernumber_sign')
+            ->join('user','order.user_id = user.id','LEFT')
+            ->where('order.slot_id',$info['id'])
+            ->where('order.order_status','IN',[10,20])
+            ->select();
+        $pay_number = list_domain_image($pay_number,['avatar']);
+
+        $info['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
+
+        $info['user_list'] = $pay_number;
+
+
+        $this->success(1,$info);
+    }
+
+    //课时申请报名
+    public function slot_sign(){
+        $this->apiLimit();
+
+        $slot_id = input('slot_id',0,'intval');
+        $signdata  = input('signdata','','htmlspecialchars_decode');
+        $signdata  = json_decode($signdata,true);
+
+        //测试
+        /*$signdata = [
+            6 => 1,
+            7 => 2,
+        ];*/
+
+
+        //检查课时状态
+        $slot_info = Db::name('lesson_slot')->where('id',$slot_id)->find();
+        if($slot_info['status'] == 20){
+            $this->error('此课程已经点过名了');
+        }
+        if($slot_info['status'] != 0){
+            $this->error('此课程不能提交点名');
+        }
+
+        //检查报名单数据
+        //逐个检查报名单,对比数量和状态。并保存
+        Db::startTrans();
+        $order_list = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->order('id asc')->lock(true)->select();
+        if(count($order_list) != count($signdata)){
+            Db::rollback();
+            $this->error('点名数据错误');
+        }
+        if(empty($order_list)){
+            Db::rollback();
+            $this->error('无人报名,请直接取消课程');
+        }
+        foreach($order_list as $order){
+            if(!isset($signdata[$order['id']])){
+                Db::rollback();
+                $this->error('点名数据错误');
+            }
+            $usernumber_sign = $signdata[$order['id']];
+
+            if($usernumber_sign < 0 || $usernumber_sign > $order['usernumber']){
+                Db::rollback();
+                $this->error('点名数据错误');
+            }
+
+            $update = [
+                'usernumber_sign' => $usernumber_sign,
+                'order_status'    => 20,
+                'finishtime'    => time(),
+            ];
+            $rs_sign = Db::name('lesson_order')->where('id',$order['id'])->update($update);
+            if($rs_sign === false){
+                Db::rollback();
+                $this->error('点名失败');
+            }
+        }
+
+        //修改课时状态
+        $update = [
+            'status'        => 20,
+            'finishtime'    => time(),
+        ];
+        $slot_rs = Db::name('lesson_slot')->where('id',$slot_id)->update($update);
+        if($slot_rs === false){
+            Db::rollback();
+            $this->error('点名失败');
+        }
+
+        Db::commit();
+        $this->success('点名完成');
+    }
+
+
+
+
+}

+ 41 - 0
application/common/controller/Api.php

@@ -13,6 +13,7 @@ use think\Request;
 use think\Response;
 use think\Route;
 use think\Validate;
+use Redis;
 
 /**
  * API控制器基类
@@ -416,5 +417,45 @@ class Api
         }
     }
 
+    /**
+     * 接口请求限制
+     * @param int $apiLimit
+     * @param int $apiLimitTime
+     * @param string $key
+     * @return bool | true:通过 false:拒绝
+     */
+    public function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
+    {
+        $userId = $this->auth->id;
+        $controller = request()->controller();
+        $action = request()->action();
+
+        if (!$key) {
+            $key = strtolower($controller) . '_' . strtolower($action) . '_' . $userId;
+        }
+
+        $redis = new Redis();
+        $redisconfig = config("redis");
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        if ($redisconfig['redis_pwd']) {
+            $redis->auth($redisconfig['redis_pwd']);
+        }
+        if($redisconfig['redis_selectdb'] > 0){
+            $redis->select($redisconfig['redis_selectdb']);
+        }
+        $check = $redis->exists($key);
+        if ($check) {
+            $redis->incr($key);
+            $count = $redis->get($key);
+            if ($count > $apiLimit) {
+                return false;
+            }
+        } else {
+            $redis->incr($key);
+            $redis->pExpire($key, $apiLimitTime);
+        }
+        return true;
+    }
+
 
 }

+ 10 - 0
application/config.php

@@ -304,6 +304,16 @@ return [
 
     //我的
 
+    'redis'                  => [
+        // 主机
+        'host'     => '127.0.0.1',
+        // 端口
+        'port'      => 6379,
+
+        'redis_pwd' => '',
+        'redis_selectdb' => 11,
+    ],
+
     //小程序
     'wxMiniProgram' => [
         'appid'=>'', //公司测试