311 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			311 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * @copyright Copyright (c) 2023-2024 美天智能科技
 | |
|  * @link http://www.meteteme.com
 | |
|  */
 | |
| 
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace app\download\controller;
 | |
| 
 | |
| use app\base\BaseController;
 | |
| use app\model\Download as DownloadList;
 | |
| use app\model\DownloadCate as DownloadCateList;
 | |
| use think\facade\Db;
 | |
| use think\facade\View;
 | |
| 
 | |
| class Index extends BaseController
 | |
| {
 | |
| 
 | |
|     public function index()
 | |
|     {
 | |
|         if (request()->isAjax()) {
 | |
|             $param = get_params();
 | |
|             $rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
 | |
| 
 | |
|             // 获取域名
 | |
|             $domain = 'https://project.meteteme.com/';
 | |
| 
 | |
|             // 获取分类列表并转换为键值对数组
 | |
|             $categories = DownloadCateList::whereNull('delete_time')
 | |
|                 ->column('name', 'id');
 | |
| 
 | |
|             // 获取用户列表并转换为键值对数组
 | |
|             $users = Db::name('Admin')->column('name', 'id');
 | |
| 
 | |
|             // 构建查询条件
 | |
|             $query = DownloadList::whereNull('delete_time');
 | |
| 
 | |
|             // 根据筛选条件添加查询条件
 | |
|             if (!empty($param['name'])) {
 | |
|                 $query->where('name', 'like', '%' . $param['name'] . '%');
 | |
|             }
 | |
|             if (!empty($param['cid'])) {
 | |
|                 // 根据分类名称筛选
 | |
|                 $categoryId = array_search($param['cid'], $categories); // 将分类名称转换为分类 ID
 | |
|                 if ($categoryId !== false) {
 | |
|                     $query->where('cid', $categoryId);
 | |
|                 } else {
 | |
|                     // 如果分类名称不存在,返回空结果
 | |
|                     return table_assign(0, '', ['data' => [], 'total' => 0]);
 | |
|                 }
 | |
|             }
 | |
|             if (!empty($param['uid'])) {
 | |
|                 $query->where('uid', $param['uid']);
 | |
|             }
 | |
| 
 | |
|             // 获取分页数据
 | |
|             $list = $query->field('id, cid, uid, name, version, path, description, download_url, download_code, size, type')
 | |
|                 ->order('id desc')
 | |
|                 ->paginate($rows, false);
 | |
| 
 | |
|             // 反查数据
 | |
|             $list->each(function ($item) use ($domain, $categories, $users) {
 | |
|                 $item['path'] = $domain . $item['path'];
 | |
|                 $item['cid'] = isset($categories[$item['cid']]) ? $categories[$item['cid']] : '未知分类';
 | |
|                 $item['uid'] = isset($users[$item['uid']]) ? $users[$item['uid']] : '未知用户';
 | |
|                 return $item;
 | |
|             });
 | |
| 
 | |
|             return table_assign(0, '', $list->toArray());
 | |
|         } else {
 | |
|             return view();
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
|     // 添加
 | |
|     public function add()
 | |
|     {
 | |
|         $param = get_params(); // 获取请求参数
 | |
|         $uid = $this->uid; // 当前用户 ID
 | |
| 
 | |
|         if (request()->isPost()) {
 | |
|             // 验证必填字段
 | |
|             if (empty($param['name'])) {
 | |
|                 return to_assign(1, '软件名称不能为空');
 | |
|             }
 | |
|             if (empty($param['cid'])) {
 | |
|                 return to_assign(1, '请选择软件分类');
 | |
|             }
 | |
| 
 | |
|             // 添加逻辑
 | |
|             $param['uid'] = $uid; // 设置上传用户
 | |
|             $param['create_time'] = date('Y-m-d H:i:s'); // 创建时间
 | |
| 
 | |
|             // 插入数据并获取自增 ID
 | |
|             $sid = DownloadList::strict(false)->field(true)->insertGetId($param);
 | |
| 
 | |
|             if ($sid) {
 | |
|                 // 添加日志
 | |
|                 add_log('add', $sid, $param);
 | |
|                 $log_data = [
 | |
|                     'module' => 'download',
 | |
|                     'download_id' => $sid,
 | |
|                     'new_content' => $param['name'],
 | |
|                     'field' => 'new',
 | |
|                     'action' => 'add',
 | |
|                     'admin_id' => $uid,
 | |
|                     'create_time' => time(),
 | |
|                 ];
 | |
|                 Db::name('Log')->strict(false)->field(true)->insert($log_data);
 | |
| 
 | |
|                 return to_assign(0, '添加成功', ['redirect' => 'javascript:history.back()']);
 | |
|             } else {
 | |
|                 return to_assign(1, '添加失败');
 | |
|             }
 | |
|         } else {
 | |
|             // 非 POST 请求,返回视图
 | |
|             return view();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //编辑
 | |
|     public function edit()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         $id = isset($param['id']) ? $param['id'] : 0;
 | |
|         $uid = $this->uid;
 | |
| 
 | |
|         $download = DownloadList::where('id', $id)
 | |
|             ->field('id, cid, uid, name, version, description, path, download_url, download_code')
 | |
|             ->find();
 | |
| 
 | |
|         if ($download) {
 | |
|             View::assign('detail', $download);
 | |
|         } else {
 | |
|             View::assign('message', '没有详细数据');
 | |
|         }
 | |
| 
 | |
|         if (request()->isPost()) {
 | |
|             $data = [
 | |
|                 'cid' => $param['cid'],
 | |
|                 'uid' => $uid,
 | |
|                 'name' => $param['name'],
 | |
|                 'version' => $param['version'],
 | |
|                 'description' => $param['description'],
 | |
|                 'path' => $param['path'],
 | |
|                 'download_url' => $param['download_url'],
 | |
|                 'download_code' => $param['download_code'],
 | |
|                 'update_time' => date('Y-m-d H:i:s'),
 | |
|             ];
 | |
| 
 | |
|             $res = DownloadList::where('id', $id)->update($data);
 | |
| 
 | |
|             if ($res) {
 | |
|                 return to_assign(2, '更新成功!');
 | |
|             } else {
 | |
|                 return to_assign(1, '更新失败');
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return view();
 | |
|     }
 | |
| 
 | |
|     //查看
 | |
|     public function view()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         $id = isset($param['id']) ? $param['id'] : 0;
 | |
|         $detail = (new DownloadList())->detail($id);
 | |
|         //        print_r($id);
 | |
|         if (empty($detail)) {
 | |
|             return to_assign(1, '2.信息不存在');
 | |
|         } else {
 | |
|             if ($this->uid != $detail['admin_id']) { // 当前登录用户必须是上传者才能查看
 | |
|                 return to_assign(1, '您没权限查看该信息');
 | |
|             }
 | |
|             $role_uid = [$detail['admin_id'], $detail['director_uid']];
 | |
|             $role_edit = 'edit';
 | |
|             $view_uid = $role_uid;
 | |
|             // if (!empty($detail['check_admin_ids'])) {
 | |
|             //     $view_uid = array_merge($role_uid, explode(",", $detail['check_admin_ids']));
 | |
|             // }
 | |
|             if (!in_array($this->uid, $view_uid) && $detail['is_open'] == 1) {
 | |
|                 return to_assign(1, '您没权限查看该信息');
 | |
|             }
 | |
|             $contact_array = Db::name('Businesscontact')
 | |
|                 ->field('i.id,i.business_id,i.contact,i.post,i.phone,i.email,i.wechat,i.qq,a.name as admin_name')
 | |
|                 ->alias('i')
 | |
|                 ->join('Admin a', 'i.admin_id = a.id', 'LEFT')
 | |
|                 ->join('DownloadList b', 'i.business_id = b.id', 'LEFT')
 | |
|                 ->order('i.create_time desc')
 | |
|                 ->where(array('i.business_id' => $id))
 | |
|                 ->where('i.delete_time', null)
 | |
|                 ->select()->toArray();
 | |
| 
 | |
|             $file_array = Db::name('FileInterfix')
 | |
|                 ->field('i.id,i.topic_id,i.admin_id,f.name,f.filesize,f.filepath,a.name as admin_name')
 | |
|                 ->alias('i')
 | |
|                 ->join('File f', 'i.file_id = f.id', 'LEFT')
 | |
|                 ->join('Admin a', 'i.admin_id = a.id', 'LEFT')
 | |
|                 ->order('i.create_time desc')
 | |
|                 ->where(array('i.topic_id' => $id, 'i.module' => 'download'))
 | |
|                 ->select()->toArray();
 | |
| 
 | |
|             $link_array = Db::name('LinkInterfix')
 | |
|                 ->field('i.id,i.topic_id,i.admin_id,i.desc,i.url,a.name as admin_name')
 | |
|                 ->alias('i')
 | |
|                 ->join('Admin a', 'i.admin_id = a.id', 'LEFT')
 | |
|                 ->order('i.create_time desc')
 | |
|                 ->where(array('i.topic_id' => $id, 'i.module' => 'download', 'delete_time' => 0))
 | |
|                 ->select()->toArray();
 | |
|             View::assign('detail', $detail);
 | |
|             View::assign('contact_array', $contact_array);
 | |
|             View::assign('file_array', $file_array);
 | |
|             View::assign('link_array', $link_array);
 | |
|             View::assign('role_edit', $role_edit);
 | |
|             View::assign('id', $id);
 | |
|             return view();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //删除
 | |
|     public function delete()
 | |
|     {
 | |
| 
 | |
|         $param = get_params();
 | |
|         $request = request();
 | |
| 
 | |
|         if ($request->isDelete() || $request->isAjax()) {
 | |
|             $id = $param['id'];
 | |
|             if (empty($id)) {
 | |
|                 return to_assign(1, "删除,缺少必要的参数");
 | |
|             }
 | |
| 
 | |
|             $detail = Db::name('Download')->where('id', $id)->find();
 | |
|             if (!$detail) {
 | |
|                 return to_assign(1, "删除,记录不存在或缺少必要字段");
 | |
|             }
 | |
| 
 | |
|             if ($detail['uid'] != $this->uid) {
 | |
|                 return to_assign(1, "你不是图片的上传人,无权限删除");
 | |
|             }
 | |
| 
 | |
|             if (Db::name('Download')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
 | |
|                 $log_data = [
 | |
|                     'module' => 'download',
 | |
|                     'field' => 'delete',
 | |
|                     'action' => 'delete',
 | |
|                     'admin_id' => $this->uid,
 | |
|                     'old_content' => '',
 | |
|                     'new_content' => $detail['name'],
 | |
|                     'create_time' => time(),
 | |
|                 ];
 | |
|                 Db::name('Log')->strict(false)->field(true)->insert($log_data);
 | |
|                 return to_assign(0, "删除成功");
 | |
|             } else {
 | |
|                 return to_assign(0, "删除失败");
 | |
|             }
 | |
|         } else {
 | |
|             return to_assign(1, "错误的请求");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //批量删除
 | |
|     public function batch_delete()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         $request = request();
 | |
|         if ($request->isDelete() || $request->isAjax()) {
 | |
|             $ids = $param['ids'];
 | |
|             if (empty($ids) || !is_array($ids)) {
 | |
|                 return to_assign(1, "批量删除,缺少必要的参数");
 | |
|             }
 | |
| 
 | |
|             foreach ($ids as $id) {
 | |
|                 $detail = Db::name('Download')->where('id', $id)->find();
 | |
|                 if (!$detail || !isset($detail['uid'])) {
 | |
|                     continue; // 跳过不存在的记录
 | |
|                 }
 | |
| 
 | |
|                 if (Db::name('Download')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
 | |
|                     $log_data = [
 | |
|                         'module' => 'Download',
 | |
|                         'field' => 'delete',
 | |
|                         'action' => 'delete',
 | |
|                         'admin_id' => $this->uid,
 | |
|                         'old_content' => '',
 | |
|                         'new_content' => $detail['name'],
 | |
|                         'create_time' => time(),
 | |
|                     ];
 | |
|                     Db::name('Log')->strict(false)->field(true)->insert($log_data);
 | |
|                 }
 | |
|             }
 | |
|             return to_assign(0, "批量删除成功");
 | |
|         } else {
 | |
|             return to_assign(1, "错误的请求");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //获取软件分类
 | |
|     public function catelist()
 | |
|     {
 | |
|         $catelist = DownloadCateList::field('id, name, description, sort')
 | |
|             ->where('delete_time', null)
 | |
|             ->select();
 | |
|         return json(['code' => 0, 'msg' => '', 'data' => $catelist]);
 | |
|     }
 | |
| 
 | |
| } |