WebUploader.php 5.9 KB

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