Просмотр исходного кода

Wesmiler 校企小程序 更新 6.14

wesmiler 3 лет назад
Родитель
Сommit
22cb6c98e0

+ 19 - 1
app/api/controller/SchoolSpeciality.php

@@ -39,12 +39,30 @@ class SchoolSpeciality extends Controller
         return $this->renderSuccess(compact('detail'));
     }
 
+    /**
+     * 同类专业学校
+     * @return Json
+     * @throws \think\db\exception\DbException
+     */
     public function sameList()
     {
         // 获取列表数据
         $model = new School;
         $pageSize = $this->request->param('pageSize', 12);
-        $list = $model->getList($this->request->param(), $pageSize);
+        $list = $model->getListBySpeciality($this->request->param(), $pageSize);
+        return $this->renderSuccess(compact('list'));
+    }
+
+    /**
+     * 获取热门专业
+     * @return Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function hots(){
+        $limit = $this->request->param('limit', 6);
+        $list = (new SchoolSpecialityModel)->getHots(0,'speciality_id,speciality_name,views', $limit);
         return $this->renderSuccess(compact('list'));
     }
 }

+ 10 - 5
app/api/model/School.php

@@ -64,7 +64,7 @@ class School extends SchoolModel
      * @return mixed|\think\model\Collection|\think\Paginator
      * @throws \think\db\exception\DbException
      */
-    public function getSpecialityList(int $specialityId, array $param = [], int $listRows = 15)
+    public function getListBySpeciality(array $param = [], int $listRows = 15)
     {
         // 整理查询参数
         $params = array_merge($param, ['audit_status' => 1]);
@@ -75,7 +75,7 @@ class School extends SchoolModel
         }
 
         // 整理列表数据并返回
-        return $this->setListDataFromApi($list);
+        return $this->setListDataFromApi($list, $params);
     }
 
     /**
@@ -83,11 +83,16 @@ class School extends SchoolModel
      * @param $info
      * @return mixed
      */
-    private function setListDataFromApi($info)
+    private function setListDataFromApi($info, $params=[])
     {
         $specialityModel = new SchoolSpeciality;
-        return $this->setListData($info, function ($data) use($specialityModel) {
-            $data['speciality'] = $specialityModel->getHots($data['id']);
+        return $this->setListData($info, function ($data) use($specialityModel,$params) {
+            $specialityName = isset($params['speciality_name'])? $params['speciality_name'] : '';
+            if($specialityName){
+                $data['speciality'] = $specialityModel->getDataByName($specialityName);
+            }else{
+                $data['speciality'] = $specialityModel->getHots($data['id']);
+            }
 
             // 整理数据 api模块
             $this->setDataFromApi($data);

+ 29 - 1
app/api/model/SchoolSpeciality.php

@@ -113,13 +113,41 @@ class SchoolSpeciality extends SchoolSpecialityModel
      */
     public function getHots(int $school_id, $field='', $limit=3)
     {
-        return $this->where(['school_id'=>$school_id,'status'=>1])
+        $where = ['status'=>1];
+        if($school_id){
+            $where['school_id'] = $school_id;
+        }
+
+        return $this->where($where)
             ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
             ->order('views desc,speciality_id desc')
             ->limit($limit?? 3)
             ->select()??[];
     }
 
+    /**
+     * 获取学校的专业
+     * @param string $name 专业名称
+     * @param string $field 返回字段
+     * @return SchoolSpeciality[]|array|\think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getDataByName(string $name, $field='')
+    {
+        $data = $this->where(['status'=>1])
+                ->where('speciality_name','like', "%{$name}%")
+                ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
+                ->order('views desc,speciality_id desc')
+                ->find();
+        if($data['speciality_id']){
+            $bookNum = SpecialityBook::getBooks($data['speciality_id']);
+            $data['book_num'] = intval($bookNum);
+            $data['remainder_num'] = max(0,$data['recruit_num'] - $bookNum);
+        }
+        return $data;
+    }
 
     /**
      * 获取详情并累计访问次数