WebUploader.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Illuminate\Support\Facades\URL;
  6. /**
  7. * @property Form $form
  8. */
  9. trait WebUploader
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $options = [];
  15. /**
  16. * @param string $extensions exp. gif,jpg,jpeg,bmp,png
  17. * @param string|null $mimeTypes exp. image/*
  18. * @return $this
  19. */
  20. public function accept(string $extensions, string $mimeTypes = null)
  21. {
  22. $this->options['accept'] = [
  23. 'extensions' => $extensions,
  24. ];
  25. if ($mimeTypes !== null) {
  26. $this->options['accept']['mimeTypes'] = $mimeTypes;
  27. }
  28. return $this;
  29. }
  30. /**
  31. * @param bool $disable
  32. * @return $this
  33. */
  34. public function disableChunked(bool $disable = true)
  35. {
  36. $this->options['chunked'] = !$disable;
  37. return $this;
  38. }
  39. /**
  40. * @param int|null $size kb
  41. * @return $this
  42. */
  43. public function chunkSize(int $size)
  44. {
  45. $this->options['chunkSize'] = $size * 1024;
  46. return $this;
  47. }
  48. /**
  49. * @param int $size kb
  50. * @return $this
  51. */
  52. public function maxSize(int $size)
  53. {
  54. $this->rules('max:'.$size);
  55. $this->options['fileSingleSizeLimit'] = $size * 1024;
  56. return $this;
  57. }
  58. /**
  59. * @param int $num
  60. * @return $this
  61. */
  62. public function threads(int $num)
  63. {
  64. $this->options['threads'] = $num;
  65. return $this;
  66. }
  67. /**
  68. * Set upload server.
  69. *
  70. * @param string $server
  71. * @return $this
  72. */
  73. public function url(string $server)
  74. {
  75. $this->options['server'] = admin_url($server);
  76. return $this;
  77. }
  78. /**
  79. * Disable auto save the path of uploaded file.
  80. *
  81. * @return $this
  82. */
  83. public function disableAutoSave()
  84. {
  85. $this->options['autoUpdateColumn'] = false;
  86. return $this;
  87. }
  88. /**
  89. * Disable remove file.
  90. *
  91. * @return $this
  92. */
  93. public function disableRemove()
  94. {
  95. $this->options['disableRemove'] = true;
  96. return $this;
  97. }
  98. /**
  99. * Set upload server.
  100. *
  101. * @param string $server
  102. * @return $this
  103. */
  104. public function deleteUrl(string $server)
  105. {
  106. $this->options['deleteUrl'] = admin_url($server);
  107. return $this;
  108. }
  109. /**
  110. * Set default options form file field.
  111. *
  112. * @return void
  113. */
  114. protected function setupDefaultOptions()
  115. {
  116. $defaultOptions = [
  117. 'isImage' => false,
  118. 'disableRemove' => false,
  119. 'chunked' => true,
  120. 'fileNumLimit' => 10,
  121. // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
  122. 'disableGlobalDnd' => true,
  123. 'fileSizeLimit' => 20971520000, // 20000M
  124. 'fileSingleSizeLimit' => 10485760, // 10M
  125. 'autoUpdateColumn' => true, // 上传完图片后自动保存图片路径
  126. 'elementName' => $this->getElementName(), // 字段name属性值
  127. 'deleteData' => [
  128. static::FILE_DELETE_FLAG => '',
  129. '_token' => csrf_token(),
  130. '_method' => 'PUT',
  131. ],
  132. 'formData' => [
  133. '_id' => Admin::user()->getKey(),
  134. 'upload_column' => $this->column,
  135. '_method' => 'PUT',
  136. '_token' => csrf_token(),
  137. ],
  138. 'lang' => trans('admin.uploader'),
  139. ];
  140. $this->options($defaultOptions);
  141. }
  142. protected function setDefaultServer()
  143. {
  144. if (!$this->form instanceof Form) {
  145. return;
  146. }
  147. if (!isset($this->options['server'])) {
  148. $this->options['server'] = $this->form->getAction();
  149. }
  150. if (!isset($this->options['deleteUrl'])) {
  151. $this->options['deleteUrl'] = $this->form->getAction();
  152. }
  153. if ($this->form->builder()->isCreating()) {
  154. unset(
  155. $this->options['formData']['_method'],
  156. $this->options['deleteData']['_method'],
  157. $this->options['autoUpdateColumn']
  158. );
  159. }
  160. }
  161. /**
  162. * Get create url.
  163. *
  164. * @return string
  165. */
  166. public function getCreateUrl()
  167. {
  168. return str_replace('/create', '', URL::full());
  169. }
  170. /**
  171. * Set preview options form image field.
  172. *
  173. * @return void
  174. */
  175. protected function setupPreviewOptions()
  176. {
  177. $this->options['preview'] = $this->initialPreviewConfig();
  178. }
  179. /**
  180. * Set options for file-upload plugin.
  181. *
  182. * @param array $options
  183. *
  184. * @return $this
  185. */
  186. public function options($options = [])
  187. {
  188. $this->options = array_merge($options, $this->options);
  189. return $this;
  190. }
  191. }