emis-app/common/untool.js

256 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-05-11 10:46:26 +00:00
import qs from "@/common/libs/qs"
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)=> {
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' })
}
const confirmModal = (title) => {
return new Promise((resolve)=> {
uni.showModal({ content: title, confirmColor: '#B12D29' }).then((res)=> {
if(res.confirm) {
resolve()
}
})
})
}
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();
}
2023-12-18 16:30:33 +00:00
const scanCode = ()=> {
return new Promise((resolve, reject)=> {
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
})
})
}
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{
let { tempFilePath } = await downloadFile(url)
console.log("save:orgin:tempFilePath", tempFilePath)
let res = await saveFile({ tempFilePath })
hideLoading()
showToast('保存成功')
resolve(res)
}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
}
})
})
}
const promiseTimeout = (time) => {
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, time)
})
}
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,
2023-12-18 16:30:33 +00:00
}