228 lines
6.6 KiB
PHP
228 lines
6.6 KiB
PHP
<?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 ProgramController extends BaseController
|
|
{
|
|
// 程序列表页
|
|
public function list()
|
|
{
|
|
// 获取分类ID
|
|
$cateId = Request::param('cate/d', 0);
|
|
|
|
// 构建查询条件
|
|
$where = [
|
|
['delete_time', '=', null],
|
|
['status', '=', 1]
|
|
];
|
|
|
|
if ($cateId > 0) {
|
|
$where[] = ['cate', '=', $cateId];
|
|
}
|
|
|
|
// 获取程序列表
|
|
$programs = 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([
|
|
'programs' => $programs,
|
|
'category' => $category,
|
|
'categories' => $categories
|
|
]);
|
|
|
|
return View::fetch('list');
|
|
}
|
|
|
|
// 程序详情页
|
|
public function detail()
|
|
{
|
|
$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'];
|
|
// 转换文件大小为合适的单位
|
|
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';
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取分类名称
|
|
$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')
|
|
->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::fetch('detail');
|
|
}
|
|
|
|
// 程序下载
|
|
public function download()
|
|
{
|
|
if (!Request::isAjax()) {
|
|
return json(['code' => 0, 'msg' => '非法请求']);
|
|
}
|
|
|
|
$id = Request::param('id/d', 0);
|
|
|
|
// 更新下载次数
|
|
$result = 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 = 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 = Resources::where('id', $id)->find();
|
|
if (!$program) {
|
|
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()]);
|
|
}
|
|
}
|
|
}
|