SystemAttachment.php 4.5 KB

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