feature view tag get data

This commit is contained in:
Default
2025-04-17 23:16:32 +08:00
parent d6656219ee
commit 7db551a21b
14 changed files with 704 additions and 58 deletions

View File

@@ -0,0 +1,28 @@
<?php
namespace app\services;
use app\model\CategoryModel;
class CategoryService
{
/**
* SiteContext
*
* @var SiteContext
*/
public $SiteContext;
/**
* CategoryModel
*
* @var CategoryModel
*/
public $CategoryModel;
public function __construct(SiteContext $SiteContext)
{
$this->SiteContext = $SiteContext;
$this->CategoryModel = CategoryModel::getInstance();
}
}

View File

@@ -22,6 +22,7 @@ class ChapterService
public function __construct(SiteContext $SiteContext)
{
$this->SiteContext = $SiteContext;
$this->ChapterModel = ChapterModel::getInstance();
}
}

View File

@@ -2,8 +2,13 @@
namespace app\services;
use app\model\CategoryModel;
use app\model\ChapterModel;
use app\model\ConverterMovel;
use app\model\DomainModel;
use app\model\NovelClicksModel;
use app\model\NovelModel;
use template\page\CusteomPage02;
class NovelService
{
@@ -21,10 +26,26 @@ class NovelService
*/
public $NovelModel;
/**
* ChapterModel
*
* @var ChapterModel
*/
public $ChapterModel;
/**
* NovelClicksModel
*
* @var NovelClicksModel
*/
public $NovelClicksModel;
public function __construct(SiteContext $SiteContext)
{
$this->SiteContext = $SiteContext;
$this->NovelModel = NovelModel::getInstance();
$this->ChapterModel = ChapterModel::getInstance();
$this->NovelClicksModel = NovelClicksModel::getInstance();
}
/**
@@ -190,73 +211,371 @@ class NovelService
protected $arrSortType = [
'day_desc' => 'daily',
'week_desc' => 'weekly',
'month_desc' => 'monthly',
'total_desc' => 'total',
'tuijian_desc' => 'total',
'all' => '全部排序',
'news' => '最新入库',
'tuijian' => '推荐',
'update' => '最新更新',
];
protected $arrStatus = [
'all' => NULL,
'all' => '所有',
'lianzai' => '连载中',
'wanjie' => '已完结',
];
protected $arrSex = [
'all' => '所有',
'man' => '男生',
'women' => '女生',
];
/**
* 获取小说数据
*
* @param array $arrParams
* @return void
* @return array
*/
public function getNovelData(array $arrParams): array
public function getNovelList(array $arrParams): array
{
// 提取参数
$intPage = (int)$arrParams['page'] ?? 0;
$intLimit = (int)$arrParams['limit'] ?? 10;
$intCount = (int)$arrParams['count'] ?? 10;
$strNCategory = $arrParams['n_category'] ?? '';
$strSortType = (string)$arrParams['sort_type'] ?? '';
$strNStatus = (string) $arrParams['n_status'] ?? '';
$strNSex = (string) $arrParams['n_sex'] ?? '';
$strKey = (string) $arrParams['key'] ?? '';
$strDiffKey = (string) $arrParams['diff_key'] ?? '';
$intCacheKey = (int)$arrParams['cache_life'] ?? 0;
echo $intPage;
print_r($arrParams);
exit;
$intCacheLifeTime = (int)$arrParams['cache_life'] ?? 0;
$arrFilter = [];
if ($intPage == 0) {
} else {
# n_category filter
if (!empty($strNCategory)) {
$strNCategory = CategoryModel::getZhByPinYin($strNCategory);
if ($strNCategory) {
$arrFilter['n_category'] = $strNCategory;
}
}
# n_status filter
if (!empty($strNStatus)) {
switch ($strNStatus) {
case 'lianzai':
$arrFilter['n_status'] = $this->arrStatus[$strNStatus];
break;
case 'wanjie':
$arrFilter['n_status'] = $this->arrStatus[$strNStatus];
break;
}
}
# n_category_sex_zh filter
if (!empty($strNSex)) {
switch ($strNSex) {
case 'man':
$arrFilter['n_category_sex_zh'] = $this->arrSex[$strNSex];
break;
case 'women':
$arrFilter['n_category_sex_zh'] = $this->arrSex[$strNSex];
break;
}
}
$arrSort = [];
# sort filter
if (!empty($strSortType)) {
switch ($strSortType) {
case 'news':
$arrSort['created_at'] = -1;
break;
case 'tuijian':
$arrSort['n_level'] = -1;
break;
case 'update':
$arrSort['n_update_time'] = -1;
break;
}
}
$strCacheKey = '';
$intLifeTime = 0;
# cache filter
if ($intCacheLifeTime > 0) {
$strCacheKey = sprintf(
'Service:getNovelList:%s:%s:%s:%s:%s',
$this->SiteContext->DomainModel->d_domain,
md5(serialize($arrFilter)),
md5(serialize($arrSort)),
$intCount,
$strDiffKey,
);
$intLifeTime = $intLifeTime;
}
$arrNovel = $this->NovelModel->findManyShuffledWithCache(
$arrFilter,
$intCount,
$arrSort,
$strCacheKey,
$intLifeTime,
);
return $arrNovel;
}
// 模拟数据处理(可以根据实际需求从数据库获取数据)
$data = [];
for ($i = 1; $i <= $arrParams['limit']; $i++) {
$data[] = [
'id' => $i,
'name' => "小说名称 {$i}",
'category' => $arrParams['n_category'] ?? '默认分类',
'status' => $arrParams['n_status'] ?? '连载中',
'order' => $arrParams['sort_type'] ?? '默认排序',
/**
* 获取小说分页数据
*
* @param array $arrParams
* @return array
*/
public function getNovelPager(array $arrParams): array
{
$arrParams = [
'page' => max((int)($arrParams['page'] ?? 1), 1),
'limit' => (int)($arrParams['limit'] ?? 10),
'n_category' => (string)($arrParams['n_category'] ?? ''),
'sort_type' => (string)($arrParams['sort_type'] ?? ''),
'n_status' => (string)($arrParams['n_status'] ?? ''),
'n_sex' => (string)($arrParams['n_sex'] ?? ''),
'diff_key' => (string)($arrParams['diff_key'] ?? ''),
'key' => (string)($arrParams['key'] ?? ''),
'cache_life' => (int)($arrParams['cache_life'] ?? 0),
'func' => (string)($arrParams['func']),
];
$intPage = $arrParams['page'];
$intLimit = $arrParams['limit'];
$strNCategory = $arrParams['n_category'];
$strSortType = $arrParams['sort_type'];
$strNStatus = $arrParams['n_status'];
$strNSex = $arrParams['n_sex'];
$strDiffKey = $arrParams['diff_key'];
$strKey = $arrParams['key'];
$intCacheLifeTime = $arrParams['cache_life'];
$Func = $arrParams['func'];
if ($intPage <= 0) {
$intPage = 1;
}
$arrFilter = [
'$and' => [],
];
# n_category filter
if (!empty($strNCategory)) {
$strNCategory = CategoryModel::getZhByPinYin($strNCategory);
if ($strNCategory) {
$arrFilter['$and'][] = ['n_category' => $strNCategory];
} else {
$arrParams['n_category'] = 'all';
}
}
# n_status filter
if (!empty($strNStatus)) {
switch ($strNStatus) {
case 'lianzai':
$arrFilter['$and'][] = ['n_status' => $this->arrStatus[$strNStatus]];
break;
case 'wanjie':
$arrFilter['$and'][] = ['n_status' => $this->arrStatus[$strNStatus]];
break;
default:
$arrParams['n_status'] = 'all';
break;
}
}
# n_category_sex_zh filter
if (!empty($strNSex)) {
switch ($strNSex) {
case 'man':
$arrFilter['$and'][] = ['n_category_sex_zh' => $this->arrSex[$strNSex]];
break;
case 'women':
$arrFilter['$and'][] = ['n_category_sex_zh' => $this->arrSex[$strNSex]];
break;
default:
$arrParams['n_sex'] = 'all';
break;
}
}
# n_category_sex_zh filter
if (!empty($strNSex)) {
$strKey = mb_substr($strKey, 0, 30);
$arrFilter['$and'][] = [
'$or' => [
['n_name' => ['$regex' => $strKey, '$options' => 'i']],
['n_author' => ['$regex' => $strKey, '$options' => 'i']],
['n_category' => ['$regex' => $strKey, '$options' => 'i']],
['n_description' => ['$regex' => $strKey, '$options' => 'i']],
]
];
$arrParams['key'] = $strKey;
}
// 分页数据
$p_data = [
'current_page' => $arrParams['page'],
'total_page' => 5,
'total_count' => 50,
$arrSort = [];
# sort filter
if (!empty($strSortType)) {
switch ($strSortType) {
case 'news':
$arrSort['created_at'] = -1;
break;
case 'tuijian':
$arrSort['n_level'] = -1;
break;
case 'update':
$arrSort['n_update_time'] = -1;
break;
default:
$arrParams['sort_type'] = 'all';
break;
}
}
$strCacheKey = '';
$intLifeTime = 0;
# cache filter
if ($intCacheLifeTime > 0) {
$strCacheKey = sprintf(
'Service:getNovelList:%s:%s:%s:%s:%s:%s',
$this->SiteContext->DomainModel->d_domain,
md5(serialize($arrFilter)),
md5(serialize($arrSort)),
$intPage,
$intLimit,
$strDiffKey,
);
$intLifeTime = $intLifeTime;
}
$arrNovel = $this->NovelModel->findPaginatedWithCache(
$arrFilter,
$intPage,
$intLimit,
$arrSort,
$strCacheKey,
$intLifeTime,
);
$arrPager = call_user_func([$this, $Func], $arrParams, $arrSort, $arrNovel);
$arrResult = [
'data' => $arrNovel['data'],
'p_data' => $arrPager,
];
return $arrResult;
}
/**
* 搜索定制分页
*
* @param array $arrFilter 查询条件字段
* @param array $arrSort 查询排序字段
* @param array $arrNovel 查询出来的数据
* @return array
*/
public function generateSearchPager(array $arrParams, array $arrSort, array $arrNovel): array
{
$strKey = $arrParams['key'];
$intPage = $arrNovel['page'];
$intLimit = $arrNovel['limit'];
$intButtomNum = 10;
$strBaseUrl = app(NovelService::class)->getNovelSearchUrl($strKey, '{page}');
$arrPaginator = CusteomPage02::generate($intPage, $arrNovel['total'], $intLimit, $strBaseUrl, $intButtomNum);
return $arrPaginator;
}
return [
'data' => $data,
'p_data' => $p_data,
];
/**
* 根据NId获取小说详情
*
* @param integer $intNId
* @return void
*/
public function getNovelByNId(int $intNId, int $intNForgeId = 0)
{
// 小说详情
$arrNovel = $this->NovelModel->getNovelByNId($intNId);
if( $intNForgeId>0){
foreach ($arrNovel['n_forge_novel'] as $arrForgeNovel) {
if ($arrForgeNovel['n_forge_id'] == $intNForgeId) {
$arrNovel['n_name'] = $arrForgeNovel['n_forge_title'];
break;
}
}
}
// 分类拼音
$arrNovel['n_category_pinyin'] = CategoryModel::getPinYin($arrNovel['n_category']);
$arrNovel['n_category_sex_en'] = $arrNovel['n_category_sex_en'] ?? 'man';
// 最后章节
$arrLatestChapter = $this->ChapterModel->getLatestChapterByNId($intNId);
// 第一章
$arrFirstChapter = $this->ChapterModel->getFirstChapterByNId($intNId, false);
$intFirstChapterSort = null;
if ($arrFirstChapter) {
$intFirstChapterSort = $arrFirstChapter['c_sort_num'];
}
$strFocus = '';
// 要点
if ($arrLatestChapter) {
$strFocus = strip_tags($arrLatestChapter['c_content']);
$intCount = mb_strlen($strFocus);
if ($intCount > 300) {
$strFocus = mb_substr($strFocus, 0, 300);
}
}
// 最近五个章节
$arrChapter = $this->ChapterModel->findMany([
'n_id' => $intNId,
'c_page' => 0,
], 10, ['c_sort_num' => -1]);
// 点击数
$arrNovelClicks = $this->NovelClicksModel->getStats($intNId);
// 推荐小说
$arrRelateveNId = array_column($arrNovel['n_related_novel'], 'n_id');
$arrRelateveNovel = $this->NovelModel->findMany(['n_id' => [
'$in' => $arrRelateveNId,
]], 10);
// 更新值
ConverterMovel::setVal([
'intPage' => 1,
'strNovelName' => $arrNovel['n_name'],
'strNovelAuthor' => $arrNovel['n_author'],
'strNovelCategoryName' => $arrNovel['n_category'],
'strNovelSex' => $arrNovel['n_category_sex_en'] == 'man' ? '男生' : '女生',
'strNovelStatus' => $arrNovel['n_status'],
'strNovelDescription' => $arrNovel['n_description'],
]);
return $arrNovel;
}
}

View File

@@ -2,11 +2,13 @@
namespace app\services;
use app\model\ConverterMovel;
use app\model\DomainModel;
use app\model\TemplatesModel;
use think\facade\Request;
use think\facade\Config;
use think\exception\HttpException;
use think\facade\View;
class SiteContext
{
@@ -69,4 +71,42 @@ class SiteContext
'view_path' => root_path(ltrim($this->TemplatesModel->t_path, '/')),
];
}
/**
* 初始化网站基本信息到全局替换类里
*
* @return void
*/
public function initCfg()
{
View::assign('Request', $this->Request);
View::assign('DomainModel', $this->DomainModel);
View::assign('TemplatesModel', $this->TemplatesModel);
ConverterMovel::setVal([
'strSiteName' => $this->DomainModel->d_name,
'strSiteDomain' => $this->DomainModel->d_domain,
'strSiteKeywords' => $this->DomainModel->d_keywords,
'strSiteDescription' => $this->DomainModel->d_description,
'strIndexTitle' => $this->DomainModel->d_index_title,
'strIndexKeywords' => $this->DomainModel->d_index_keywords,
'strIndexDescription' => $this->DomainModel->d_index_description,
]);
}
/**
* 变量转换
*
* @param string $strCode
* @return string
*/
public function converterTemplate(string $strCode): string
{
$strTitleTemplate = $this->DomainModel->getFomartSubject($strCode);
return ConverterMovel::convert($strTitleTemplate);
}
}