Files
yunzer/app/index/controller/ProgramController.php
T
2025-12-25 23:16:10 +08:00

1153 lines
36 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\Response;
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 ProgramController extends BaseController
{
// 获取officeResources分类
public function getOfficeResourcesCategory()
{
// 获取名为 '办公资源' 的分类
$resourceCategory = ResourcesCategory::where('name', '办公资源')
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->find();
if (!$resourceCategory) {
return json([
'code' => 1,
'msg' => '未找到办公资源分类'
]);
}
// 只返回其子分类,cid等于'办公资源'的id
$children = ResourcesCategory::where('cid', $resourceCategory['id'])
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->select()
->toArray();
return json([
'code' => 0,
'msg' => '获取办公资源子分类成功',
'data' => $children
]);
}
// 获取officeResources内容
public function getOfficeResourcesLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,desc,uploader,content,views,downloads,push,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
// 获取officeResources内容
public function getOfficeResourcesSimpleLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,views,downloads,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
// 获取downloadPrograms分类
public function getDownloadProgramsCategory()
{
// 获取名为 '程序下载' 的分类
$resourceCategory = ResourcesCategory::where('name', '程序下载')
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->find();
if (!$resourceCategory) {
return json([
'code' => 1,
'msg' => '未找到程序下载分类'
]);
}
// 只返回其子分类,cid等于'程序下载'的id
$children = ResourcesCategory::where('cid', $resourceCategory['id'])
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->select()
->toArray();
return json([
'code' => 0,
'msg' => '获取程序下载子分类成功',
'data' => $children
]);
}
// 获取downloadPrograms内容
public function getDownloadProgramsLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,desc,uploader,content,views,downloads,push,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
// 获取downloadPrograms内容
public function getDownloadProgramsSimpleLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,views,downloads,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
// 获取downloadGames分类
public function getDownloadGamesCategory()
{
// 获取名为 '游戏下载' 的分类
$resourceCategory = ResourcesCategory::where('name', '游戏下载')
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->find();
if (!$resourceCategory) {
return json([
'code' => 1,
'msg' => '未找到游戏下载分类'
]);
}
// 只返回其子分类,cid等于'游戏下载'的id
$children = ResourcesCategory::where('cid', $resourceCategory['id'])
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,icon,sort')
->select()
->toArray();
return json([
'code' => 0,
'msg' => '获取游戏下载子分类成功',
'data' => $children
]);
}
// 获取downloadGames内容
public function getDownloadGamesLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,desc,uploader,content,views,downloads,push,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
// 获取downloadGames内容
public function getDownloadGamesSimpleLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Resources::where($where)->count();
// 查询文章列表
$resources = Resources::where($where)
->field('id,title,cate,icon,views,downloads,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($resources as &$resource) {
// 如果文章没有封面图,使用分类封面图
if (empty($resource['icon'])) {
$resource['icon'] = $category['icon'] ?? '';
}
// 格式化时间
$resource['create_time'] = date('Y-m-d H:i:s', strtotime($resource['create_time']));
// 获取分类名称
$resourceCategory = ResourcesCategory::where('id', $resource['cate'])->find();
$resource['category_name'] = $resourceCategory ? $resourceCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'resources' => $resources,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
//资源中心
public function index()
{
// 获取前端传来的分类ID
$cateid = input('cateid/d', 0);
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取所有顶级分类
$categories = ResourcesCategory::where('cid', 0)
->where('delete_time', null)
->where('status', 1)
->select()
->toArray();
// 获取顶级分类信息
$category = null;
if ($cateid > 0) {
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
}
// 获取所有子分类
$subCategories = [];
if ($cateid > 0) {
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->select()
->toArray();
}
// 获取所有子分类ID
$subCategoryIds = array_column($subCategories, 'id');
if ($cateid > 0) {
$subCategoryIds[] = $cateid;
}
// 构建资源查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1]
];
if (!empty($subCategoryIds)) {
$where[] = ['cate', 'in', $subCategoryIds];
}
// 查询资源
$programs = Resources::where($where)
->order('id DESC')
->page($page, $limit)
->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) {
$groupedPrograms[$subCategory['id']] = [
'id' => $subCategory['id'],
'name' => $subCategory['name'],
'list' => []
];
}
// 将资源分配到对应的子分类
foreach ($programs as $program) {
if (isset($groupedPrograms[$program['cate']])) {
$groupedPrograms[$program['cate']]['list'][] = $program;
}
}
// 获取总数
$total = Resources::where($where)->count();
// 准备返回数据
$data = [
'cate' => [
'id' => $cateid,
'name' => $category ? $category['name'] : '',
'desc' => $category ? $category['desc'] : '',
'image' => $category ? $category['image'] : '',
'subCategories' => array_values($groupedPrograms),
'total' => $total,
'page' => $page,
'limit' => $limit
]
];
// 根据请求方式返回不同的输出
if ($this->request->isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => $data
]);
}
// GET请求渲染页面
return view('index', [
'categories' => $categories,
'cate' => $data['cate']
]);
}
// 程序列表页
public function list()
{
// 获取分类ID
$cateId = Request::param('cate/d', 0);
// 构建查询条件
$where = [
['a.delete_time', '=', null],
['a.status', '=', 1]
];
if ($cateId > 0) {
$where[] = ['a.cate', '=', $cateId];
}
// 获取程序列表
$programs = Resources::alias('a')
->join('resources_category c', 'a.cate = c.id')
->where($where)
->field([
'a.*',
'IF(a.icon IS NULL OR a.icon = "", c.icon, a.icon) as icon'
])
->order('a.id DESC')
->paginate([
'list_rows' => 10,
'query' => Request::instance()->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();
// 如果是POST请求,返回JSON数据
if (Request::isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'programs' => $programs->items(),
'total' => $programs->total(),
'current_page' => $programs->currentPage(),
'per_page' => $programs->listRows(),
'category' => $category
]
]);
}
// GET请求返回渲染的视图
View::assign([
'programs' => $programs,
'category' => $category,
'categories' => $categories
]);
return View::fetch('list');
}
// 程序详情页
public function detail($id)
{
// 获取资源详情
$program = Resources::where('id', $id)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$program) {
if ($this->request->isPost()) {
return json(['code' => 0, 'msg' => '资源不存在']);
}
$this->error('资源不存在');
}
// 获取分类名称
$cateName = ResourcesCategory::where('id', $program['cate'])
->value('name');
// 获取上传者信息
$uploaderInfo = Users::where('name', $program['uploader'])
->field(['name', 'avatar'])
->find();
if ($uploaderInfo) {
$uploaderInfo = $uploaderInfo->toArray();
// 如果没有头像,使用默认头像
if (empty($uploaderInfo['avatar'])) {
$uploaderInfo['avatar'] = '/static/images/avatar.png';
}
} else {
$uploaderInfo = [
'name' => $program['uploader'],
'avatar' => '/static/images/avatar.png'
];
}
// 添加上传者的资源数和文章数统计
$uploaderInfo['resource_count'] = Resources::where('uploader', $program['uploader'])
->where('delete_time', null)
->where('status', 1)
->count();
$uploaderInfo['article_count'] = Articles::where('author', $program['uploader'])
->where('delete_time', null)
->where('status', 2)
->count();
// 处理资源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';
}
}
// 获取上一个和下一个程序
$prevProgram = Resources::where('id', '<', $id)
->where('delete_time', null)
->where('status', 1)
->where('cate', $program['cate'])
->field(['id', 'title'])
->order('id DESC')
->find();
$nextProgram = Resources::where('id', '>', $id)
->where('delete_time', null)
->where('status', 1)
->where('cate', $program['cate'])
->field(['id', 'title'])
->order('id ASC')
->find();
// 获取相关程序(同分类的其他程序)
$relatedPrograms = 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',
'COALESCE(p.icon, c.icon) as icon'
])
->order('p.id DESC')
->limit(3)
->select()
->toArray();
// 准备返回数据
$data = [
'program' => $program,
'uploaderInfo' => $uploaderInfo,
'cateName' => $cateName,
'prevProgram' => $prevProgram,
'nextProgram' => $nextProgram,
'relatedPrograms' => $relatedPrograms
];
// 根据请求方式返回不同的输出
if ($this->request->isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => $data
]);
}
// GET请求渲染页面
return view('', $data);
}
// 程序下载
public function download()
{
$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' => '程序不存在或已被删除']);
}
// 更新下载次数
Resources::where('id', $id)->inc('downloads')->update();
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' => [
'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()]);
}
}
}