UploadImg.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. >
  9. <input
  10. type="file"
  11. class="upload"
  12. @change="addImg"
  13. accept="image/*"
  14. ref="inp"
  15. />
  16. <img src="../images/upload.png">
  17. <p>点击上传</p>
  18. </li>
  19. <li v-for='(value, key) in imgs'>
  20. <p class="imgbox">
  21. <img
  22. :src="value"
  23. :preview="item.id"
  24. class="img"
  25. preview-text=""
  26. >
  27. </p>
  28. <a
  29. class="close"
  30. @click="delImg(key)"
  31. ></a>
  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; // 0.5M
  92. var fileSize = file.size / 1024; //kb 图片大小
  93. var uploadSrc;
  94. var uploadFile;
  95. if (fileSize > maxSize) { // 如果图片大小大于0.5M,进行压缩
  96. uploadSrc = canvas.toDataURL(file.type, 0.1);
  97. uploadFile = that.dataURLtoFile(uploadSrc, file.name.split('.')[0]); // 转成file文件
  98. // uploadFile = that.convertBase64UrlToBlob(uploadSrc); // 转成blob
  99. } else {
  100. uploadSrc = image.src;
  101. uploadFile = file;
  102. }
  103. var compressedSize = uploadFile.size / 1024 / 1024;
  104. if (compressedSize.toFixed(2) > 4.00) {
  105. alert('上传图片不可超过4M');
  106. } else {
  107. let key = file.name + new Date().getTime();
  108. that.$set(that.imgs, key, uploadSrc);
  109. that.imgLen++;
  110. // 将图片信息存到store
  111. that.$store.commit('setImgFile', { type: that.moduleType, pId: that.item.id, key: key, file: uploadFile })
  112. that.$store.commit('setImgSrc', { key: key, src: uploadSrc, type: that.moduleType })
  113. that.$previewRefresh(); //异步获取的图片需要刷新下
  114. }
  115. // that.$refs.inp.value = '';
  116. fileTag.value = '';
  117. that.flag = true
  118. });
  119. },
  120. imgBase64(file, callback) {
  121. var self = this;
  122. // 看支持不支持FileReader
  123. if (!file || !window.FileReader) return;
  124. // 创建一个 Image 对象
  125. var image = new Image();
  126. // 绑定 load 事件处理器,加载完成后执行
  127. image.onload = function () {
  128. // 获取 canvas DOM 对象
  129. var canvas = document.createElement('canvas')
  130. // 返回一个用于在画布上绘图的环境, '2d' 指定了您想要在画布上绘制的类型
  131. var ctx = canvas.getContext('2d')
  132. // 如果高度超标 // 参数,最大高度
  133. var MAX_HEIGHT = 3000;
  134. if (image.height > MAX_HEIGHT) {
  135. // 宽度等比例缩放 *=
  136. image.width *= MAX_HEIGHT / image.height;
  137. image.height = MAX_HEIGHT;
  138. }
  139. // 获取 canvas的 2d 环境对象,
  140. // canvas清屏
  141. ctx.clearRect(0, 0, canvas.width, canvas.height);
  142. // 重置canvas宽高
  143. canvas.width = image.width;
  144. canvas.height = image.height;
  145. // 将图像绘制到canvas上
  146. ctx.drawImage(image, 0, 0, image.width, image.height);
  147. callback(image, canvas);
  148. };
  149. if (/^image/.test(file.type)) {
  150. var reader = new FileReader();
  151. // 将图片将转成 base64 格式
  152. reader.readAsDataURL(file);
  153. // 读取成功后的回调
  154. reader.onload = function () {
  155. // 设置src属性,浏览器会自动加载。
  156. // 记住必须先绑定事件,才能设置src属性,否则会出同步问题。
  157. image.src = this.result;
  158. }
  159. }
  160. },
  161. // 将以base64的图片url数据转换为Blob
  162. convertBase64UrlToBlob(urlData) {
  163. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  164. //处理异常,将ascii码小于0的转换为大于0
  165. var ab = new ArrayBuffer(bytes.length);
  166. var ia = new Uint8Array(ab);
  167. for (var i = 0; i < bytes.length; i++) {
  168. ia[i] = bytes.charCodeAt(i);
  169. }
  170. return new Blob([ab], { type: 'image/jpg' });
  171. },
  172. // 将以base64的图片url数据转换为file
  173. dataURLtoFile(dataurl, filename) { //将base64转换为文件
  174. var arr = dataurl.split(','),
  175. mime = arr[0].match(/:(.*?);/)[1],
  176. bstr = atob(arr[1]),
  177. n = bstr.length,
  178. u8arr = new Uint8Array(n);
  179. while (n--) {
  180. u8arr[n] = bstr.charCodeAt(n);
  181. }
  182. return new File([u8arr], filename, { type: mime });
  183. },
  184. }
  185. }
  186. </script>
  187. <style lang="less" scoped>
  188. .img-wrap {
  189. font-size: 0.3rem;
  190. .upload-imgs {
  191. margin-bottom: 0.2rem;
  192. .upload{
  193. width: 1.86rem;
  194. height: 100%;
  195. position: absolute;
  196. left: 0;
  197. z-index: 66;
  198. opacity: 0;
  199. }
  200. .uploadBox {
  201. border: 1px solid #dfe0e4;
  202. box-sizing: border-box;
  203. text-align: center;
  204. img {
  205. width: 0.6rem;
  206. margin: 0.45rem 0 0.23rem 0;
  207. }
  208. }
  209. li {
  210. width: 1.86rem;
  211. height: 1.9rem;
  212. border: 1px solid #dfe0e4;
  213. display: inline-block;
  214. position: relative;
  215. vertical-align: top;
  216. border-radius: 0.08rem;
  217. margin: 0 0 0.3rem 0.3rem;
  218. .close {
  219. width: 0.54rem;
  220. height: 0.54rem;
  221. background: url(../images/del-pic.png) no-repeat;
  222. background-size: cover;
  223. position: absolute;
  224. top: -0.27rem;
  225. right: -0.27rem;
  226. z-index: 3;
  227. }
  228. }
  229. li:nth-child(3n + 1) {
  230. margin-left: 0;
  231. }
  232. .imgbox {
  233. width: 100%;
  234. height: 100%;
  235. overflow: hidden;
  236. border-radius: 0.08rem;
  237. position: relative;
  238. .img {
  239. width: 100%;
  240. position: absolute;
  241. left: 50%;
  242. top: 50%;
  243. transform: translate(-50%,-50%);
  244. }
  245. }
  246. }
  247. }
  248. </style>