Attachment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\controller\admin\system\attachment;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\attachment\AttachmentCategoryRepository;
  5. use app\common\repositories\system\attachment\AttachmentRepository;
  6. use ln\services\UploadService;
  7. use Exception;
  8. use think\App;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\response\Json;
  13. /**
  14. * Class Attachment
  15. * @package app\controller\admin\system\attachment
  16. * @author zfy
  17. * @day 2020-04-16
  18. */
  19. class Attachment extends BaseController
  20. {
  21. /**
  22. * @var AttachmentRepository
  23. */
  24. protected $repository;
  25. /**
  26. * @var int
  27. */
  28. protected $merId;
  29. /**
  30. * Attachment constructor.
  31. * @param App $app
  32. * @param AttachmentRepository $repository
  33. */
  34. public function __construct(App $app, AttachmentRepository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. $this->merId = $this->request->merId();
  39. }
  40. /**
  41. * @param int $id
  42. * @param string $field
  43. * @param AttachmentCategoryRepository $repository
  44. * @return mixed
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. * @author zfy
  49. * @day 2020-04-15
  50. */
  51. public function image($id,$field, AttachmentCategoryRepository $repository)
  52. {
  53. $file = $this->request->file($field);
  54. if (!$file)
  55. return app('json')->fail('请上传图片');
  56. $file = is_array($file) ? $file[0] : $file;
  57. if ($id) {
  58. if (!$category = $repository->get($id, $this->merId))
  59. return app('json')->fail('目录不存在');
  60. $info = [
  61. 'enname' => $category->attachment_category_enname,
  62. 'id' => $category->attachment_category_id
  63. ];
  64. } else {
  65. $info = [
  66. 'enname' => 'def',
  67. 'id' => 0
  68. ];
  69. }
  70. validate(["$field|图片" => [
  71. 'fileSize' => config('upload.filesize'),
  72. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  73. 'fileMime' => 'image/jpeg,image/png,image/gif',
  74. ]])->check([$field => $file]);
  75. $upload = UploadService::create();
  76. $data = $upload->to($info['enname'])->move($field);
  77. if ($data === false) {
  78. return app('json')->fail($upload->getError());
  79. }
  80. $res = $upload->getUploadInfo();
  81. $res['dir'] = tidy_url($res['dir']);
  82. $_name = '.'.$file->getOriginalExtension();
  83. $data = [
  84. 'attachment_category_id' => $info['id'],
  85. 'attachment_name' => str_replace($_name,'',$file->getOriginalName()),
  86. 'attachment_src' => $res['dir']
  87. ];
  88. $this->repository->create(1, $this->merId, $this->request->adminId(), $data);
  89. return app('json')->success(['src' => $data['attachment_src']]);
  90. }
  91. /**
  92. * 获取列表
  93. * @return Json
  94. * @throws DataNotFoundException
  95. * @throws DbException
  96. * @throws ModelNotFoundException
  97. * @author 张先生
  98. * @date 2020-03-27
  99. */
  100. public function getList()
  101. {
  102. [$page, $limit] = $this->getPage();
  103. $where = $this->request->params([['attachment_category_id', 0],'order','attachment_name']);
  104. $where['user_type'] = $this->merId;
  105. return app('json')->success($this->repository->getList($where, $page, $limit));
  106. }
  107. /**
  108. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  109. * @return mixed
  110. * @throws DbException
  111. * @author zfy
  112. * @day 2020-04-16
  113. */
  114. public function batchChangeCategory(AttachmentCategoryRepository $attachmentCategoryRepository)
  115. {
  116. [$ids, $attachment_category_id] = $this->request->params([['ids', []], 'attachment_category_id'], true);
  117. if ($attachment_category_id && !$attachmentCategoryRepository->merExists($this->merId, $attachment_category_id))
  118. return app('json')->fail('分类不存在');
  119. if (!is_array($ids) || !count($ids))
  120. return app('json')->fail('请选择要修改分类的附件');
  121. $this->repository->batchChangeCategory(array_map('intval', $ids), intval($attachment_category_id), $this->merId);
  122. return app('json')->success('图片移动成功');
  123. }
  124. /**
  125. * 批量删除
  126. *
  127. * @return Json
  128. * @throws Exception
  129. * @author 张先生
  130. * @date 2020-03-30
  131. */
  132. public function delete()
  133. {
  134. $ids = (array)$this->request->param('ids', []);
  135. if (!count($ids))
  136. return app('json')->fail('数据不存在');
  137. $this->repository->batchDelete($ids, $this->merId);
  138. return app('json')->success('删除成功');
  139. }
  140. public function updateForm($id)
  141. {
  142. if(!$this->repository->getWhereCount(['attachment_id' => $id,'user_type' => $this->request->merId()]))
  143. return app('json')->fail('数据不存在');
  144. return app('json')->success(formToData($this->repository->form($id,$this->request->merId())));
  145. }
  146. public function update($id)
  147. {
  148. $data= $this->request->params(['attachment_name']);
  149. if(!$this->repository->getWhereCount(['attachment_id' => $id,'user_type' => $this->request->merId()]))
  150. return app('json')->fail('数据不存在');
  151. $this->repository->update($id,$data);
  152. return app('json')->success('修改成功');
  153. }
  154. }