UploadImg.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="img-wrap">
  3. <div class="box">
  4. <ul class="upload-imgs">
  5. <li
  6. v-show="imgLen<6"
  7. class="uploadBox"
  8. @click="handleUpload"
  9. >
  10. <input
  11. type="file"
  12. class="upload"
  13. @change="addImg"
  14. accept="image/*"
  15. ref="inp"
  16. style="display:none"
  17. />
  18. <img src="../images/upload.png">
  19. <p>点击上传</p>
  20. </li>
  21. <li v-for='(value, key) in imgs'>
  22. <p class="imgbox">
  23. <img
  24. :src="value"
  25. :preview="item.id"
  26. class="img"
  27. preview-text=""
  28. >
  29. </p>
  30. <a
  31. class="close"
  32. @click="delImg(key)"
  33. ></a>
  34. </li>
  35. </ul>
  36. </div>
  37. </div>
  38. </template>
  39. <script type="text/javascript">
  40. import {isIos,isWX} from '@utils/tools';
  41. import $ from 'jquery';
  42. export default {
  43. name: 'UploadImg',
  44. data() {
  45. return {
  46. mag: "上传图片",
  47. imgs: {},
  48. imgLen: 0,
  49. flag:true //图片处理完成后才可以再次点击上传
  50. }
  51. },
  52. props: ['item', 'moduleType', 'imgList'],//moduleType-哪个模块下上传的图
  53. mounted() {
  54. this.imgs = this.imgList; //回读
  55. this.imgLen = Object.keys(this.imgList).length;
  56. this.$previewRefresh();//预览刷新
  57. if(isIos()){
  58. $('.upload').removeAttr("capture")
  59. }else if(!isWX()){
  60. // 微信端添加这个属性直接调拍照
  61. //安卓手机且非微信端添加相机属性,否则QQ浏览器打不开相机
  62. $('.upload').attr("capture","camera")
  63. }
  64. },
  65. methods: {
  66. handleUpload() {
  67. // navigator.getUserMedia({video: true,audio:true}, function onSuccess(stream) {
  68. /*navigator.getUserMedia({video: true}, function onSuccess(stream) {
  69. const inp = this.$refs.inp;
  70. inp.click();
  71. }, function onError(error) {
  72. alert("请开启权限设置")
  73. })*/
  74. const inp = this.$refs.inp;
  75. if(this.flag){
  76. inp.click();
  77. }
  78. },
  79. delImg(key) {
  80. let obj = this.imgs;
  81. delete (obj[key]);
  82. this.imgLen--;
  83. this.imgs = Object.assign({}, obj);
  84. this.$store.commit('deleImg', { key: key, type: this.moduleType });
  85. this.$store.commit('deleSrc', { key: key, type: this.moduleType });
  86. },
  87. addImg() {
  88. // 上传图片进行压缩,压缩后超过4M则不允许上传
  89. this.flag = false;
  90. let fileTag = this.$refs.inp;
  91. // let img = this.$refs.img;
  92. let file = fileTag.files[0];
  93. const that = this;
  94. this.imgBase64(file, function (image, canvas) {
  95. var maxSize = 4 * 1024; // 4M
  96. var fileSize = file.size / 1024; //kb 图片大小
  97. var uploadSrc;
  98. var uploadFile;
  99. if (fileSize > maxSize) { // 如果图片大小大于4m,进行压缩
  100. // console.log(maxSize,fileSize, maxSize/fileSize );
  101. uploadSrc = canvas.toDataURL(file.type, maxSize / fileSize);
  102. uploadFile = that.dataURLtoFile(uploadSrc, file.name.split('.')[0]); // 转成file文件
  103. // uploadFile = that.convertBase64UrlToBlob(uploadSrc); // 转成blob
  104. } else {
  105. uploadSrc = image.src; //canvas.toDataURL(file.type,0.5);
  106. uploadFile = file;
  107. }
  108. var compressedSize = uploadFile.size / 1024 / 1024;
  109. if (compressedSize.toFixed(2) > 4.00) {
  110. alert('上传图片不可超过4M');
  111. } else {
  112. let key = file.name + new Date().getTime();
  113. that.$set(that.imgs, key, uploadSrc);
  114. that.imgLen++;
  115. // 将图片信息存到store
  116. that.$store.commit('setImgFile', { type: that.moduleType, pId: that.item.id, key: key, file: uploadFile })
  117. that.$store.commit('setImgSrc', { key: key, src: uploadSrc, type: that.moduleType })
  118. that.$previewRefresh(); //异步获取的图片需要刷新下
  119. }
  120. // that.$refs.inp.value = '';
  121. fileTag.value = '';
  122. that.flag = true
  123. });
  124. },
  125. imgBase64(file, callback) {
  126. var self = this;
  127. // 看支持不支持FileReader
  128. if (!file || !window.FileReader) return;
  129. // 创建一个 Image 对象
  130. var image = new Image();
  131. // 绑定 load 事件处理器,加载完成后执行
  132. image.onload = function () {
  133. // 获取 canvas DOM 对象
  134. var canvas = document.createElement('canvas')
  135. // 返回一个用于在画布上绘图的环境, '2d' 指定了您想要在画布上绘制的类型
  136. var ctx = canvas.getContext('2d')
  137. // 如果高度超标 // 参数,最大高度
  138. var MAX_HEIGHT = 3000;
  139. if (image.height > MAX_HEIGHT) {
  140. // 宽度等比例缩放 *=
  141. image.width *= MAX_HEIGHT / image.height;
  142. image.height = MAX_HEIGHT;
  143. }
  144. // 获取 canvas的 2d 环境对象,
  145. // canvas清屏
  146. ctx.clearRect(0, 0, canvas.width, canvas.height);
  147. // 重置canvas宽高
  148. canvas.width = image.width;
  149. canvas.height = image.height;
  150. // 将图像绘制到canvas上
  151. ctx.drawImage(image, 0, 0, image.width, image.height);
  152. callback(image, canvas);
  153. };
  154. if (/^image/.test(file.type)) {
  155. var reader = new FileReader();
  156. // 将图片将转成 base64 格式
  157. reader.readAsDataURL(file);
  158. // 读取成功后的回调
  159. reader.onload = function () {
  160. // 设置src属性,浏览器会自动加载。
  161. // 记住必须先绑定事件,才能设置src属性,否则会出同步问题。
  162. image.src = this.result;
  163. }
  164. }
  165. },
  166. // 将以base64的图片url数据转换为Blob
  167. convertBase64UrlToBlob(urlData) {
  168. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  169. //处理异常,将ascii码小于0的转换为大于0
  170. var ab = new ArrayBuffer(bytes.length);
  171. var ia = new Uint8Array(ab);
  172. for (var i = 0; i < bytes.length; i++) {
  173. ia[i] = bytes.charCodeAt(i);
  174. }
  175. return new Blob([ab], { type: 'image/jpg' });
  176. },
  177. // 将以base64的图片url数据转换为file
  178. dataURLtoFile(dataurl, filename) { //将base64转换为文件
  179. var arr = dataurl.split(','),
  180. mime = arr[0].match(/:(.*?);/)[1],
  181. bstr = atob(arr[1]),
  182. n = bstr.length,
  183. u8arr = new Uint8Array(n);
  184. while (n--) {
  185. u8arr[n] = bstr.charCodeAt(n);
  186. }
  187. return new File([u8arr], filename, { type: mime });
  188. },
  189. }
  190. }
  191. </script>
  192. <style lang="less" scoped>
  193. .img-wrap {
  194. font-size: 0.3rem;
  195. .upload-imgs {
  196. margin-bottom: 0.2rem;
  197. .uploadBox {
  198. border: 1px solid #dfe0e4;
  199. box-sizing: border-box;
  200. text-align: center;
  201. img {
  202. width: 0.6rem;
  203. margin: 0.45rem 0 0.23rem 0;
  204. }
  205. }
  206. li {
  207. width: 1.86rem;
  208. height: 1.9rem;
  209. border: 1px solid #dfe0e4;
  210. display: inline-block;
  211. position: relative;
  212. vertical-align: top;
  213. border-radius: 0.08rem;
  214. margin: 0 0 0.3rem 0.3rem;
  215. .close {
  216. width: 0.54rem;
  217. height: 0.54rem;
  218. background: url(../images/del-pic.png) no-repeat;
  219. background-size: cover;
  220. position: absolute;
  221. top: -0.27rem;
  222. right: -0.27rem;
  223. z-index: 3;
  224. }
  225. }
  226. li:nth-child(3n + 1) {
  227. margin-left: 0;
  228. }
  229. .imgbox {
  230. width: 100%;
  231. height: 100%;
  232. overflow: hidden;
  233. border-radius: 0.08rem;
  234. position: relative;
  235. .img {
  236. width: 100%;
  237. position: absolute;
  238. left: 50%;
  239. top: 50%;
  240. transform: translate(-50%,-50%);
  241. }
  242. }
  243. }
  244. }
  245. </style>