shop-applet/utils/uniMPevent.js

63 lines
1.7 KiB
JavaScript

const mp = uni.requireNativePlugin('uniMP');
import {
uniMPgetLocation,
test
} from "@/utils/uniMPfunction.js"
export const initEvent = () => {
mp.onUniMPEventReceive(async (ret) => {
console.log('小程序事件: ', ret);
if (ret.event == 'closeApp') {
mp.closeUniMP(ret.fromAppid, (ret) => {
console.log('closeUniMP: ' + JSON.stringify(ret));
});
}
if (ret.event == 'getLocation') {
try {
console.log('获取定位');
let res = await uniMPgetLocation();
console.log(res);
// plus.geolocation.getCurrentPosition(function(position) {
// console.log('经度:' + position.coords.longitude);
// console.log('纬度:' + position.coords.latitude);
// // that.markers[1].latitude = position.coords.longitude;
// // that.markers[1].longitude = position.coords.longitude;
// mp.sendUniMPEvent(
// ret.fromAppid,
// 'getLocation', {...position.coords},
// (ret) => {
// console.log('Host sendEvent: ' + JSON.stringify(ret));
// });
// }, function(error) {
// console.error('获取位置失败:', error.message);
// }, {provider:'gps'});
mp.sendUniMPEvent(
ret.fromAppid,
'getLocation', {
...res
},
(ret) => {
console.log('Host sendEvent: ' + JSON.stringify(ret));
});
} catch (e) {
console.log(e);
}
}
if (ret.event == 'test') {
console.log('测试');
mp.sendUniMPEvent(
ret.fromAppid,
'test', {
'key': 'value',
'name': 'data'
},
(ret) => {
console.log('Host sendEvent: ' + JSON.stringify(ret));
});
}
if (ret.event == 'log') {
console.log('收到小程序事件: ' + JSON.stringify(ret));
}
});
}