增加游戏模块

This commit is contained in:
云泽网
2025-05-20 23:28:47 +08:00
parent 813619212c
commit d148d818a1
39 changed files with 5173 additions and 227 deletions
+242
View File
@@ -0,0 +1,242 @@
<?php
/**
* 游戏下载控制器
*/
namespace app\index\controller;
use app\index\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\index\model\Resources\Resources;
use app\index\model\Resources\ResourcesCategory;
use app\index\model\Attachments;
class GameController extends BaseController
{
// 游戏列表页
public function list()
{
// 获取分类ID
$cateId = Request::param('cate/d', 0);
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1]
];
if ($cateId > 0) {
$where[] = ['cate', '=', $cateId];
}
// 获取游戏列表
$games = Resources::where($where)
->order('id DESC')
->paginate([
'list_rows' => 10,
'query' => Request::param()
]);
// 获取分类信息
$category = null;
if ($cateId > 0) {
$category = ResourcesCategory::where('id', $cateId)
->where('delete_time', null)
->where('status', 1)
->find();
}
// 获取所有分类
$categories = ResourcesCategory::where('delete_time', null)
->where('status', 1)
->select()
->toArray();
// 将变量传递给视图
View::assign([
'games' => $games,
'category' => $category,
'categories' => $categories
]);
return View::fetch('list');
}
// 游戏详情页
public function detail()
{
$id = Request::param('id/d', 0);
$game = Resources::where('id', $id)->find();
if (!$game) {
return json(['code' => 0, 'msg' => '游戏不存在或已被删除']);
}
// 如果size没有,从附件表中获取
if (empty($game['size']) && !empty($game['fileurl'])) {
$attachment = Attachments::where('src', $game['fileurl'])
->find();
if ($attachment && !empty($attachment['size'])) {
$size = $attachment['size'];
// 转换文件大小为合适的单位
if ($size >= 1073741824) { // 1GB = 1024MB = 1024*1024KB = 1024*1024*1024B
$game['size'] = round($size / 1073741824, 2) . 'GB';
} elseif ($size >= 1048576) { // 1MB = 1024KB = 1024*1024B
$game['size'] = round($size / 1048576, 2) . 'MB';
} else {
$game['size'] = round($size / 1024, 2) . 'KB';
}
}
}
// 获取分类名称
$cateName = ResourcesCategory::where('id', $game['cate'])
->value('name');
// 获取上一个和下一个游戏
$prevGame = Resources::where('id', '<', $id)
->where('delete_time', null)
->where('status', 1)
->order('id DESC')
->find();
$nextGame = Resources::where('id', '>', $id)
->where('delete_time', null)
->where('status', 1)
->order('id ASC')
->find();
// 获取相关游戏(同分类的其他游戏)
$relatedGames = Db::table('yz_resources')
->alias('g')
->join('yz_resources_category c', 'g.cate = c.id')
->where('g.cate', $game['cate'])
->where('g.id', '<>', $id)
->where('g.delete_time', null)
->where('g.status', 1)
->field([
'g.id',
'g.title',
'g.desc',
'IF(g.icon IS NULL OR g.icon = "", c.icon, g.icon) as icon'
])
->order('g.id DESC')
->limit(3)
->select()
->toArray();
// 如果是 AJAX 请求,返回 JSON 数据
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'game' => $game,
'cateName' => $cateName,
'prevGame' => $prevGame,
'nextGame' => $nextGame,
'relatedGames' => $relatedGames
]
]);
}
// 非 AJAX 请求返回视图
View::assign([
'game' => $game,
'cateName' => $cateName,
'prevGame' => $prevGame,
'nextGame' => $nextGame,
'relatedGames' => $relatedGames
]);
return View::fetch('detail');
}
// 游戏下载
public function downloadurl()
{
if (!Request::isAjax()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::param('id/d', 0);
// 获取游戏信息
$game = Resources::where('id', $id)
->where('delete_time', null)
->find();
if (!$game) {
return json(['code' => 0, 'msg' => '游戏不存在']);
}
// 更新下载次数
$result = Resources::where('id', $id)
->where('delete_time', null)
->inc('downloads', 1)
->update();
if ($result) {
return json([
'code' => 1,
'msg' => '下载成功',
'data' => [
'url' => $game['url']
]
]);
} else {
return json(['code' => 0, 'msg' => '下载失败']);
}
}
// 获取访问统计
public function viewStats()
{
$id = Request::param('id/d', 0);
// 获取总访问量
$totalViews = Resources::where('id', $id)
->value('views');
return json([
'code' => 1,
'data' => [
'total' => $totalViews
]
]);
}
/**
* 更新游戏访问次数
*/
public function updateViews()
{
if (!Request::isPost()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::post('id');
if (!$id) {
return json(['code' => 0, 'msg' => '参数错误']);
}
try {
// 更新访问次数
$game = Resources::where('id', $id)->find();
if (!$game) {
return json(['code' => 0, 'msg' => '游戏不存在']);
}
// 更新访问次数
Resources::where('id', $id)->inc('views')->update();
// 获取更新后的访问次数
$newViews = Resources::where('id', $id)->value('views');
return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
}
+71
View File
@@ -270,4 +270,75 @@ class IndexController extends BaseController
'categories' => $categoryData
]);
}
/**
* 获取游戏下载列表
*/
public function gameList()
{
// 获取游戏分类(顶级分类id为8的子分类)
$categories = ResourcesCategory::where('cid', 8)
->where('delete_time', null)
->select()
->toArray();
// 组装分类数据
$categoryData = [];
$categoryImageMap = [];
$programsByCategory = [];
foreach ($categories as $category) {
$categoryData[] = [
'id' => $category['id'],
'name' => $category['name'],
'image' => $category['image'] ?? ''
];
// 获取每个分类下的游戏,限制8条
$programs = Resources::where('cate', $category['id'])
->where('delete_time', null)
->where('status', 1)
->order('id', 'desc')
->field('id, cate, title, desc, downloads, create_time, icon, views, uploader, number, url, code')
->limit(8)
->select()
->toArray();
// 处理游戏数据
foreach ($programs as &$program) {
// 如果没有图标,使用分类图片
if (empty($program['icon']) && !empty($category['image'])) {
$program['icon'] = $category['image'];
}
// 格式化时间
$program['create_time'] = date('Y-m-d H:i:s', $program['create_time']);
}
unset($program);
$programsByCategory[$category['id']] = $programs;
}
// 合并所有分类的游戏
$allPrograms = [];
foreach ($programsByCategory as $programs) {
$allPrograms = array_merge($allPrograms, $programs);
}
// 按上传时间排序
usort($allPrograms, function($a, $b) {
return strtotime($b['create_time']) - strtotime($a['create_time']);
});
// 只取最新的8条
$allPrograms = array_slice($allPrograms, 0, 8);
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'games' => $allPrograms,
'categories' => $categoryData
]
]);
}
}
+109
View File
@@ -84,6 +84,27 @@
</div>
</div>
<!-- 游戏下载模块 -->
<div class="core-block core-module" id="gameDownload" style="order: 3;">
<div class="module-header">
<div>
<div class="ModuleTitle_titleWrapper">
<h3 class="ModuleTitle_title">游戏下载</h3>
<div class="tab-container">
<div class="tab-header">
<div class="tab-item active" data-tab="all">全部</div>
<!-- 分类标签将通过JavaScript动态加载 -->
</div>
</div>
</div>
</div>
<div class="more-btn">更多</div>
</div>
<div class="product-list" id="gameDownloadList">
<!-- 游戏将通过JavaScript动态加载 -->
</div>
</div>
</div>
</main>
@@ -204,6 +225,32 @@
});
}
// 加载游戏下载
function loadGames() {
fetch('/index/index/gameList')
.then(response => response.json())
.then(result => {
if (result.code === 0) {
// 渲染分类标签
if (result.data.categories) {
renderCategoryTabs(result.data.categories, 'gameDownload');
}
// 渲染游戏列表
if (result.data.games && result.data.games.length > 0) {
renderGames(result.data.games, 'gameDownloadList');
} else {
showNoData('gameDownloadList');
}
} else {
showNoData('gameDownloadList');
}
})
.catch(error => {
console.error('请求失败:', error);
showError('gameDownloadList');
});
}
// 显示无数据提示
function showNoData(containerId) {
document.getElementById(containerId).innerHTML = '<div class="no-data">暂无数据</div>';
@@ -266,6 +313,9 @@
case 'programDownload':
loadCategoryPrograms(selectedCategoryId, 'programDownloadList');
break;
case 'gameDownload':
loadCategoryGames(selectedCategoryId, 'gameDownloadList');
break;
}
});
});
@@ -506,11 +556,70 @@
});
}
// 渲染游戏列表
function renderGames(games, containerId) {
const container = document.getElementById(containerId);
if (!container) return;
let html = '';
if (Array.isArray(games)) {
games.forEach(game => {
html += createGameHtml(game);
});
}
container.innerHTML = html || '<div class="no-data">暂无数据</div>';
}
// 创建游戏HTML
function createGameHtml(game) {
if (!game) return '';
return `
<div class="opencourse product-item" onclick="window.open('/index/game/detail?id=${game.id || ''}', '_blank')">
<div class="video">
<img src="${game.icon || '/static/images/default-game.png'}" alt="" class="cover">
</div>
<div class="introduction">
<div class="title">${game.title || '无标题'}</div>
<div class="publishdate">${game.create_time || ''}</div>
</div>
<div class="bottom">
<div class="views"><i class="fa-solid fa-eye"></i><span style="margin-left: 5px;">${game.views || 0}</span></div>
<div class="author"><i class="fa-regular fa-user"></i><span style="margin-left: 5px;">${game.uploader || '未知作者'}</span></div>
</div>
</div>
`;
}
// 加载分类游戏
function loadCategoryGames(categoryId, containerId) {
fetch('/index/index/gameList')
.then(response => response.json())
.then(result => {
if (result.code === 0) {
if (categoryId === 'all') {
renderGames(result.data.games, containerId);
} else {
const filteredGames = result.data.games.filter(game => game.cate == categoryId);
renderGames(filteredGames, containerId);
}
} else {
showNoData(containerId);
}
})
.catch(error => {
console.error('请求失败:', error);
showError(containerId);
});
}
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
loadWebArticles();
loadTechArticles();
loadResources();
loadPrograms();
loadGames();
});
</script>
+414
View File
@@ -0,0 +1,414 @@
{include file="component/head" /}
{include file="component/header-simple" /}
<div class="main">
<div class="location">
<div class="container">
<div class="location-item">
<a href="/">首页</a>
<span>></span>
<a href="/index/game/list" id="cateLink"><?php echo $cateName; ?></a>
</div>
</div>
</div>
<div class="game-detail">
<div class="game-info">
<div class="game-header">
<h1 class="game-title"><?php echo $game['title']; ?></h1>
<div class="game-meta">
<span class="game-category"><?php echo $cateName; ?></span>
<span class="game-views"><i class="fa-solid fa-eye"></i> <?php echo $game['views']; ?> 次浏览</span>
<span class="game-downloads"><i class="fa-solid fa-download"></i> <span
id="gameDownloads"><?php echo $game['downloads']; ?></span> 次下载</span>
</div>
</div>
<div class="game-content">
<div class="game-cover">
<img src="<?php echo $game['icon'] ?: '/static/images/default-game.png'; ?>"
alt="<?php echo $game['title']; ?>">
</div>
<div class="game-desc">
<?php echo $game['content']; ?>
</div>
</div>
<div class="game-actions">
<div style="display: flex;gap: 30px;}">
<button id="downloadBtn" class="btn btn-primary">
<i class="fa-solid fa-download"></i> 立即下载
</button>
<button id="codeBtn" class="codebtn">
<i class="fa-solid fa-download"></i> 分享码:<?php echo $game['code']; ?>
</button>
</div>
</div>
</div>
<!-- 相关游戏 -->
<?php if (!empty($relatedGames)): ?>
<div class="related-games">
<h3>相关游戏</h3>
<div class="game-list">
<?php foreach ($relatedGames as $related): ?>
<div class="game-item"
onclick="window.location.href='/index/game/detail?id=<?php echo $related['id']; ?>'">
<div class="game-cover">
<img src="<?php echo $related['icon'] ?: '/static/images/default-game.png'; ?>"
alt="<?php echo $related['title']; ?>">
</div>
<div class="game-info">
<h4 class="game-title"><?php echo $related['title']; ?></h4>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
</div>
</div>
<!-- 返回顶部按钮 -->
<div class="go-to-top" id="goToTop">
<i class="layui-icon layui-icon-top"></i>
</div>
{include file="component/footer" /}
<script>
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function () {
// 获取游戏ID
const gameId = new URLSearchParams(window.location.search).get('id');
if (!gameId) {
alert('游戏ID不存在');
return;
}
// 更新访问次数
updateGameViews(gameId);
// 下载功能
const downloadBtn = document.getElementById('downloadBtn');
if (downloadBtn) {
downloadBtn.addEventListener('click', function () {
fetch('/index/game/downloadurl?id=' + gameId, {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
if (data.code === 1) {
const downloadsElement = document.getElementById('gameDownloads');
let downloads = parseInt(downloadsElement.textContent);
downloadsElement.textContent = downloads + 1;
// 直接使用返回的URL
if (data.data && data.data.url) {
window.location.href = data.data.url;
} else {
alert('下载地址不存在');
}
} else {
alert('下载失败:' + data.msg);
}
})
.catch(error => {
console.error('下载请求失败:', error);
alert('下载请求失败,请稍后重试');
});
});
}
//复制分享码
const codeBtn = document.getElementById('codeBtn');
if (codeBtn) {
codeBtn.addEventListener('click', function() {
const code = '{$game.code}';
if (code) {
// 创建一个临时输入框
const tempInput = document.createElement('input');
tempInput.value = code;
document.body.appendChild(tempInput);
tempInput.select();
try {
// 尝试使用传统的复制方法
document.execCommand('copy');
layer.msg('分享码已复制到剪贴板');
} catch (err) {
console.error('复制失败:', err);
layer.msg('复制失败,请手动复制');
} finally {
// 移除临时输入框
document.body.removeChild(tempInput);
}
} else {
layer.msg('分享码不存在');
}
});
}
// 返回顶部功能
const goToTop = document.getElementById('goToTop');
// 监听滚动事件
window.addEventListener('scroll', function () {
if (window.pageYOffset > 300) {
goToTop.classList.add('show');
} else {
goToTop.classList.remove('show');
}
});
// 点击返回顶部
goToTop.addEventListener('click', function () {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
});
// 更新游戏访问次数
function updateGameViews(gameId) {
fetch('/index/game/updateViews', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
},
body: 'id=' + gameId
})
.then(response => response.json())
.then(result => {
if (result.code === 1) {
const viewsElement = document.querySelector('.game-views');
if (viewsElement) {
viewsElement.innerHTML = `<i class="fa-solid fa-eye"></i> ${result.data.views} 次浏览`;
}
}
})
.catch(error => {
console.error('更新访问次数失败:', error);
});
}
</script>
<style>
.location {
max-width: 1000px;
margin: 30px auto;
}
.game-detail {
max-width: 1000px;
margin: 30px auto;
padding: 50px;
background: #fff;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
.game-header {
margin-bottom: 30px;
border-bottom: 1px solid #eee;
padding-bottom: 20px;
}
.game-title {
font-size: 28px;
font-weight: 700;
color: #333;
margin-bottom: 15px;
line-height: 1.4;
}
.game-meta {
display: flex;
flex-wrap: wrap;
gap: 20px;
color: #666;
font-size: 14px;
}
.game-meta span {
display: flex;
align-items: center;
}
.game-meta i {
margin-right: 5px;
}
.game-content {
line-height: 1.8;
color: #333;
font-size: 16px;
margin-bottom: 30px;
}
.game-cover {
margin-bottom: 20px;
}
.game-cover img {
width: 100%;
height: 300px;
object-fit: cover;
border-radius: 8px;
}
.game-desc {
margin-bottom: 30px;
}
.game-actions {
display: flex;
justify-content: center;
gap: 40px;
margin: 30px 0;
padding: 20px 0;
border-top: 1px solid #eee;
border-bottom: 1px solid #eee;
}
.btn {
/* background: #f57005; */
color: #fff;
padding: 15px 30px;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
}
.btn:hover {
/* background: #e66600; */
transform: translateY(-2px);
}
.codebtn{
color:#0d6efd;
padding: 15px 30px;
border-radius: 8px;
border: 1px solid #0d6efd;
cursor: pointer;
transition: all 0.3s ease;
background-color: #fff;
}
.codebtn:hover {
transform: translateY(-2px);
}
.related-games {
margin: 40px 0;
}
.related-title {
font-size: 20px;
font-weight: 600;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}
.game-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.game-item {
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.3s;
}
.game-item:hover {
transform: translateY(-5px);
}
.game-item a {
text-decoration: none;
color: inherit;
}
.game-cover img {
width: 100%;
height: 150px;
object-fit: cover;
}
.game-info {
padding: 10px;
}
.game-title {
font-size: 16px;
font-weight: 600;
margin-bottom: 5px;
color: #333;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.go-to-top {
position: fixed;
right: 30px;
bottom: 30px;
width: 40px;
height: 40px;
background: #f57005;
color: #fff;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
z-index: 1000;
}
.go-to-top.show {
opacity: 1;
visibility: visible;
}
.go-to-top:hover {
background: #e66600;
transform: translateY(-3px);
}
@media (max-width: 768px) {
.game-title {
font-size: 24px;
}
.game-list {
grid-template-columns: repeat(1, 1fr);
}
.game-meta {
gap: 10px;
}
.go-to-top {
right: 20px;
bottom: 20px;
width: 36px;
height: 36px;
}
}
.location-item a {
color: #000 !important;
}
</style>
{include file="component/foot" /}