123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /**
- base64String: 需要转换的base64 图片
- w: 压缩后的图片的宽度
- quality: 图片的压缩比例
- */
- function compress(base64String, w, quality) {
- var getMimeType = function(urlData) {
- var arr = urlData.split(",");
- var mime = arr[0].match(/:(.*?);/)[1];
- // return mime.replace("image/", "");
- return mime;
- };
- var newImage = new Image();
- var imgWidth, imgHeight;
- var promise = new Promise((resolve) => (newImage.onload = resolve));
- newImage.src = base64String;
- return promise.then(() => {
- imgWidth = newImage.width;
- imgHeight = newImage.height;
- var canvas = document.createElement("canvas");
- var ctx = canvas.getContext("2d");
- if (Math.max(imgWidth, imgHeight) > w) {
- if (imgWidth > imgHeight) {
- canvas.width = w;
- canvas.height = (w * imgHeight) / imgWidth;
- } else {
- canvas.height = w;
- canvas.width = (w * imgWidth) / imgHeight;
- }
- } else {
- canvas.width = imgWidth;
- canvas.height = imgHeight;
- }
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- ctx.drawImage(newImage, 0, 0, canvas.width, canvas.height);
- var base64 = canvas.toDataURL(getMimeType(base64String), quality);
- return base64;
- });
- }
- // compress(base64String, 800, 0.5).then(function (val) {
- // imgb.src = val;
- // });
- // 防止处理多次点击
- function noMultipleClicks(methods, info) {
- // methods是点击后需要执行的函数, info是函数需要传的参数
- let that = this;
- if (that.noClick) {
- // 第一次点击
- that.noClick = false;
- if ((info && info !== '') || info == 0) {
- // info是执行函数需要传的参数
- methods(info);
- } else {
- methods();
- }
- setTimeout(() => {
- that.noClick = true;
- }, 2000)
- } else {
- // 这里是重复点击的判断
- }
- }
- //导出
- // 图片预加载
- function preloader(imgList, next) {
- let count = 0;
- // let loadingInstance1 = Toast({
- // type: 'loading',
- // message: '加载中...',
- // forbidClick: true,
- // });
- console.log("预加载图片", imgList)
- for (let img of imgList) {
- uni.getImageInfo({
- src: img.imgUrl,
- success: function(image) {
- count++;
- img.imgUrl = image.path;
- if (count == imgList.length) {
- // loadingInstance1.close();
- // Toast.clear();
- next();
- }
- }
- });
- // let image = new Image();
- // image.src = img.imgUrl;
- }
- }
- function dateFormat(fmt, date) {
- let ret;
- const opt = {
- "Y+": date.getFullYear().toString(), // 年
- "m+": (date.getMonth() + 1).toString(), // 月
- "d+": date.getDate().toString(), // 日
- "H+": date.getHours().toString(), // 时
- "M+": date.getMinutes().toString(), // 分
- "S+": date.getSeconds().toString() // 秒
- // 有其他格式化字符需求可以继续添加,必须转化成字符串
- };
- for (let k in opt) {
- ret = new RegExp("(" + k + ")").exec(fmt);
- if (ret) {
- fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
- };
- };
- return fmt;
- }
- export function timestampFormatter(inputTime, isPop = false) {
- // 最终时间结果对象
- const finalDateObj = {
- h: null, // 小时
- m: null, // 分钟
- s: null, // 秒
- }
- // 剩余时间总的毫秒数 除以 1000 变为总秒数(时间戳为13位 需要除以1000,为10位 则不需要)
- let dec = inputTime / 1000;
- if (dec <= 0) {
- dec = 0;
- }
- // 得到小时 格式化成前缀加零的样式
- let h = Math.trunc(dec / 3600);
- h = h < 10 ? '0' + h : h;
- // 得到分钟 格式化成前缀加零的样式
- let m = Math.trunc(dec % 3600 / 60);
- m = m < 10 ? '0' + m : m;
- // 得到秒 格式化成前缀加零的样式
- let s = Math.trunc(dec % 3600 % 60);
- // s = s < 10 ? '0' + s : s;
- finalDateObj.h = h;
- finalDateObj.m = m;
- finalDateObj.s = s;
- return isPop ? `${h}小时${m}分${s}秒` : finalDateObj;
- }
- export function getTestTime(str) {
- return str.replace("年", "/").replace("月", "/").replace("日", " ").replace("时", ":").replace("分", ":").replace("秒",
- "");
- }
- export default {
- compress,
- noMultipleClicks, // 禁止多次点击
- preloader,
- dateFormat,
- timestampFormatter
- }
|