SystemAttachment.php 4.4 KB

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