You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.7 KiB

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2019 Blobt.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace iron\widgets;
  26. use iron\web\UploadAsset;
  27. use yii\helpers\Html;
  28. use yii\web\NotFoundHttpException;
  29. use yii\widgets\InputWidget;
  30. /***
  31. * @author iron <weiriron@gmail.com>
  32. * @created Sep 11, 2019
  33. */
  34. class Upload extends InputWidget
  35. {
  36. /**
  37. * @var string
  38. * 上传url
  39. */
  40. public $url;
  41. /**
  42. * @var bool
  43. * 是否支持多图
  44. */
  45. public $multiple;
  46. /**
  47. * @var string
  48. * 文件保存后ajax异步操作(如操作数据库)
  49. */
  50. public $afterSave;
  51. /**
  52. * @var bool
  53. * 显示预览
  54. */
  55. public $showPreviews;
  56. /**
  57. * @var integer
  58. * 最大数量
  59. */
  60. public $maxCount;
  61. /**
  62. * @var string
  63. * 支持上传的文件类型
  64. */
  65. public $acceptFile;
  66. /**
  67. * @var int
  68. * 最大现在尺寸(kB)
  69. */
  70. public $maxSize;
  71. /**
  72. * @var array
  73. * 预览设置
  74. */
  75. public $previewConfig;
  76. /**
  77. * @var string
  78. * 删除图片
  79. */
  80. public $deleteUrl;
  81. /**
  82. * @var bool
  83. * 显示删除按钮
  84. */
  85. public $showDelete;
  86. /**
  87. * @var int
  88. * 预览框大小
  89. */
  90. public $statusBarWidth;
  91. /**
  92. * @var int
  93. * 拉拽框大小
  94. */
  95. public $dragdropWidth;
  96. /**
  97. * @throws NotFoundHttpException
  98. * @throws \yii\base\InvalidConfigException
  99. * 初始化参数
  100. */
  101. public function init()
  102. {
  103. parent::init();
  104. if (!$this->url) {
  105. throw new NotFoundHttpException('(upload) 必须配置上传url');
  106. }
  107. if (isset($this->previewConfig['url'])) {
  108. $this->previewConfig['height'] = $this->previewConfig['height'] ?? '80px';
  109. $this->previewConfig['width'] = $this->previewConfig['width'] ?? '80px';
  110. }
  111. $this->showPreviews = $this->previewConfig['url'] ? 'true' : 'false';//默认不显示预览
  112. $this->showPreviews = $this->showPreviews ?: 'false';//默认关闭预览
  113. $this->statusBarWidth = $this->statusBarWidth ?: 300;//默认关闭预览
  114. $this->dragdropWidth = $this->dragdropWidth ?: 800;//默认关闭预览
  115. $this->acceptFile = $this->acceptFile ?: '*';//默认无限制文件类型
  116. $this->maxCount = $this->maxCount ?: 10;//默认数量限制十张图
  117. $this->showDelete = $this->deleteUrl ? 'true' : 'false';//默认不显示删除按钮
  118. $this->maxSize = $this->maxSize ? $this->maxSize *= 1024 : 2 * 1024 * 1024;//默认限制2M大小;
  119. }
  120. /**
  121. * @return string
  122. * 执行
  123. */
  124. public function run()
  125. {
  126. $this->registerAsset();
  127. return $this->renderUploadHtml();
  128. }
  129. /**
  130. * 注册js和css
  131. */
  132. protected function registerAsset()
  133. {
  134. $view = $this->getView();
  135. UploadAsset::register($view);
  136. $js = <<< SCRIPT
  137. $("#upload-file").uploadFile({
  138. url:"upload",
  139. returnType: "json",
  140. multiple:true,
  141. dragDrop:true,
  142. fileName:"file",
  143. statusBarWidth:{$this->statusBarWidth},
  144. dragdropWidth:{$this->dragdropWidth},
  145. dragDropStr:"<span><b>拖动上传</b></span>",
  146. sizeErrorStr:"超过最大尺寸限制",
  147. maxFileCountErrorStr:"超过最大上传数量",
  148. uploadErrorStr:"上传失败",
  149. maxFileCount:"{$this->acceptFile}",
  150. maxFileCount:{$this->maxCount},
  151. maxFileSize:{$this->maxSize},
  152. showPreview:{$this->showPreviews},
  153. previewHeight:"{$this->previewConfig['height']}",
  154. previewWidth:"{$this->previewConfig['width']}",
  155. showDelete: {$this->showDelete},
  156. onSuccess: function (files, data) {
  157. $.ajax({
  158. url: "{$this->afterSave}",
  159. dataType: "json",
  160. data: {data: data, fileName: files},
  161. });
  162. },
  163. onLoad:function(obj)
  164. {
  165. $.ajax({
  166. cache: false,
  167. url: "{$this->previewConfig['url']}",
  168. dataType: "json",
  169. success: function(data)
  170. {
  171. for(var i=0;i<data.length;i++)
  172. {
  173. obj.createProgress(data[i]["name"],data[i]["path"],data[i]["size"]);
  174. }
  175. }
  176. });
  177. },
  178. deleteCallback: function (data, pd) {
  179. for (var i = 0; i < data.length; i++) {
  180. $.post("{$this->deleteUrl}", {op: "delete",name: data[i]},
  181. function (resp,textStatus, jqXHR) {
  182. });
  183. }
  184. pd.statusbar.hide(); //You choice.
  185. },
  186. });
  187. SCRIPT;
  188. $view->registerJs($js);
  189. }
  190. /**
  191. * @return string
  192. * 渲染html
  193. */
  194. protected function renderUploadHtml()
  195. {
  196. return '<div id="upload-file">
  197. <div class="ajax-upload-dragdrop" style="vertical-align: top; width: 600px;">
  198. <div class="ajax-file-upload" style="position: relative; overflow: hidden; cursor: default;">Upload
  199. <form method="POST" action="upload.php" enctype="multipart/form-data" style="margin: 0px; padding: 0px;">
  200. <input type="file" id="ajax-upload-id-1573635462220" name="myfile[]" accept="*" multiple="" style="position: absolute; cursor: pointer; top: 0px; width: 100%; height: 100%; left: 0px; z-index: 100; opacity: 0;">
  201. </form>
  202. </div>
  203. <span><b>Drag &amp; Drop Files</b></span>
  204. </div>
  205. <div>';
  206. }
  207. }