You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/model/Article.php

152 lines
4.2 KiB

<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\model;
use app\api\model\article\Category as CategoryModel;
use app\common\model\Article as ArticleModel;
/**
* 商品评价模型
* Class Article
* @package app\api\model
*/
class Article extends ArticleModel
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'virtual_views',
'actual_views',
'is_delete',
'store_id',
'update_time'
];
/**
* 获取器:文章详情HTML实体转换回普通字符
* @param $value
* @return string
*/
public function getArticleContentAttr($value): string
{
return htmlspecialchars_decode($value);
}
/**
* 获取文章详情
* @param int $articleId 文章ID
* @return Article|array|null
* @throws \cores\exception\BaseException
*/
public static function getDetail(int $articleId)
{
// 获取文章详情
$detail = parent::detail($articleId, ['image']);
if (empty($detail) || $detail['is_delete']) {
throwError('很抱歉,当前文章不存在');
}
// 累积文章实际阅读数
static::setIncActualViews($articleId);
return $detail;
}
/**
* 累积文章实际阅读数
* @param int $articleId 文章ID
* @return void
*/
private static function setIncActualViews(int $articleId): void
{
(new static)->setInc($articleId, 'actual_views', 1);
}
/**
* 获取文章列表
* @param int $categoryId
* @param int $limit
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(int $categoryId = 0, int $limit = 15, string $title = ''): \think\Paginator
{
// 检索查询条件
$filter = [];
$categoryId > 0 && $filter[] = ['category_id', '=', $categoryId];
if ($title) {
$filter[] = ['title', 'like', "%$title%"];
}
// 获取列表数据
$list = $this->where($filter)
->where('status', '=', 1)
->where('is_delete', '=', 0)
->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate($limit);
return static::preload($list, ['image', 'category']);
}
/**
* 新增记录
* @param array $data
* @return bool
*/
public function add(array $data): bool
{
$data['store_id'] = self::$storeId;
return $this->save($data);
}
public function helpCenter()
{
$cat = CategoryModel::where(['status' => 1])->with(['catImg'])->order('sort asc')->select()->toArray();
foreach ($cat as $k => $v) {
$cat[$k]['child'] = $this->where('status', '=', 1)
->where('is_delete', '=', 0)
->where('category_id', '=', $v['category_id'])
->order(['sort' => 'asc', 'create_time' => 'desc'])
->select()->toArray();
}
return $cat;
}
/**
* 更新记录
* @param array $data
* @return bool
*/
public function edit(array $data): bool
{
return $this->save($data) !== false;
}
/**
* 软删除
* @return bool
*/
public function setDelete(): bool
{
return $this->save(['is_delete' => 1]);
}
/**
* 获取文章总数量
* @param array $where
* @return int
*/
public static function getArticleTotal(array $where = []): int
{
return (new static)->where($where)->where('is_delete', '=', 0)->count();
}
}