WebUploader.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * Disable remove file.
  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. * Set upload server.
  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. * @param array $data
  124. *
  125. * @return $this
  126. */
  127. public function withFormData(array $data)
  128. {
  129. $this->options['formData'] = array_merge($this->options['formData'], $data);
  130. return $this;
  131. }
  132. /**
  133. * 是否开启自动上传.
  134. *
  135. * @param bool $value
  136. *
  137. * @return $this
  138. */
  139. public function autoUpload(bool $value = true)
  140. {
  141. $this->options['autoUpload'] = $value;
  142. return $this;
  143. }
  144. /**
  145. * Set default options form file field.
  146. *
  147. * @return void
  148. */
  149. protected function setupDefaultOptions()
  150. {
  151. $defaultOptions = [
  152. 'name' => WebUploaderHelper::FILE_NAME,
  153. 'fileVal' => WebUploaderHelper::FILE_NAME,
  154. 'isImage' => false,
  155. 'disableRemove' => false,
  156. 'chunked' => true,
  157. 'fileNumLimit' => 10,
  158. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  159. 'disableGlobalDnd' => true,
  160. 'fileSizeLimit' => 20971520000, // 20000M
  161. 'fileSingleSizeLimit' => 10485760, // 10M
  162. 'elementName' => $this->getElementName(), // 字段name属性值
  163. 'lang' => trans('admin.uploader'),
  164. 'compress' => false,
  165. 'deleteData' => [
  166. static::FILE_DELETE_FLAG => '',
  167. '_token' => csrf_token(),
  168. ],
  169. 'formData' => [
  170. '_id' => Str::random(),
  171. '_token' => csrf_token(),
  172. 'upload_column' => $this->column(),
  173. ],
  174. ];
  175. $this->options($defaultOptions);
  176. }
  177. protected function setDefaultServer()
  178. {
  179. if (! $this->form || ! method_exists($this->form, 'action')) {
  180. return;
  181. }
  182. if (empty($this->options['server'])) {
  183. $this->options['server'] = $this->form->action();
  184. }
  185. if (empty($this->options['updateServer'])) {
  186. $this->options['updateServer'] = $this->form->action();
  187. }
  188. if (empty($this->options['deleteUrl'])) {
  189. $this->options['deleteUrl'] = $this->form->action();
  190. }
  191. if (
  192. method_exists($this->form, 'builder')
  193. && $this->form->builder()
  194. && $this->form->builder()->isEditing()
  195. ) {
  196. $this->options['formData']['_method'] = 'PUT';
  197. $this->options['deleteData']['_method'] = 'PUT';
  198. if (! isset($this->options['autoUpdateColumn'])) {
  199. $this->options['autoUpdateColumn'] = true;
  200. }
  201. }
  202. }
  203. /**
  204. * 获取创建链接.
  205. *
  206. * @return string
  207. */
  208. public function getCreateUrl()
  209. {
  210. return str_replace('/create', '', URL::full());
  211. }
  212. /**
  213. * 图片预览设置.
  214. *
  215. * @return void
  216. */
  217. protected function setupPreviewOptions()
  218. {
  219. $this->options['preview'] = $this->initialPreviewConfig();
  220. }
  221. }