wanyu_frontend/utils/helpers.js
2025-03-31 22:05:57 +08:00

90 lines
2.1 KiB
JavaScript
Raw 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',
//提交订单
'order-submit': '/pages/order/order-submit',
//首页
'index': '/pages/index/index',
}
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}`;
}
}
export default helpers