WebUploader.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. * @return $this
  90. */
  91. public function disableAutoSave()
  92. {
  93. $this->options['autoUpdateColumn'] = false;
  94. return $this;
  95. }
  96. /**
  97. * Disable remove file.
  98. *
  99. * @return $this
  100. */
  101. public function disableRemove()
  102. {
  103. $this->options['disableRemove'] = true;
  104. return $this;
  105. }
  106. /**
  107. * Set upload server.
  108. *
  109. * @param string $server
  110. *
  111. * @return $this
  112. */
  113. public function deleteUrl(string $server)
  114. {
  115. $this->options['deleteUrl'] = admin_url($server);
  116. return $this;
  117. }
  118. /**
  119. * @param array $data
  120. *
  121. * @return $this
  122. */
  123. public function withFormData(array $data)
  124. {
  125. $this->options['formData'] = array_merge($this->options['formData'], $data);
  126. return $this;
  127. }
  128. /**
  129. * Set default options form file field.
  130. *
  131. * @return void
  132. */
  133. protected function setupDefaultOptions()
  134. {
  135. $defaultOptions = [
  136. 'name' => WebUploaderHelper::FILE_NAME,
  137. 'fileVal' => WebUploaderHelper::FILE_NAME,
  138. 'isImage' => false,
  139. 'disableRemove' => false,
  140. 'chunked' => true,
  141. 'fileNumLimit' => 10,
  142. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  143. 'disableGlobalDnd' => true,
  144. 'fileSizeLimit' => 20971520000, // 20000M
  145. 'fileSingleSizeLimit' => 10485760, // 10M
  146. 'autoUpdateColumn' => false, // 上传完图片后自动保存图片路径
  147. 'elementName' => $this->getElementName(), // 字段name属性值
  148. 'lang' => trans('admin.uploader'),
  149. 'deleteData' => [
  150. static::FILE_DELETE_FLAG => '',
  151. '_token' => csrf_token(),
  152. ],
  153. 'formData' => [
  154. '_id' => Str::random(),
  155. '_token' => csrf_token(),
  156. 'upload_column' => $this->column(),
  157. ],
  158. ];
  159. $this->options($defaultOptions);
  160. }
  161. protected function setDefaultServer()
  162. {
  163. if (! $this->form || ! method_exists($this->form, 'action')) {
  164. return;
  165. }
  166. if (empty($this->options['server'])) {
  167. $this->options['server'] = $this->form->action();
  168. }
  169. if (empty($this->options['updateServer'])) {
  170. $this->options['updateServer'] = $this->form->action();
  171. }
  172. if (empty($this->options['deleteUrl'])) {
  173. $this->options['deleteUrl'] = $this->form->action();
  174. }
  175. if (
  176. method_exists($this->form, 'builder')
  177. && $this->form->builder()
  178. && $this->form->builder()->isEditing()
  179. ) {
  180. $this->options['formData']['_method'] = 'PUT';
  181. $this->options['deleteData']['_method'] = 'PUT';
  182. $this->options['autoUpdateColumn'] = true;
  183. }
  184. }
  185. /**
  186. * 获取创建链接.
  187. *
  188. * @return string
  189. */
  190. public function getCreateUrl()
  191. {
  192. return str_replace('/create', '', URL::full());
  193. }
  194. /**
  195. * 图片预览设置.
  196. *
  197. * @return void
  198. */
  199. protected function setupPreviewOptions()
  200. {
  201. $this->options['preview'] = $this->initialPreviewConfig();
  202. }
  203. }