WebUploader.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Support\WebUploader as WebUploaderHelper;
  5. use Illuminate\Support\Facades\URL;
  6. use Illuminate\Support\Str;
  7. /**
  8. * @property Form $form
  9. */
  10. trait WebUploader
  11. {
  12. /**
  13. * @param string $extensions exp. gif,jpg,jpeg,bmp,png
  14. * @param string|null $mimeTypes exp. image/*
  15. *
  16. * @return $this
  17. */
  18. public function accept(string $extensions, string $mimeTypes = null)
  19. {
  20. $this->options['accept'] = [
  21. 'extensions' => $extensions,
  22. ];
  23. if ($mimeTypes !== null) {
  24. $this->options['accept']['mimeTypes'] = $mimeTypes;
  25. }
  26. return $this;
  27. }
  28. /**
  29. * @param bool $value
  30. *
  31. * @return $this
  32. */
  33. public function chunked(bool $value = true)
  34. {
  35. $this->options['chunked'] = $value;
  36. return $this;
  37. }
  38. /**
  39. * @param int|null $size kb
  40. *
  41. * @return $this
  42. */
  43. public function chunkSize(int $size)
  44. {
  45. $this->options['chunkSize'] = $size * 1024;
  46. $this->checked(true);
  47. return $this;
  48. }
  49. /**
  50. * @param int $size kb
  51. *
  52. * @return $this
  53. */
  54. public function maxSize(int $size)
  55. {
  56. $this->rules('max:'.$size);
  57. $this->options['fileSingleSizeLimit'] = $size * 1024;
  58. return $this;
  59. }
  60. /**
  61. * @param int $num
  62. *
  63. * @return $this
  64. */
  65. public function threads(int $num)
  66. {
  67. $this->options['threads'] = $num;
  68. return $this;
  69. }
  70. /**
  71. * 设置上传接口.
  72. *
  73. * @param string $server
  74. *
  75. * @return $this
  76. */
  77. public function url(string $server)
  78. {
  79. $this->options['server'] = admin_url($server);
  80. $this->deleteUrl($server);
  81. return $this;
  82. }
  83. /**
  84. * 禁止上传文件后自动更新字段值.
  85. *
  86. * @param bool $value
  87. *
  88. * @return $this
  89. */
  90. public function autoSave(bool $value = true)
  91. {
  92. $this->options['autoUpdateColumn'] = $value;
  93. return $this;
  94. }
  95. /**
  96. * 禁用前端删除功能.
  97. *
  98. * @param bool $value
  99. *
  100. * @return $this
  101. */
  102. public function removeable(bool $value = true)
  103. {
  104. $this->options['disableRemove'] = ! $value;
  105. return $this;
  106. }
  107. /**
  108. * 设置图片删除地址.
  109. *
  110. * @param string $server
  111. *
  112. * @return $this
  113. */
  114. public function deleteUrl(string $server)
  115. {
  116. $this->options['deleteUrl'] = admin_url($server);
  117. return $this;
  118. }
  119. /**
  120. * 设置上传表单请求参数.
  121. *
  122. * @param array $data
  123. *
  124. * @return $this
  125. */
  126. public function withFormData(array $data)
  127. {
  128. $this->options['formData'] = array_merge($this->options['formData'], $data);
  129. return $this;
  130. }
  131. /**
  132. * 设置删除图片请求参数.
  133. *
  134. * @param array $data
  135. *
  136. * @return $this
  137. */
  138. public function withDeleteData(array $data)
  139. {
  140. $this->options['deleteData'] = array_merge($this->options['deleteData'], $data);
  141. return $this;
  142. }
  143. /**
  144. * 是否开启自动上传.
  145. *
  146. * @param bool $value
  147. *
  148. * @return $this
  149. */
  150. public function autoUpload(bool $value = true)
  151. {
  152. $this->options['autoUpload'] = $value;
  153. return $this;
  154. }
  155. /**
  156. * 是否开启图片压缩.
  157. *
  158. * @param bool|array $compress
  159. *
  160. * @return $this
  161. */
  162. public function compress($compress = true)
  163. {
  164. $this->options['compress'] = $compress;
  165. return $this;
  166. }
  167. /**
  168. * 默认上传配置.
  169. *
  170. * @return void
  171. */
  172. protected function setUpDefaultOptions()
  173. {
  174. $key = optional($this->form)->getKey();
  175. $defaultOptions = [
  176. 'name' => WebUploaderHelper::FILE_NAME,
  177. 'fileVal' => WebUploaderHelper::FILE_NAME,
  178. 'isImage' => false,
  179. 'disableRemove' => false,
  180. 'chunked' => false,
  181. 'fileNumLimit' => 10,
  182. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  183. 'disableGlobalDnd' => true,
  184. 'fileSizeLimit' => 20971520000, // 20000M
  185. 'fileSingleSizeLimit' => 10485760, // 10M
  186. 'elementName' => $this->getElementName(), // 字段name属性值
  187. 'lang' => trans('admin.uploader'),
  188. 'compress' => false,
  189. 'deleteData' => [
  190. static::FILE_DELETE_FLAG => '',
  191. 'primary_key' => $key,
  192. ],
  193. 'formData' => [
  194. '_id' => Str::random(),
  195. '_token' => csrf_token(),
  196. 'upload_column' => $this->column(),
  197. 'primary_key' => $key,
  198. ],
  199. ];
  200. $this->options($defaultOptions);
  201. }
  202. protected function setDefaultServer()
  203. {
  204. if (! $this->form || ! method_exists($this->form, 'action')) {
  205. return;
  206. }
  207. if (empty($this->options['server'])) {
  208. $this->options['server'] = $this->form->action();
  209. }
  210. if (empty($this->options['updateServer'])) {
  211. $this->options['updateServer'] = $this->form->action();
  212. }
  213. if (empty($this->options['deleteUrl'])) {
  214. $this->options['deleteUrl'] = $this->form->action();
  215. }
  216. if (
  217. method_exists($this->form, 'builder')
  218. && $this->form->builder()
  219. && $this->form->builder()->isEditing()
  220. ) {
  221. $this->options['formData']['_method'] = 'PUT';
  222. $this->options['deleteData']['_method'] = 'PUT';
  223. if (! isset($this->options['autoUpdateColumn'])) {
  224. $this->options['autoUpdateColumn'] = true;
  225. }
  226. }
  227. }
  228. /**
  229. * 获取创建链接.
  230. *
  231. * @return string
  232. */
  233. public function getCreateUrl()
  234. {
  235. return str_replace('/create', '', URL::full());
  236. }
  237. /**
  238. * 图片预览设置.
  239. *
  240. * @return void
  241. */
  242. protected function setupPreviewOptions()
  243. {
  244. $this->options['preview'] = $this->initialPreviewConfig();
  245. }
  246. }