From 902224ebf1f1709d4d394970b51081856cf41da0 Mon Sep 17 00:00:00 2001 From: weipengfei <2187978347@qq.com> Date: Wed, 20 Sep 2023 20:11:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 13 +++---------- utils/uniMPevent.js | 43 ++++++++++++++++++++++++++++++++++++++++++ utils/uniMPfunction.js | 20 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 utils/uniMPevent.js create mode 100644 utils/uniMPfunction.js diff --git a/App.vue b/App.vue index 3fe566e..3ec68f2 100644 --- a/App.vue +++ b/App.vue @@ -10,7 +10,7 @@ // +---------------------------------------------------------------------- // #ifdef APP-PLUS let jpushModule = uni.requireNativePlugin("JG-JPush"); - const mp = uni.requireNativePlugin('uniMP'); + import { initEvent } from "@/utils/uniMPevent.js"; // #endif import { checkLogin @@ -87,15 +87,8 @@ // #ifdef APP-PLUS - //监听uni小程序发送的事件 - mp.onUniMPEventReceive(ret => { - console.log('小程序事件: ', ret); - if (ret.event == 'closeApp') { - mp.closeUniMP(ret.fromAppid, (ret) => { - console.log('closeUniMP: ' + JSON.stringify(ret)); - }); - } - }); + //初始化uniMP小程序发送的事件以及其他操作 + initEvent(); // #endif let that = this; diff --git a/utils/uniMPevent.js b/utils/uniMPevent.js new file mode 100644 index 0000000..eea9892 --- /dev/null +++ b/utils/uniMPevent.js @@ -0,0 +1,43 @@ +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, (e) => { + // console.log('发送成功', e); + // }) + 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)); + }); + } + }); +} \ No newline at end of file diff --git a/utils/uniMPfunction.js b/utils/uniMPfunction.js new file mode 100644 index 0000000..cdce7d1 --- /dev/null +++ b/utils/uniMPfunction.js @@ -0,0 +1,20 @@ + +export const test = ()=>{ + return '测试' +} + +export const uniMPgetLocation = ()=>{ + return new Promise((resolve, reject)=>{ + uni.getLocation({ + type: 'gcj02', + geocode: true, + isHighAccuracy: true, + success: (res)=> { + resolve(res) + }, + fail(e) { + reject(e) + } + }); + }) +} \ No newline at end of file