This commit is contained in:
云泽网
2025-05-20 23:32:46 +08:00
20 changed files with 2658 additions and 283 deletions
+19 -1
View File
@@ -142,6 +142,16 @@ class ArticlesController extends BaseController
$id = Request::param('id/d', 0);
// 检查文章是否存在
$article = Articles::where('id', $id)
->where('delete_time', null)
->where('status', 2)
->find();
if (!$article) {
return json(['code' => 0, 'msg' => '文章不存在或已被删除']);
}
// 更新点赞数
$result = Articles::where('id', $id)
->where('delete_time', null)
@@ -149,7 +159,15 @@ class ArticlesController extends BaseController
->update();
if ($result) {
return json(['code' => 1, 'msg' => '点赞成功']);
// 返回更新后的点赞数
$newLikes = $article['likes'] + 1;
return json([
'code' => 1,
'msg' => '点赞成功',
'data' => [
'likes' => $newLikes
]
]);
} else {
return json(['code' => 0, 'msg' => '点赞失败']);
}
+43 -28
View File
@@ -4,10 +4,11 @@
*/
namespace app\index\controller;
use app\index\controller\BaseController;
use think\Response;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\index\model\Resources\Resources;
use app\index\model\Resources\Resources;
use app\index\model\Resources\ResourcesCategory;
use app\index\model\Attachments;
@@ -67,16 +68,16 @@ class ProgramController extends BaseController
{
$id = Request::param('id/d', 0);
$program = Resources::where('id', $id)->find();
if (!$program) {
return json(['code' => 0, 'msg' => '程序不存在或已被删除']);
}
// 如果size没有,从附件表中获取
if (empty($program['size']) && !empty($program['fileurl'])) {
$attachment = Attachments::where('src', $program['fileurl'])
->find();
if ($attachment && !empty($attachment['size'])) {
$size = $attachment['size'];
// 转换文件大小为合适的单位
@@ -90,26 +91,37 @@ class ProgramController extends BaseController
}
}
// 如果size存在,确保转换为合适的单位
if (!empty($program['size']) && is_numeric($program['size'])) {
$size = $program['size'];
if ($size >= 1073741824) {
$program['size'] = round($size / 1073741824, 2) . 'GB';
} elseif ($size >= 1048576) {
$program['size'] = round($size / 1048576, 2) . 'MB';
} else {
$program['size'] = round($size / 1024, 2) . 'KB';
}
}
// 获取分类名称
$cateName = ResourcesCategory::where('id', $program['cate'])
->value('name');
// 获取上一个和下一个程序
$prevProgram = Resources::where('id', '<', $id)
->where('delete_time', null)
->where('status', 1)
->order('id DESC')
->find();
$nextProgram = Resources::where('id', '>', $id)
->where('delete_time', null)
->where('status', 1)
->order('id ASC')
->find();
// 获取相关程序(同分类的其他程序)
$relatedPrograms = Db::table('yz_resources')
->alias('p')
$relatedPrograms = Resources::alias('p')
->join('yz_resources_category c', 'p.cate = c.id')
->where('p.cate', $program['cate'])
->where('p.id', '<>', $id)
@@ -119,7 +131,7 @@ class ProgramController extends BaseController
'p.id',
'p.title',
'p.desc',
'IF(p.icon IS NULL OR p.icon = "", c.icon, p.icon) as icon'
'COALESCE(p.icon, c.icon) as icon'
])
->order('p.id DESC')
->limit(3)
@@ -140,7 +152,7 @@ class ProgramController extends BaseController
]
]);
}
// 非 AJAX 请求返回视图
View::assign([
'program' => $program,
@@ -156,34 +168,37 @@ class ProgramController extends BaseController
// 程序下载
public function download()
{
if (!Request::isAjax()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::param('id/d', 0);
$program = Resources::where('id', $id)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$program) {
return json(['code' => 0, 'msg' => '程序不存在或已被删除']);
}
// 更新下载次数
$result = Resources::where('id', $id)
->where('delete_time', null)
->inc('downloads', 1)
->update();
Resources::where('id', $id)->inc('downloads')->update();
if ($result) {
return json(['code' => 1, 'msg' => '下载成功']);
} else {
return json(['code' => 0, 'msg' => '下载失败']);
}
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'fileurl' => $program['fileurl']
]
]);
}
// 获取访问统计
public function viewStats()
{
$id = Request::param('id/d', 0);
// 获取总访问量
$totalViews = Resources::where('id', $id)
->value('views');
return json([
'code' => 1,
'data' => [
@@ -215,10 +230,10 @@ class ProgramController extends BaseController
// 更新访问次数
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()]);
@@ -0,0 +1,53 @@
<?php
namespace app\index\controller;
use think\facade\Request;
use think\facade\Filesystem;
use think\Response;
use app\index\model\Resources\Resources;
class StorageController extends BaseController
{
/**
* 处理文件下载
* @return \think\Response
*/
public function download()
{
// 获取请求的文件路径
$path = Request::pathinfo();
// 移除 'storage/' 前缀
$path = str_replace('storage/', '', $path);
// 构建完整的文件路径
$filePath = public_path() . 'storage/' . $path;
// 检查文件是否存在
if (!file_exists($filePath)) {
return Response::create('文件不存在', 'html', 404);
}
// 获取文件信息
$fileInfo = pathinfo($filePath);
$fileName = $fileInfo['basename'];
$fileSize = filesize($filePath);
$fileType = mime_content_type($filePath);
// 设置响应头
$headers = [
'Content-Type' => $fileType,
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
'Content-Length' => $fileSize,
'Cache-Control' => 'no-cache, must-revalidate',
'Pragma' => 'no-cache',
'Expires' => '0'
];
// 读取文件内容
$content = file_get_contents($filePath);
// 返回文件下载响应
return Response::create($content, 'file', 200, $headers);
}
}
+96 -47
View File
@@ -30,12 +30,12 @@
</div>
<div class="article-actions">
<div class="action-item like-btn">
<div class="action-item like-btn" id="likeBtn">
<i class="fa fa-thumbs-up"></i>
<span class="action-text">点赞</span>
<span class="action-count" id="articleLikes">0</span>
</div>
<div class="action-item share-btn">
<div class="action-item share-btn" id="shareBtn">
<i class="fa fa-share-alt"></i>
<span class="action-text">分享</span>
</div>
@@ -432,31 +432,31 @@
function renderArticleDetail(data) {
// 渲染分类链接
document.getElementById('cateLink').textContent = data.cateName;
// 渲染文章标题
document.getElementById('articleTitle').textContent = data.article.title;
// 渲染文章元信息
document.getElementById('articleAuthor').textContent = data.article.author;
document.getElementById('articleDate').textContent = formatDate(data.article.create_time);
document.getElementById('articleViews').textContent = data.article.views;
// 渲染文章内容
document.getElementById('articleContent').innerHTML = data.article.content;
// 渲染标签
const tagsContainer = document.getElementById('articleTags');
if (data.article.tags && data.article.tags.length > 0) {
tagsContainer.innerHTML = data.article.tags.map(tag =>
tagsContainer.innerHTML = data.article.tags.map(tag =>
`<span class="tag-item">${tag}</span>`
).join('');
} else {
tagsContainer.innerHTML = '<span class="no-tags">暂无标签</span>';
}
// 渲染点赞数
document.getElementById('articleLikes').textContent = data.article.likes || 0;
// 渲染上一篇
const prevArticle = document.getElementById('prevArticle');
if (data.prevArticle) {
@@ -468,7 +468,7 @@
} else {
prevArticle.innerHTML = '<span class="disabled"><i class="fa fa-arrow-left"></i> 没有上一篇了</span>';
}
// 渲染下一篇
const nextArticle = document.getElementById('nextArticle');
if (data.nextArticle) {
@@ -480,7 +480,7 @@
} else {
nextArticle.innerHTML = '<span class="disabled">没有下一篇了 <i class="fa fa-arrow-right"></i></span>';
}
// 渲染相关文章
const relatedArticles = document.getElementById('relatedArticles');
if (data.relatedArticles && data.relatedArticles.length > 0) {
@@ -503,7 +503,7 @@
}
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
// 获取文章ID
const articleId = new URLSearchParams(window.location.search).get('id');
if (!articleId) {
@@ -518,12 +518,12 @@
}
})
.then(response => {
console.log('Response status:', response.status);
console.log('Response headers:', response.headers);
// console.log('Response status:', response.status);
// console.log('Response headers:', response.headers);
return response.json();
})
.then(result => {
console.log('API response:', result);
// console.log('API response:', result);
if (result.code === 1) {
renderArticleDetail(result.data);
// 更新访问次数
@@ -543,44 +543,69 @@
});
// 点赞功能
const likeBtn = document.querySelector('.like-btn');
const likeBtn = document.getElementById('likeBtn');
if (likeBtn) {
likeBtn.addEventListener('click', function() {
// 检查是否已经点赞
if (likeBtn.classList.contains('liked')) {
likeBtn.style.pointerEvents = 'none'; // 禁用点击
likeBtn.style.cursor = 'default'; // 改变鼠标样式
return;
}
likeBtn.addEventListener('click', function () {
// 立即禁用按钮,防止重复点击
likeBtn.style.pointerEvents = 'none';
likeBtn.style.cursor = 'default';
fetch('/index/articles/like?id=' + articleId, {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data.code === 1) {
const countElement = this.querySelector('.action-count');
let count = parseInt(countElement.textContent);
countElement.textContent = count + 1;
this.classList.add('liked');
this.style.color = '#f57005';
} else {
alert('点赞失败:' + data.msg);
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.catch(error => {
console.error('点赞请求失败:', error);
});
.then(response => response.json())
.then(data => {
if (data.code === 1) {
const countElement = document.getElementById('articlesLikes');
if (countElement) {
let count = parseInt(countElement.textContent) || 0;
countElement.textContent = count + 1;
}
// 添加点赞状态
likeBtn.classList.add('liked');
likeBtn.querySelector('i').style.color = '#f57005';
layer.msg('点赞成功', {icon: 1});
} else {
// 如果请求失败,恢复按钮状态
likeBtn.style.pointerEvents = 'auto';
likeBtn.style.cursor = 'pointer';
layer.msg('点赞失败:' + data.msg, {icon: 2});
}
})
.catch(error => {
// 如果请求失败,恢复按钮状态
likeBtn.style.pointerEvents = 'auto';
likeBtn.style.cursor = 'pointer';
console.error('点赞请求失败:', error);
layer.msg('点赞失败,请稍后重试', {icon: 2});
});
});
}
// 返回顶部功能
const goToTop = document.getElementById('goToTop');
// 监听滚动事件
window.addEventListener('scroll', function() {
window.addEventListener('scroll', function () {
if (window.pageYOffset > 300) {
goToTop.classList.add('show');
} else {
goToTop.classList.remove('show');
}
});
// 点击返回顶部
goToTop.addEventListener('click', function() {
goToTop.addEventListener('click', function () {
window.scrollTo({
top: 0,
behavior: 'smooth'
@@ -599,18 +624,42 @@
id: articleId
})
})
.then(response => response.json())
.then(result => {
if (result.code === 1) {
// 更新成功,更新页面上的访问次数显示
const viewsElement = document.getElementById('articleViews');
if (viewsElement) {
viewsElement.textContent = result.data.views;
.then(response => response.json())
.then(result => {
if (result.code === 1) {
// 更新成功,更新页面上的访问次数显示
const viewsElement = document.getElementById('articleViews');
if (viewsElement) {
viewsElement.textContent = result.data.views;
}
}
}
})
.catch(error => {
console.error('更新访问次数失败:', error);
})
.catch(error => {
console.error('更新访问次数失败:', error);
});
}
// 分享功能
const shareBtn = document.getElementById('shareBtn');
if (shareBtn) {
shareBtn.addEventListener('click', function () {
// 获取当前页面URL
const currentUrl = window.location.href;
// 创建临时输入框
const tempInput = document.createElement('input');
tempInput.value = currentUrl;
document.body.appendChild(tempInput);
// 选择并复制文本
tempInput.select();
document.execCommand('copy');
// 移除临时输入框
document.body.removeChild(tempInput);
// 提示用户复制成功
alert('链接已复制到剪贴板');
});
}
</script>
+2 -2
View File
@@ -28,7 +28,7 @@
<div class="main-menu">
<div class="container">
<div class="main-menu__logo">
<a href="index.html"><img src="__IMAGES__/logo.png" width="186" alt="Logo"></a>
<a href="index.html"><img src="__IMAGES__/logo1.png" width="186" alt="Logo"></a>
</div>
<div class="main-menu__nav">
<ul class="main-menu__list">
@@ -70,7 +70,7 @@
<div class="sticky-nav" style="display: none;">
<div class="container">
<div class="sticky-nav__logo">
<a href="index.html"><img src="__IMAGES__/logo.png" width="150" alt="Logo"></a>
<a href="index.html"><img src="__IMAGES__/logo1.png" width="150" alt="Logo"></a>
</div>
<div class="sticky-nav__menu">
<ul>
+88 -61
View File
@@ -18,7 +18,8 @@
<span class="program-author"><i class="fa fa-user"></i> <span id="programAuthor"></span></span>
<span class="program-date"><i class="fa fa-calendar"></i> <span id="programDate"></span></span>
<span class="program-views"><i class="fa-solid fa-eye"></i> <span id="programViews"></span> 浏览</span>
<span class="program-downloads"><i class="fa-solid fa-download"></i> <span id="programDownloads"></span> 下载</span>
<span class="program-downloads"><i class="fa-solid fa-download"></i> <span id="programDownloads"></span>
下载</span>
</div>
</div>
@@ -44,12 +45,16 @@
</div>
</div>
<div class="program-content">
</div>
<div class="program-actions">
<div class="action-item download-btn" id="downloadBtn">
<i class="fa fa-download"></i>
<span class="action-text">立即下载</span>
</div>
<div class="action-item share-btn">
<div class="action-item share-btn" id="shareBtn">
<i class="fa fa-share-alt"></i>
<span class="action-text">分享</span>
</div>
@@ -203,6 +208,11 @@
margin: 30px 0;
}
.program-navigation a {
color: #333;
text-decoration: none;
}
.prev-program,
.next-program {
max-width: 45%;
@@ -358,25 +368,25 @@
function renderProgramDetail(data) {
// 渲染分类链接
document.getElementById('cateLink').textContent = data.cateName;
// 渲染程序标题
document.getElementById('programTitle').textContent = data.program.title;
// 渲染程序元信息
document.getElementById('programAuthor').textContent = data.program.author;
document.getElementById('programDate').textContent = formatDate(data.program.create_time);
document.getElementById('programViews').textContent = data.program.views;
document.getElementById('programDownloads').textContent = data.program.downloads;
// 渲染程序内容
document.getElementById('programContent').innerHTML = data.program.content;
// 渲染程序信息
document.getElementById('programSize').textContent = data.program.size || '未知';
document.getElementById('programEnvironment').textContent = data.program.environment || '通用';
document.getElementById('programUpdateTime').textContent = formatDate(data.program.update_time);
document.getElementById('programVersion').textContent = data.program.version || '1.0.0';
// 渲染上一个程序
const prevProgram = document.getElementById('prevProgram');
if (data.prevProgram) {
@@ -388,7 +398,7 @@
} else {
prevProgram.innerHTML = '<span class="disabled"><i class="fa fa-arrow-left"></i> 没有上一个了</span>';
}
// 渲染下一个程序
const nextProgram = document.getElementById('nextProgram');
if (data.nextProgram) {
@@ -400,7 +410,7 @@
} else {
nextProgram.innerHTML = '<span class="disabled">没有下一个了 <i class="fa fa-arrow-right"></i></span>';
}
// 渲染相关程序
const relatedPrograms = document.getElementById('relatedPrograms');
if (data.relatedPrograms && data.relatedPrograms.length > 0) {
@@ -423,7 +433,7 @@
}
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
// 获取程序ID
const programId = new URLSearchParams(window.location.search).get('id');
if (!programId) {
@@ -437,72 +447,63 @@
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(result => {
if (result.code === 1) {
renderProgramDetail(result.data);
// 更新访问次数
updateProgramViews(programId);
} else {
alert(result.msg || '获取程序详情失败');
}
})
.catch(error => {
console.error('获取程序详情失败:', error);
alert('获取程序详情失败,请检查网络连接或刷新页面重试');
});
.then(response => response.json())
.then(result => {
if (result.code === 1) {
renderProgramDetail(result.data);
// 更新访问次数
updateProgramViews(programId);
// 初始化分享功能
initShareFunction();
} else {
alert(result.msg || '获取程序详情失败');
}
})
.catch(error => {
console.error('获取程序详情失败:', error);
alert('获取程序详情失败,请检查网络连接或刷新页面重试');
});
// 下载功能
const downloadBtn = document.getElementById('downloadBtn');
if (downloadBtn) {
downloadBtn.addEventListener('click', function() {
downloadBtn.addEventListener('click', function () {
fetch('/index/program/download?id=' + programId, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
if (data.code === 1) {
const downloadsElement = document.getElementById('programDownloads');
let downloads = parseInt(downloadsElement.textContent);
downloadsElement.textContent = downloads + 1;
// 获取当前域名
const domain = window.location.origin;
// 拼接完整的下载地址
if (data.data && data.data.fileurl) {
const downloadUrl = domain + data.data.fileurl;
.then(response => response.json())
.then(data => {
if (data.code === 1 && data.data && data.data.fileurl) {
const downloadUrl = window.location.origin + data.data.fileurl;
window.location.href = downloadUrl;
} else {
alert('下载地址不存在');
}
} else {
alert('下载失败:' + data.msg);
}
})
.catch(error => {
console.error('下载请求失败:', error);
alert('下载请求失败,请稍后重试');
});
})
.catch(error => {
console.error('下载请求失败:', error);
alert('下载请求失败,请稍后重试');
});
});
}
// 返回顶部功能
const goToTop = document.getElementById('goToTop');
// 监听滚动事件
window.addEventListener('scroll', function() {
window.addEventListener('scroll', function () {
if (window.pageYOffset > 300) {
goToTop.classList.add('show');
} else {
goToTop.classList.remove('show');
}
});
// 点击返回顶部
goToTop.addEventListener('click', function() {
goToTop.addEventListener('click', function () {
window.scrollTo({
top: 0,
behavior: 'smooth'
@@ -522,19 +523,45 @@
id: programId
})
})
.then(response => response.json())
.then(result => {
if (result.code === 1) {
// 更新成功,更新页面上的访问次数显示
const viewsElement = document.getElementById('programViews');
if (viewsElement) {
viewsElement.textContent = result.data.views;
.then(response => response.json())
.then(result => {
if (result.code === 1) {
// 更新成功,更新页面上的访问次数显示
const viewsElement = document.getElementById('programViews');
if (viewsElement) {
viewsElement.textContent = result.data.views;
}
}
}
})
.catch(error => {
console.error('更新访问次数失败:', error);
});
})
.catch(error => {
console.error('更新访问次数失败:', error);
});
}
// 初始化分享功能
function initShareFunction() {
const shareBtn = document.getElementById('shareBtn');
if (shareBtn) {
shareBtn.addEventListener('click', function () {
// 获取当前页面URL
const currentUrl = window.location.href;
// 创建临时输入框
const tempInput = document.createElement('input');
tempInput.value = currentUrl;
document.body.appendChild(tempInput);
// 选择并复制文本
tempInput.select();
document.execCommand('copy');
// 移除临时输入框
document.body.removeChild(tempInput);
// 提示用户复制成功
layer.msg('链接已复制到剪贴板');
});
}
}
</script>
{include file="component/foot" /}