Attachment.php 6.0 KB

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