|
|
@@ -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) : '';
|
|
|
+ }
|
|
|
+}
|