emis-app/common/untool.js

338 lines
6.3 KiB
JavaScript
Raw Normal View History

2024-05-11 10:46:26 +00:00
import qs from "@/common/libs/qs"
2024-05-27 14:41:23 +00:00
// #ifdef H5
2024-05-27 10:36:37 +00:00
import { saveAs } from "file-saver"
2024-05-27 14:41:23 +00:00
// #endif
2024-08-04 16:49:59 +00:00
// #ifdef APP-PLUS
const appCloudScanner = uni.requireNativePlugin('Ba-Scanner');
// #endif
2024-05-11 10:46:26 +00:00
2023-12-18 16:30:33 +00:00
const tabBarUrls = [
2023-12-18 17:39:22 +00:00
'/pages/tabBar/deal/deal',
'/pages/tabBar/home/home',
'/pages/tabBar/mine/mine',
'/pages/tabBar/order/order'
2023-12-18 16:30:33 +00:00
]
const goto = (url, type = 1)=> {
2024-06-08 18:06:43 +00:00
if(!url) {
console.error("goto 缺少入参 url=========")
return
}
2023-12-18 16:30:33 +00:00
url = `/pages/${url}`
if(tabBarUrls.indexOf(url) > -1) {
return uni.switchTab({
url
});
}
if (type == 1){
uni.navigateTo({
url
});
2024-02-28 09:48:44 +00:00
} else if (type == 2){
2023-12-18 16:30:33 +00:00
uni.redirectTo({
url
});
}
}
const goBack = (opt = {})=> {
uni.navigateBack(opt)
}
const reLaunch = (opt = {})=> {
uni.reLaunch(opt)
}
const setClipboardData = (val)=> {
uni.setClipboardData({
data: val,
success: () => {
showToast('复制成功')
},
})
}
const showToast = (val)=> {
uni.showToast({ title: val, icon: 'none' })
}
2024-06-12 17:55:24 +00:00
const confirmModal = (title, opt = {}) => {
2023-12-18 16:30:33 +00:00
return new Promise((resolve)=> {
2024-06-12 17:55:24 +00:00
uni.showModal({ content: title, confirmColor: '#B12D29', ...opt }).then((res)=> {
2023-12-18 16:30:33 +00:00
if(res.confirm) {
2024-06-12 17:55:24 +00:00
resolve(true)
} else {
resolve(false)
2023-12-18 16:30:33 +00:00
}
})
})
}
2024-05-09 18:29:39 +00:00
const showLoading = (title)=> {
2023-12-18 16:30:33 +00:00
uni.showLoading({
mask: true,
2024-05-09 18:29:39 +00:00
title: title || '加载中'
2023-12-18 16:30:33 +00:00
});
}
const hideLoading = ()=> {
uni.hideLoading();
}
2024-08-04 16:49:59 +00:00
const customAppScanCode = ({resolve, reject})=> {
appCloudScanner.onScan(
{
// isContinuous: true,
barcodeFormats: [
// "QR Code"
],
// scanTimeSpace: 2000,
// isShowVibrate: true,
// isShowBeep: false,
hintText: '扫码',
},
(res) => {
console.log("scanCode res========", res)
2024-08-09 08:59:11 +00:00
let code = res.result
if (code) {
// 去除 扫码多出来的特殊字符
code = code.replace(//g, '')
resolve(code)
2024-08-04 16:49:59 +00:00
}
}
);
}
2023-12-18 16:30:33 +00:00
const scanCode = ()=> {
return new Promise((resolve, reject)=> {
2024-08-04 16:49:59 +00:00
// #ifdef APP-PLUS
customAppScanCode({resolve, reject})
// #endif
// #ifndef APP-PLUS
2023-12-18 16:30:33 +00:00
uni.scanCode({
onlyFromCamera: false,
2023-12-23 07:50:49 +00:00
success: (res) =>{
console.log("scanCode res========", res)
resolve(res.result)
},
2023-12-18 16:30:33 +00:00
fail: reject
})
2024-08-04 16:49:59 +00:00
// #endif0
2023-12-18 16:30:33 +00:00
})
}
const makePhoneCall = (phoneNumber)=> {
uni.makePhoneCall({
phoneNumber: phoneNumber
});
}
2024-05-11 10:46:26 +00:00
const downloadFile = (url, ext = {})=> {
2023-12-18 16:30:33 +00:00
return new Promise((resolve, reject)=> {
uni.downloadFile({
url: url,
2024-05-11 10:46:26 +00:00
...ext,
2023-12-18 16:30:33 +00:00
success: (res) => {
2024-05-11 08:06:24 +00:00
console.log("downloadFile:success:res======", res)
if (res.statusCode === 200 || res.statusCode === 400) {
2023-12-18 16:30:33 +00:00
console.log('下载成功');
2024-05-10 09:20:35 +00:00
resolve(res)
2023-12-18 16:30:33 +00:00
}
},
fail: (err)=>{
2024-05-10 09:20:35 +00:00
console.log("downloadFile:err", err)
2023-12-18 16:30:33 +00:00
reject(err)
}
});
})
}
2024-05-10 09:20:35 +00:00
const saveNetFile = (url) => {
return new Promise(async (resolve, reject)=> {
showLoading('正在保存文件')
try{
2024-05-27 14:41:23 +00:00
let dRes = await getImageInfo(url)
console.log("save:downloadFile:getImageInfo", dRes)
let { tempFilePath, path } = dRes
2024-05-27 10:36:37 +00:00
// #ifdef H5
2024-05-27 14:41:23 +00:00
saveAs(path)
2024-05-27 10:36:37 +00:00
// #endif
2024-05-27 14:41:23 +00:00
// let res = await saveFile({ path })
2024-05-27 10:15:58 +00:00
// #ifdef APP-PLUS
2024-05-27 14:41:23 +00:00
plus.io.convertLocalFileSystemURL(path);
plus.runtime.openFile(path); //选择软件打开文件
2024-05-27 10:15:58 +00:00
// #endif
2024-05-10 09:20:35 +00:00
hideLoading()
showToast('保存成功')
2024-05-27 10:36:37 +00:00
resolve(tempFilePath)
2024-05-10 09:20:35 +00:00
}catch(e){
hideLoading()
showToast('保存失败')
reject(e)
}
})
}
const saveFile = ({ tempFilePath })=> {
return new Promise(async (resolve, reject)=> {
//保存到本地
uni.saveFile({
tempFilePath,//文件的临时路径
success: function(res) {
console.log("saveFile:success", res)
resolve(res)
},
fail: function(err) {
console.log("saveFile:err", err)
reject(err)
},
complete(res){
console.log("saveFile:complete", res)
}
});
})
}
2024-05-11 09:38:16 +00:00
const getImageInfo = (src)=> {
return new Promise(async (resolve, reject)=> {
uni.getImageInfo({
src,
success(res) {
resolve(res)
},
fail(e) {
console.log("获取图片信息失败=======", e)
reject(e)
}
})
})
}
2024-05-11 10:46:26 +00:00
const getImageTempFilePath = (options) => {
2024-05-11 09:38:16 +00:00
return new Promise(async (resolve, reject) => {
2024-05-11 10:46:26 +00:00
let { url, reqHeader, param } = options
if(param) {
let qsStr =qs.stringify(param)
url += `${url.indexOf("?")>-1?'&':'?'}${qsStr}`
}
let res = await downloadFile(url, { header: reqHeader })
resolve(res.tempFilePath)
2024-05-11 09:38:16 +00:00
})
}
2024-05-12 03:18:44 +00:00
const canvasGetImageData = (params = {})=> {
return new Promise((resolve, reject)=> {
uni.canvasGetImageData({
x: 0,
y: 0,
...params,
success(res) {
console.log("img====",res);
resolve(res)
if(res.data) {
let data = res.data
resolve(res);
} else {
showToast('canvasGetImageData:失败')
reject('canvasGetImageData:失败')
}
},
fail(err){
showToast('canvasGetImageData:失败')
console.log("canvasGetImageData:失败====", err);
}
})
})
}
2024-05-13 08:03:12 +00:00
const uniPromiseApi = (name, options = {}, extObj = { })=> {
return new Promise((resolve, reject)=> {
if(!(uni?.[name])) {
console.error(`uni.${name} api不存在 `)
return reject(`uni.${name} api不存在`)
}
uni?.[name]?.({
...options,
success(res){
2024-05-13 09:47:19 +00:00
console.log(`uni.${name} -execResult:success====`, res)
2024-05-13 08:03:12 +00:00
if(extObj.successText) {
showToast(successText)
}
2024-05-13 09:47:19 +00:00
resolve({ res })
2024-05-13 08:03:12 +00:00
},
fail(err){
console.error(`uni.${name} -execResult:fail====`, err)
if(extObj.failText) {
showToast(failText)
}
2024-05-13 09:47:19 +00:00
resolve({ err })
2024-05-13 08:03:12 +00:00
}
})
})
}
2024-07-11 14:33:38 +00:00
const promiseTimeout = (time = 300) => {
2024-05-13 08:03:12 +00:00
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, time)
})
}
2023-12-18 16:30:33 +00:00
2024-06-12 17:55:24 +00:00
const setNavigationBarTitle = (title)=> {
uni.setNavigationBarTitle({
title
})
}
const reloadCurrentPage = (_thisPage)=> {
let { fullPath } = _thisPage.$page
uni.redirectTo({
url: fullPath
});
}
2024-06-14 15:43:50 +00:00
const playAudio = (src)=> {
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = src
innerAudioContext.onPlay(() => {
console.log('开始播放===');
});
innerAudioContext.onError((res) => {
console.log("playAudio onError ======", res);
});
}
2024-06-12 17:55:24 +00:00
2023-12-18 16:30:33 +00:00
export {
reLaunch,
goto,
goBack,
setClipboardData,
confirmModal,
showLoading,
hideLoading,
2023-12-18 16:30:33 +00:00
showToast,
scanCode,
makePhoneCall,
2024-05-10 09:20:35 +00:00
downloadFile,
saveNetFile,
saveFile,
2024-05-11 09:38:16 +00:00
getImageTempFilePath,
2024-05-12 03:18:44 +00:00
canvasGetImageData,
2024-05-13 08:03:12 +00:00
getImageInfo,
uniPromiseApi,
promiseTimeout,
2024-06-12 17:55:24 +00:00
setNavigationBarTitle,
reloadCurrentPage,
2024-06-14 15:43:50 +00:00
playAudio
2023-12-18 16:30:33 +00:00
}