wechat_yxcl/common/utils.js
2021-12-10 20:18:59 +08:00

169 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import store from '../store/store.js'
import request from '@/util/index.js'
const QQMapWX = require('./qqmap-wx-jssdk.min.js')
const qqmapsdk = new QQMapWX({
key: "SVKBZ-P6QCJ-NH7F7-KOPJW-CBNEV-FUBRT"
})
/**
* 地图两点之间的距离
* @doc 文档参考https://lbs.qq.com/qqmap_wx_jssdk/method-search.html
* @export
* @param {*} to 坐标 { latitude: 39.984060, longitude: 116.307520 }
* @param {*} location 坐标:{ latitude: 39.984060, longitude: 116.307520 }
* @returns
*/
function calculateDistance (fromp, to) {
return new Promise((resolve, reject) => {
qqmapsdk.calculateDistance({
mode: 'straight',
to: [{
latitude: to.latitude,
longitude: to.longitude
}],
'from': {
latitude: fromp.latitude,
longitude: fromp.longitude
},
success: res => {
resolve(res)
},
fail: err => {
reject(err)
uni.showToast({
title: err.message,
icon: 'none',
duration: 3000
})
}
})
})
}
/**
* 坐标转换,百度地图坐标转换成腾讯地图坐标
* lng 腾讯经度pointy
* lat 腾讯纬度pointx
* 经度>纬度
*/
function bMapToQQMap(lng, lat) {
if (lng == null || lng == '' || lat == null || lat == '')
return [lng, lat];
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
var x = parseFloat(lng) - 0.0065;
var y = parseFloat(lat) - 0.006;
var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
var lng = (z * Math.cos(theta)); //.toFixed(7);
var lat = (z * Math.sin(theta)); //.toFixed(7);
return [lng, lat];
}
const netChange = () => {
uni.onNetworkStatusChange(function(res){
// console.log(res.isConnected)
store.mutations.isConnect( res.isConnected)
if(!res.isConnected) {
uni.showToast({
title:'网络未连接',
icon:'none'
})
}
})
}
const cutDate = (dd, type, daynum) => {
dd = new Date(dd) || new Date();
type = type || 'YYYY/MM/DD' //hh:mm:ss
daynum = daynum*24*60*60*1000 || 0
// console.log(daynum,dd,dd.getTime()+daynum)
dd = new Date(dd.getTime()+daynum )// 获取AddDayCount天后的日期
var y = dd.getFullYear()
var m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) // 获取当前月份的日期不足10补0
var d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号不足10补0
var h = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()
var mi = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()
var s = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
return type.replace('YYYY', y).replace('MM', m).replace('DD', d).replace('hh', h).replace('mm', mi).replace('ss', s)
}
// 金额格式化处理方法
const fmoney = (s, n)=> {
/*
* 参数说明:
* s要格式化的数字
* n保留几位小数
* */
n = n > 0 && n <= 20 ? n : 2;
var fuhao = ''; //如果数字小于零则值为-
if(s<0){
s = Math.abs(s);
fuhao = '-'
}
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
let t = "";
for (let i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return fuhao+[...t].reverse().join("") + "." + r;
}
function toNextRoute (type,url) {
uni[type]({
url: url,
success: (result) => {
addUserBehavior({ intoRoute: url })
}
})
}
function addUserBehavior (obj) {
var pages = getCurrentPages() // 获取加载的页面
let len = pages.length
if(len){
var currentPage = pages[len - 1] // 获取当前页面的对象
var nowRoute = '/' + currentPage.route // 当前页面url
if (obj) {
obj.outtoRoute = (obj.outtoRoute === '' || obj.outtoRoute) ? obj.outtoRoute : nowRoute
obj.intoRoute = obj.intoRoute ? obj.intoRoute.split('?')[0] : ''
} else {
obj = {}
obj.outtoRoute = nowRoute
obj.intoRoute = len >1 ? '/' + pages[len - 2].route : '/pages/index/index'
}
}
obj.visitChannels = store.state.visitChannels
// request.$webGet('WeChat/AddUserBehaviorNew',obj).then(res => {
request.$webGet('WeChatPushAPI/Member/AddUserBehaviorNew',obj).then(res => {
// console.log(obj)
})
}
// 视频播放
const EZUIPlayer = require('./ezuikit.js');
function playVideo(opt) {
debugger
return new EZUIPlayer(opt)
}
export default {
netChange,
cutDate,
fmoney,
toNextRoute,
addUserBehavior,
calculateDistance,
bMapToQQMap,
playVideo
}