uploadImgs.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <!--
  2. @param keysArr 承接数组
  3. @param readOnly 删除按钮是否显示,上传是否显示
  4. @param max 上传最大数限制
  5. @click change 承接数组发生变化时触发函数
  6. -->
  7. <template>
  8. <div id="sagaUploads">
  9. <div class="saga-upload-images">
  10. <div v-if="type != 'video'" class="point-image" v-for="(item,index) in imagesArr">
  11. <i v-if="!readOnly" class="el-icon-delete" @click="delImage(index,item)"></i>
  12. <img :src="imageGetUrl + '&key=' +item" alt v-load>
  13. </div>
  14. <div v-if="type == 'video'" class="point-image" v-for="(item,index) in imagesArr">
  15. <i v-if="!readOnly" class="el-icon-delete" @click="delImage(index,item)"></i>
  16. <video
  17. width="100%"
  18. height="100%;"
  19. :src="imageGetUrl + '&key=' +item"
  20. controls="controls"
  21. >您的浏览器不支持 video 标签。</video>
  22. </div>
  23. <div v-if="!readOnly && imagesArr.length < max" style="float:left;">
  24. <el-upload
  25. class="avatar-uploader"
  26. :http-request="uploadAndSubmit"
  27. :show-file-list="false"
  28. :accept="accept"
  29. action
  30. drag
  31. style="position: relation"
  32. >
  33. <i class="el-icon-plus avatar-uploader-icon"></i>
  34. </el-upload>
  35. <video style="display:none;" id="video" controls/>
  36. </div>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import tools from "@/utils/scan/tools";
  42. export default {
  43. props: {
  44. keysArr: {
  45. type: [Array, String]
  46. },
  47. readOnly: {
  48. type: Boolean,
  49. default: false
  50. },
  51. max: {
  52. type: [Number, String],
  53. default: 6
  54. },
  55. accept: {
  56. type: String,
  57. default: "image/*"
  58. },
  59. type: {
  60. type: String,
  61. default: "image"
  62. },
  63. defined: null,
  64. videoPicArr: {
  65. type: Array,
  66. default: function () {
  67. return []
  68. }
  69. }
  70. },
  71. data() {
  72. return {
  73. baseUrl: "",
  74. imageGetUrl: "/image-service/common/image_get?systemId=dataPlatform",
  75. imageUploadUrl: "/image-service/common/image_upload?systemId=dataPlatform&secret=9e0891a7a8c8e885&overwrite=true",
  76. imagesArr: []
  77. };
  78. },
  79. created() {
  80. this.imageFalg();
  81. },
  82. methods: {
  83. //判断是否为空
  84. imageFalg() {
  85. let type = typeof this.keysArr;
  86. console.log(this.keysArr)
  87. if (type == "string") {
  88. this.imagesArr = [this.keysArr];
  89. } else {
  90. this.imagesArr = tools.deepCopy(this.keysArr);
  91. }
  92. if (!this.keysArr) {
  93. this.imagesArr = [];
  94. }
  95. },
  96. //删除图片
  97. delImage(i, key) {
  98. if (this.type == "video") {
  99. this.videoPicArr = this.videoPicArr.map(item => {
  100. if (item.substring(0, item.length - 3) == this.imagesArr[i].substring(0, this.imagesArr[i].length - 3)) {
  101. return undefined
  102. } else {
  103. return item
  104. }
  105. }).filter(p => p)
  106. }
  107. this.imagesArr.splice(i, 1);
  108. this.$emit("change", this.imagesArr, this.defined, this.videoPicArr);
  109. },
  110. //上传
  111. uploadAndSubmit(item, key) {
  112. // var form = document.forms["demoForm"];
  113. // if (form["file"].files.length > 0) {
  114. // 寻找表单域中的 <input type="file" ... /> 标签
  115. // var file = form["file"].files[0];
  116. let file = item.file;
  117. // try sending
  118. let reader = new FileReader();
  119. let vm = this;
  120. let fileType = file.name.split(".");
  121. let type = fileType[fileType.length - 1];
  122. let uploadKey = file.uid
  123. if (!!key) {
  124. uploadKey = key
  125. }
  126. reader.onloadstart = function () {
  127. // 这个事件在读取开始时触发
  128. };
  129. reader.onprogress = function (p) {
  130. // 这个事件在读取进行中定时触发
  131. };
  132. reader.onload = function () {
  133. // 这个事件在读取成功结束后触发
  134. };
  135. reader.onloadend = function () {
  136. // 这个事件在读取结束后,无论成功或者失败都会触发
  137. if (reader.error) {
  138. } else {
  139. // document.getElementById("bytesRead").textContent = file.size;
  140. // 构造 XMLHttpRequest 对象,发送文件 Binary 数据
  141. var xhr = new XMLHttpRequest();
  142. xhr.open(
  143. /* method */
  144. "POST",
  145. /* target url */
  146. vm.imageUploadUrl + "&key=" + uploadKey + "." + type
  147. /*, async, default to true */
  148. );
  149. //xhr.overrideMimeType("application/octet-stream");
  150. xhr.send(reader.result);
  151. xhr.onreadystatechange = function () {
  152. if (xhr.readyState == 4) {
  153. if (xhr.status == 200) {
  154. if (vm.type == 'image') {
  155. vm.imagesArr.push({
  156. name: uploadKey + '',
  157. key: uploadKey + "." + type,
  158. systemId: "dataPlatform",
  159. type: "image"
  160. });
  161. }
  162. if (type == 'mp4') {
  163. vm.imagesArr.push({
  164. name: uploadKey + '',
  165. key: uploadKey + "." + type,
  166. systemId: "dataPlatform",
  167. type: "video"
  168. });
  169. vm.creatImg(vm.imageGetUrl + "&key=" + uploadKey + "." + type, uploadKey)
  170. }
  171. console.log(vm.type, type)
  172. if (vm.type == "video" && type == "png") {
  173. console.log("触发时评上传图片回调")
  174. vm.videoPicArr.push(uploadKey + "." + type)
  175. }
  176. vm.$emit("change", vm.imagesArr, vm.defined, vm.videoPicArr);
  177. }
  178. }
  179. };
  180. }
  181. };
  182. reader.readAsArrayBuffer(file);
  183. },
  184. dataURLtoBlob: function (dataURI, type) {
  185. var binary = atob(dataURI.split(',')[1]);
  186. var array = [];
  187. for (var i = 0; i < binary.length; i++) {
  188. array.push(binary.charCodeAt(i));
  189. }
  190. return new Blob([new Uint8Array(array)], { type: type });
  191. },
  192. creatImg(reader, key) {
  193. var videoDom = document.getElementById('video');
  194. videoDom.src = reader;
  195. let vm = this
  196. videoDom.onloadeddata = function () {
  197. // 这里可以打印视频时长
  198. // 这里取得视频封面
  199. var canvas = document.createElement('canvas');
  200. canvas.width = 300;
  201. canvas.height = 300 * this.videoHeight / this.videoWidth;
  202. canvas.getContext('2d').drawImage(this, 0, 0, canvas.width, canvas.height);
  203. //将canvas的base64位图片转换成图片png的file
  204. var blob = vm.dataURLtoBlob(canvas.toDataURL('image/png'), "image/png")
  205. //将其转换成file对象
  206. var file = new File([blob], "video_image.png", { type: "image/png", lastModified: Date.now() })//blob转file
  207. vm.uploadAndSubmit({ file: file }, key)
  208. }
  209. },
  210. },
  211. watch: {
  212. keysArr: function (val) {
  213. this.imageFalg();
  214. }
  215. },
  216. //自定义指令
  217. directives: {
  218. load: function (el) {
  219. let imgDom = document.createElement("img");
  220. imgDom.style.position = "absolute";
  221. imgDom.style.top = "-999px";
  222. imgDom.style.opacity = 0;
  223. imgDom.src = el.src;
  224. el.src = "";
  225. imgDom.onload = () => {
  226. let width = imgDom.width;
  227. let height = imgDom.height;
  228. if (width > height) {
  229. el.style.height = "100%";
  230. el.style.width = "auto";
  231. el.style.position = "absolute";
  232. el.style.left = "50%";
  233. el.style.top = "0";
  234. el.style.transform = "translateX(-50%)";
  235. el.style.webkitTransform = "translateX(-50%) translateY(0)";
  236. el.style.MozTransform = "translateX(-50%) translateY(0)";
  237. el.style.msTransform = "translateX(-50%) translateY(0)";
  238. el.style.OTransform = "translateX(-50%) translateY(0)";
  239. } else if (width < height) {
  240. el.src = imgDom.src;
  241. el.style.width = "100%";
  242. el.style.height = "auto";
  243. el.style.position = "absolute";
  244. el.style.top = "50%";
  245. el.style.left = "0";
  246. el.style.transform = "translateY(-50%) translateX(0)";
  247. el.style.webkitTransform = "translateY(-50%) translateX(0)";
  248. el.style.MozTransform = "translateY(-50%) translateX(0)";
  249. el.style.msTransform = "translateY(-50%) translateX(0)";
  250. el.style.OTransform = "translateY(-50%) translateX(0)";
  251. } else {
  252. el.style.width = "100%";
  253. el.style.height = "100%";
  254. el.style.position = "absolute";
  255. el.style.top = "0";
  256. el.style.left = "0";
  257. el.style.transform = "translateY(0) translateX(0)";
  258. el.style.webkitTransform = "translateY(0) translateX(0)";
  259. el.style.MozTransform = "translateY(0) translateX(0)";
  260. el.style.msTransform = "translateY(0) translateX(0)";
  261. el.style.OTransform = "translateY(0) translateX(0)";
  262. }
  263. el.src = imgDom.src;
  264. };
  265. }
  266. }
  267. };
  268. </script>
  269. <style lang="less">
  270. #sagaUploads {
  271. overflow: hidden;
  272. .avatar-uploader {
  273. height: 180px;
  274. width: 180px;
  275. overflow: hidden;
  276. .el-upload {
  277. width: 180px;
  278. height: 180px;
  279. .el-upload-dragger {
  280. width: 180px;
  281. height: 180px;
  282. .el-icon-plus {
  283. display: block;
  284. width: 20px;
  285. height: 20px;
  286. font-size: 20px;
  287. margin: 80px;
  288. }
  289. }
  290. }
  291. }
  292. .point-image {
  293. width: 180px;
  294. height: 180px;
  295. float: left;
  296. position: relative;
  297. margin-right: 10px;
  298. margin-bottom: 10px;
  299. border: 1px solid #ccc;
  300. overflow: hidden;
  301. image {
  302. z-index: 11;
  303. }
  304. i {
  305. position: absolute;
  306. bottom: 10px;
  307. right: 10px;
  308. background-color: #fff;
  309. padding: 5px;
  310. cursor: pointer;
  311. z-index: 66;
  312. }
  313. }
  314. }
  315. </style>