WebUploader.php 5.9 KB

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