提交 750abbcf 编写于 作者: Q qiang

fix: 解决纯 nvue 环境 uni.scanCode、uni.chooseLocation 回调不执行的 Bug

上级 e03f4c9e
......@@ -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)
}
......@@ -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)
}
......@@ -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 || {})
}
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册