增加program详情页

This commit is contained in:
云泽网
2025-05-17 01:38:45 +08:00
parent f6f8f6ac52
commit 4c6f7277b0
11 changed files with 3101 additions and 351 deletions
+65 -22
View File
@@ -94,38 +94,48 @@ class Article extends Base
// 获取相关文章(同分类的其他文章)
$relatedArticles = Db::table('yz_article')
->where('cate', $article['cate'])
->where('id', '<>', $id)
->where('delete_time', null)
->where('status', '<>', 3)
->order('id DESC')
->alias('a')
->join('yz_article_category c', 'a.cate = c.id')
->where('a.cate', $article['cate'])
->where('a.id', '<>', $id)
->where('a.delete_time', null)
->where('a.status', '=', 2)
->field([
'a.id',
'a.title',
'a.desc',
'IF(a.image IS NULL OR a.image = "", c.image, a.image) as image'
])
->order('a.id DESC')
->limit(3)
->select()
->toArray();
// 如果是 AJAX 请求,返回 JSON 数据
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'article' => $article,
'cateName' => $cateName,
'prevArticle' => $prevArticle,
'nextArticle' => $nextArticle,
'relatedArticles' => $relatedArticles
]
]);
}
// 获取评论列表
// $comments = Db::table('yz_article_comment')
// ->where('article_id', $id)
// ->where('delete_time', null)
// ->order('id DESC')
// ->select()
// ->toArray();
// 更新浏览量(使用Cookie和IP双重验证)
$ip = Request::ip();
$cookieKey = 'article_view_' . $id;
$viewCookie = Request::cookie($cookieKey);
// 非 AJAX 请求返回视图
View::assign([
'article' => $article,
'cateName' => $cateName,
'prevArticle' => $prevArticle,
'nextArticle' => $nextArticle,
'relatedArticles' => $relatedArticles,
// 'comments' => $comments
'relatedArticles' => $relatedArticles
]);
return View::fetch();
return view('detail');
}
// 文章点赞
@@ -229,4 +239,37 @@ class Article extends Base
]
]);
}
/**
* 更新文章访问次数
*/
public function updateViews()
{
if (!Request::isPost()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::post('id');
if (!$id) {
return json(['code' => 0, 'msg' => '参数错误']);
}
try {
// 更新访问次数
$article = Db::table('yz_article')->where('id', $id)->find();
if (!$article) {
return json(['code' => 0, 'msg' => '文章不存在']);
}
// 更新访问次数
Db::table('yz_article')->where('id', $id)->inc('views')->update();
// 获取更新后的访问次数
$newViews = Db::table('yz_article')->where('id', $id)->value('views');
return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
}
+233
View File
@@ -0,0 +1,233 @@
<?php
/**
* 程序下载控制器
*/
namespace app\index\controller;
use app\index\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
class Program extends Base
{
// 程序列表页
public function list()
{
// 获取分类ID
$cateId = Request::param('cate/d', 0);
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1]
];
if ($cateId > 0) {
$where[] = ['cate', '=', $cateId];
}
// 获取程序列表
$programs = Db::table('yz_resources')
->where($where)
->order('id DESC')
->paginate([
'list_rows' => 10,
'query' => Request::instance()->param()
]);
// 获取分类信息
$category = null;
if ($cateId > 0) {
$category = Db::table('yz_resources_category')
->where('id', $cateId)
->where('delete_time', null)
->where('status', 1)
->find();
}
// 获取所有分类
$categories = Db::table('yz_resources_category')
->where('delete_time', null)
->where('status', 1)
->select()
->toArray();
// 将变量传递给视图
View::assign([
'programs' => $programs,
'category' => $category,
'categories' => $categories
]);
return view('list');
}
// 程序详情页
public function detail()
{
$id = Request::param('id/d', 0);
$program = Db::table('yz_resources')->where('id', $id)->find();
// 如果size没有,从附件表中获取
if (empty($program['size']) && !empty($program['fileurl'])) {
$attachment = Db::table('yz_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';
}
}
}
if (!$program) {
return json(['code' => 0, 'msg' => '程序不存在或已被删除']);
}
// 获取分类名称
$cateName = Db::table('yz_resources_category')
->where('id', $program['cate'])
->value('name');
// 获取上一个和下一个程序
$prevProgram = Db::table('yz_resources')
->where('id', '<', $id)
->where('delete_time', null)
->where('status', 1)
->order('id DESC')
->find();
$nextProgram = Db::table('yz_resources')
->where('id', '>', $id)
->where('delete_time', null)
->where('status', 1)
->order('id ASC')
->find();
// 获取相关程序(同分类的其他程序)
$relatedPrograms = Db::table('yz_resources')
->alias('p')
->join('yz_resources_category c', 'p.cate = c.id')
->where('p.cate', $program['cate'])
->where('p.id', '<>', $id)
->where('p.delete_time', null)
->where('p.status', 1)
->field([
'p.id',
'p.title',
'p.desc',
'IF(p.icon IS NULL OR p.icon = "", c.icon, p.icon) as icon'
])
->order('p.id DESC')
->limit(3)
->select()
->toArray();
// 如果是 AJAX 请求,返回 JSON 数据
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'program' => $program,
'cateName' => $cateName,
'prevProgram' => $prevProgram,
'nextProgram' => $nextProgram,
'relatedPrograms' => $relatedPrograms
]
]);
}
// 非 AJAX 请求返回视图
View::assign([
'program' => $program,
'cateName' => $cateName,
'prevProgram' => $prevProgram,
'nextProgram' => $nextProgram,
'relatedPrograms' => $relatedPrograms
]);
return view('detail');
}
// 程序下载
public function download()
{
if (!Request::isAjax()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::param('id/d', 0);
// 更新下载次数
$result = Db::table('yz_resources')
->where('id', $id)
->where('delete_time', null)
->inc('downloads', 1)
->update();
if ($result) {
return json(['code' => 1, 'msg' => '下载成功']);
} else {
return json(['code' => 0, 'msg' => '下载失败']);
}
}
// 获取访问统计
public function viewStats()
{
$id = Request::param('id/d', 0);
// 获取总访问量
$totalViews = Db::table('yz_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 {
// 更新访问次数
$program = Db::table('yz_resources')->where('id', $id)->find();
if (!$program) {
return json(['code' => 0, 'msg' => '程序不存在']);
}
// 更新访问次数
Db::table('yz_resources')->where('id', $id)->inc('views')->update();
// 获取更新后的访问次数
$newViews = Db::table('yz_resources')->where('id', $id)->value('views');
return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
} catch (\Exception $e) {
return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
}
}
}