const tabBarUrls = [ '/pages/tabBar/deal/deal', '/pages/tabBar/home/home', '/pages/tabBar/mine/mine', '/pages/tabBar/order/order' ] const goto = (url, type = 1)=> { url = `/pages/${url}` if(tabBarUrls.indexOf(url) > -1) { return uni.switchTab({ url }); } if (type == 1){ uni.navigateTo({ url }); } else if (type == 2){ 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() } }) }) } const showLoading = (title)=> { uni.showLoading({ mask: true, title: title || '加载中' }); } const hideLoading = ()=> { uni.hideLoading(); } const scanCode = ()=> { return new Promise((resolve, reject)=> { uni.scanCode({ onlyFromCamera: false, success: (res) =>{ console.log("scanCode res========", res) resolve(res.result) }, fail: reject }) }) } const makePhoneCall = (phoneNumber)=> { uni.makePhoneCall({ phoneNumber: phoneNumber }); } const downloadFile = (url)=> { return new Promise((resolve, reject)=> { uni.downloadFile({ url: url, success: (res) => { if (res.statusCode === 200) { console.log('下载成功'); resolve(res) } }, fail: (err)=>{ console.log("downloadFile:err", err) reject(err) } }); }) } 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) } }); }) } export { reLaunch, goto, goBack, setClipboardData, confirmModal, showLoading, hideLoading, showToast, scanCode, makePhoneCall, downloadFile, saveNetFile, saveFile, }