recycapp/common/util.js

154 lines
4.6 KiB
JavaScript

var QQMapWX = require('@/common/qqmap-wx-jssdk.min.js')
const util = {
openMap(address, latitude, longitude) {
var url = '';
// const address = accept_address; //地址
// const latitude = latitude;//纬度
// const longitude = longitude;//精度
url = 'geo:' + latitude + ',' + longitude + '?q=' + encodeURIComponent(address);
// geo:34.24710702460227,108.90977498072209?q=%E8%A5%BF%E5%AE%89%E5%B8%82%E8%B4%A2%E5%AF%8C%E4%B8%AD%E5%BF%83C%E5%BA%A71002
console.log(address,latitude,longitude)
if (uni.getSystemInfoSync().platform == 'android') {
plus.runtime.openURL(url);
} else {
plus.nativeUI.actionSheet({title:"选择地图应用",cancel:"取消",buttons:[{title:"Apple地图"},{title:"百度地图"},{title:"高德地图"},{title:"google地图"}]}, function(e){
console.log("e.index: " + e.index);
switch (e.index){
case 1:
url = `http://maps.apple.com/?q=${encodeURIComponent(address)}&ll=${latitude},${longitude}&spn=0.008766,0.019441`
break;
case 2:
url = `baidumap://map/marker?location=${latitude},${longitude}&title=DCloud&src=Hello%20uni-app`;
break;
case 3:
url = `iosamap://viewMap?sourceApplication=Hello%20uni-app&poiname=DCloud&lat=${latitude}&lon=${longitude}&dev=0`;
break;
case 4:
url = `comgooglemaps://?q=${encodeURIComponent(address)}&center=${latitude},${longitude}`;
break;
plus.runtime.openURL(url, function( e ){
plus.nativeUI.alert("未安装此地图");
});
}
})
}
},
getAddress(callBack) {
console.log('+++++++++++++++++')
/* #ifdef H5 */
callBack({
city: '南京市',
longitude: 118.710193,
latitude: 32.202905
})
/* #endif */
var that = this
uni.getLocation({
type: 'wgs84',
geocode: true,
success: function(res) {
console.log('当前位置的经度:' + res.longitude,res);
console.log('当前位置的纬度:' + res.latitude);
that.longitude = res.longitude
that.latitude = res.latitude
let location = {
longitude: res.longitude,
latitude: res.latitude
}
console.log(location,'fffffff')
const qqmapsdk = new QQMapWX({
key: 'EO2BZ-YGE33-NHJ3S-RCRUJ-WT47J-5DB3I'
})
qqmapsdk.reverseGeocoder({
location,
success: function(res){
console.log(res,res.result.address,'9999')
that.position = res.result.address_component.city
if(callBack){
callBack({
city: that.position,
longitude: location.longitude,
latitude: location.latitude
})
}
}
})
},
fail(res) {
console.log('dddddddd---',res)
},
complete(res) {
console.log('getLocaltion===+++---',res)
}
})
},
delRepeat(arr,key){ //去重
let resArr = []
arr.some(function(item,index){
console.log(item);
let isRepeat = false;
resArr.some(function(item1,index1){
if(item.tn == item1.tn){
isRepeat = true;
}
});
if(!isRepeat){
resArr.push(item);
}
})
return resArr;
},
Format(date,fmt){
date = new Date(date);
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
},
formatDate(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
const date = new Date(timestamp);
const map = {
'YYYY': date.getFullYear(),
'MM': String(date.getMonth() + 1).padStart(2, '0'),
'DD': String(date.getDate()).padStart(2, '0'),
'HH': String(date.getHours()).padStart(2, '0'),
'mm': String(date.getMinutes()).padStart(2, '0'),
'ss': String(date.getSeconds()).padStart(2, '0')
};
return format.replace(/YYYY|MM|DD|HH|mm|ss/g, match => map[match]);
},
showMsg(msg,icon,duration = 1600) {
uni.showToast({
title: msg,
duration: duration,
icon: icon||'none'
});
},
getDate(time){
let timeStr = this.Format(time,'yyyy/MM/dd hh:mm');
let timeParse = Date.parse(new Date(timeStr))
let timer = {};
timer.timeStr = timeStr;
timer.timeParse = timeParse;
return timer
},
backPage(time){
setTimeout(function(){
uni.navigateBack({
delta:1
})
},time||3000)
}
}
export default util