164 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			164 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
|  | <?php | |||
|  | 
 | |||
|  | namespace app\controller\manager; | |||
|  | 
 | |||
|  | use app\model\{DownloadModel, Category as MCategory, Log as MLog, System}; | |||
|  | use think\exception\ValidateException; | |||
|  | use app\validate\{DownloadValidate}; | |||
|  | use think\facade\Db; | |||
|  | 
 | |||
|  | /** | |||
|  |  * 下载中心 | |||
|  |  * Class Download | |||
|  |  * @package app\controller\manager | |||
|  |  */ | |||
|  | class Download extends Base | |||
|  | { | |||
|  |     public function add() | |||
|  |     { | |||
|  |         if (request()->isPost()) { | |||
|  |             $params = input('post.item/a', []); | |||
|  |             $file   = input('file'); | |||
|  |             $params = arrayHtmlFilter($params); | |||
|  |             try { | |||
|  |                 $fileInfo = \app\model\File::where('src', $file)->order('id', 'desc')->find(); | |||
|  |                 if (!$fileInfo) { | |||
|  |                     return $this->json(1, '文件不存在,请重新上传'); | |||
|  |                 } | |||
|  |                 $params['size']      = $fileInfo['size']; | |||
|  |                 $params['mime_type'] = $fileInfo['mime_type']; | |||
|  |                 $params['suffix']    = $fileInfo['suffix']; | |||
|  |                 $params['file']      = $fileInfo['src']; | |||
|  | 
 | |||
|  |                 validate(DownloadValidate::class)->check($params); | |||
|  |                 $newItem = DownloadModel::create($params); | |||
|  |                 MLog::write('download', 'add', '新增下载中心,ID:'.$newItem->id); | |||
|  |             } catch (ValidateException $e) { | |||
|  |                 return $this->json(1, $e->getError()); | |||
|  |             } | |||
|  |             return $this->json(); | |||
|  |         } else { | |||
|  |             $categoryId             = input('param.category_id/d', 0); | |||
|  |             $category               = MCategory::getById($categoryId); | |||
|  |             $this->data['category'] = $category; | |||
|  |             return $this->view(); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     public function edit() | |||
|  |     { | |||
|  |         $id   = input('param.id/d', 0); | |||
|  |         $item = DownloadModel::getById($id); | |||
|  |         if (count($item) == 0) { | |||
|  |             return $this->json(1, '该记录不存在'); | |||
|  |         } | |||
|  |         if (request()->isPost()) { | |||
|  |             $params = input('post.item/a', []); | |||
|  |             $file   = input('file'); | |||
|  |             $params = arrayHtmlFilter($params); | |||
|  |             try { | |||
|  |                 if (!empty($file)) { | |||
|  |                     $fileInfo = \app\model\File::where('src', $file)->order('id', 'desc')->find(); | |||
|  |                     if (!$fileInfo) { | |||
|  |                         return $this->json(1, '文件不存在,请重新上传'); | |||
|  |                     } | |||
|  |                     $params['size']      = $fileInfo['size']; | |||
|  |                     $params['mime_type'] = $fileInfo['mime_type']; | |||
|  |                     $params['suffix']    = $fileInfo['suffix']; | |||
|  |                     $params['file']      = $fileInfo['src']; | |||
|  |                 } | |||
|  | 
 | |||
|  |                 validate(DownloadValidate::class)->check($params); | |||
|  |                 DownloadModel::updateById($id, $params); | |||
|  |                 MLog::write('download', 'edit', '修改下载中心,ID:'.$id); | |||
|  |             } catch (ValidateException $e) { | |||
|  |                 return $this->json(2, $e->getError()); | |||
|  |             } | |||
|  |             return $this->json(); | |||
|  |         } else { | |||
|  |             $this->data['item'] = $item; | |||
|  |             return $this->view(); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     public function sort() | |||
|  |     { | |||
|  |         if (request()->isPost()) { | |||
|  |             $id   = input('post.id/d'); | |||
|  |             $sort = input('post.sort'); | |||
|  |             $num  = input('post.num/d', 1); | |||
|  |             if ($num <= 0) { | |||
|  |                 $num = 1; | |||
|  |             } | |||
|  |             if (!in_array($sort, ['up', 'down'], true)) { | |||
|  |                 return $this->json(2, '参数错误'); | |||
|  |             } | |||
|  |             $item = DownloadModel::getById($id); | |||
|  |             if (empty($item)) { | |||
|  |                 return $this->json(3, '该记录不存在'); | |||
|  |             } | |||
|  |             if ($sort == 'up') { | |||
|  |                 $where = "category_id='{$item['category_id']}' and sort < {$item['sort']}"; | |||
|  |                 $order = "sort desc"; | |||
|  |             } else { | |||
|  |                 $where = "category_id='{$item['category_id']}' and sort > {$item['sort']}"; | |||
|  |                 $order = "sort asc"; | |||
|  |             } | |||
|  |             $forSortItems = DownloadModel::getListByWhereAndOrder($where, $order, $num); | |||
|  |             if (!empty($forSortItems)) { | |||
|  |                 $updateData   = []; | |||
|  |                 $forSortCount = count($forSortItems); | |||
|  |                 for ($i = 0; $i < $forSortCount; $i++) { | |||
|  |                     if ($i == 0) { | |||
|  |                         $updateData[] = [ | |||
|  |                             'id'   => $forSortItems[$i]['id'], | |||
|  |                             'sort' => $item['sort'] | |||
|  |                         ]; | |||
|  |                     } else { | |||
|  |                         $updateData[] = [ | |||
|  |                             'id'   => $forSortItems[$i]['id'], | |||
|  |                             'sort' => $forSortItems[$i - 1]['sort'] | |||
|  |                         ]; | |||
|  |                     } | |||
|  |                 } | |||
|  |                 $updateData[] = [ | |||
|  |                     'id'   => $item['id'], | |||
|  |                     'sort' => $forSortItems[$i - 1]['sort'] | |||
|  |                 ]; | |||
|  |                 if (!empty($updateData)) { | |||
|  |                     $model = new DownloadModel(); | |||
|  |                     $model->saveAll($updateData); | |||
|  |                     $sortStr = $sort == 'up' ? '上移' : '下调'; | |||
|  |                     MLog::write('download', 'sort', "下载中心排序,ID:{$id} ,{$sortStr}了{$num}位"); | |||
|  |                     return $this->json(); | |||
|  |                 } | |||
|  |             } | |||
|  |             return $this->json(4, '无须调整排序!'); | |||
|  |         } | |||
|  |         return $this->json(1, '无此操作'); | |||
|  |     } | |||
|  | 
 | |||
|  |     // 删除
 | |||
|  |     public function del() | |||
|  |     { | |||
|  |         if (request()->isPost()) { | |||
|  |             $downloadId = input('param.id/d', 0); | |||
|  |             $item       = DownloadModel::getById($downloadId); | |||
|  |             if (count($item) == 0) { | |||
|  |                 return $this->json(2, '该记录不存在'); | |||
|  |             } | |||
|  |             Db::startTrans(); | |||
|  |             try { | |||
|  |                 @unlink(public_path().$item['file'] ?? ''); | |||
|  |                 DownloadModel::destroy($downloadId); | |||
|  |                 MLog::write('download', 'del', '删除历程,ID:'.$downloadId); | |||
|  |                 Db::commit(); | |||
|  |             } catch (\Exception $e) { | |||
|  |                 Db::rollback(); | |||
|  |                 return $this->json(3, '删除失败,'.$e->getMessage()); | |||
|  |             } | |||
|  |             return $this->json(); | |||
|  |         } | |||
|  |         return $this->json(1, '无此操作'); | |||
|  |     } | |||
|  | } |