emis-sapp/common/untool.js

316 lines
6.1 KiB
JavaScript
Raw Normal View History

2024-05-31 10:42:15 +00:00
import qs from "@/common/libs/qs"
// #ifdef H5
import { saveAs } from "file-saver"
// #endif
2023-12-02 08:54:31 +00:00
const tabBarUrls = [
2023-12-12 16:51:07 +00:00
'/pages/tabBar/sending/sending',
'/pages/tabBar/search/search',
'/pages/tabBar/mine/mine'
2023-12-02 08:54:31 +00:00
]
const goto = (url, type = 1)=> {
2024-06-13 08:10:41 +00:00
if(!url) {
console.error("goto 缺少入参 url=========")
return
}
2024-08-10 13:46:20 +00:00
url = url.replace(`/pages/`, '')
2024-08-10 02:50:45 +00:00
url = url.replace(`pages/`, '')
2023-12-02 08:54:31 +00:00
url = `/pages/${url}`
2024-10-09 07:45:34 +00:00
if(tabBarUrls.filter(t=> url.indexOf(t) > -1).length > 0) {
// tabBar 使用 reLaunch切换,防止页面不刷新
return uni.reLaunch({
2023-12-02 08:54:31 +00:00
url
});
}
if (type == 1){
uni.navigateTo({
url
});
2024-05-31 10:42:15 +00:00
} else if (type == 2){
2023-12-02 08:54:31 +00:00
uni.redirectTo({
url
});
}
}
const goBack = (opt = {})=> {
2024-08-07 09:52:10 +00:00
uni.navigateBack({
fail: ()=>{
2024-08-10 01:10:19 +00:00
goto(opt._finalUrl || `tabBar/sending/sending`, 2)
2024-08-07 09:52:10 +00:00
},
...opt,
})
2023-12-02 08:54:31 +00:00
}
const reLaunch = (opt = {})=> {
uni.reLaunch(opt)
}
2023-12-04 08:14:24 +00:00
const setClipboardData = (val)=> {
uni.setClipboardData({
data: val,
success: () => {
2023-12-12 16:51:07 +00:00
showToast('复制成功')
2023-12-04 08:14:24 +00:00
},
})
}
2023-12-12 00:50:19 +00:00
const showToast = (val)=> {
uni.showToast({ title: val, icon: 'none' })
}
2024-06-13 08:10:41 +00:00
const confirmModal = (title, opt = {}) => {
2023-12-04 08:14:24 +00:00
return new Promise((resolve)=> {
2024-06-13 08:10:41 +00:00
uni.showModal({ content: title, confirmColor: '#B12D29', ...opt }).then((res)=> {
2023-12-04 08:14:24 +00:00
if(res.confirm) {
2024-06-13 08:10:41 +00:00
resolve(true)
} else {
resolve(false)
2023-12-04 08:14:24 +00:00
}
})
})
}
2024-05-31 10:42:15 +00:00
const showLoading = (title)=> {
2023-12-04 08:14:24 +00:00
uni.showLoading({
mask: true,
2024-05-31 10:42:15 +00:00
title: title || '加载中'
2023-12-04 08:14:24 +00:00
});
}
2024-05-31 10:42:15 +00:00
const hideLoading = ()=> {
uni.hideLoading();
}
2023-12-13 04:27:01 +00:00
const scanCode = ()=> {
return new Promise((resolve, reject)=> {
uni.scanCode({
onlyFromCamera: false,
2023-12-23 07:51:10 +00:00
success: (res) =>{
console.log("scanCode res========", res)
resolve(res.result)
},
2023-12-13 04:27:01 +00:00
fail: reject
})
})
}
2023-12-16 17:34:29 +00:00
const makePhoneCall = (phoneNumber)=> {
uni.makePhoneCall({
phoneNumber: phoneNumber
});
}
2023-12-17 15:28:03 +00:00
2024-05-31 10:42:15 +00:00
const downloadFile = (url, ext = {})=> {
2023-12-17 15:28:03 +00:00
return new Promise((resolve, reject)=> {
uni.downloadFile({
url: url,
2024-05-31 10:42:15 +00:00
...ext,
2023-12-17 15:28:03 +00:00
success: (res) => {
2024-05-31 10:42:15 +00:00
console.log("downloadFile:success:res======", res)
if (res.statusCode === 200 || res.statusCode === 400) {
2023-12-17 15:28:03 +00:00
console.log('下载成功');
2024-05-31 10:42:15 +00:00
resolve(res)
2024-09-02 09:06:25 +00:00
} else {
reject('下载失败')
2023-12-17 15:28:03 +00:00
}
},
fail: (err)=>{
2024-05-31 10:42:15 +00:00
console.log("downloadFile:err", err)
2023-12-17 15:28:03 +00:00
reject(err)
}
});
})
}
2024-05-31 10:42:15 +00:00
2024-09-02 09:06:25 +00:00
const saveNetFile = ({ url, header } = {}) => {
return new Promise(async (resolve, reject) => {
2024-05-31 10:42:15 +00:00
showLoading('正在保存文件')
2024-09-02 15:14:22 +00:00
console.log("下载文件====", url)
2024-09-02 09:06:25 +00:00
try {
let dRes = await downloadFile(url, { header })
console.log("saveNetFile:downloadFile==", dRes)
let {
tempFilePath,
path
} = dRes
path = path || tempFilePath
2024-05-31 10:42:15 +00:00
// #ifdef H5
saveAs(path)
// #endif
// let res = await saveFile({ path })
2024-09-02 09:06:25 +00:00
2024-05-31 10:42:15 +00:00
// #ifdef APP-PLUS
plus.io.convertLocalFileSystemURL(path);
plus.runtime.openFile(path); //选择软件打开文件
// #endif
2024-09-02 09:06:25 +00:00
// #ifdef MP-WEIXIN
await uniPromiseApi('openDocument', { filePath: path, showMenu: true, })
// #endif
2024-05-31 10:42:15 +00:00
hideLoading()
showToast('保存成功')
2024-09-02 15:14:22 +00:00
setClipboardData(url)
2024-05-31 10:42:15 +00:00
resolve(tempFilePath)
2024-09-02 09:06:25 +00:00
} catch (e) {
2024-05-31 10:42:15 +00:00
hideLoading()
showToast('保存失败')
reject(e)
}
})
2024-09-02 09:06:25 +00:00
2024-05-31 10:42:15 +00:00
}
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)
}
});
})
}
const getImageInfo = (src)=> {
return new Promise(async (resolve, reject)=> {
uni.getImageInfo({
src,
success(res) {
resolve(res)
},
fail(e) {
console.log("获取图片信息失败=======", e)
reject(e)
}
})
})
}
const getImageTempFilePath = (options) => {
return new Promise(async (resolve, reject) => {
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)
})
2023-12-16 17:34:29 +00:00
2024-05-31 10:42:15 +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);
}
})
})
}
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){
console.log(`uni.${name} -execResult:success====`, res)
if(extObj.successText) {
showToast(successText)
}
resolve({ res })
},
fail(err){
console.error(`uni.${name} -execResult:fail====`, err)
if(extObj.failText) {
showToast(failText)
}
resolve({ err })
}
})
})
}
const promiseTimeout = (time) => {
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, time)
})
}
2023-12-16 17:34:29 +00:00
2024-06-13 08:10:41 +00:00
const setNavigationBarTitle = (title)=> {
uni.setNavigationBarTitle({
title
})
}
const reloadCurrentPage = (_thisPage)=> {
2024-07-31 18:59:34 +00:00
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
let curPage = routes[routes.length - 1] // 获取当前页面路由,也就是最后一个打开的页面路由
_thisPage = curPage || _thisPage
2024-06-13 08:10:41 +00:00
let { fullPath } = _thisPage.$page
2024-08-10 01:03:39 +00:00
goto(fullPath, 2)
2024-06-13 08:10:41 +00:00
}
2024-10-31 05:13:32 +00:00
const gotoWebview = ({ url })=> {
goto(`webview/index?url=${encodeURIComponent(url)}`)
}
2024-06-13 08:10:41 +00:00
2023-12-02 08:54:31 +00:00
export {
2024-10-31 05:13:32 +00:00
reLaunch,
gotoWebview,
2023-12-02 08:54:31 +00:00
goto,
2023-12-04 08:14:24 +00:00
goBack,
setClipboardData,
confirmModal,
showLoading,
2024-05-31 10:42:15 +00:00
hideLoading,
2023-12-13 04:27:01 +00:00
showToast,
2023-12-16 17:34:29 +00:00
scanCode,
2023-12-17 15:28:03 +00:00
makePhoneCall,
2024-05-31 10:42:15 +00:00
downloadFile,
saveNetFile,
saveFile,
getImageTempFilePath,
canvasGetImageData,
getImageInfo,
uniPromiseApi,
promiseTimeout,
2024-06-13 08:10:41 +00:00
setNavigationBarTitle,
reloadCurrentPage,
2023-12-02 08:54:31 +00:00
}