This commit is contained in:
Default
2025-03-27 23:10:03 +08:00
parent 81b573c296
commit dd0d52f693
45 changed files with 2502 additions and 1279 deletions

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace app\model;
use DateTime;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
*/
class CategoryModel extends MongoModel
{
protected $strCollection = 'category';
static $arrCategory = [
1 => '玄幻修真',
2 => '军史小说',
3 => '网游小说',
4 => '科幻小说',
5 => '重生穿越',
6 => '都市小说',
7 => '灵异小说',
8 => '言情小说',
9 => '其他小说',
];
static $arrMan = [
1 => '玄幻修真',
2 => '军史小说',
3 => '网游小说',
4 => '科幻小说',
];
static $arrWomen = [
5 => '重生穿越',
6 => '都市小说',
7 => '灵异小说',
8 => '言情小说',
9 => '其他小说',
];
static public function getCategory():array
{
return self::$arrCategory;
}
static public function getManCategory():array
{
return self::$arrMan;
}
static public function getWomenCategory():array
{
return self::$arrWomen;
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace app\model;
use DateTime;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
*/
class ChapterModel extends MongoModel
{
protected $strCollection = 'chapter';
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace app\model;
use db\mongo\MongoBase;
use ddl\DDLManage;
use MongoDB\Collection;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
*/
abstract class MongoModel
{
/**
* collection name
*
* @var string
*/
protected $strCollection;
/**
* Undocumented variable
*
* @var MongoBase
*/
private $MongoBase;
/**
* Undocumented function
*/
public function __construct()
{
DDLManage::load('MongoBase');
$this->MongoBase = MongoBase::getInstance();
}
/**
* Undocumented function
*
* @return MongoBase
*/
public function getMongoBase(): MongoBase
{
return $this->MongoBase;
}
/**
* Undocumented function
*
* @return Collection
*/
public function getCol(): Collection
{
return $this->getMongoBase()->getDb()->selectCollection($this->getCollectionName());
}
/**
* Undocumented function
*
* @return string
*/
public function getCollectionName(): string
{
return $this->strCollection;
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace app\model;
use DateTime;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
*/
class NovelClicksModel extends MongoModel
{
protected $strCollection = 'novel_clicks';
/**
* Undocumented function
*
* @param integer $intNId
* @return void
*/
public function addStatis(int $intNId)
{
$NovelModel = new NovelModel;
$Novel = $NovelModel->getCol()->findOne([
"n_id" => $intNId,
]);
if (empty($Novel)) {
return false;
}
$Now = new DateTime();
$strDailyDate = $Now->format('Y-m-d');
$strWeeklyDate = $Now->modify('monday this week')->format('Y-m-d');
$MonthlyDate = $Now->format('Y-m-01');
$arrClickTypes = [
'daily' => $strDailyDate,
'weekly' => $strWeeklyDate,
'monthly' => $MonthlyDate,
'total' => null,
];
foreach ($arrClickTypes as $strType => $strDate) {
$this->getCol()->updateOne(
[
'n_id' => $intNId,
'nc_date' => $strDate,
'nc_type' => $strType,
],
[
'$inc' => ['nc_clicks' => 1],
'$set' => ['n_category' => $Novel->og_novel_category]
],
[
'upsert' => true
]
);
}
return true;
}
}

View File

@@ -0,0 +1,329 @@
<?php
declare(strict_types=1);
namespace app\model;
use DateTime;
use Exception;
use storage\StorageCore;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
*/
class NovelModel extends MongoModel
{
/**
* Undocumented variable
*
* @var string
*/
protected $strCollection = 'novel';
/**
* 根据关键词数组更新小说推荐等级
* @param array $arrKeywords 关键词数组,例如 ["斗罗", "遮天", "凡人"]
* @param int $intLevel 推荐等级1~9例如 3
* @return array 更新结果
*/
public function updateByKeywords(array $arrKeywords, int $intLevel)
{
if (empty($arrKeywords)) {
return false;
}
try {
$arrConditions = array_map(function ($strKeyword) {
return ['og_title' => ['$regex' => trim($strKeyword), '$arrOptions' => 'i']];
}, $arrKeywords);
$UpdateResult = $this->getCol()->updateMany(
['$or' => $arrConditions],
['$set' => ['n_level' => $intLevel]]
);
$intUpdateNum = $UpdateResult->getModifiedCount();
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Undocumented function
*
* @param string $strSign
* @param integer $intCount
* @param integer $intType
* @param integer $intLevel
* @return array
*/
public function getHighLevelNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0): array
{
try {
$strKey = sprintf('%s:PC:HOME:FIRST', $strSign);
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
if ($intType == 1) {
$arrCategory = array_values(CategoryModel::getManCategory());
$arrFilter = [
'og_novel_category' => ['$in' => $arrCategory]
];
} elseif ($intType == 2) {
$arrCategory = array_values(CategoryModel::getWomenCategory());
$arrFilter = [
'og_novel_category' => ['$in' => $arrCategory]
];
} else {
$arrFilter = [];
}
$arrOptions = [
'sort' => ['n_level' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
'og_novel_book_name' => 1,
'og_novel_pin_yin' => 1,
'n_cover_path' => 1,
'n_novel_author' => 1,
'og_novel_category' => 1,
'og_description' => 1,
],
];
$Cursor = $this->getCol()->find($arrFilter, $arrOptions);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
}
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
/**
* Undocumented function
*
* @param string $strSign
* @param integer $intCount
* @return array
*/
public function getFinishedNovelOnCache(string $strSign, int $intCount = 10): array
{
try {
$strKey = sprintf('%s:PC:HOME:FINISHED', $strSign);
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [
'og_novel_status' => "已完结",
];
$arrOptions = [
'sort' => ['og_novel_update_time' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
'og_novel_book_name' => 1,
'og_novel_pin_yin' => 1,
'n_cover_path' => 1,
'og_novel_author' => 1,
'og_novel_category' => 1,
'og_description' => 1,
],
];
$Cursor = $this->getCol()->find($arrFilter, $arrOptions);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
}
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
/**
* Undocumented function
*
* @param string $strSign
* @param integer $intCount
* @param integer $intType
* @param integer $intLevel
* @return array
*/
public function getSexNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0): array
{
try {
$strKey = sprintf('%s:PC:HOME:SIX:%s', $strSign, $intType);
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
if ($intType == 1) {
$arrCategory = array_values(CategoryModel::getManCategory());
$arrFilter = [
'og_novel_category' => ['$in' => $arrCategory]
];
} elseif ($intType == 2) {
$arrCategory = array_values(CategoryModel::getWomenCategory());
$arrFilter = [
'og_novel_category' => ['$in' => $arrCategory]
];
} else {
$arrFilter = [];
}
$arrOptions = [
'sort' => ['og_novel_update_time' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
'og_novel_book_name' => 1,
'og_novel_pin_yin' => 1,
'n_cover_path' => 1,
'n_novel_author' => 1,
'og_novel_category' => 1,
'og_description' => 1,
],
];
$Cursor = $this->getCol()->find($arrFilter, $arrOptions);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
}
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
/**
* Undocumented function
*
* @param string $strSign
* @param integer $intCount
* @param integer $intType
* @param string strDateType
* * daily 天
* * weekly 周
* * monthly 月
* * total 总
* @return array
*/
public function getRankNovelOnCache(string $strSign, int $intCount = 10, int $intType = 0, $strDateType = 'daily'): array
{
try {
$strKey = sprintf('%s:PC:HOME:SIX:%s:TYPE:%s', $strSign, $intType, $strDateType);
$intLifeTime = 24 * 3600;
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$arrFilter = [
'nc_type' => $strDateType,
];
if ($intType == 1) {
$arrCategory = array_values(CategoryModel::getManCategory());
$arrFilter['n_category'] = ['$in' => $arrCategory];
} elseif ($intType == 2) {
$arrCategory = array_values(CategoryModel::getWomenCategory());
$arrFilter['n_category'] = ['$in' => $arrCategory];
}
$arrOptions = [
'sort' => ['nc_clicks' => -1],
'limit' => $intCount * 3,
'projection' => [
'n_id' => 1,
],
];
$NovelClicksModel = new NovelClicksModel;
$Cursor = $NovelClicksModel->getCol()->find($arrFilter, $arrOptions);
$arrNId = [];
foreach ($Cursor as $Doc) {
$arrNId[] = $Doc['n_id'];
}
$Cursor = $this->getCol()->find(
['n_id' => ['$in' => $arrNId]],
[
'projection' => [
'n_id' => 1,
'og_novel_book_name' => 1,
'og_novel_pin_yin' => 1,
'n_cover_path' => 1,
'n_novel_author' => 1,
'og_novel_category' => 1,
'og_description' => 1,
],
]
);
$arrResult = iterator_to_array($Cursor);
shuffle($arrResult);
$arrResult = array_slice($arrResult, 0, $intCount);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
}
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
}