util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. base64String: 需要转换的base64 图片
  3. w: 压缩后的图片的宽度
  4. quality: 图片的压缩比例
  5. */
  6. function compress(base64String, w, quality) {
  7. var getMimeType = function(urlData) {
  8. var arr = urlData.split(",");
  9. var mime = arr[0].match(/:(.*?);/)[1];
  10. // return mime.replace("image/", "");
  11. return mime;
  12. };
  13. var newImage = new Image();
  14. var imgWidth, imgHeight;
  15. var promise = new Promise((resolve) => (newImage.onload = resolve));
  16. newImage.src = base64String;
  17. return promise.then(() => {
  18. imgWidth = newImage.width;
  19. imgHeight = newImage.height;
  20. var canvas = document.createElement("canvas");
  21. var ctx = canvas.getContext("2d");
  22. if (Math.max(imgWidth, imgHeight) > w) {
  23. if (imgWidth > imgHeight) {
  24. canvas.width = w;
  25. canvas.height = (w * imgHeight) / imgWidth;
  26. } else {
  27. canvas.height = w;
  28. canvas.width = (w * imgWidth) / imgHeight;
  29. }
  30. } else {
  31. canvas.width = imgWidth;
  32. canvas.height = imgHeight;
  33. }
  34. ctx.clearRect(0, 0, canvas.width, canvas.height);
  35. ctx.drawImage(newImage, 0, 0, canvas.width, canvas.height);
  36. var base64 = canvas.toDataURL(getMimeType(base64String), quality);
  37. return base64;
  38. });
  39. }
  40. // compress(base64String, 800, 0.5).then(function (val) {
  41. // imgb.src = val;
  42. // });
  43. // 防止处理多次点击
  44. function noMultipleClicks(methods, info) {
  45. // methods是点击后需要执行的函数, info是函数需要传的参数
  46. let that = this;
  47. if (that.noClick) {
  48. // 第一次点击
  49. that.noClick = false;
  50. if ((info && info !== '') || info == 0) {
  51. // info是执行函数需要传的参数
  52. methods(info);
  53. } else {
  54. methods();
  55. }
  56. setTimeout(() => {
  57. that.noClick = true;
  58. }, 2000)
  59. } else {
  60. // 这里是重复点击的判断
  61. }
  62. }
  63. //导出
  64. // 图片预加载
  65. function preloader(imgList, next) {
  66. let count = 0;
  67. // let loadingInstance1 = Toast({
  68. // type: 'loading',
  69. // message: '加载中...',
  70. // forbidClick: true,
  71. // });
  72. console.log("预加载图片", imgList)
  73. for (let img of imgList) {
  74. uni.getImageInfo({
  75. src: img.imgUrl,
  76. success: function(image) {
  77. count++;
  78. img.imgUrl = image.path;
  79. if (count == imgList.length) {
  80. // loadingInstance1.close();
  81. // Toast.clear();
  82. next();
  83. }
  84. }
  85. });
  86. // let image = new Image();
  87. // image.src = img.imgUrl;
  88. }
  89. }
  90. function dateFormat(fmt, date) {
  91. let ret;
  92. const opt = {
  93. "Y+": date.getFullYear().toString(), // 年
  94. "m+": (date.getMonth() + 1).toString(), // 月
  95. "d+": date.getDate().toString(), // 日
  96. "H+": date.getHours().toString(), // 时
  97. "M+": date.getMinutes().toString(), // 分
  98. "S+": date.getSeconds().toString() // 秒
  99. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  100. };
  101. for (let k in opt) {
  102. ret = new RegExp("(" + k + ")").exec(fmt);
  103. if (ret) {
  104. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  105. };
  106. };
  107. return fmt;
  108. }
  109. export function timestampFormatter(inputTime, isPop = false) {
  110. // 最终时间结果对象
  111. const finalDateObj = {
  112. h: null, // 小时
  113. m: null, // 分钟
  114. s: null, // 秒
  115. }
  116. // 剩余时间总的毫秒数 除以 1000 变为总秒数(时间戳为13位 需要除以1000,为10位 则不需要)
  117. let dec = inputTime / 1000;
  118. if (dec <= 0) {
  119. dec = 0;
  120. }
  121. // 得到小时 格式化成前缀加零的样式
  122. let h = Math.trunc(dec / 3600);
  123. h = h < 10 ? '0' + h : h;
  124. // 得到分钟 格式化成前缀加零的样式
  125. let m = Math.trunc(dec % 3600 / 60);
  126. m = m < 10 ? '0' + m : m;
  127. // 得到秒 格式化成前缀加零的样式
  128. let s = Math.trunc(dec % 3600 % 60);
  129. // s = s < 10 ? '0' + s : s;
  130. finalDateObj.h = h;
  131. finalDateObj.m = m;
  132. finalDateObj.s = s;
  133. return isPop ? `${h}小时${m}分${s}秒` : finalDateObj;
  134. }
  135. export function getTestTime(str) {
  136. return str.replace("年", "/").replace("月", "/").replace("日", " ").replace("时", ":").replace("分", ":").replace("秒",
  137. "");
  138. }
  139. export default {
  140. compress,
  141. noMultipleClicks, // 禁止多次点击
  142. preloader,
  143. dateFormat,
  144. timestampFormatter
  145. }