自用uniapp常用方法整合

//页面跳转

const to = (url, animationType = 'pop-in', animationDuration = 300) => {
	uni.navigateTo({
		url,
		animationType,
		animationDuration,
		success: function(res) {
			
		},
		fail: function(e) {
			
		}
	})
}

//系统高度

const wanlsys = () => {
	const sys = uni.getSystemInfoSync();
	const data = {
		top: sys.statusBarHeight,
		height: sys.statusBarHeight + uni.upx2px(90),
		screenHeight: sys.screenHeight,
		platform: sys.platform,
		model: sys.model,
		windowHeight: sys.windowHeight,
		windowBottom: sys.windowBottom,
		deviceId: sys.deviceId
	};
	return data;
}

//图片url拼接

const appstc = (url) => {
	let str = RegExp('http');
	let newUrl;
	//通过三元运算符进行判断该图片是否含有http域名,没有就拼接上去
	str.test(url) ? newUrl = url : newUrl = '公共域名' + url;
	return newUrl;
}

//上传文件路径

const upLoadUrl = () => {

	return $http.baseUrl + '/upload';
}

//信息弹窗

const msg = (title, icon = 'none', duration = 1500, mask = false) => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
	uni.vibrateShort();//抖动
}

//返回上页

const back = (num = 1) => {
	uni.navigateBack({
		delta: num
	});
}

//数字格式化

const toFormat = (number, type) => {
	//格式千位以上
	if (type == 'thousand') {
		if (number > 9999) {
			number = (number / 10000).toFixed(1) + 'w'
		} else if (number > 999) {
			number = (number / 1000).toFixed(1) + 'k'
		}
	}
	//格式百位
	if (type == 'hundred' && number > 99) {
		number = '99+';
	}
	return number;
}

//精度加减乘除

add(a, b){
	    var c, d, e;
	    try {
	        c = a.toString().split(".")[1].length
	    } catch(f) {
	        c = 0
	    }
	    try {
	        d = b.toString().split(".")[1].length
	    } catch(f) {
	        d = 0
	    }
	    return e = Math.pow(10, Math.max(c, d)),(this.mul(a, e) + this.mul(b, e)) / e
	}

sub(a, b){
	    var c, d, e;
	    try {
	        c = a.toString().split(".")[1].length
	    } catch(f) {
	        c = 0
	    }
	    try {
	        d = b.toString().split(".")[1].length
	    } catch(f) {
	        d = 0
	    }
	    return e = Math.pow(10, Math.max(c, d)),(this.mul(a, e) - this.mul(b, e)) / e
	}

mul(a, b){
	    var c = 0,
	    d = a.toString(),
	    e = b.toString();
	    try {
	        c += d.split(".")[1].length
	    } catch(f) {}
	    try {
	        c += e.split(".")[1].length
	    } catch(f) {}
	    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c)
	}

div(a, b){
	    var c, d, e = 0,
	    f = 0;
	    try {
	        e = a.toString().split(".")[1].length
	    } catch(g) {}
	    try {
	        f = b.toString().split(".")[1].length
	    } catch(g) {}
	    return c = Number(a.toString().replace(".", "")),d = Number(b.toString().replace(".", "")),this.mul(c / d, Math.pow(10, f - e))
	}

//时间格式化

const timeFormat = (timestamp = null, fmt = 'yyyy-mm-dd') => {
	// 其他更多是格式化有如下:
	// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
	timestamp = parseInt(timestamp);
	// 如果为null,则格式化当前时间
	if (!timestamp) timestamp = Number(new Date());
	// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
	if (timestamp.toString().length == 10) timestamp *= 1000;
	let date = new Date(timestamp);
	let ret;
	let 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;
}

//社交时间友好

const timeToDate = (timestamp = null, format = 'yyyy-mm-dd') => {
	if (timestamp == null) timestamp = Number(new Date());
	timestamp = parseInt(timestamp);
	// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
	if (timestamp.toString().length == 10) timestamp *= 1000;
	var timer = (new Date()).getTime() - timestamp;
	timer = parseInt(timer / 1000);
	// 如果小于5分钟,则返回"刚刚",其他以此类推
	let tips = '';
	switch (true) {
		case timer < 300:
			tips = '刚刚';
			break;
		case timer >= 300 && timer < 3600:
			tips = parseInt(timer / 60) + '分钟前';
			break;
		case timer >= 3600 && timer < 86400:
			tips = parseInt(timer / 3600) + '小时前';
			break;
		case timer >= 86400 && timer < 2592000:
			tips = parseInt(timer / 86400) + '天前';
			break;
		default:
			// 如果format为false,则无论什么时间戳,都显示xx之前
			if(format === false) {
				if(timer >= 2592000 && timer < 365 * 86400) {
					tips = parseInt(timer / (86400 * 30)) + '个月前';
				} else {
					tips = parseInt(timer / (86400 * 365)) + '年前';
				}
			} else {
				tips = timeFormat(timestamp, format);
			}
	}
	return tips;
}

//IM时间友好

const timeToChat = (timestamp = null) => {
	if (timestamp == null) timestamp = Number(new Date());
	timestamp = parseInt(timestamp);
	// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
	if (timestamp.toString().length == 10) timestamp *= 1000;
	var timer = (new Date()).getTime() - timestamp;
	timer = parseInt(timer / 1000);
	// 如果小于5分钟,则返回"刚刚",其他以此类推
	let tips = '';
	switch (true) {
		case timer < 86400:
			tips = timeFormat(timestamp, 'hh:MM');
			break;
		case timer >= 86400 && timer < 86400 * 7:
			var now = new Date(timestamp);
			var week = ['日', '一', '二', '三', '四', '五', '六'];
			switch (new Date().getDate() - now.getDate()) {
				case 1:
					tips = timeFormat(timestamp, '昨天 hh:MM');
					break;
				case 2:
					tips = timeFormat(timestamp, '前天 hh:MM');
					break;
				default:
					tips = '星期' + week[now.getDay()] + timeFormat(timestamp, 'hh:MM');
			}
			break;
		case timer >= 86400 * 7:
			tips = timeFormat(timestamp, 'mm-dd hh:MM');
			break;
		default:
			tips = timeFormat(timestamp, 'yyyy-mm-dd hh:MM');
	}
	return tips;
}

//拨打电话

const phone = (number) => {
	uni.makePhoneCall({
	    phoneNumber: number
	});
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值