import showDialog from '@/common/dialogMessage' function msg(title, duration = 1000) { uni.showToast({ title: title, duration: duration, icon: 'none', mask: true, }); } function show(title, msg, showCancel = true, cancelText = '取消', confirmText = '确定') { return new Promise((resolve, reject) => { // 创建通用参数对象 const dialogOptions = { title, content: msg, showCancel, cancelText, confirmText, success: (res) => { // 统一处理成功回调,解析为布尔值 resolve(!!res.confirm); } }; if (getPhoneEnvBool('AND')) { // 安卓环境使用showDialog,补充cancel回调 showDialog({ ...dialogOptions, cancel: () => { console.log('用户点击取消'); resolve(false); // 取消时也返回Promise结果 } }); } else { // 其他环境使用uni.showModal,补充fail回调 uni.showModal({ ...dialogOptions, fail: (res) => { reject(res); } }); } }); } const loading = (title = '加载中', mask = true) => uni.showLoading({ title: title, mask: mask }) const hideLoading = () => uni.hideLoading() // 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。 const navigateTo = (url, data = {}) => uni.navigateTo({ url: url, ...data }); // 关闭所有页面,打开到应用内的某个页面。 const reLaunch = url => { uni.reLaunch({ url: url }); } // 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。 const switchTab = url => { uni.switchTab({ url: url }); } // 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。 const navigateBack = (delta = 1, fun = () => {}) => { uni.navigateBack({ delta: delta, success() { fun() } }) } //关闭当前页面,跳转到应用内的某个页面。 const redirectTo = (url, obj = {}) => uni.redirectTo({ url: url, ...obj }); // 时间转换时间戳 const dateToTime = (dateString) => { let date = new Date(dateString); let timestamp = date.getTime(); return timestamp } // 数字格式化 const formatDate2 = (timestamp, format = 'yyyy-MM-dd HH:mm:ss') => { if (!timestamp) return '' let date try { if (timestamp) { if (typeof timestamp == 'number' && timestamp <= 9999999999) { timestamp *= 1000 } date = new Date(timestamp) } else { date = new Date() } } catch (e) { return '' } let day = date.getDate() let month = date.getMonth() + 1 let year = date.getFullYear() let hours = date.getHours() let minutes = date.getMinutes() let seconds = date.getSeconds() // 替换日期格式中的占位符 return format .replace('yyyy', year) .replace('MM', month.toString().padStart(2, '0')) .replace('dd', day.toString().padStart(2, '0')) .replace('HH', hours.toString().padStart(2, '0')) .replace('mm', minutes.toString().padStart(2, '0')) .replace('ss', seconds.toString().padStart(2, '0')) } // 获取token export const getToken = () => uni.getStorageSync('token') export const setToken = data => uni.setStorageSync('token', data) export const setUserInfo = data => uni.setStorageSync('userInfo', data) export const getUserInfo = (key = '') => { const userInfo = uni.getStorageSync('userInfo') return '' === key ? userInfo : userInfo[key]; } /** * 判断当前运行平台是否匹配指定标识 * @param {string} flag - 平台标识:'IOS' 表示苹果iOS平台,'AND' 表示安卓平台 * @returns {boolean} 如果当前平台匹配指定标识返回true,否则返回false */ export const getPhoneEnvBool = (flag) => { // 获取当前设备的平台信息 const { platform } = uni.getSystemInfoSync(); // 平台与标识的映射关系 const platformMap = { ios: 'IOS', android: 'AND', }; // 根据当前平台返回匹配结果 return platformMap[platform] === flag; }; // 将对象和字符串相互转换 export const objToJson = obj => encodeURLComponent('page_data', obj) export const jsonToObj = () => uni.getStorageSync('page_data') // 将对象存入缓存 export const setPageCache = (name, obj) => { uni.setStorageSync(name, obj) } // 将对象取出并且删除缓存 export const getPageCache = name => { const cache = uni.getStorageSync(name) uni.removeStorageSync(name) return cache } function setValue(key, value) { uni.setStorageSync(key, value); } function getValue(key) { return uni.getStorageSync(key) } function isLogin() { return !!getToken(); } export default { getPageCache, setPageCache, objToJson, jsonToObj, hideLoading, loading, msg, show, redirectTo, navigateTo, navigateBack, formatDate2, setValue, getValue, isLogin, reLaunch, switchTab, dateToTime, }