486 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			486 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * 后台管理系统-资源管理
 | 
						|
 */
 | 
						|
namespace app\admin\controller;
 | 
						|
use app\admin\controller\BaseController;
 | 
						|
use app\admin\model\Resource\Resource;
 | 
						|
use app\admin\model\Resource\ResourceCategory;
 | 
						|
use think\facade\View;
 | 
						|
use think\facade\Request;
 | 
						|
use think\facade\Db;
 | 
						|
use app\admin\controller\LogController as Log;
 | 
						|
use think\App;
 | 
						|
 | 
						|
class ResourcesController extends BaseController
 | 
						|
{
 | 
						|
    // 资源列表
 | 
						|
    public function lists()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $params = [
 | 
						|
                'category' => input('post.category'),
 | 
						|
                'name' => input('post.name'),
 | 
						|
                'uploader' => input('post.uploader')
 | 
						|
            ];
 | 
						|
            $page = (int) input('post.page', 1);
 | 
						|
            $limit = (int) input('post.limit', 10);
 | 
						|
 | 
						|
            $query = Resource::where('delete_time', null)
 | 
						|
                ->where('status', 1);
 | 
						|
 | 
						|
            // 分类筛选
 | 
						|
            if (!empty($params['category'])) {
 | 
						|
                $cateInfo = ResourceCategory::where('name', $params['category'])
 | 
						|
                    ->where('delete_time', null)
 | 
						|
                    ->where('status', 1)
 | 
						|
                    ->field('id')
 | 
						|
                    ->find();
 | 
						|
 | 
						|
                if ($cateInfo) {
 | 
						|
                    $query = $query->where('cate', (int) $cateInfo['id']);
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            // 名称搜索
 | 
						|
            if (!empty($params['name'])) {
 | 
						|
                $query = $query->where('name', 'like', '%' . $params['name'] . '%');
 | 
						|
            }
 | 
						|
 | 
						|
            // 上传者搜索
 | 
						|
            if (!empty($params['uploader'])) {
 | 
						|
                $query = $query->where('uploader', 'like', '%' . $params['uploader'] . '%');
 | 
						|
            }
 | 
						|
 | 
						|
            $count = $query->count();
 | 
						|
 | 
						|
            $lists = $query->order('id DESC')
 | 
						|
                ->page($page, $limit)
 | 
						|
                ->select()
 | 
						|
                ->each(function ($item) {
 | 
						|
                    // 获取分类信息
 | 
						|
                    $cateInfo = ResourceCategory::where('id', (int) $item['cate'])
 | 
						|
                        ->field('name, icon')
 | 
						|
                        ->find();
 | 
						|
                    if ($cateInfo) {
 | 
						|
                        $item['cate'] = $cateInfo['name'];
 | 
						|
                        if (empty($item['icon']) && !empty($cateInfo['icon'])) {
 | 
						|
                            $item['icon'] = $cateInfo['icon'];
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                    $item['create_time'] = date('Y-m-d H:i:s', (int) $item['create_time']);
 | 
						|
                    return $item;
 | 
						|
                });
 | 
						|
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'count' => $count,
 | 
						|
                'data' => $lists
 | 
						|
            ]);
 | 
						|
        } else {
 | 
						|
            $allCategories = ResourceCategory::where('delete_time', null)
 | 
						|
                ->where('status', 1)
 | 
						|
                ->field('id, name, cid, icon')
 | 
						|
                ->order('sort asc, id asc')
 | 
						|
                ->select()
 | 
						|
                ->toArray();
 | 
						|
 | 
						|
            $categories = $this->buildParentChild($allCategories);
 | 
						|
 | 
						|
            View::assign([
 | 
						|
                'categories' => $categories
 | 
						|
            ]);
 | 
						|
            return View::fetch();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // 添加资源
 | 
						|
    public function add()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $data = [
 | 
						|
                'title' => input('post.title'),
 | 
						|
                'cate' => input('post.cate'),
 | 
						|
                'icon' => input('post.icon'),
 | 
						|
                'url' => input('post.url'),
 | 
						|
                'fileurl' => input('post.fileurl'),
 | 
						|
                'code' => input('post.code'),
 | 
						|
                'uploader' => input('post.uploader'),
 | 
						|
                'desc' => input('post.desc'),
 | 
						|
                'status' => input('post.status', 1),
 | 
						|
                'create_time' => time()
 | 
						|
            ];
 | 
						|
 | 
						|
            $insert = Resource::insert($data);
 | 
						|
            if (empty($insert)) {
 | 
						|
                Log::record('添加资源', 0, '添加资源失败', '资源管理');
 | 
						|
                return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
 | 
						|
            }
 | 
						|
            Log::record('添加资源', 1, '', '资源管理');
 | 
						|
            return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
 | 
						|
        } else {
 | 
						|
            $lists = Resource::where('delete_time', null)
 | 
						|
                ->where('status', '<>', 3)
 | 
						|
                ->select()
 | 
						|
                ->toArray();
 | 
						|
            View::assign([
 | 
						|
                'lists' => $lists
 | 
						|
            ]);
 | 
						|
            return View::fetch();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 编辑资源
 | 
						|
     */
 | 
						|
    public function edit()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $data = input('post.');
 | 
						|
            $id = input('id/d', 0);
 | 
						|
 | 
						|
            if (!$id) {
 | 
						|
                Log::record('编辑资源', 0, '参数错误', '资源管理');
 | 
						|
                return json(['code' => 1, 'msg' => '参数错误']);
 | 
						|
            }
 | 
						|
 | 
						|
            $updateData = [
 | 
						|
                'title' => $data['title'],
 | 
						|
                'cate' => $data['cate'],
 | 
						|
                'desc' => $data['desc'],
 | 
						|
                'uploader' => $data['uploader'],
 | 
						|
                'icon' => $data['icon'],
 | 
						|
                'fileurl' => $data['fileurl'],
 | 
						|
                'url' => $data['url'],
 | 
						|
                'code' => $data['code'],
 | 
						|
                'sort' => $data['sort'],
 | 
						|
                'update_time' => time()
 | 
						|
            ];
 | 
						|
 | 
						|
            $result = Resource::where('id', $id)
 | 
						|
                ->update($updateData);
 | 
						|
 | 
						|
            if ($result !== false) {
 | 
						|
                Log::record('编辑资源', 1, '', '资源管理');
 | 
						|
                return json(['code' => 0, 'msg' => '编辑成功']);
 | 
						|
            } else {
 | 
						|
                Log::record('编辑资源', 0, '编辑资源失败', '资源管理');
 | 
						|
                return json(['code' => 1, 'msg' => '编辑失败']);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        $id = input('id/d', 0);
 | 
						|
        if (!$id) {
 | 
						|
            Log::record('编辑资源', 0, '参数错误', '资源管理');
 | 
						|
            $this->error('参数错误');
 | 
						|
        }
 | 
						|
 | 
						|
        $resource = Resource::where('id', $id)
 | 
						|
            ->where('delete_time', null)
 | 
						|
            ->find();
 | 
						|
 | 
						|
        if (!$resource) {
 | 
						|
            Log::record('编辑资源', 0, '资源不存在', '资源管理');
 | 
						|
            $this->error('资源不存在');
 | 
						|
        }
 | 
						|
 | 
						|
        View::assign('resource', $resource);
 | 
						|
        return View::fetch();
 | 
						|
    }
 | 
						|
 | 
						|
    // 删除资源
 | 
						|
    public function delete()
 | 
						|
    {
 | 
						|
        $id = input('post.id');
 | 
						|
        $delete = Resource::where('id', $id)
 | 
						|
            ->update(['delete_time' => time()]);
 | 
						|
        if ($delete === false) {
 | 
						|
            Log::record('删除资源', 0, '删除资源失败', '资源管理');
 | 
						|
            return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
 | 
						|
        }
 | 
						|
        Log::record('删除资源', 1, '', '资源管理');
 | 
						|
        return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
 | 
						|
    }
 | 
						|
 | 
						|
    // 资源分类
 | 
						|
    public function cate()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $lists = ResourceCategory::where('delete_time', null)
 | 
						|
                ->where('status', 1)
 | 
						|
                ->order('sort asc, id asc')
 | 
						|
                ->select()
 | 
						|
                ->toArray();
 | 
						|
            $tree = $this->buildTree($lists);
 | 
						|
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
 | 
						|
        }
 | 
						|
        return View::fetch();
 | 
						|
    }
 | 
						|
 | 
						|
    //获取分类结构
 | 
						|
    public function getcate()
 | 
						|
    {
 | 
						|
        $lists = ResourceCategory::where('delete_time', null)
 | 
						|
            ->where('status', 1)
 | 
						|
            ->order('sort asc, id asc')
 | 
						|
            ->select()
 | 
						|
            ->toArray();
 | 
						|
        $tree = $this->buildParentChild($lists);
 | 
						|
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
 | 
						|
    }
 | 
						|
 | 
						|
    // 添加资源分类
 | 
						|
    public function cateadd()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $data = [
 | 
						|
                'name' => input('post.name'),
 | 
						|
                'icon' => input('post.icon'),
 | 
						|
                'cid' => input('post.cid'),
 | 
						|
                'sort' => input('post.sort', 0),
 | 
						|
                'status' => input('post.status', 1),
 | 
						|
                'create_time' => time()
 | 
						|
            ];
 | 
						|
 | 
						|
            $insert = ResourceCategory::insert($data);
 | 
						|
            if (empty($insert)) {
 | 
						|
                Log::record('添加资源分类', 0, '添加资源分类失败', '资源分类');
 | 
						|
                return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
 | 
						|
            }
 | 
						|
            Log::record('添加资源分类', 1, '', '资源分类');
 | 
						|
            return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
 | 
						|
        } else {
 | 
						|
            $parentCategories = ResourceCategory::where('delete_time', null)
 | 
						|
                ->where('status', 1)
 | 
						|
                ->where('cid', 0)
 | 
						|
                ->field('id, name')
 | 
						|
                ->select()
 | 
						|
                ->toArray();
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'data' => [
 | 
						|
                    'parentOptions' => $parentCategories
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    //编辑资源分类
 | 
						|
    public function cateedit()
 | 
						|
    {
 | 
						|
        if (Request::isPost()) {
 | 
						|
            $data = [
 | 
						|
                'id' => input('post.id'),
 | 
						|
                'name' => input('post.name'),
 | 
						|
                'icon' => input('post.icon'),
 | 
						|
                'cid' => input('post.cid'),
 | 
						|
                'sort' => input('post.sort', 0),
 | 
						|
                'status' => input('post.status', 1),
 | 
						|
                'update_time' => time()
 | 
						|
            ];
 | 
						|
 | 
						|
            $update = ResourceCategory::where('id', $data['id'])
 | 
						|
                ->update($data);
 | 
						|
            if ($update === false) {
 | 
						|
                Log::record('编辑资源分类', 0, '更新资源分类失败', '资源分类');
 | 
						|
                return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
 | 
						|
            }
 | 
						|
            Log::record('编辑资源分类', 1, '', '资源分类');
 | 
						|
            return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
 | 
						|
        } else {
 | 
						|
            $id = input('get.id');
 | 
						|
            $info = ResourceCategory::where('id', $id)->find();
 | 
						|
            $parentCategories = ResourceCategory::where('delete_time', null)
 | 
						|
                ->where('status', 1)
 | 
						|
                ->where('cid', 0)
 | 
						|
                ->where('id', '<>', $id)
 | 
						|
                ->field('id, name')
 | 
						|
                ->select()
 | 
						|
                ->toArray();
 | 
						|
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'data' => [
 | 
						|
                    'info' => $info,
 | 
						|
                    'parentOptions' => $parentCategories
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    //删除资源分类
 | 
						|
    public function catedel()
 | 
						|
    {
 | 
						|
        $id = input('post.id');
 | 
						|
 | 
						|
        // 检查是否有子分类
 | 
						|
        $hasChildren = ResourceCategory::where('cid', $id)
 | 
						|
            ->where('delete_time', null)
 | 
						|
            ->find();
 | 
						|
        if ($hasChildren) {
 | 
						|
            Log::record('删除资源分类', 0, '该分类下有子分类,无法删除', '资源分类');
 | 
						|
            return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
 | 
						|
        }
 | 
						|
 | 
						|
        $delete = ResourceCategory::where('id', $id)
 | 
						|
            ->update(['delete_time' => time()]);
 | 
						|
        if ($delete === false) {
 | 
						|
            Log::record('删除资源分类', 0, '删除资源分类失败', '资源分类');
 | 
						|
            return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
 | 
						|
        }
 | 
						|
        Log::record('删除资源分类', 1, '', '资源分类');
 | 
						|
        return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取资源详情
 | 
						|
     */
 | 
						|
    public function get()
 | 
						|
    {
 | 
						|
        $id = input('id/d', 0);
 | 
						|
        if (!$id) {
 | 
						|
            Log::record('获取资源详情', 0, '参数错误', '资源管理');
 | 
						|
            return json(['code' => 1, 'msg' => '参数错误']);
 | 
						|
        }
 | 
						|
 | 
						|
        $resource = Resource::where('id', $id)
 | 
						|
            ->where('delete_time', null)
 | 
						|
            ->find();
 | 
						|
 | 
						|
        if (!$resource) {
 | 
						|
            Log::record('获取资源详情', 0, '资源不存在', '资源管理');
 | 
						|
            return json(['code' => 1, 'msg' => '资源不存在']);
 | 
						|
        }
 | 
						|
 | 
						|
        // 获取分类信息
 | 
						|
        $cateInfo = ResourceCategory::where('id', $resource['cate'])
 | 
						|
            ->field('name')
 | 
						|
            ->find();
 | 
						|
 | 
						|
        if ($cateInfo) {
 | 
						|
            $resource['cate_name'] = $cateInfo['name'];
 | 
						|
        }
 | 
						|
 | 
						|
        Log::record('获取资源详情', 1, '', '资源管理');
 | 
						|
        return json(['code' => 0, 'msg' => '获取成功', 'data' => $resource]);
 | 
						|
    }
 | 
						|
 | 
						|
    //统计资源数量
 | 
						|
    public function counts()
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            // 获取资源总数
 | 
						|
            $total = Resource::where('delete_time', null)
 | 
						|
                ->where('status', '<>', 3)
 | 
						|
                ->count();
 | 
						|
 | 
						|
            // 获取今日新增资源数
 | 
						|
            $today = strtotime(date('Y-m-d'));
 | 
						|
            $todayNew = Resource::where('delete_time', null)
 | 
						|
                ->where('status', '<>', 3)
 | 
						|
                ->where('create_time', '>=', $today)
 | 
						|
                ->count();
 | 
						|
 | 
						|
            // 获取最近7天的资源数据
 | 
						|
            $dates = [];
 | 
						|
            $counts = [];
 | 
						|
            $totalCounts = []; // 存储每天的总资源数
 | 
						|
 | 
						|
            for ($i = 6; $i >= 0; $i--) {
 | 
						|
                $date = date('Y-m-d', strtotime("-$i days"));
 | 
						|
                $start = strtotime($date);
 | 
						|
                $end = $start + 86400;
 | 
						|
 | 
						|
                // 获取当天新增资源数
 | 
						|
                $count = Resource::where('delete_time', null)
 | 
						|
                    ->where('status', '<>', 3)
 | 
						|
                    ->where('create_time', '>=', $start)
 | 
						|
                    ->where('create_time', '<', $end)
 | 
						|
                    ->count();
 | 
						|
 | 
						|
                // 获取截至当天的总资源数
 | 
						|
                $totalCount = Resource::where('delete_time', null)
 | 
						|
                    ->where('status', '<>', 3)
 | 
						|
                    ->where('create_time', '<', $end)
 | 
						|
                    ->count();
 | 
						|
 | 
						|
                $dates[] = $date;
 | 
						|
                $counts[] = $count;
 | 
						|
                $totalCounts[] = $totalCount;
 | 
						|
            }
 | 
						|
 | 
						|
            return json([
 | 
						|
                'code' => 0,
 | 
						|
                'msg' => '获取成功',
 | 
						|
                'data' => [
 | 
						|
                    'total' => $total,
 | 
						|
                    'todayNew' => $todayNew,
 | 
						|
                    'dates' => $dates,
 | 
						|
                    'counts' => $counts,
 | 
						|
                    'totalCounts' => $totalCounts
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
        } catch (\Exception $e) {
 | 
						|
            return json([
 | 
						|
                'code' => 1,
 | 
						|
                'msg' => '获取失败:' . $e->getMessage()
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // 构建树形结构
 | 
						|
    private function buildTree($lists)
 | 
						|
    {
 | 
						|
        $tree = [];
 | 
						|
        foreach ($lists as $item) {
 | 
						|
            if ($item['cid'] == 0) {
 | 
						|
                $node = [
 | 
						|
                    'id' => $item['id'],
 | 
						|
                    'title' => $item['name'],
 | 
						|
                    'children' => []
 | 
						|
                ];
 | 
						|
 | 
						|
                // 查找子分类
 | 
						|
                foreach ($lists as $subItem) {
 | 
						|
                    if ($subItem['cid'] == $item['id']) {
 | 
						|
                        $node['children'][] = [
 | 
						|
                            'id' => $subItem['id'],
 | 
						|
                            'title' => $subItem['name'],
 | 
						|
                            'children' => []
 | 
						|
                        ];
 | 
						|
                    }
 | 
						|
                }
 | 
						|
 | 
						|
                $tree[] = $node;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $tree;
 | 
						|
    }
 | 
						|
 | 
						|
    // 构建父子结构
 | 
						|
    private function buildParentChild($lists)
 | 
						|
    {
 | 
						|
        $tree = [];
 | 
						|
        foreach ($lists as $item) {
 | 
						|
            if ($item['cid'] == 0) {
 | 
						|
                // 顶级分类
 | 
						|
                $tree[] = $item;
 | 
						|
            } else {
 | 
						|
                // 子分类
 | 
						|
                foreach ($tree as &$parent) {
 | 
						|
                    if ($parent['id'] == $item['cid']) {
 | 
						|
                        if (!isset($parent['children'])) {
 | 
						|
                            $parent['children'] = [];
 | 
						|
                        }
 | 
						|
                        $parent['children'][] = $item;
 | 
						|
                        break;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $tree;
 | 
						|
    }
 | 
						|
} |