307 lines
7.3 KiB
JavaScript
307 lines
7.3 KiB
JavaScript
|
const baseurl = import.meta.env.VITE_REQUEST_BASE_URL + '/jeecg-boot/sys/common/static/'
|
|||
|
/**提示*/
|
|||
|
export const toast = (title, icon, duration) => {
|
|||
|
uni.showToast({
|
|||
|
title,
|
|||
|
icon: icon || 'none',
|
|||
|
duration: duration || 2000
|
|||
|
})
|
|||
|
}
|
|||
|
import {
|
|||
|
useStore
|
|||
|
} from '@/store';
|
|||
|
import {
|
|||
|
getUserPermissionApi
|
|||
|
} from '@/api/login.js'
|
|||
|
/**判断用户跳转前是否可以进入该页面*/
|
|||
|
export const beforeJump = (url, callback) => {
|
|||
|
const store = useStore();
|
|||
|
// let page = store.allowPage
|
|||
|
getUserPermissionApi({
|
|||
|
token: store.token,
|
|||
|
type: 'mobile'
|
|||
|
}).then((res) => {
|
|||
|
if (res.success) {
|
|||
|
// 登录人被允许访问的页面
|
|||
|
let page = handleAllowPage(res.result?.menu || [])
|
|||
|
// console.log('@!1',res.result?.menu);
|
|||
|
if (page.some(item => url.indexOf(item) !== -1)) {
|
|||
|
callback()
|
|||
|
} else {
|
|||
|
toast('无查看权限!')
|
|||
|
}
|
|||
|
}
|
|||
|
}).catch((err) => {
|
|||
|
console.log('err@', err);
|
|||
|
})
|
|||
|
// 用户点击的页面路径是否被允许
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// 处理接口返回的允许访问页面的数据
|
|||
|
const handleAllowPage = (menu, arr = []) => {
|
|||
|
if (!menu.length) {
|
|||
|
return []
|
|||
|
}
|
|||
|
menu.forEach((item) => {
|
|||
|
if (item.children) {
|
|||
|
arr.push(...handleAllowPage(item.children))
|
|||
|
}
|
|||
|
arr.push(item.path)
|
|||
|
})
|
|||
|
return arr
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 判断是否有新版本
|
|||
|
* @param version 接口返回的版本号
|
|||
|
* @param isWgt 是否是热资源更新 默认不是
|
|||
|
*/
|
|||
|
export const hasNewVersion = (version, isWgt = false) => {
|
|||
|
// #ifdef APP_PLUS
|
|||
|
return new Promise((resolve) => {
|
|||
|
const transfer = (str) => str.replace(/\./g, '')
|
|||
|
if (isWgt) {
|
|||
|
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
|
|||
|
const currentVersion = widgetInfo.version
|
|||
|
resolve(+transfer(version) > +transfer(currentVersion))
|
|||
|
})
|
|||
|
} else {
|
|||
|
const currentVersion = plus.runtime.version
|
|||
|
resolve(+transfer(version) > +transfer(currentVersion))
|
|||
|
}
|
|||
|
})
|
|||
|
// #endif
|
|||
|
}
|
|||
|
|
|||
|
function downloadApp(url) {
|
|||
|
console.log('url', url);
|
|||
|
var dtask = plus.downloader.createDownload(url, {
|
|||
|
filename: `_downloads/wgt-${Date.now()}.wgt` //利用保存路径,实现下载文件的重命名
|
|||
|
}, function(d, status) {
|
|||
|
//d为下载的文件对象
|
|||
|
if (status == 200) {
|
|||
|
//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
|
|||
|
var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
|
|||
|
console.log('fileSaveUrl', fileSaveUrl);
|
|||
|
installApp(fileSaveUrl)
|
|||
|
} else {
|
|||
|
//下载失败
|
|||
|
plus.downloader.clear(); //清除下载任务
|
|||
|
uni.showToast({
|
|||
|
title: 'App下载失败!',
|
|||
|
icon: 'error'
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
let prg = 0
|
|||
|
let showLoading = plus.nativeUI.showWaiting('正在下載')
|
|||
|
dtask.start()
|
|||
|
dtask.addEventListener('statechanged', (task, status) => {
|
|||
|
// 给下载任务设置一个监听 并根据状态 做操作
|
|||
|
switch (task.state) {
|
|||
|
case 1:
|
|||
|
showLoading.setTitle("正在下载")
|
|||
|
break
|
|||
|
case 2:
|
|||
|
showLoading.setTitle("已连接到服务器")
|
|||
|
break
|
|||
|
case 3:
|
|||
|
prg = parseInt(
|
|||
|
(
|
|||
|
parseFloat(task.downloadedSize) /
|
|||
|
parseFloat(task.totalSize)
|
|||
|
) * 100
|
|||
|
)
|
|||
|
showLoading.setTitle(" 正在下载")
|
|||
|
break
|
|||
|
case 4:
|
|||
|
plus.nativeUI.closeWaiting()
|
|||
|
//下载完成
|
|||
|
break
|
|||
|
}
|
|||
|
})
|
|||
|
// const downloadTask = uni.downloadFile({
|
|||
|
// url,
|
|||
|
// success: res => {
|
|||
|
// if (res.statusCode === 200) {
|
|||
|
// console.log('res.tempFilePath',res.tempFilePath);
|
|||
|
// installApp(res.tempFilePath)
|
|||
|
// } else {
|
|||
|
// uni.showToast({
|
|||
|
// title: 'App下载失败!',
|
|||
|
// icon: 'error'
|
|||
|
// })
|
|||
|
// }
|
|||
|
// }
|
|||
|
// })
|
|||
|
|
|||
|
// downloadTask.onProgressUpdate(res => {
|
|||
|
// progress.value = res.progress
|
|||
|
// })
|
|||
|
}
|
|||
|
|
|||
|
function installApp(tempFilePath) {
|
|||
|
// #ifdef APP-PLUS
|
|||
|
plus.runtime.install(
|
|||
|
tempFilePath, {
|
|||
|
force: true
|
|||
|
},
|
|||
|
() => {
|
|||
|
uni.showModal({
|
|||
|
title: '更新',
|
|||
|
content: '更新成功,请点击确认后重启',
|
|||
|
showCancel: false,
|
|||
|
success(res) {
|
|||
|
if (res.confirm) {
|
|||
|
plus.runtime.restart()
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
},
|
|||
|
() =>
|
|||
|
uni.showToast({
|
|||
|
title: '安装失败!',
|
|||
|
icon: 'error'
|
|||
|
})
|
|||
|
)
|
|||
|
// #endif
|
|||
|
}
|
|||
|
|
|||
|
export function onClickUpdate(updateType, url) {
|
|||
|
// #ifdef APP-PLUS
|
|||
|
if (updateType != 'wgt') plus.runtime.openURL(url.apkUrl)
|
|||
|
else downloadApp(url.wgtUrl)
|
|||
|
// #endif
|
|||
|
}
|
|||
|
/**
|
|||
|
* 获取年月日
|
|||
|
*/
|
|||
|
export const getTime = () => {
|
|||
|
let date = new Date(); //Fri Oct 29 2021 16:37:56 GMT+0800 (CST)
|
|||
|
let timestamp = new Date().getTime(); //1635496676223 (毫秒级)
|
|||
|
let y = date.getFullYear(); //获取完整的年份(4位)
|
|||
|
let m = (date.getMonth() + 1).toString().padStart(2, 0); //获取当前月份(0-11,0代表1月)
|
|||
|
let d = (date.getDate()).toString().padStart(2, 0); //获取当前日(1-31)
|
|||
|
return `${y}-${m}-${d}`
|
|||
|
}
|
|||
|
/**
|
|||
|
* 定位
|
|||
|
*/
|
|||
|
export const getLocation = () => {
|
|||
|
const store = useStore()
|
|||
|
if (!store.positionSwitch) {
|
|||
|
uni.setStorageSync('position', '濮阳市')
|
|||
|
store.setPosition('濮阳市')
|
|||
|
getWeather()
|
|||
|
} else {
|
|||
|
toast('定位刷新中')
|
|||
|
uni.getLocation({
|
|||
|
type: 'wgs84',
|
|||
|
success: function(position) {
|
|||
|
uni.request({
|
|||
|
url: 'http://api.tianditu.gov.cn/geocoder',
|
|||
|
method: 'GET',
|
|||
|
data: {
|
|||
|
postStr: JSON.stringify({
|
|||
|
lon: position.longitude,
|
|||
|
lat: position.latitude,
|
|||
|
ver: 1
|
|||
|
}),
|
|||
|
type: 'geocode',
|
|||
|
tk: '30fe0f0c1b2320e112bde797f3ddaff4'
|
|||
|
},
|
|||
|
success: function(res) {
|
|||
|
let data = res.data;
|
|||
|
if (data.status == 0) {
|
|||
|
const obj = data.result.addressComponent
|
|||
|
let info = obj.city ? obj.city : obj.province
|
|||
|
uni.setStorageSync('position', info)
|
|||
|
store.setPosition(info)
|
|||
|
getWeather(position.latitude, position.longitude)
|
|||
|
} else {
|
|||
|
console.log(data.message);
|
|||
|
}
|
|||
|
},
|
|||
|
fail: function(err) {
|
|||
|
toast('获取定位失败');
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
export const getWeather = (lat, lon) => {
|
|||
|
const store = useStore()
|
|||
|
let params = {}
|
|||
|
if (!store.positionSwitch) {
|
|||
|
params.q = '濮阳市'
|
|||
|
weatherRequest(params)
|
|||
|
} else {
|
|||
|
params.lat = lat
|
|||
|
params.lon = lon
|
|||
|
weatherRequest(params)
|
|||
|
}
|
|||
|
}
|
|||
|
const weatherRequest = (params) => {
|
|||
|
const store = useStore()
|
|||
|
uni.request({
|
|||
|
url: 'https://api.openweathermap.org/data/2.5/weather',
|
|||
|
method: 'GET',
|
|||
|
data: {
|
|||
|
...params,
|
|||
|
appid: '600a60694b0e453dfbaafa862f1d1482',
|
|||
|
lang: 'zh_cn'
|
|||
|
},
|
|||
|
success: function(res) {
|
|||
|
uni.setStorageSync('wendu', Math.round(res.data.main.temp - 273.15))
|
|||
|
uni.setStorageSync('wenduIcon', res.data.weather[0].icon)
|
|||
|
store.setWeather(Math.round(res.data.main.temp - 273.15), res.data
|
|||
|
.weather[0].icon)
|
|||
|
},
|
|||
|
fail: function(err) {
|
|||
|
toast('天气获取失败');
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
export const opendocument = (url) => {
|
|||
|
uni.downloadFile({
|
|||
|
url: baseurl + url,
|
|||
|
success: function(res) {
|
|||
|
var filePath = res.tempFilePath;
|
|||
|
uni.openDocument({
|
|||
|
filePath: filePath,
|
|||
|
showMenu: true,
|
|||
|
fail: function(err) {
|
|||
|
toast(err.errMsg);
|
|||
|
}
|
|||
|
});
|
|||
|
},
|
|||
|
fail: function(err) {
|
|||
|
console.error('文件下载失败', err);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
export const preview = (current, urls) => {
|
|||
|
console.log(current, urls, 'current, urls');
|
|||
|
uni.previewImage({
|
|||
|
// current,
|
|||
|
urls,
|
|||
|
indicator: 'default',
|
|||
|
loop: false,
|
|||
|
longPressActions: {
|
|||
|
itemList: ['保存图片'],
|
|||
|
success: function(data) {
|
|||
|
console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
|
|||
|
},
|
|||
|
fail: function(err) {
|
|||
|
console.log(err.errMsg);
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
export const imgUrl = (url) => {
|
|||
|
return baseurl + `/${url}`
|
|||
|
}
|