wanyu_frontend/utils/helpers.js

220 lines
4.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class helpers {
/**
* 跳转页面
* @param pathName //路径名
* @param params //查询参数
* id=12
* @param type //跳转类型
* navigateTo = 保留当前页面,跳转到应用内的某个页面
* redirectTo = 关闭当前页面,跳转到应用内的某个页面
* reLaunch = 关闭所有页面,打开到应用内的某个页面
* switchTab = 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
*/
static jumpToPage(pathName, params = '', type = 'navigateTo') {
let url
let paths = {
//登录页
'login': '/pages/user/login',
//订单详情
'order-info': '/pages/order/order-info',
//首页
'index': '/pages/index/index',
//选择上门时间
'select-time': '/pages/order/select-time',
//完成上门
'arrived-on-site': '/pages/order/arrived-on-site',
//完成服务
'complete-service': '/pages/order/complete-service',
//上报异常
'report-order-exception': '/pages/order/report-order-exception',
//更新师傅备注
'worker-remark': '/pages/order/worker-remark',
//用户协议
'user-agreement': '/pages/user/user-agreement',
//隐私政策
'privacy-policy': '/pages/user/privacy-policy',
//更新进度
'update-progress': '/pages/order/update-progress',
}
url = paths[pathName]
if (url === undefined) {
return this.showToast('跳转页面不存在')
}
if (params !== '') {
url += `?${params}`
}
uni[type]({
url
})
}
static showToast(msg) {
let duration = 2000
if (msg.length > 7) {
duration = 3000
}
if (msg.length > 14) {
duration = 5000
}
uni.showToast({
title: msg,
icon: "none",
duration: duration
})
}
/**
* 深拷贝数据
* @param data
* @returns {any}
*/
static deepObj(data) {
if (typeof data === 'string' || typeof data === 'number') {
return data;
}
if (data) {
return JSON.parse(JSON.stringify(data));
}
return data;
}
static formatDate(dateString) {
// 兼容 iOS替换 "-" 为 "/"
const compatibleDateString = dateString.replace(/-/g, '/');
const date = new Date(compatibleDateString);
const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const weekDay = weekdays[date.getDay()];
const year = date.getFullYear().toString().slice(2); // 获取两位数年份
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
return `${weekDay} ${year}${month}${day}${hours}:${minutes}`;
}
static rad(d) {
return d * Math.PI / 180.0;
}
static getDistances(lat1, lng1, lat2, lng2) {
var radLat1 = this.rad(lat1);
var radLat2 = this.rad(lat2);
var a = radLat1 - radLat2;
var b = this.rad(lng1) - this.rad(lng2);
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378.137; // EARTH_RADIUS;
// 输出为公里
s = Math.round(s * 10000) / 10000;
var distance = s;
var unit;
if (parseInt(distance) >= 1) {
distance = distance.toFixed(2);
unit = "公里";
} else {
distance = (distance * 1000).toFixed(2);
if (distance >= 1000) {
distance = 999
}
unit = "米";
}
return {
distance: distance,
unit: unit
}
}
static removeCommas(str) {
return str.replace(/,/g, '');
}
static removeSeconds(datetimeStr) {
if (datetimeStr === null) {
return ''
}
return datetimeStr.slice(0, 16);
}
/**
* 打开客户地址
* @param lat
* @param lng
*/
static openLocation (lat, lng) {
uni.openLocation({
latitude: Number(lat),
longitude: Number(lng),
fail: function () {
helpers.showToast('客户经纬度不正确,请联系平台')
}
});
}
/**
* 拨打电话
* @param tel
*/
static makePhoneCall (tel){
uni.makePhoneCall({
phoneNumber: tel,
fail: function () {
}
});
}
static delayHideLoading (timeout = 200) {
setTimeout(() => {
uni.hideLoading()
}, timeout)
}
static previewImage = (url) => {
uni.previewImage({
urls: [url],
longPressActions: {
itemList: ['发送给朋友', '保存图片', '收藏'],
success: function(data) {
},
fail: function(err) {
console.log(err.errMsg);
}
}
});
}
static copy = (content) => {
if (!content) {
helpers.showToast('复制的内容不能为空')
return
}
// 复制内容,必须字符串,数字需要转换为字符串
content = typeof content === 'string' ? content : content.toString()
uni.setClipboardData({
data: content,
success: function() {
helpers.showToast('复制成功')
},
fail:function(){
helpers.showToast('复制失败')
}
});
}
}
export default helpers