更新程序界面和后端
This commit is contained in:
@@ -90,6 +90,19 @@ class ProgramController extends BaseController
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 处理每个资源的size
|
||||
foreach ($programs as &$program) {
|
||||
if (empty($program['size'])) {
|
||||
// 从Attachments表中查找对应的src
|
||||
$attachment = Attachments::where('src', $program['icon'])
|
||||
->field('size')
|
||||
->find();
|
||||
if ($attachment) {
|
||||
$program['size'] = $attachment['size'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按子分类分组资源
|
||||
$groupedPrograms = [];
|
||||
foreach ($subCategories as $subCategory) {
|
||||
@@ -125,20 +138,19 @@ class ProgramController extends BaseController
|
||||
];
|
||||
|
||||
// 根据请求方式返回不同的输出
|
||||
if (request()->isPost()) {
|
||||
if ($this->request->isPost()) {
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '获取成功',
|
||||
'data' => $data
|
||||
]);
|
||||
} else {
|
||||
// 为视图准备数据
|
||||
$viewData = [
|
||||
'categories' => $categories,
|
||||
'cate' => $data['cate']
|
||||
];
|
||||
return view('index', $viewData);
|
||||
}
|
||||
|
||||
// GET请求渲染页面
|
||||
return view('index', [
|
||||
'categories' => $categories,
|
||||
'cate' => $data['cate']
|
||||
]);
|
||||
}
|
||||
|
||||
// 程序列表页
|
||||
@@ -212,43 +224,19 @@ class ProgramController extends BaseController
|
||||
}
|
||||
|
||||
// 程序详情页
|
||||
public function detail()
|
||||
public function detail($id)
|
||||
{
|
||||
$id = Request::param('id/d', 0);
|
||||
$program = Resources::where('id', $id)->find();
|
||||
// 获取资源详情
|
||||
$program = Resources::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->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'];
|
||||
// 转换文件大小为合适的单位
|
||||
if ($size >= 1073741824) { // 1GB = 1024MB = 1024*1024KB = 1024*1024*1024B
|
||||
$program['size'] = round($size / 1073741824, 2) . 'GB';
|
||||
} elseif ($size >= 1048576) { // 1MB = 1024KB = 1024*1024B
|
||||
$program['size'] = round($size / 1048576, 2) . 'MB';
|
||||
} else {
|
||||
$program['size'] = round($size / 1024, 2) . 'KB';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果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';
|
||||
if ($this->request->isPost()) {
|
||||
return json(['code' => 0, 'msg' => '资源不存在']);
|
||||
}
|
||||
$this->error('资源不存在');
|
||||
}
|
||||
|
||||
// 获取分类名称
|
||||
@@ -256,26 +244,33 @@ class ProgramController extends BaseController
|
||||
->value('name');
|
||||
|
||||
// 获取作者信息
|
||||
$authorInfo = Users::where('name', $program['uploader'])->find();
|
||||
if ($authorInfo) {
|
||||
// 统计作者的文章数
|
||||
$articleCount = Articles::where('author', $program['uploader'])->count();
|
||||
// 统计作者的资源数
|
||||
$resourceCount = Resources::where('uploader', $program['uploader'])->count();
|
||||
$uploaderInfo = [
|
||||
'name' => $program['uploader'],
|
||||
'avatar' => $program['icon'],
|
||||
'resource_count' => Resources::where('uploader', $program['uploader'])->count(),
|
||||
'article_count' => 0
|
||||
];
|
||||
|
||||
$authorData = [
|
||||
'avatar' => $authorInfo['avatar'] ?: '/static/images/avatar.png',
|
||||
'name' => $authorInfo['name'],
|
||||
'resource_count' => $resourceCount,
|
||||
'article_count' => $articleCount
|
||||
];
|
||||
} else {
|
||||
$authorData = [
|
||||
'avatar' => '/static/images/avatar.png',
|
||||
'name' => $program['uploader'],
|
||||
'resource_count' => 0,
|
||||
'article_count' => 0
|
||||
];
|
||||
// 处理资源size
|
||||
if (empty($program['size'])) {
|
||||
$attachment = Attachments::where('src', $program['icon'])
|
||||
->field('size')
|
||||
->find();
|
||||
if ($attachment) {
|
||||
$program['size'] = $attachment['size'];
|
||||
}
|
||||
}
|
||||
|
||||
// 转换文件大小为合适的单位
|
||||
if (!empty($program['size']) && is_numeric($program['size'])) {
|
||||
$size = $program['size'];
|
||||
if ($size >= 1073741824) { // 1GB = 1024MB = 1024*1024KB = 1024*1024*1024B
|
||||
$program['size'] = round($size / 1073741824, 2) . 'GB';
|
||||
} elseif ($size >= 1048576) { // 1MB = 1024KB = 1024*1024B
|
||||
$program['size'] = round($size / 1048576, 2) . 'MB';
|
||||
} else {
|
||||
$program['size'] = round($size / 1024, 2) . 'KB';
|
||||
}
|
||||
}
|
||||
|
||||
// 获取上一个和下一个程序
|
||||
@@ -312,33 +307,27 @@ class ProgramController extends BaseController
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 如果是 AJAX 请求,返回 JSON 数据
|
||||
if (Request::isAjax()) {
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'authorInfo' => $authorData,
|
||||
'program' => $program,
|
||||
'cateName' => $cateName,
|
||||
'prevProgram' => $prevProgram,
|
||||
'nextProgram' => $nextProgram,
|
||||
'relatedPrograms' => $relatedPrograms
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 非 AJAX 请求返回视图
|
||||
View::assign([
|
||||
'authorInfo' => $authorData,
|
||||
// 准备返回数据
|
||||
$data = [
|
||||
'program' => $program,
|
||||
'uploaderInfo' => $uploaderInfo,
|
||||
'cateName' => $cateName,
|
||||
'prevProgram' => $prevProgram,
|
||||
'nextProgram' => $nextProgram,
|
||||
'relatedPrograms' => $relatedPrograms
|
||||
]);
|
||||
];
|
||||
|
||||
return View::fetch('detail');
|
||||
// 根据请求方式返回不同的输出
|
||||
if ($this->request->isPost()) {
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '获取成功',
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
// GET请求渲染页面
|
||||
return view('', $data);
|
||||
}
|
||||
|
||||
// 程序下载
|
||||
|
||||
Reference in New Issue
Block a user