Files
xl-mobile/src/utils/uils.js

164 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-05-12 19:45:27 +08:00
import {BASEURL} from "./request.js";
import {useUserStore} from "../pinia/UserStore/index.js";
2025-03-25 16:35:39 +08:00
export const showToast = (options) => {
if (typeof options === 'string') {
uni.showToast({
2025-05-07 10:49:08 +08:00
title: options, icon: 'none',
2025-03-25 16:35:39 +08:00
}).then();
} else {
uni.showToast(options).then();
}
}
export const isWXWeb = () => {
const userAgent = navigator.userAgent;
return userAgent.includes('MicroMessenger');
}
2025-03-26 19:10:41 +08:00
2025-04-14 17:26:40 +08:00
export const toPage = async (url) => {
try {
await uni.navigateTo({
url: url,
})
} catch (e) {
await uni.reLaunch({
url: url,
})
}
2025-03-26 19:10:41 +08:00
}
2025-03-27 15:38:21 +08:00
2025-05-12 19:45:27 +08:00
export const clearObject = (obj) => {
Object.keys(obj).forEach(key => {
delete obj[key];
});
};
2025-03-27 15:38:21 +08:00
export const backPage = () => {
2025-04-14 17:26:40 +08:00
uni.navigateBack().then();
2025-03-27 15:38:21 +08:00
}
2025-04-07 18:04:39 +08:00
export const copy = (context) => {
2025-05-07 10:49:08 +08:00
// #ifndef APP-PLUS || MP-WEIXIN
2025-04-07 18:04:39 +08:00
try {
navigator.clipboard.writeText(context)
.then(() => {
showToast('已复制');
})
.catch(() => {
showToast('复制失败');
});
} catch (e) {
showToast('复制失败');
}
2025-05-07 10:49:08 +08:00
// #endif
// #ifdef APP-PLUS || MP-WEIXIN
uni.setClipboardData({
data: context, success: () => {
showToast('已复制');
}, fail: () => {
showToast('复制失败');
}
})
// #endif
2025-04-07 18:04:39 +08:00
}
export const download = (urls) => {
2025-05-07 10:49:08 +08:00
// #ifndef APP-PLUS || MP-WEIXIN
2025-05-06 11:36:14 +08:00
const promises1 = urls.map(url => new Promise((resolve, reject) => {
2025-04-07 18:04:39 +08:00
const iframe = document.createElement('iframe');
iframe.src = url;
iframe.style.display = 'none';
document.body.appendChild(iframe);
resolve(true);
}));
2025-05-06 11:36:14 +08:00
Promise.all(promises1).then(() => {
showToast('保存成功');
}).catch(() => {
showToast('保存失败');
2025-04-07 18:04:39 +08:00
});
2025-05-06 11:36:14 +08:00
// #endif
// #ifdef APP-PLUS
const promises2 = urls.map(v => new Promise((resolve, reject) => {
2025-05-07 10:49:08 +08:00
uni.downloadFile({
url: v,
success: () => {
resolve(true);
},
fail: () => {
reject(false);
}
2025-05-06 11:36:14 +08:00
})
}));
Promise.all(promises2).then(() => {
showToast('保存成功');
}).catch(() => {
showToast('保存失败');
});
// #endif
2025-05-07 10:49:08 +08:00
// #ifdef MP-WEIXIN
const promises3 = urls.map(v => new Promise((resolve, reject) => {
uni.downloadFile({
url: v,
success: ({tempFilePath}) => {
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
resolve(true);
},
fail: (err) => {
console.log(err);
reject(false);
}
})
},
fail: () => {
reject(false);
}
})
}));
Promise.all(promises3).then(() => {
showToast('保存成功');
}).catch(() => {
showToast('保存失败');
});
// #endif
2025-04-07 18:04:39 +08:00
}
2025-05-12 19:45:27 +08:00
export const numberToCharacter = (number) => {
return ['一', '二', '三', '四', '五'][number];
}
export const uploadFile = ({count}) => {
const UserStore = useUserStore();
return new Promise((resolve, reject) => {
uni.chooseImage({
count: count,
success: ({tempFilePaths}) => {
const file = tempFilePaths[0];
uni.uploadFile({
url: BASEURL + '/upload/upload',
filePath: file,
name: "file",
header: {
token: UserStore.token
},
success: ({data}) => {
resolve(JSON.parse(data));
},
fail: (err) => {
showToast(err.errMsg);
reject(err);
}
});
},
fail: (err) => {
showToast(err.errMsg);
reject(err);
}
});
});
}