nk-shop2.0/utils/uniMPevent.js
2024-03-15 14:46:33 +08:00

58 lines
1.6 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);
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));
}
//直播小程序跳转商城购买商品
if (ret.event == 'live_shop') {
console.log(JSON.stringify(ret))
let arr1=ret.data
mp.closeUniMP(ret.fromAppid, (ret) => {
// console.log('closeUniMP: ' + JSON.stringify(ret));
setTimeout(()=>{
uni.navigateTo({
url:'/pages/goods_details/index?id='+arr1.app_goods_id
},1000)
})
});
// console.log('收到小程序事件: ' + JSON.stringify(ret));
}
});
}