Attachment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\system\controller\v1;
  3. use app\BaseController;
  4. use think\facade\Route as Url;
  5. use library\services\UtilService;
  6. use app\models\system\{
  7. Attachment as AttachmentModel, AttachmentCategory as Category
  8. };
  9. use think\Request;
  10. use library\basic\BaseUpload as Upload;
  11. use library\utils\Qiniu;
  12. /**
  13. * 图片管理类
  14. * Class Attachment
  15. * @package app\system\controller\v1
  16. */
  17. class Attachment extends BaseController
  18. {
  19. /**
  20. * 显示资源列表
  21. *
  22. * @return \think\Response
  23. */
  24. public function index()
  25. {
  26. $where = UtilService::getMore([
  27. ['page', 1],
  28. ['limit', 18],
  29. ['pid', 0]
  30. ]);
  31. return $this->success(AttachmentModel::getImageList($where));
  32. }
  33. /**
  34. * 删除指定资源
  35. *
  36. * @param string $ids
  37. * @return \think\Response
  38. */
  39. public function delete()
  40. {
  41. $request = app('request');
  42. $ids = $request->post('ids');
  43. $ids = explode(',', $ids);
  44. if (empty($ids))
  45. return $this->fail('请选择要删除的图片');
  46. foreach ($ids as $v) {
  47. self::deleteImg($v);
  48. }
  49. return $this->success('删除成功');
  50. }
  51. /**
  52. * 图片管理上传图片
  53. * @return \think\response\Json
  54. */
  55. public function upload()
  56. {
  57. [$pid, $file] = UtilService::getMore([
  58. ['pid', 0],
  59. ['file', 'file']
  60. ], $this->request, true);
  61. try {
  62. $path = make_path('attach', 2, true);
  63. $res = Upload::to($path)->validate()->move($file);
  64. if ($res === false) {
  65. return $this->fail(Upload::getError());
  66. } else {
  67. $img_url = Qiniu::updateFile('img', $path, $path);
  68. $fileInfo = Upload::getUploadInfo();
  69. if ($fileInfo) {
  70. AttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, 2, $fileInfo['time'], 1, 0);
  71. }
  72. return $this->success('上传成功', ['src' => $res->filePath]);
  73. }
  74. } catch (\Exception $e) {
  75. return $this->fail($e->getMessage());
  76. }
  77. }
  78. /**删除图片和数据记录
  79. * @param $att_id
  80. */
  81. public function deleteImg($att_id)
  82. {
  83. $attinfo = AttachmentModel::get($att_id);
  84. if ($attinfo) {
  85. try {
  86. Upload::delete($attinfo['name']);
  87. } catch (\Throwable $e) {
  88. }
  89. AttachmentModel::where('att_id', $att_id)->delete();
  90. }
  91. }
  92. /**
  93. * 移动图片分类显示
  94. */
  95. public function move($images)
  96. {
  97. $formbuider = [];
  98. $formbuider[] = Form::hidden('images', $images);
  99. $formbuider[] = Form::select('pid', '选择分类')->setOptions(function () {
  100. $list = Category::getCateList();
  101. $options = [['value' => 0, 'label' => '所有分类']];
  102. foreach ($list as $id => $cateName) {
  103. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  104. }
  105. return $options;
  106. })->filterable(1);
  107. return $this->makePostForm('编辑分类', $formbuider, Url::buildUrl('/file/do_move'), 'PUT');
  108. }
  109. /**
  110. * 移动图片分类操作
  111. */
  112. public function moveImageCate()
  113. {
  114. $data = Util::postMore([
  115. 'pid',
  116. 'images'
  117. ]);
  118. if ($data['images'] == '') return $this->fail('请选择图片');
  119. if (!$data['pid']) return $this->fail('请选择分类');
  120. $res = SystemAttachmentModel::where('att_id', 'in', $data['images'])->update(['pid' => $data['pid']]);
  121. if ($res)
  122. return $this->success('移动成功');
  123. else
  124. return $this->fail('移动失败!');
  125. }
  126. }