From 750abbcfb9f12741d0f80102a68a56afe7d595a7 Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 28 Aug 2019 15:12:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BA=AF=20nvue=20?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=20uni.scanCode=E3=80=81uni.chooseLocation=20?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E4=B8=8D=E6=89=A7=E8=A1=8C=E7=9A=84=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app-plus/service/api/device/scan-code.js | 31 +++++----- .../service/api/location/choose-location.js | 57 ++++++++++++------- .../app-plus/service/framework/app.js | 13 ++++- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/platforms/app-plus/service/api/device/scan-code.js b/src/platforms/app-plus/service/api/device/scan-code.js index b14998b966..8b5682d224 100644 --- a/src/platforms/app-plus/service/api/device/scan-code.js +++ b/src/platforms/app-plus/service/api/device/scan-code.js @@ -12,7 +12,8 @@ import { } from '../../constants' import { - registerPlusMessage + registerPlusMessage, + consumePlusMessage } from '../../framework/plus-message' export const SCAN_ID = '__UNIAPP_SCAN' @@ -97,12 +98,12 @@ export function scanCode ({ barcode.scan(file, (type, code) => { if (isDark) { plus.navigator.setStatusBarStyle('isDark') + } + result = { + type, + code } webview.close('auto') - result = { - type, - code - } }, () => { plus.nativeUI.toast('识别失败') }, filters) @@ -143,7 +144,7 @@ export function scanCode ({ }) }) webview.addEventListener('close', () => { - if (result && 'code' in result) { + if (result) { invoke(callbackId, { result: result.code, scanType: SCAN_MAPS[result.type] || '', @@ -151,11 +152,12 @@ export function scanCode ({ path: '', errMsg: 'scanCode:ok' }) - } else { - invoke(callbackId, { - errMsg: 'scanCode:fail cancel' - }) + } else { + invoke(callbackId, { + errMsg: 'scanCode:fail cancel' + }) } + consumePlusMessage(MESSAGE_TYPE) }) if (isDark) { // 状态栏前景色 plus.navigator.setStatusBarStyle('light') @@ -170,15 +172,10 @@ export function scanCode ({ } }) } - // fixed by hxy 注册扫码事件 + registerPlusMessage(MESSAGE_TYPE, function (res) { - if (res && !res.errMsg) { + if (res && 'code' in res) { result = res - } else { - const errMsg = res && res.errMsg ? ' ' + res.errMsg : '' - result = { - errMsg: 'scanCode:fail' + errMsg - } } }, false) } diff --git a/src/platforms/app-plus/service/api/location/choose-location.js b/src/platforms/app-plus/service/api/location/choose-location.js index 21c0973d7d..b1920803d5 100644 --- a/src/platforms/app-plus/service/api/location/choose-location.js +++ b/src/platforms/app-plus/service/api/location/choose-location.js @@ -11,10 +11,13 @@ import { } from '../../constants' import { - registerPlusMessage + registerPlusMessage, + consumePlusMessage } from '../../framework/plus-message' -const CHOOSE_LOCATION_PATH = '_www/__uniappchooselocation.html' +const CHOOSE_LOCATION_PATH = '_www/__uniappchooselocation.html' + +const MESSAGE_TYPE = 'chooseLocation' export function chooseLocation (params, callbackId) { const statusBarStyle = plus.navigator.getStatusBarStyle() @@ -56,27 +59,39 @@ export function chooseLocation (params, callbackId) { plus.navigator.setStatusBarStyle('light') } }) + } + let index = 0 + let onShow = function () { + index++ + if (index === 2) { + webview.evalJS(`__chooseLocation__(${JSON.stringify(params)})`) + } } + webview.addEventListener('loaded', onShow) + webview.show('slide-in-bottom', ANI_DURATION, onShow) + + let result + + webview.addEventListener('close', () => { + if (result) { + invoke(callbackId, { + name: result.poiname, + address: result.poiaddress, + latitude: result.latlng.lat, + longitude: result.latlng.lng, + errMsg: 'chooseLocation:ok' + }) + } else { + consumePlusMessage(MESSAGE_TYPE) + invoke(callbackId, { + errMsg: 'chooseLocation:fail cancel' + }) + } + }) - webview.show('slide-in-bottom', ANI_DURATION, () => { - webview.evalJS(`__chooseLocation__(${JSON.stringify(params)})`) - }) - - // fixed by hxy - registerPlusMessage('chooseLocation', function (res) { - if (res && !res.errMsg) { - invoke(callbackId, { - name: res.poiname, - address: res.poiaddress, - latitude: res.latlng.lat, - longitude: res.latlng.lng, - errMsg: 'chooseLocation:ok' - }) - } else { - const errMsg = res && res.errMsg ? ' ' + res.errMsg : '' - invoke(callbackId, { - errMsg: 'chooseLocation:fail' + errMsg - }) + registerPlusMessage(MESSAGE_TYPE, function (res) { + if (res && 'latlng' in res) { + result = res } }, false) } diff --git a/src/platforms/app-plus/service/framework/app.js b/src/platforms/app-plus/service/framework/app.js index 532b8693e9..5d211a7eae 100644 --- a/src/platforms/app-plus/service/framework/app.js +++ b/src/platforms/app-plus/service/framework/app.js @@ -9,7 +9,8 @@ import { } from './page' import { - registerPlusMessage + registerPlusMessage, + consumePlusMessage } from './plus-message' import { @@ -68,6 +69,16 @@ function initGlobalListeners () { publish('onKeyboardHeightChange', { height: event.height }) + }) + + plus.globalEvent.addEventListener('plusMessage', function (e) { + if (process.env.NODE_ENV !== 'production') { + console.log('UNIAPP[plusMessage]:[' + Date.now() + ']' + JSON.stringify(e.data)) + } + if (e.data && e.data.type) { + const type = e.data.type + consumePlusMessage(type, e.data.args || {}) + } }) } -- GitLab