Attachment.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\common\repositories\system\attachment\AttachmentCategoryRepository;
  13. use app\common\repositories\system\attachment\AttachmentRepository;
  14. use crmeb\basic\BaseController;
  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. $ueditor = $this->request->param('ueditor');
  64. if (!$file)
  65. return app('json')->fail('请上传图片');
  66. $file = is_array($file) ? $file[0] : $file;
  67. if ($id) {
  68. if (!$category = $repository->get($id, $this->merId))
  69. return app('json')->fail('目录不存在');
  70. $info = [
  71. 'enname' => $category->attachment_category_enname,
  72. 'id' => $category->attachment_category_id
  73. ];
  74. } else {
  75. $info = [
  76. 'enname' => 'def',
  77. 'id' => 0
  78. ];
  79. }
  80. validate(["$field|图片" => [
  81. 'fileSize' => config('upload.filesize'),
  82. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  83. 'fileMime' => 'image/jpeg,image/png,image/gif',
  84. ]])->check([$field => $file]);
  85. $type = (int)systemConfig('upload_type') ?: 1;
  86. $upload = UploadService::create($type);
  87. $data = $upload->to($info['enname'])->move($field);
  88. if ($data === false) {
  89. return app('json')->fail($upload->getError());
  90. }
  91. $res = $upload->getUploadInfo();
  92. $res['dir'] = tidy_url($res['dir']);
  93. $_name = '.' . $file->getOriginalExtension();
  94. $data = [
  95. 'attachment_category_id' => $info['id'],
  96. 'attachment_name' => str_replace($_name, '', $file->getOriginalName()),
  97. 'attachment_src' => $res['dir']
  98. ];
  99. $this->repository->create($type, $this->merId, $this->request->adminId(), $data);
  100. if ($ueditor)
  101. return response([
  102. 'state' => 'SUCCESS',
  103. 'url' => $data['attachment_src'],
  104. 'title' => $data['attachment_src'],
  105. 'original' => $data['attachment_src'],
  106. ], 200, [], 'json');
  107. return app('json')->success(['src' => $data['attachment_src']]);
  108. }
  109. /**
  110. * 获取列表
  111. * @return Json
  112. * @throws DataNotFoundException
  113. * @throws DbException
  114. * @throws ModelNotFoundException
  115. * @author 张先生
  116. * @date 2020-03-27
  117. */
  118. public function getList()
  119. {
  120. [$page, $limit] = $this->getPage();
  121. $where = $this->request->params([['attachment_category_id', 0],'order','attachment_name']);
  122. $where['user_type'] = $this->merId;
  123. return app('json')->success($this->repository->getList($where, $page, $limit));
  124. }
  125. /**
  126. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  127. * @return mixed
  128. * @throws DbException
  129. * @author xaboy
  130. * @day 2020-04-16
  131. */
  132. public function batchChangeCategory(AttachmentCategoryRepository $attachmentCategoryRepository)
  133. {
  134. [$ids, $attachment_category_id] = $this->request->params([['ids', []], 'attachment_category_id'], true);
  135. if ($attachment_category_id && !$attachmentCategoryRepository->merExists($this->merId, $attachment_category_id))
  136. return app('json')->fail('分类不存在');
  137. if (!is_array($ids) || !count($ids))
  138. return app('json')->fail('请选择要修改分类的附件');
  139. $this->repository->batchChangeCategory(array_map('intval', $ids), intval($attachment_category_id), $this->merId);
  140. return app('json')->success('图片移动成功');
  141. }
  142. /**
  143. * 批量删除
  144. *
  145. * @return Json
  146. * @throws Exception
  147. * @author 张先生
  148. * @date 2020-03-30
  149. */
  150. public function delete()
  151. {
  152. $ids = (array)$this->request->param('ids', []);
  153. if (!count($ids))
  154. return app('json')->fail('数据不存在');
  155. $this->repository->batchDelete($ids, $this->merId);
  156. return app('json')->success('删除成功');
  157. }
  158. public function updateForm($id)
  159. {
  160. if(!$this->repository->getWhereCount(['attachment_id' => $id,'user_type' => $this->request->merId()]))
  161. return app('json')->fail('数据不存在');
  162. return app('json')->success(formToData($this->repository->form($id,$this->request->merId())));
  163. }
  164. public function update($id)
  165. {
  166. $data= $this->request->params(['attachment_name']);
  167. if(!$this->repository->getWhereCount(['attachment_id' => $id,'user_type' => $this->request->merId()]))
  168. return app('json')->fail('数据不存在');
  169. $this->repository->update($id,$data);
  170. return app('json')->success('修改成功');
  171. }
  172. }