Files
tra-app/src/common/common.js
T
2025-11-19 17:20:04 +08:00

378 lines
9.1 KiB
JavaScript

import { get_base_url } from '@/api/request';
// #ifdef APP-PLUS
import showDialog from '@/common/dialogMessage'
import common from '@/common/common'
// #endif
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);
}
};
// #ifdef APP-PLUS
if (getPhoneEnvBool('AND')) {
// 安卓环境使用showDialog,补充cancel回调
showDialog({
...dialogOptions,
cancel: () => {
console.log('用户点击取消');
resolve(false); // 取消时也返回Promise结果
}
});
} else {
// 其他环境使用uni.showModal,补充fail回调
uni.showModal({
...dialogOptions,
fail: (res) => {
reject(res);
}
});
}
// #endif
// #ifdef H5
uni.showModal({
...dialogOptions,
fail: (res) => {
reject(res);
}
});
// #endif
});
}
const loading = (title = '加载中', mask = true) => uni.showLoading({
title: title,
mask: mask
})
const hideLoading = () => uni.hideLoading()
// 根据姓名的长度生成对应的带星号格式
export const formatName = (name) => {
// 处理空值或非字符串情况
if (!name || typeof name !== 'string') {
return '';
}
const familyName = name.charAt(0); // 取姓氏(第一个字符)
const givenNameLength = name.length - 1; // 名字部分的长度
// 根据名字长度计算星号数量,最多4个
const starCount = Math.min(givenNameLength, 3);
const stars = '*'.repeat(starCount);
return familyName + stars;
}
// 防抖工具函数
function debounce(fn, wait = 300) {
let isLocked = false; // 锁定状态标记
return function(...args) {
// 如果处于锁定状态,直接返回不执行
if (isLocked) {
return;
}
// 执行函数
fn.apply(this, args);
// 锁定,防止再次执行
isLocked = true;
// 等待指定时间后解锁
setTimeout(() => {
isLocked = false;
}, wait);
};
}
// 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。
const navigateTo = debounce(function(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 = () => {}, data = {}) => {
uni.navigateBack({
delta: delta,
...data,
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];
}
// 数字转换,把传入的秒转换为HH:mm:ss格式
export const formatSeconds = (seconds) => {
// 处理非数字或负数情况
if (typeof seconds !== 'number' || isNaN(seconds) || seconds < 0) {
return '00:00:00';
}
// 计算小时、分钟和剩余秒数
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = Math.floor(seconds % 60);
// 补零函数:将数字转为两位数字符串
const padZero = (num) => num.toString().padStart(2, '0');
// 拼接成00:00:00格式
return `${padZero(hours)}:${padZero(minutes)}:${padZero(remainingSeconds)}`;
};
/**
* 判断当前运行平台是否匹配指定标识
* @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, defaultValue=null) {
return uni.getStorageSync(key) ?? defaultValue
}
function isLogin() {
return !!getToken();
}
// 封装键盘高度变化处理
export function setupKeyboardHeightListener(updateCallback) {
// #ifndef APP-PLUS
return () => {};
// #endif
// #ifdef APP-PLUS
// 获取系统信息
const systemInfo = uni.getSystemInfoSync();
// 计算安全区域底部高度
const safeAreaBottomHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom;
// 定义处理函数
const handleKeyboardHeightChange = (res) => {
let keyboardHeight = res.height;
// 仅在iOS端补偿安全区域高度
if (systemInfo.platform === 'ios') {
keyboardHeight = Math.max(0, keyboardHeight - safeAreaBottomHeight); // 避免负数
}
// 通过回调函数更新外部的响应式变量
updateCallback(keyboardHeight);
};
// 监听键盘高度变化
uni.onKeyboardHeightChange(handleKeyboardHeightChange);
// 返回取消监听的函数,方便组件卸载时清理
return () => {
uni.offKeyboardHeightChange(handleKeyboardHeightChange);
};
// #endif
}
export const previewFileFnc = ({fileId, getFileSrc, fileName}) => {
let userInfo = getUserInfo()
let userParams = '/' + (userInfo?.loginName??'')
if (getPhoneEnvBool('AND')) {
common.navigateTo('/pages/index/preview?fileId=' + fileId +'&fileApiSrc=' + getFileSrc);
} else {
if (isDownloading.value) return
const base_url = get_base_url(getFileSrc);
let fileUrl = base_url + getFileSrc + fileId + userParams;
const localFilePath = '_doc/pdf_view/' + fileName + '.pdf';
isDownloading.value = true
downloadFileFnc(fileUrl, localFilePath).then((res) => {
uni.openDocument({
filePath: res,
showMenu: true,
success: function() {
console.log('打开文档成功');
}
})
}).finally(() => {
setTimeout(() => {
isDownloading.value = false
}, 1000)
});
}
}
const downloadFileFnc = async (fileUrl, localFilePath) => {
try {
//#ifdef APP-PLUS
return new Promise((resolve, reject) => {
console.log('开始下载:', fileUrl);
const dtask = plus.downloader.createDownload(
fileUrl, {
filename: localFilePath
},
(d, status) => {
if (status === 200) {
console.log('保存路径:', d.filename);
resolve(d.filename);
} else {
console.error('文件下载失败:', status);
reject(new Error(`下载失败,状态码: ${status}`));
}
}
);
dtask.start();
});
// #endif
//#ifdef H5
return new Promise((resolve, reject) => {
resolve();
});
// #endif
} catch (error) {
console.error('创建目录失败:', error);
throw error;
}
};
export default {
getPageCache,
setPageCache,
objToJson,
jsonToObj,
hideLoading,
loading,
msg,
show,
redirectTo,
navigateTo,
navigateBack,
formatDate2,
setValue,
getValue,
isLogin,
reLaunch,
switchTab,
dateToTime,
}