177 lines
3.8 KiB
JavaScript
177 lines
3.8 KiB
JavaScript
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',
|
||
}
|
||
|
||
url = paths[pathName]
|
||
if (url === undefined) {
|
||
return this.showToast('跳转页面不存在')
|
||
}
|
||
|
||
if (params !== '') {
|
||
url += `?${params}`
|
||
}
|
||
|
||
uni[type]({
|
||
url
|
||
})
|
||
}
|
||
|
||
static showToast(msg) {
|
||
let duration = 1500
|
||
if (msg.length > 7) {
|
||
duration = 2000
|
||
}
|
||
|
||
if (msg.length > 14) {
|
||
duration = 4000
|
||
}
|
||
|
||
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) {
|
||
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)
|
||
}
|
||
|
||
}
|
||
export default helpers
|