ZxcZxc123 před 2 roky
rodič
revize
3ede3c43e7
2 změnil soubory, kde provedl 57 přidání a 25 odebrání
  1. 10 25
      app/api/controller/Pub.php
  2. 47 0
      app/model/api/Education.php

+ 10 - 25
app/api/controller/Pub.php

@@ -11,6 +11,7 @@
 declare (strict_types=1);
 declare (strict_types=1);
 
 
 namespace app\api\controller;
 namespace app\api\controller;
+
 use app\model\api\ArticleModel;
 use app\model\api\ArticleModel;
 use think\facade\View;
 use think\facade\View;
 use app\BaseController;
 use app\BaseController;
@@ -33,6 +34,7 @@ use app\model\api\UserWorkType;
 use app\model\api\InfoAudit;
 use app\model\api\InfoAudit;
 use app\model\api\TypeAudit;
 use app\model\api\TypeAudit;
 use app\model\api\Sys as SysModel;
 use app\model\api\Sys as SysModel;
+use app\model\api\Education;
 
 
 use app\model\api\Advert as AdvertModel;
 use app\model\api\Advert as AdvertModel;
 use library\utils\WxpayV2 as wxpayApi;
 use library\utils\WxpayV2 as wxpayApi;
@@ -375,37 +377,20 @@ class Pub extends BaseController
     {
     {
         $userInfo = Db::name("document_user")->where('uid', $this->uid)->find();
         $userInfo = Db::name("document_user")->where('uid', $this->uid)->find();
 
 
-        $EducationModel = model('Education');
+        $EducationModel = new Education();
         $where["audit"] = 1;
         $where["audit"] = 1;
-        $type = input("type", "");
-        if ($type !== "") {
-            $where["type1"] = $type;
-        }
-        $grlist = Db::name("education_gr")->where($where)->limit(20)->order("od asc,recommend desc,gr_id desc")->select();
-        foreach ($grlist as $v) {
-            $count = Db::name("education_course")->where("course_status", 1)->where(["gr_id" => $v["gr_id"], "course_audit" => 1])->field("count(*) as count,sum(course_play_count) as alll")->find();
-            $v["count"] = $count["count"];
-            $count["alll"] += $v["times"];
-            $v["alll"] = $count["alll"] ? $count["alll"] : 0;;
-            $ngrlist[] = $v;
+        $type1 = input("type1", "");
+        if ($type1 !== "") {
+            $where["type1"] = $type1;
         }
         }
+        $grlist = $EducationModel->getEducationGrList($where, 20, "od asc,recommend desc,gr_id desc");
 
 
-        $where["recommend"] = 1;
-        $recommendList = Db::name("education_gr")->where($where)->limit(9)->order("od asc,recommend desc,gr_id desc")->select();
-        $recommendListResult = array();
-        if (count($recommendList) > 0) {
-            foreach ($recommendList as $recommend) {
-                $recommendListResult[] = array(
-                    "img" => $recommend["indeximg"],
-                    "href" => url('kcz', array("gr_id" => $recommend["gr_id"]))
-                );
-            }
-        }
+        $recommendList = $EducationModel->getRecommendEducationGrList(9, "od asc,recommend desc,gr_id desc");
 
 
         $result = [
         $result = [
             'userInfo' => $userInfo,
             'userInfo' => $userInfo,
-            'grlist' => $ngrlist,
-            'recommendList' => $recommendListResult,
+            'grlist' => $grlist,
+            'recommendList' => $recommendList,
         ];
         ];
         return json($result);
         return json($result);
     }
     }

+ 47 - 0
app/model/api/Education.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace app\model\api;
+
+use think\model;
+
+class Education extends model
+{
+    protected $table = 'education_gr';
+
+    // 获取教育组列表
+    public function getEducationGrList($where = [], $limit = 20, $order = 'od asc,recommend desc,gr_id desc')
+    {
+        $query = $this->where($where)->limit($limit)->order($order);
+        $grList = $query->select();
+        foreach ($grList as &$gr) {
+            $count = $this->getCourseCount($gr['gr_id']);
+            $gr['count'] = $count['count'];
+            $gr['alll'] = $count['alll'] + $gr['times'];
+        }
+        return $grList;
+    }
+
+
+    // 获取课程总数和播放总数
+    public function getCourseCount($grId)
+    {
+        $count = $this->table('education_course')->where(['gr_id' => $grId, 'course_status' => 1, 'course_audit' => 1])->field('count(*) as count, sum(course_play_count) as alll')->find();
+        return $count ?: ['count' => 0, 'alll' => 0];
+    }
+
+
+    // 获取推荐教育组列表
+    public function getRecommendEducationGrList($limit = 9, $order = 'od asc,recommend desc,gr_id desc')
+    {
+        $recommendList = $this->where(['recommend' => 1])->limit($limit)->order($order)->select();
+        $recommendListResult = [];
+        foreach ($recommendList as $recommend) {
+            $recommendListResult[] = [
+                'img' => $recommend['indeximg'],
+                'href' => url('kcz', ['gr_id' => $recommend['gr_id']]),
+            ];
+        }
+        return $recommendListResult;
+    }
+
+}