UploadImg.vue 7.8 KB

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