修复下载功能
This commit is contained in:
@@ -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'];
|
||||
// 转换文件大小为合适的单位
|
||||
@@ -105,20 +106,20 @@ class ProgramController extends BaseController
|
||||
// 获取分类名称
|
||||
$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 = Resources::alias('p')
|
||||
->join('yz_resources_category c', 'p.cate = c.id')
|
||||
@@ -151,7 +152,7 @@ class ProgramController extends BaseController
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// 非 AJAX 请求返回视图
|
||||
View::assign([
|
||||
'program' => $program,
|
||||
@@ -167,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' => [
|
||||
@@ -226,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);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -203,6 +203,11 @@
|
||||
margin: 30px 0;
|
||||
}
|
||||
|
||||
.program-navigation a{
|
||||
color: #333;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.prev-program,
|
||||
.next-program {
|
||||
max-width: 45%;
|
||||
@@ -464,22 +469,11 @@
|
||||
})
|
||||
.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;
|
||||
window.location.href = downloadUrl;
|
||||
} else {
|
||||
alert('下载地址不存在');
|
||||
}
|
||||
if (data.code === 1 && data.data && data.data.fileurl) {
|
||||
const downloadUrl = window.location.origin + data.data.fileurl;
|
||||
window.location.href = downloadUrl;
|
||||
} else {
|
||||
alert('下载失败:' + data.msg);
|
||||
alert('下载地址不存在');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
|
||||
Reference in New Issue
Block a user