138 lines
4.1 KiB
TypeScript
138 lines
4.1 KiB
TypeScript
/**
|
||
* 将分转换为元(保留2位小数,带千分位)
|
||
* @param cent 分为单位的金额
|
||
* @returns 元为单位的金额字符串(带千分位)
|
||
*/
|
||
export const centToYuanWithComma = (cent : number) : string => {
|
||
if (!cent && cent !== 0) return '0.00'
|
||
return (cent / 100).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
||
}
|
||
|
||
/**
|
||
* 将秒转换为小时(保留2位小数)
|
||
* @param seconds 秒数
|
||
* @returns 小时数(字符串格式,保留2位小数)
|
||
*/
|
||
export const secondsToHours = (seconds : number) : string => {
|
||
if (!seconds && seconds !== 0) return '0.00'
|
||
return (seconds / 3600).toFixed(2)
|
||
}
|
||
|
||
|
||
|
||
|
||
// 检查app是否开启了通知权限 安卓苹果通用
|
||
export const checkNotificationAuthorized = () => {
|
||
const notificationAuthorized = uni.getAppAuthorizeSetting().notificationAuthorized
|
||
if (notificationAuthorized !== 'authorized') {
|
||
uni.showModal({
|
||
title: '通知权限',
|
||
content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
|
||
confirmText: '去设置',
|
||
showCancel: false,
|
||
success: (res) => {
|
||
if (res.confirm) uni.openAppAuthorizeSetting()
|
||
},
|
||
})
|
||
}
|
||
}
|
||
|
||
|
||
|
||
// 监听消息推送
|
||
export const pushListener = () => {
|
||
//收到透传消息
|
||
//只有APP在线时,才会触发receive事件,透传消息不会触发系统消息,需要创建本地消息
|
||
plus.push.addEventListener(
|
||
'receive',
|
||
function (msg) {
|
||
console.log('111')
|
||
console.log('(receive):' + JSON.stringify(msg))
|
||
if (plus.os.name == 'Android') {
|
||
plus.push.createMessage(msg.content, msg.payload, {
|
||
title: msg.title,
|
||
})
|
||
if (msg.payload.audio) {
|
||
audioPlayer(msg.payload.audio)
|
||
}
|
||
// getApp().globalData.msgReadNum++
|
||
// plus.runtime.setBadgeNumber(getApp().globalData.msgReadNum)
|
||
} else if (plus.os.name == 'iOS') {
|
||
// ios
|
||
if (msg.aps == null && msg.type == 'receive') {
|
||
var option = {
|
||
cover: true,
|
||
title: msg.title,
|
||
}
|
||
plus.push.createMessage(msg.payload.body, msg.payload, option)
|
||
// getApp().globalData.msgReadNum++
|
||
// plus.runtime.setBadgeNumber(getApp().globalData.msgReadNum)
|
||
}
|
||
}
|
||
},
|
||
false,
|
||
)
|
||
|
||
//消息点击事件
|
||
//【APP在线】,收到透传消息通过,不会提醒至通知栏目,需要发送本地消息,再进行点击触发的点击事件。
|
||
//【APP离线】,收到离线透传消息,必须通过Java后台的Intent字符串携带payload,且符合格式才能触发click事件,格式不符合不会触发。
|
||
plus.push.addEventListener(
|
||
'click',
|
||
function (msg) {
|
||
console.log('2222')
|
||
console.log('(click):' + JSON.stringify(msg))
|
||
if (plus.os.name == 'iOS') {
|
||
//如果是IOS
|
||
var payload
|
||
if (msg.type == 'click') {
|
||
//APP离线点击包含click属性,这时payload是JSON对象
|
||
payload = msg.payload
|
||
} else {
|
||
//APP在线,收到消息不会包含type属性,这时的payload是JSON字符串,需要转为JSON对象
|
||
payload = JSON.parse(msg.payload)
|
||
}
|
||
if (payload != null || payload != undefined) {
|
||
var messageType = payload.messageType
|
||
// messageClick(messageType, payload);
|
||
}
|
||
}
|
||
if (plus.os.name == 'Android') {
|
||
console.log('Android', msg.payload)
|
||
if (msg.payload) {
|
||
uni.navigateTo({
|
||
url: msg.payload.url,
|
||
})
|
||
}
|
||
}
|
||
},
|
||
false,
|
||
)
|
||
}
|
||
|
||
/*
|
||
获取城市编码
|
||
*/
|
||
export const getCityCode = async (longitude : number, latitude : number) : Promise<any> => {
|
||
const apiKey = '' // 高德地图 API Key
|
||
const url = `https://restapi.amap.com/v3/geocode/regeo?key=${apiKey}&location=${longitude},${latitude}&output=json`
|
||
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
url,
|
||
success: (res) => {
|
||
console.log(res.data, 'res.data')
|
||
if (res.data && res.data.regeocode && res.data.regeocode.addressComponent) {
|
||
// console.log(res.data.regeocode.addressComponent,'res.data.regeocode.addressComponent');
|
||
const cityCode = res.data.regeocode.addressComponent.citycode // 获取城市编码
|
||
resolve({ data: res.data, cityCode })
|
||
} else {
|
||
reject('无法获取当前城市位置')
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
console.error('获取城市位置失败:', err)
|
||
reject(err)
|
||
},
|
||
})
|
||
})
|
||
} |