Files
yunzer/app/index/controller/ResourcesController.php
T
2025-12-26 15:30:15 +08:00

433 lines
14 KiB
PHP

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
/**
* 资源下载控制器
*/
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;
use app\index\model\Users;
use app\index\model\Articles\Articles;
class ResourcesController extends BaseController
{
// 获取resourceDetail详情
public function getResourceDetail()
{
try {
// 获取前端传递的ID
$id = input('id/d', 0);
// 验证ID
if ($id <= 0) {
return json([
'code' => 1,
'msg' => 'ID参数错误'
]);
}
// 构建查询条件
$where = [
['r.id', '=', $id],
['r.delete_time', '=', null],
['r.status', '=', 1], // 已审核的资源
['r.push', '=', 1], // 已推送的资源
];
// 查询资源详情,联查分类名称
$resource = Resources::where($where)
->field('r.id,r.title,r.cate,r.icon,r.price,r.code,r.desc,r.number,r.content,r.views,r.downloads,r.create_time,r.update_time,r.url,r.fileurl,c.name as cate_name')
->alias('r')
->join('resources_category c', 'r.cate = c.id')
->find();
if (!$resource) {
return json([
'code' => 1,
'msg' => '资源不存在或已删除'
]);
}
// 转换为数组并处理数据
$resource = $resource->toArray();
// 获取当前资源的分类ID(用于查询相关内容)
$currentCateId = $resource['cate'];
// 查询上一篇(同分类,ID更小的,按ID倒序取第一个)
$prevResource = Resources::where([
['id', '<', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon')
->order('id', 'desc')
->find();
// 查询下一篇(同分类,ID更大的,按ID正序取第一个)
$nextResource = Resources::where([
['id', '>', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon')
->order('id', 'asc')
->find();
// 查询相关文章(同分类,排除当前文章,按时间倒序取5个)
$relatedResources = Resources::where([
['id', '<>', $id],
['cate', '=', $currentCateId],
['delete_time', '=', null],
['status', '=', 1],
['push', '=', 1]
])
->field('id,title,icon,views,downloads,create_time')
->order('create_time', 'desc')
->limit(5)
->select();
// 将cate字段替换为分类名称
$resource['cate'] = $resource['cate_name'];
unset($resource['cate_name']);
// 处理相关文章数据
$relatedData = [];
if ($relatedResources) {
foreach ($relatedResources as $related) {
$relatedData[] = [
'id' => $related['id'],
'title' => $related['title'],
'icon' => $related['icon']
];
}
}
// 将相关数据添加到资源对象中,保持原有数据结构
$resource['prev_resource'] = $prevResource ? [
'id' => $prevResource['id'],
'title' => $prevResource['title']
] : null;
$resource['next_resource'] = $nextResource ? [
'id' => $nextResource['id'],
'title' => $nextResource['title']
] : null;
$resource['related_resources'] = $relatedData;
// 返回数据,保持原有结构
return json([
'code' => 0,
'msg' => '获取成功',
'data' => $resource
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
//资源中心
public function index()
{
// 获取所有顶级分类
$parentCategories = ResourcesCategory::where('cid', 0)
->where('status', 1)
->order('sort', 'asc')
->select();
// 获取每个顶级分类下的子分类
$categories = [];
foreach ($parentCategories as $parent) {
$subCategories = ResourcesCategory::where('cid', $parent->id)
->where('status', 1)
->order('sort', 'asc')
->select();
// 获取每个子分类下的资源数量
foreach ($subCategories as &$subCategory) {
$subCategory['resource_count'] = Resources::where('cate', $subCategory->id)
->where('status', 1)
->count();
}
$categories[] = [
'parent' => $parent,
'subCategories' => $subCategories
];
}
// 将数据传递给视图
View::assign('categories', $categories);
return View::fetch();
}
// 资源列表页
public function list()
{
$cid = input('cid/d', 0);
$page = input('page/d', 1);
// 获取分类信息
$category = ResourcesCategory::where('id', $cid)
->where('status', 1)
->find();
if (!$category) {
$this->error('分类不存在');
}
// 获取该分类下的资源,带分页
$resources = Resources::where('cate', $cid)
->where('status', 1)
->order('sort', 'asc')
->paginate([
'list_rows' => 20,
'page' => $page,
'query' => Request::instance()->param()
]);
// 将数据传递给视图
View::assign('category', $category);
View::assign('data', $resources);
View::assign('page', $resources->render()); // 新增这一行
return View::fetch('list');
}
// 资源详情页
public function detail()
{
$id = Request::param('id/d', 0);
$resources = Resources::where('id', $id)->find();
if (!$resources) {
return json(['code' => 0, 'msg' => '资源不存在或已被删除']);
}
// 如果size没有,从附件表中获取
if (empty($resources['size']) && !empty($resources['fileurl'])) {
$attachment = Attachments::where('src', $resources['fileurl'])
->find();
if ($attachment && !empty($attachment['size'])) {
$size = $attachment['size'];
// 转换文件大小为合适的单位
if ($size >= 1073741824) { // 1GB = 1024MB = 1024*1024KB = 1024*1024*1024B
$resources['size'] = round($size / 1073741824, 2) . 'GB';
} elseif ($size >= 1048576) { // 1MB = 1024KB = 1024*1024B
$resources['size'] = round($size / 1048576, 2) . 'MB';
} else {
$resources['size'] = round($size / 1024, 2) . 'KB';
}
}
}
// 获取分类名称
$cateName = ResourcesCategory::where('id', $resources['cate'])
->value('name');
// 获取作者信息
$authorInfo = Users::where('name', $resources['uploader'])->find();
// var_dump($authorInfo);
if ($authorInfo) {
// 统计作者的文章数
$resourcesCount = Articles::where('author', $resources['uploader'])->count();
// 统计作者的资源数
$resourceCount = Resources::where('uploader', $resources['uploader'])->count();
$authorData = [
'avatar' => $authorInfo['avatar'] ?: '/static/images/avatar.png',
'name' => $authorInfo['name'],
'resource_count' => $resourceCount,
'article_count' => $resourcesCount
];
} else {
$authorData = [
'avatar' => '/static/images/avatar.png',
'name' => $resources['author'],
'resource_count' => 0,
'article_count' => 0
];
}
// 获取上一个和下一个资源
$prevResources = Resources::where('id', '<', $id)
->where('delete_time', null)
->where('status', 1)
->where('cate', $resources['cate'])
->field(['id', 'title'])
->order('id DESC')
->find();
$nextResources = Resources::where('id', '>', $id)
->where('delete_time', null)
->where('status', 1)
->where('cate', $resources['cate'])
->field(['id', 'title'])
->order('id ASC')
->find();
// 获取相关资源(同分类的其他资源)
$relatedResourcess = Db::table('yz_resources')
->alias('g')
->join('yz_resources_category c', 'g.cate = c.id')
->where('g.cate', $resources['cate'])
->where('g.id', '<>', $id)
->where('g.delete_time', null)
->where('g.status', 1)
->field([
'g.id',
'g.title',
'IF(g.icon IS NULL OR g.icon = "", c.icon, g.icon) as icon'
])
->order('g.id DESC')
->limit(5)
->select()
->toArray();
// 如果是 AJAX 请求,返回 JSON 数据
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'resources' => $resources,
'cateName' => $cateName,
'prevResources' => $prevResources,
'nextResources' => $nextResources,
'relatedResourcess' => $relatedResourcess
]
]);
}
// 非 AJAX 请求返回视图
View::assign([
'resources' => $resources,
'cateName' => $cateName,
'authorInfo' => $authorData,
'prevResources' => $prevResources,
'nextResources' => $nextResources,
'relatedResourcess' => $relatedResourcess
]);
return View::fetch('detail');
}
// 资源下载
public function downloadurl()
{
if (!Request::isAjax()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
$id = Request::param('id/d', 0);
// 获取资源信息
$resources = Resources::where('id', $id)
->where('delete_time', null)
->find();
if (!$resources) {
return json(['code' => 0, 'msg' => '资源不存在']);
}
// 更新下载次数
$result = Resources::where('id', $id)
->where('delete_time', null)
->inc('downloads', 1)
->update();
if ($result) {
return json([
'code' => 1,
'msg' => '下载成功',
'data' => [
'url' => $resources['url']
]
]);
} 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 {
// 更新访问次数
$resources = Resources::where('id', $id)->find();
if (!$resources) {
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()]);
}
}
}