481 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			481 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * 后台管理系统-资源管理
 | |
|  */
 | |
| namespace app\admin\controller;
 | |
| use app\admin\controller\Base;
 | |
| use think\facade\Db;
 | |
| use think\facade\View;
 | |
| use think\facade\Request;
 | |
| use app\admin\controller\Log;
 | |
| 
 | |
| class Resources extends Base
 | |
| {
 | |
|     // 资源列表
 | |
|     public function lists()
 | |
|     {
 | |
|         if (Request::isPost()) {
 | |
|             $category = input('post.category');
 | |
|             $page = input('post.page', 1);
 | |
|             $limit = input('post.limit', 10);
 | |
|             $name = input('post.name');
 | |
|             $uploader = input('post.uploader');
 | |
|             
 | |
|             $query = Db::table('yz_resources')
 | |
|                 ->where('delete_time', null)
 | |
|                 ->where('status', '<>', 3);
 | |
|             
 | |
|             // 分类筛选
 | |
|             if (!empty($category)) {
 | |
|                 // 先获取分类ID
 | |
|                 $cateInfo = Db::table('yz_resources_category')
 | |
|                     ->where('name', $category)
 | |
|                     ->where('delete_time', null)
 | |
|                     ->where('status', 1)
 | |
|                     ->find();
 | |
|                 
 | |
|                 if ($cateInfo) {
 | |
|                     $query = $query->where('cate', $cateInfo['id']);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             // 名称搜索
 | |
|             if (!empty($name)) {
 | |
|                 $query = $query->where('name', 'like', '%'.$name.'%');
 | |
|             }
 | |
| 
 | |
|             // 上传者搜索
 | |
|             if (!empty($uploader)) {
 | |
|                 $query = $query->where('uploader', 'like', '%'.$uploader.'%');
 | |
|             }
 | |
|             
 | |
|             // 获取总记录数
 | |
|             $count = $query->count();
 | |
|             
 | |
|             // 获取分页数据
 | |
|             $lists = $query->order('id DESC')
 | |
|                 ->page($page, $limit)
 | |
|                 ->select()
 | |
|                 ->each(function ($item) {
 | |
|                     // 获取分类信息
 | |
|                     $cateInfo = Db::table('yz_resources_category')
 | |
|                         ->where('id', $item['cate'])
 | |
|                         ->field('name, icon')
 | |
|                         ->find();
 | |
| 
 | |
|                     // 设置分类名称
 | |
|                     $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', $item['create_time']);
 | |
| 
 | |
|                     return $item;
 | |
|                 });
 | |
| 
 | |
|             return json([
 | |
|                 'code' => 0, 
 | |
|                 'msg' => '获取成功', 
 | |
|                 'count' => $count,
 | |
|                 'data' => $lists
 | |
|             ]);
 | |
|         } else {
 | |
|             // 获取所有分类并构建父子结构
 | |
|             $allCategories = Db::table('yz_resources_category')
 | |
|                 ->where('delete_time', null)
 | |
|                 ->where('status', 1)
 | |
|                 ->order('sort asc, id asc')
 | |
|                 ->select()
 | |
|                 ->toArray();
 | |
| 
 | |
|             $categories = [];
 | |
|             foreach ($allCategories as $category) {
 | |
|                 if ($category['cid'] == 0) {
 | |
|                     $category['children'] = [];
 | |
|                     foreach ($allCategories as $subCategory) {
 | |
|                         if ($subCategory['cid'] == $category['id']) {
 | |
|                             $category['children'][] = $subCategory;
 | |
|                         }
 | |
|                     }
 | |
|                     $categories[] = $category;
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             View::assign([
 | |
|                 'categories' => $categories
 | |
|             ]);
 | |
|             return View::fetch();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 添加资源
 | |
|     public function add()
 | |
|     {
 | |
|         if (Request::isPost()) {
 | |
|             $data = [
 | |
|                 'name' => input('post.name'),
 | |
|                 'cate' => input('post.cate'),
 | |
|                 'icon' => input('post.icon'),
 | |
|                 'url' => input('post.url'),
 | |
|                 'code' => input('post.code'),
 | |
|                 'uploader' => input('post.uploader'),
 | |
|                 'desc' => input('post.desc'),
 | |
|                 'status' => input('post.status', 2),
 | |
|                 'create_time' => time()
 | |
|             ];
 | |
| 
 | |
|             $insert = Db::table('yz_resources')->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 = Db::table('yz_resources')
 | |
|                 ->order('id DESC')
 | |
|                 ->select()
 | |
|                 ->each(function ($item, $key) {
 | |
|                     $item['create_time'] = time();
 | |
|                     return $item;
 | |
|                 });
 | |
|             View::assign([
 | |
|                 'lists' => $lists
 | |
|             ]);
 | |
|             return View::fetch();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 删除资源
 | |
|     public function delete()
 | |
|     {
 | |
|         $id = input('post.id');
 | |
|         $data = [
 | |
|             'delete_time' => time(),
 | |
|         ];
 | |
|         $delete = Db::table('yz_resources')->where('id', $id)->update($data);
 | |
|         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 = Db::table('yz_resources_category')
 | |
|                 ->where('delete_time', null)
 | |
|                 ->where('status', 1)
 | |
|                 ->order('sort asc, id asc')
 | |
|                 ->select()
 | |
|                 ->toArray();
 | |
| 
 | |
|             // 构建树形结构
 | |
|             $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 json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
 | |
|         }
 | |
|         
 | |
|         // 非 POST 请求返回视图
 | |
|         return View::fetch();
 | |
|     }
 | |
| 
 | |
|     //获取分类结构
 | |
|     public function getcate()
 | |
|     {
 | |
|         // 获取所有分类
 | |
|         $lists = Db::table('yz_resources_category')
 | |
|             ->where('delete_time', null)
 | |
|             ->where('status', 1)
 | |
|             ->order('sort asc, id asc')
 | |
|             ->select()
 | |
|             ->toArray();
 | |
| 
 | |
|         // 构建父子结构
 | |
|         $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 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 = Db::table('yz_resources_category')->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 = Db::table('yz_resources_category')
 | |
|                 ->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 = Db::table('yz_resources_category')
 | |
|                 ->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 = Db::table('yz_resources_category')->where('id', $id)->find();
 | |
|             
 | |
|             // 获取所有可选的父级分类
 | |
|             $parentCategories = Db::table('yz_resources_category')
 | |
|                 ->where('delete_time', null)
 | |
|                 ->where('status', 1)
 | |
|                 ->where('id', '<>', $id) // 排除自己
 | |
|                 ->where(function ($query) use ($id) {
 | |
|                     // 排除自己的所有子分类
 | |
|                     $query->where('cid', '<>', $id);
 | |
|                 })
 | |
|                 ->field('id, name, cid')
 | |
|                 ->select()
 | |
|                 ->toArray();
 | |
| 
 | |
|             // 构建父级分类选项
 | |
|             $parentOptions = [];
 | |
|             foreach ($parentCategories as $category) {
 | |
|                 if ($category['cid'] == 0) {
 | |
|                     $parentOptions[] = [
 | |
|                         'id' => $category['id'],
 | |
|                         'name' => $category['name']
 | |
|                     ];
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             return json([
 | |
|                 'code' => 0,
 | |
|                 'msg' => '获取成功',
 | |
|                 'data' => [
 | |
|                     'info' => $info,
 | |
|                     'parentOptions' => $parentOptions
 | |
|                 ]
 | |
|             ]);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //删除资源分类
 | |
|     public function catedel()
 | |
|     {
 | |
|         $id = input('post.id');
 | |
| 
 | |
|         // 检查是否有子分类
 | |
|         $hasChildren = Db::table('yz_resources_category')
 | |
|             ->where('cid', $id)
 | |
|             ->where('delete_time', null)
 | |
|             ->find();
 | |
| 
 | |
|         if ($hasChildren) {
 | |
|             Log::record('删除资源分类', 0, '该分类下有子分类,无法删除', '资源分类');
 | |
|             return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
 | |
|         }
 | |
| 
 | |
|         $delete = Db::table('yz_resources_category')
 | |
|             ->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 = Db::table('yz_resources')
 | |
|             ->where('id', $id)
 | |
|             ->where('delete_time', null)
 | |
|             ->find();
 | |
|             
 | |
|         if (!$resource) {
 | |
|             Log::record('获取资源详情', 0, '资源不存在', '资源管理');
 | |
|             return json(['code' => 1, 'msg' => '资源不存在']);
 | |
|         }
 | |
| 
 | |
|         // 获取分类信息
 | |
|         $cateInfo = Db::table('yz_resources_category')
 | |
|             ->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 edit()
 | |
|     {
 | |
|         if (Request::isPost()) {
 | |
|             $data = input('post.');
 | |
|             $id = input('id/d', 0);
 | |
|             
 | |
|             if (!$id) {
 | |
|                 Log::record('编辑资源', 0, '参数错误', '资源管理');
 | |
|                 return json(['code' => 1, 'msg' => '参数错误']);
 | |
|             }
 | |
| 
 | |
|             // 更新数据
 | |
|             $result = Db::table('yz_resources')->where('id', $id)->update([
 | |
|                 '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()
 | |
|             ]);
 | |
| 
 | |
|             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 = Db::table('yz_resources')->where('id', $id)->find();
 | |
|         if (!$resource) {
 | |
|             Log::record('编辑资源', 0, '资源不存在', '资源管理');
 | |
|             $this->error('资源不存在');
 | |
|         }
 | |
| 
 | |
|         View::assign('resource', $resource);
 | |
|         return View::fetch();
 | |
|     }
 | |
| 
 | |
|     //统计资源数量
 | |
|     public function counts()  {
 | |
|         $total = Db::table('yz_resources')
 | |
|             ->where('delete_time', null)
 | |
|             ->where('status', '<>', 3)
 | |
|             ->count();
 | |
|             
 | |
|         return json([
 | |
|             'code' => 0,
 | |
|             'msg' => '获取成功',
 | |
|             'data' => [
 | |
|                 'total' => $total
 | |
|             ]
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
| } | 
