| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\system\controller\v1;
- use app\BaseController;
- use think\facade\Route as Url;
- use library\services\UtilService;
- use app\models\system\{
- Attachment as AttachmentModel, AttachmentCategory as Category
- };
- use think\Request;
- use library\basic\BaseUpload as Upload;
- use library\utils\Qiniu;
- /**
- * 图片管理类
- * Class Attachment
- * @package app\system\controller\v1
- */
- class Attachment extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 18],
- ['pid', 0]
- ]);
- return $this->success(AttachmentModel::getImageList($where));
- }
- /**
- * 删除指定资源
- *
- * @param string $ids
- * @return \think\Response
- */
- public function delete()
- {
- $request = app('request');
- $ids = $request->post('ids');
- $ids = explode(',', $ids);
- if (empty($ids))
- return $this->fail('请选择要删除的图片');
- foreach ($ids as $v) {
- self::deleteImg($v);
- }
- return $this->success('删除成功');
- }
- /**
- * 图片管理上传图片
- * @return \think\response\Json
- */
- public function upload()
- {
- [$pid, $file] = UtilService::getMore([
- ['pid', 0],
- ['file', 'file']
- ], $this->request, true);
- try {
- $path = make_path('attach', 2, true);
- $res = Upload::to($path)->validate()->move($file);
- if ($res === false) {
- return $this->fail(Upload::getError());
- } else {
- $img_url = Qiniu::updateFile('img', $path, $path);
- $fileInfo = Upload::getUploadInfo();
- if ($fileInfo) {
- AttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, 2, $fileInfo['time'], 1, 0);
- }
- return $this->success('上传成功', ['src' => $res->filePath]);
- }
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- }
- /**删除图片和数据记录
- * @param $att_id
- */
- public function deleteImg($att_id)
- {
- $attinfo = AttachmentModel::get($att_id);
- if ($attinfo) {
- try {
- Upload::delete($attinfo['name']);
- } catch (\Throwable $e) {
- }
- AttachmentModel::where('att_id', $att_id)->delete();
- }
- }
- /**
- * 移动图片分类显示
- */
- public function move($images)
- {
- $formbuider = [];
- $formbuider[] = Form::hidden('images', $images);
- $formbuider[] = Form::select('pid', '选择分类')->setOptions(function () {
- $list = Category::getCateList();
- $options = [['value' => 0, 'label' => '所有分类']];
- foreach ($list as $id => $cateName) {
- $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
- }
- return $options;
- })->filterable(1);
- return $this->makePostForm('编辑分类', $formbuider, Url::buildUrl('/file/do_move'), 'PUT');
- }
- /**
- * 移动图片分类操作
- */
- public function moveImageCate()
- {
- $data = Util::postMore([
- 'pid',
- 'images'
- ]);
- if ($data['images'] == '') return $this->fail('请选择图片');
- if (!$data['pid']) return $this->fail('请选择分类');
- $res = SystemAttachmentModel::where('att_id', 'in', $data['images'])->update(['pid' => $data['pid']]);
- if ($res)
- return $this->success('移动成功');
- else
- return $this->fail('移动失败!');
- }
- }
|