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

Wesmiler 校企小程序 更新

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

+ 47 - 0
app/api/controller/School.php

@@ -3,6 +3,10 @@
 namespace app\api\controller;
 
 use app\api\model\School as SchoolModel;
+use app\api\model\SchoolNew;
+use app\api\model\UserDynamic;
+use app\api\service\User as UserService;
+use app\common\model\SchoolAlbum;
 
 /**
  * 学校控制器
@@ -36,4 +40,47 @@ class School extends Controller
         $list = $model->getOptionList($this->request->param());
         return $this->renderSuccess(compact('list'));
     }
+
+    /**
+     * 获取学校主页数据
+     * @return \think\response\Json
+     * @throws \cores\exception\BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function homeData()
+    {
+        $schoolId = $this->request->param('school_id', 0);
+        if($schoolId<=0){
+            $userInfo = UserService::getCurrentLoginUser(true);
+            $schoolId = isset($userInfo['info']['school_id'])? intval($userInfo['info']['school_id']) : 0;
+            $userType = isset($userInfo['user_type'])? $userInfo['user_type'] : 0;
+            $schoolId = $userType != 3? 0 : $schoolId;
+        }
+
+        if($schoolId <= 0){
+            return $this->renderSuccess('您无权访问或未绑定学校信息,请先绑定');
+        }
+
+        $data = [
+            'info'=> SchoolModel::detail($schoolId),
+            'albums'=> SchoolAlbum::getShowList($schoolId, 6),
+            'news'=> SchoolNew::getShowList($schoolId, 1, 3),
+            'scenery'=> SchoolNew::getShowList($schoolId, 2, 6)
+        ];
+
+        return $this->renderSuccess($data);
+    }
+
+    /**
+     * 校园时态
+     * @return \think\response\Json
+     * @throws \think\db\exception\DbException
+     */
+    public function dynamic(){
+        $model = new UserDynamic();
+        $list = $model->getList($this->request->param());
+        return $this->renderSuccess(compact('list'));
+    }
 }

+ 18 - 2
app/api/model/School.php

@@ -141,9 +141,25 @@ class School extends SchoolModel
         if (is_array($where)) {
             $filter = array_merge($filter, $where);
         } else {
-            $filter['user_id'] = (int)$where;
+            $filter['id'] = (int)$where;
         }
-        return static::get($filter, $with);
+        $data = static::get($filter, $with);
+        $data->hidden(['book_fields']);
+        if($data['logo']){
+            $data['logo'] = getPreview($data['logo']);
+        }
+        return $data;
+    }
+
+    /**
+     * 获取用户所属招生学校
+     * @param $userId
+     * @param string $field
+     * @return mixed
+     */
+    public static function getUserSchool($userId, $field=''){
+        $model = new SchoolModel;
+        return $model->userSchool($userId, $field??'id, school_name');
     }
 
 

+ 14 - 0
app/api/model/SchoolAlbum.php

@@ -30,4 +30,18 @@ class SchoolAlbum extends SchoolAlbumModel
     protected $hidden = [
         'update_time'
     ];
+
+
+    /**
+     * @param $schoolId
+     * @param int $limitRow
+     * @return array|mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getShowList($schoolId, $limitRow = 0)
+    {
+        return parent::getShowList($schoolId, $limitRow); // TODO: Change the autogenerated stub
+    }
 }

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

@@ -0,0 +1,47 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\api\model;
+use app\common\model\SchoolNew as SchoolNewModel;
+
+/**
+ * 学校新闻资讯模型类
+ * Class SchoolNew
+ * @package app\api\model
+ */
+class SchoolNew extends SchoolNewModel
+{
+
+    protected $globalScope = [''];
+
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    protected $hidden = [
+        'update_time'
+    ];
+
+    /**
+     * @param $schoolId
+     * @param int $type
+     * @param int $limitRow
+     * @return array|mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getShowList($schoolId, $type = 1, $limitRow = 0)
+    {
+        return parent::getShowList($schoolId, $type, $limitRow); // TODO: Change the autogenerated stub
+    }
+}

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

@@ -43,7 +43,7 @@ class SchoolSpeciality extends SchoolSpecialityModel
      */
     public function getHots(int $school_id, $field='', $limit=3)
     {
-        return $this->where(['school_id'=>$school_id])
+        return $this->where(['school_id'=>$school_id,'status'=>1])
             ->field($field?:'speciality_id,speciality_name,school_id,recruit_num')
             ->order('views desc,speciality_id desc')
             ->limit($limit?? 3)

+ 3 - 4
app/api/model/User.php

@@ -67,7 +67,9 @@ class User extends UserModel
         // 用户基本信息
         $userInfo = self::detail($userId);
         $userInfo['info']=$userInfo['info']? $userInfo['info'] : [];
-        $userInfo['info']['school']=$userInfo['info']['school']? $userInfo['info']['school'] : [];
+        if($userInfo['info']){
+            $userInfo['info']['school']= isset($userInfo['info']['school']) && $userInfo['info']['school']? $userInfo['info']['school'] : [];
+        }
         if (empty($userInfo) || $userInfo['is_delete']) {
             throwError('很抱歉,用户信息不存在或已删除', config('status.not_logged'));
         }
@@ -241,9 +243,6 @@ class User extends UserModel
         }
 
         if($data['user_type'] == 1){
-            if(empty($data['student_no'])){
-                throwError('学号不为空');
-            }
 
             if(empty($data['admission_year'])){
                 throwError('请选择入学年份');

+ 123 - 0
app/api/model/UserDynamic.php

@@ -0,0 +1,123 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\api\model;
+use app\common\model\UserDynamic as UserDynamicModel;
+
+/**
+ * 用户动态模型类
+ * Class UserDynamic
+ * @package app\api\model
+ */
+class UserDynamic extends UserDynamicModel
+{
+
+    protected $globalScope = [''];
+
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    protected $hidden = [
+        'update_time'
+    ];
+
+    /**
+     * 获取列表
+     * @param array $param
+     * @param int $listRows
+     * @return mixed
+     */
+    public function getList(array $param, int $listRows = 15)
+    {
+        // 整理查询参数
+        $params = array_merge($param, ['status' => 1]);
+        // 获取商品列表
+        $list = parent::getList($params, $listRows);
+        if ($list->isEmpty()) {
+            return $list;
+        }
+
+        // 隐藏冗余的字段
+        $list->hidden(array_merge($this->hidden, ['status']));
+        // 整理列表数据并返回
+        return $this->setListDataFromApi($list);
+    }
+
+    /**
+     * 设置展示的数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setListDataFromApi($info)
+    {
+        return $this->setListData($info, function ($data) {
+
+            // 整理数据 api模块
+            $this->setDataFromApi($data);
+        });
+    }
+
+    /**
+     * 整理数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setDataFromApi($info)
+    {
+        return $this->setData($info, function ($data) {
+
+            // logo封面
+            $data['logo'] = $data['logo']? getPreview($data['logo']) : '';
+            $data['file_url'] = $data['file_url']? getPreview($data['file_url']) : '';
+
+            // 点赞喜欢数
+            if(!is_null($data['like_num'])){
+                $data['like_num'] = $data['like_num']? ($data['like_num']<10000? "{$data['like_num']}" : round($data['like_num']/10000,1).'w') :'';
+            }
+
+            // 收藏数
+            if(!is_null($data['collect_num'])){
+                $data['collect_num'] = $data['collect_num']? ($data['collect_num']<10000? "{$data['collect_num']}" : round($data['collect_num']/10000,1).'w') :'';
+            }
+
+            // 浏览数
+            if(!is_null($data['views'])){
+                $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
+            }
+
+        });
+    }
+
+
+    /**
+     * 获取详情信息
+     * @param $where
+     * @param array $with
+     * @return static|array|false|null
+     */
+    public static function detail($where, array $with = [])
+    {
+        $filter = [];
+        if (is_array($where)) {
+            $filter = array_merge($filter, $where);
+        } else {
+            $filter['id'] = (int)$where;
+        }
+        return static::get($filter, $with);
+    }
+
+    public static function getListByUserSchool($userId){
+
+    }
+
+}

+ 22 - 0
app/common/model/School.php

@@ -13,6 +13,7 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
+use think\facade\Cache;
 use think\model\Collection;
 use think\model\relation\HasMany;
 use think\model\relation\HasOne;
@@ -190,4 +191,25 @@ class School extends BaseModel
     {
         return $this->HasOne('region','id','region_id')->bind(['name as region_name']);
     }
+
+    /**
+     * @param $userId
+     * @return mixed
+     */
+    public function userSchool($userId, $field=''){
+        $data = \think\facade\Cache::get("caches:user:schoolData:{$userId}");
+        if($data){
+            return $data;
+        }
+
+        $data = self::alias($this->name)
+            ->leftJoin('user_info ui','ui.school_id='.$this->name.'.id')
+            ->where(['ui.user_id'=> $userId])
+            ->find();
+        if($data){
+            Cache::set("caches:user:schoolData:{$userId}", $data, rand(5, 10));
+        }
+
+        return $data;
+    }
 }

+ 34 - 1
app/common/model/SchoolAlbum.php

@@ -13,7 +13,6 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
-use think\model\relation\HasOne;
 
 /**
  * 学校图库模型类
@@ -22,6 +21,8 @@ use think\model\relation\HasOne;
  */
 class SchoolAlbum extends BaseModel
 {
+    protected $globalScope = [''];
+
     // 定义表名
     protected $name = 'school_album';
 
@@ -29,4 +30,36 @@ class SchoolAlbum extends BaseModel
     protected $pk = 'album_id';
 
 
+    /**
+     * 获取学校的图库列表
+     * @param $schoolId
+     * @param int $limitRow
+     * @return array|mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getShowList($schoolId, $limitRow=0){
+        if($list = \think\facade\Cache::get('caches:schools:albums:'.$schoolId)){
+            return $list;
+        }
+
+        $query = self::where(['school_id'=>$schoolId]);
+        if($limitRow){
+            $query->limit($limitRow);
+        }
+
+        $list = $query->field('album_id,title,album_url')
+            ->order('album_id desc')
+            ->select();
+        $list = $list? $list->toArray() :[];
+        if($list){
+            foreach ($list as &$item){
+                $item['album_url_preview'] = $item['album_url']? getPreview($item['album_url']) : '';
+            }
+            \think\facade\Cache::set('caches:schools:albums:'.$schoolId, $list, rand(5, 10));
+        }
+
+        return $list;
+    }
 }

+ 70 - 0
app/common/model/SchoolNew.php

@@ -0,0 +1,70 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\model;
+
+use cores\BaseModel;
+
+/**
+ * 学校图库模型类
+ * Class SchoolNew
+ * @package app\common\model
+ */
+class SchoolNew extends BaseModel
+{
+    protected $globalScope = [''];
+
+    // 定义表名
+    protected $name = 'school_news';
+
+    // 定义主键
+    protected $pk = 'news_id';
+
+    /**
+     * 获取学校新闻资讯列表
+     * @param $schoolId
+     * @param int $limitRow
+     * @return array|mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getShowList($schoolId, $type = 1, $limitRow=0){
+        $cacheKey = "caches:schools:news:{$schoolId}_{$type}_{$limitRow}";
+        if($list = \think\facade\Cache::get($cacheKey)){
+            return $list;
+        }
+
+        $query = self::where(['school_id'=>$schoolId, 'type'=> $type, 'status'=>1]);
+        if($limitRow){
+            $query->limit($limitRow);
+        }
+
+        $list = $query->order('addtime desc,news_id desc')->select();
+        $list->hidden(['content','update_time','status']);
+        $list = $list? $list->toArray() :[];
+        if($list){
+            foreach ($list as &$item){
+                $item['logo_preview'] = $item['logo']? getPreview($item['logo']) : '';
+                $item['publish_time'] = $item['addtime']? date('Y/m/d', strtotime($item['addtime'])) : '';
+            }
+
+            \think\facade\Cache::set($cacheKey, $list, rand(10, 30));
+        }
+
+        return $list;
+    }
+
+    public function getList($where, $limitRow= 15){
+
+    }
+}

+ 158 - 0
app/common/model/UserDynamic.php

@@ -0,0 +1,158 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\model;
+
+use cores\BaseModel;
+use think\model\Collection;
+use think\model\relation\HasOne;
+use think\Paginator;
+
+/**
+ * 用户动态模型类
+ * Class UserDynamic
+ * @package app\common\model
+ */
+class UserDynamic extends BaseModel
+{
+    // 定义表名
+    protected $name = 'user_dynamic';
+
+    // 定义主键
+    protected $pk = 'id';
+
+    /**
+     * 获取列表
+     * @param array $param 查询条件
+     * @param int $listRows 分页数量
+     * @return mixed
+     * @throws \think\db\exception\DbException
+     */
+    public function getList(array $param = [], int $listRows = 15)
+    {
+        // 筛选条件
+        $query = $this->getQueryFilter($param);
+        // 排序条件
+        $sort = $this->setQuerySort($param);
+
+        // 执行查询
+        $list = $query->alias($this->name)
+            ->order($sort)
+            ->paginate($listRows);
+
+        // 整理列表数据并返回
+        return $list;
+    }
+
+    /**
+     * 设置商品展示的数据
+     * @param Collection|Paginator $list 商品列表
+     * @param callable|null $callback 回调函数
+     * @return mixed
+     */
+    protected function setListData($list, callable $callback = null)
+    {
+        if ($list->isEmpty()) return $list;
+        // 遍历商品列表整理数据
+        foreach ($list as &$item) {
+            $data = $this->setData($item, $callback);
+        }
+        return $list;
+    }
+
+    /**
+     * 整理数据
+     * @param Collection|static $info
+     * @param callable|null $callback
+     * @return mixed
+     */
+    protected function setData($info, callable $callback = null)
+    {
+        // 回调函数
+        is_callable($callback) && call_user_func($callback, $info);
+        return $info->hidden(array_merge($this->hidden, ['status']));
+    }
+
+    /**
+     * 检索查询条件
+     * @param array $params
+     * @return \think\db\BaseQuery
+     */
+    private function getQueryFilter(array $params)
+    {
+        $filter = [];
+
+        // 实例化新查询对象
+        $query = $this->getNewQuery();
+
+        // 浏览类型
+        !empty($params['look_type']) && $filter[] = ['look_type', '=', "{$params['look_type']}"];
+
+        // 类型
+        !empty($params['type']) && $filter[] = ['type', '=', "{$params['type']}"];
+
+        // 实例化新查询对象
+        return $query->where($filter)->where(function($query) use ($params){
+            // 关键词
+            if(!empty($params['keyword'])){
+                $query->where('content','like', "%{$params['keyword']}%");
+            }
+
+            if(!empty($params['tag'])){
+                $query->where('content','like', "%#{$params['tag']}%");
+            }
+        });
+    }
+
+
+    /**
+     * 检索排序条件
+     * @param array $param
+     * @return array|string[]
+     */
+    private function setQuerySort(array $param = [])
+    {
+        $params = $this->setQueryDefaultValue($param, [
+            'sortType' => 'all',    // 排序类型
+            'create_time' => false,   // 热门排序 (true高到低 false低到高)
+        ]);
+        // 排序规则
+        $sort = [];
+        if ($params['sortType'] === 'all') {
+            $sort = ['create_time' => 'desc'];
+        } elseif ($params['sortType'] === 'view') {
+            $sort = ['views' => 'desc'];
+        }
+
+        return array_merge($sort, [$this->getPk() => 'desc']);
+    }
+
+    /**
+     * 封面览图
+     * @param $value
+     * @return string|string[]|null
+     */
+    public function getImageAttr($value): string
+    {
+        return $value? getPreview($value) : '';
+    }
+
+    /**
+     * 资源文件
+     * @param $value
+     * @return string|string[]|null
+     */
+    public function getFileUrlAttr($value): string
+    {
+        return $value? getPreview($value) : '';
+    }
+}