From d9d0300b90f5895b9346ee1e0e66caf14b2700aa Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 8 Nov 2019 15:10:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20uni.scanCode=E3=80=81uni.openLocation?= =?UTF-8?q?=E3=80=81uni.chooseLocation=20=E5=85=BC=E5=AE=B9=20weex=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 修复 ipa 支付没有回调支付结果的问题 fix: uni.scanCode、uni.openLocation、uni.chooseLocation 兼容 weex 编译模式 --- .../service/api/device/scan-code-webview.js | 181 ++++++++++++++++++ .../service/api/device/scan-code-weex.js | 84 ++++++++ .../app-plus/service/api/device/scan-code.js | 87 +-------- .../api/location/choose-location-webview.js | 97 ++++++++++ .../api/location/choose-location-weex.js | 90 +++++++++ .../service/api/location/choose-location.js | 93 +-------- .../api/location/open-location-webview.js | 52 +++++ .../api/location/open-location-weex.js | 18 ++ .../service/api/location/open-location.js | 23 +-- src/platforms/app-plus/service/api/page.js | 22 +-- .../app-plus/service/api/plugin/payment.js | 5 +- 11 files changed, 551 insertions(+), 201 deletions(-) create mode 100644 src/platforms/app-plus/service/api/device/scan-code-webview.js create mode 100644 src/platforms/app-plus/service/api/device/scan-code-weex.js create mode 100644 src/platforms/app-plus/service/api/location/choose-location-webview.js create mode 100644 src/platforms/app-plus/service/api/location/choose-location-weex.js create mode 100644 src/platforms/app-plus/service/api/location/open-location-webview.js create mode 100644 src/platforms/app-plus/service/api/location/open-location-weex.js diff --git a/src/platforms/app-plus/service/api/device/scan-code-webview.js b/src/platforms/app-plus/service/api/device/scan-code-webview.js new file mode 100644 index 000000000..8b5682d22 --- /dev/null +++ b/src/platforms/app-plus/service/api/device/scan-code-webview.js @@ -0,0 +1,181 @@ +import { + getStatusBarStyle +} from '../util' + +import { + invoke +} from '../../bridge' + +import { + ANI_SHOW, + ANI_DURATION +} from '../../constants' + +import { + registerPlusMessage, + consumePlusMessage +} from '../../framework/plus-message' + +export const SCAN_ID = '__UNIAPP_SCAN' +export const SCAN_PATH = '_www/__uniappscan.html' + +const MESSAGE_TYPE = 'scanCode' + +export function scanCode ({ + onlyFromCamera = false, + scanType +}, callbackId) { + const barcode = plus.barcode + const SCAN_TYPES = { + 'qrCode': [ + barcode.QR, + barcode.AZTEC, + barcode.MAXICODE + ], + 'barCode': [ + barcode.EAN13, + barcode.EAN8, + barcode.UPCA, + barcode.UPCE, + barcode.CODABAR, + barcode.CODE128, + barcode.CODE39, + barcode.CODE93, + barcode.ITF, + barcode.RSS14, + barcode.RSSEXPANDED + ], + 'datamatrix': [barcode.DATAMATRIX], + 'pdf417': [barcode.PDF417] + } + + const SCAN_MAPS = { + [barcode.QR]: 'QR_CODE', + [barcode.EAN13]: 'EAN_13', + [barcode.EAN8]: 'EAN_8', + [barcode.DATAMATRIX]: 'DATA_MATRIX', + [barcode.UPCA]: 'UPC_A', + [barcode.UPCE]: 'UPC_E', + [barcode.CODABAR]: 'CODABAR', + [barcode.CODE39]: 'CODE_39', + [barcode.CODE93]: 'CODE_93', + [barcode.CODE128]: 'CODE_128', + [barcode.ITF]: 'CODE_25', + [barcode.PDF417]: 'PDF_417', + [barcode.AZTEC]: 'AZTEC', + [barcode.RSS14]: 'RSS_14', + [barcode.RSSEXPANDED]: 'RSSEXPANDED' + } + + const statusBarStyle = getStatusBarStyle() + const isDark = statusBarStyle !== 'light' + + let result + + let filters = [] + if (Array.isArray(scanType) && scanType.length) { + scanType.forEach(type => { // 暂不考虑去重 + const types = SCAN_TYPES[type] + if (types) { + filters = filters.concat(types) + } + }) + } + if (!filters.length) { + filters = filters.concat(SCAN_TYPES['qrCode']).concat(SCAN_TYPES['barCode']).concat(SCAN_TYPES['datamatrix']).concat( + SCAN_TYPES['pdf417']) + } + + const buttons = [] + if (!onlyFromCamera) { + buttons.push({ + 'float': 'right', + 'text': '相册', + 'fontSize': '17px', + 'width': '60px', + 'onclick': function () { + plus.gallery.pick(file => { + barcode.scan(file, (type, code) => { + if (isDark) { + plus.navigator.setStatusBarStyle('isDark') + } + result = { + type, + code + } + webview.close('auto') + }, () => { + plus.nativeUI.toast('识别失败') + }, filters) + }, err => { + if (err.code !== 12) { + plus.nativeUI.toast('选择失败') + } + }, { + multiple: false, + system: false + }) + } + }) + } + + const webview = plus.webview.create(SCAN_PATH, SCAN_ID, { + titleNView: { + autoBackButton: true, + type: 'float', + backgroundColor: 'rgba(0,0,0,0)', + titleColor: '#ffffff', + titleText: '扫码', + titleSize: '17px', + buttons + }, + popGesture: 'close', + backButtonAutoControl: 'close' + }, { + __uniapp_type: 'scan', + __uniapp_dark: isDark, + __uniapp_scan_type: filters, + 'uni-app': 'none' + }) + const waiting = plus.nativeUI.showWaiting() + webview.addEventListener('titleUpdate', () => { + webview.show(ANI_SHOW, ANI_DURATION, () => { + waiting.close() + }) + }) + webview.addEventListener('close', () => { + if (result) { + invoke(callbackId, { + result: result.code, + scanType: SCAN_MAPS[result.type] || '', + charSet: 'utf8', + path: '', + errMsg: 'scanCode:ok' + }) + } else { + invoke(callbackId, { + errMsg: 'scanCode:fail cancel' + }) + } + consumePlusMessage(MESSAGE_TYPE) + }) + if (isDark) { // 状态栏前景色 + plus.navigator.setStatusBarStyle('light') + webview.addEventListener('popGesture', ({ + type, + result + }) => { + if (type === 'start') { + plus.navigator.setStatusBarStyle('dark') + } else if (type === 'end' && !result) { + plus.navigator.setStatusBarStyle('light') + } + }) + } + + registerPlusMessage(MESSAGE_TYPE, function (res) { + if (res && 'code' in res) { + result = res + } + }, false) +} diff --git a/src/platforms/app-plus/service/api/device/scan-code-weex.js b/src/platforms/app-plus/service/api/device/scan-code-weex.js new file mode 100644 index 000000000..dca381c41 --- /dev/null +++ b/src/platforms/app-plus/service/api/device/scan-code-weex.js @@ -0,0 +1,84 @@ +import { + invoke +} from '../../bridge' + +import { + showPage +} from '../page.js' + +function getStatusBarStyle () { + let style = plus.navigator.getStatusBarStyle() + if (style === 'UIStatusBarStyleBlackTranslucent' || style === 'UIStatusBarStyleBlackOpaque' || style === 'null') { + style = 'light' + } else if (style === 'UIStatusBarStyleDefault') { + style = 'dark' + } + return style +} + +export function scanCode (options, callbackId) { + const statusBarStyle = getStatusBarStyle() + const isDark = statusBarStyle !== 'light' + + let result + const page = showPage({ + url: '__uniappscan', + data: { + scanType: options.scanType + }, + style: { + animationType: options.animationType || 'pop-in', + titleNView: { + autoBackButton: true, + type: 'float', + titleText: options.titleText || '扫码', + titleColor: '#ffffff', + backgroundColor: 'rgba(0,0,0,0)', + buttons: !options.onlyFromCamera ? [{ + text: options.albumText || '相册', + fontSize: '17px', + onclick: () => { + page.sendMessage({ + type: 'gallery' + }) + } + }] : [] + }, + popGesture: 'close', + backButtonAutoControl: 'close' + }, + onMessage ({ + event, + detail + }) { + result = detail + if (event === 'marked') { + result.errMsg = 'scanCode:ok' + } else { + result.errMsg = 'scanCode:fail ' + detail.message + } + }, + onClose () { + if (isDark) { + plus.navigator.setStatusBarStyle('dark') + } + invoke(callbackId, result || { + errMsg: 'scanCode:fail cancel' + }) + } + }) + + if (isDark) { + plus.navigator.setStatusBarStyle('light') + page.webview.addEventListener('popGesture', ({ + type, + result + }) => { + if (type === 'start') { + plus.navigator.setStatusBarStyle('dark') + } else if (type === 'end' && !result) { + plus.navigator.setStatusBarStyle('light') + } + }) + } +} 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 820468a30..87444c15f 100644 --- a/src/platforms/app-plus/service/api/device/scan-code.js +++ b/src/platforms/app-plus/service/api/device/scan-code.js @@ -1,84 +1,7 @@ -import { - invoke -} from '../../bridge' +import * as webview from './scan-code-webview' +import * as weex from './scan-code-weex' -import { - showPage -} from '../page.js' - -function getStatusBarStyle() { - let style = plus.navigator.getStatusBarStyle() - if (style === 'UIStatusBarStyleBlackTranslucent' || style === 'UIStatusBarStyleBlackOpaque' || style === 'null') { - style = 'light' - } else if (style === 'UIStatusBarStyleDefault') { - style = 'dark' - } - return style -} - -export function scanCode(options, callbackId) { - const statusBarStyle = getStatusBarStyle() - const isDark = statusBarStyle !== 'light' - - let result - const page = showPage({ - url: '__uniappscan', - data: { - scanType: options.scanType - }, - style: { - animationType: options.animationType || 'pop-in', - titleNView: { - autoBackButton: true, - type: 'float', - titleText: options.titleText || "扫码", - titleColor: '#ffffff', - backgroundColor: 'rgba(0,0,0,0)', - buttons: !options.onlyFromCamera ? [{ - text: options.albumText || "相册", - fontSize: "17px", - onclick: () => { - page.sendMessage({ - type: "gallery" - }) - } - }] : [] - }, - popGesture: 'close', - backButtonAutoControl: 'close' - }, - onMessage({ - event, - detail - }) { - result = detail - if (event === 'marked') { - result.errMsg = 'scanCode:ok' - } else { - result.errMsg = 'scanCode:fail ' + detail.message - } - }, - onClose() { - if (isDark) { - plus.navigator.setStatusBarStyle('dark') - } - invoke(callbackId, result || { - errMsg: 'scanCode:fail cancel' - }) - } - }) - - if (isDark) { - plus.navigator.setStatusBarStyle('light') - page.webview.addEventListener('popGesture', ({ - type, - result - }) => { - if (type === 'start') { - plus.navigator.setStatusBarStyle('dark') - } else if (type === 'end' && !result) { - plus.navigator.setStatusBarStyle('light') - } - }) - } +export function scanCode (...array) { + const api = __uniConfig.nvueCompiler === 'uni-app' ? weex : webview + return api.scanCode(...array) } diff --git a/src/platforms/app-plus/service/api/location/choose-location-webview.js b/src/platforms/app-plus/service/api/location/choose-location-webview.js new file mode 100644 index 000000000..b1920803d --- /dev/null +++ b/src/platforms/app-plus/service/api/location/choose-location-webview.js @@ -0,0 +1,97 @@ +import { + MAP_ID +} from '../constants' + +import { + invoke +} from '../../bridge' + +import { + ANI_DURATION +} from '../../constants' + +import { + registerPlusMessage, + consumePlusMessage +} from '../../framework/plus-message' + +const CHOOSE_LOCATION_PATH = '_www/__uniappchooselocation.html' + +const MESSAGE_TYPE = 'chooseLocation' + +export function chooseLocation (params, callbackId) { + const statusBarStyle = plus.navigator.getStatusBarStyle() + const webview = plus.webview.create( + CHOOSE_LOCATION_PATH, + MAP_ID, { + titleNView: { + autoBackButton: true, + backgroundColor: '#000000', + titleColor: '#ffffff', + titleText: '选择位置', + titleSize: '17px', + buttons: [{ + float: 'right', + text: '完成', + fontSize: '17px', + onclick: function () { + webview.evalJS('__chooseLocationConfirm__()') + } + }] + }, + popGesture: 'close', + scrollIndicator: 'none' + }, { + __uniapp_type: 'map', + __uniapp_statusbar_style: statusBarStyle, + 'uni-app': 'none' + } + ) + if (statusBarStyle === 'dark') { + plus.navigator.setStatusBarStyle('light') + webview.addEventListener('popGesture', ({ + type, + result + }) => { + if (type === 'start') { + plus.navigator.setStatusBarStyle('dark') + } else if (type === 'end' && !result) { + 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' + }) + } + }) + + registerPlusMessage(MESSAGE_TYPE, function (res) { + if (res && 'latlng' in res) { + result = res + } + }, false) +} diff --git a/src/platforms/app-plus/service/api/location/choose-location-weex.js b/src/platforms/app-plus/service/api/location/choose-location-weex.js new file mode 100644 index 000000000..dff7fa507 --- /dev/null +++ b/src/platforms/app-plus/service/api/location/choose-location-weex.js @@ -0,0 +1,90 @@ +import { + invoke +} from '../../bridge' + +import { + showPage +} from '../page.js' + +function getStatusBarStyle () { + let style = plus.navigator.getStatusBarStyle() + if (style === 'UIStatusBarStyleBlackTranslucent' || style === 'UIStatusBarStyleBlackOpaque' || style === 'null') { + style = 'light' + } else if (style === 'UIStatusBarStyleDefault') { + style = 'dark' + } + return style +} + +export function chooseLocation (options, callbackId) { + const statusBarStyle = getStatusBarStyle() + const isDark = statusBarStyle !== 'light' + + let result + const page = showPage({ + url: '__uniappchooselocation', + data: { + keyword: options.keyword + }, + style: { + animationType: options.animationType || 'slide-in-bottom', + titleNView: { + autoBackButton: false, + titleText: options.titleText || '选择位置', + titleColor: '#ffffff', + backgroundColor: 'rgba(0,0,0,1)', + buttons: [{ + // text: options.cancelText || "取消", + // fontSize: "17px", + type: 'close', + float: 'left', + onclick: () => { + page.close() + } + }, { + text: options.doneText || '完成', + fontSize: '17px', + onclick: () => { + page.sendMessage({ + type: 'done' + }) + } + }] + }, + popGesture: 'close', + scrollIndicator: 'none' + }, + onMessage ({ + event, + detail + }) { + if (event === 'selected') { + result = detail + result.errMsg = 'chooseLocation:ok' + } + }, + onClose () { + if (isDark) { + plus.navigator.setStatusBarStyle('dark') + } + + invoke(callbackId, result || { + errMsg: 'chooseLocation:fail cancel' + }) + } + }) + + if (isDark) { + plus.navigator.setStatusBarStyle('light') + page.webview.addEventListener('popGesture', ({ + type, + result + }) => { + if (type === 'start') { + plus.navigator.setStatusBarStyle('dark') + } else if (type === 'end' && !result) { + plus.navigator.setStatusBarStyle('light') + } + }) + } +} 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 5070519ee..bc5305a54 100644 --- a/src/platforms/app-plus/service/api/location/choose-location.js +++ b/src/platforms/app-plus/service/api/location/choose-location.js @@ -1,90 +1,7 @@ -import { - invoke -} from '../../bridge' +import * as webview from './choose-location-webview' +import * as weex from './choose-location-weex' -import { - showPage -} from '../page.js' - -function getStatusBarStyle() { - let style = plus.navigator.getStatusBarStyle() - if (style === 'UIStatusBarStyleBlackTranslucent' || style === 'UIStatusBarStyleBlackOpaque' || style === 'null') { - style = 'light' - } else if (style === 'UIStatusBarStyleDefault') { - style = 'dark' - } - return style -} - -export function chooseLocation(options, callbackId) { - const statusBarStyle = getStatusBarStyle() - const isDark = statusBarStyle !== 'light' - - let result - const page = showPage({ - url: '__uniappchooselocation', - data: { - keyword: options.keyword - }, - style: { - animationType: options.animationType || 'slide-in-bottom', - titleNView: { - autoBackButton: false, - titleText: options.titleText || "选择位置", - titleColor: '#ffffff', - backgroundColor: 'rgba(0,0,0,1)', - buttons: [{ - // text: options.cancelText || "取消", - // fontSize: "17px", - type: "close", - float: "left", - onclick: () => { - page.close() - } - }, { - text: options.doneText || "完成", - fontSize: "17px", - onclick: () => { - page.sendMessage({ - type: "done" - }) - } - }] - }, - popGesture: 'close', - scrollIndicator: 'none' - }, - onMessage({ - event, - detail - }) { - if (event === 'selected') { - result = detail - result.errMsg = 'chooseLocation:ok' - } - }, - onClose() { - if (isDark) { - plus.navigator.setStatusBarStyle('dark') - } - - invoke(callbackId, result || { - errMsg: 'chooseLocation:fail cancel' - }) - } - }) - - if (isDark) { - plus.navigator.setStatusBarStyle('light') - page.webview.addEventListener('popGesture', ({ - type, - result - }) => { - if (type === 'start') { - plus.navigator.setStatusBarStyle('dark') - } else if (type === 'end' && !result) { - plus.navigator.setStatusBarStyle('light') - } - }) - } +export function chooseLocation (...array) { + const api = __uniConfig.nvueCompiler === 'uni-app' ? weex : webview + return api.chooseLocation(...array) } diff --git a/src/platforms/app-plus/service/api/location/open-location-webview.js b/src/platforms/app-plus/service/api/location/open-location-webview.js new file mode 100644 index 000000000..e5cf71c14 --- /dev/null +++ b/src/platforms/app-plus/service/api/location/open-location-webview.js @@ -0,0 +1,52 @@ +import { + MAP_ID +} from '../constants' + +import { + ANI_SHOW, + ANI_DURATION +} from '../../constants' + +const OPEN_LOCATION_PATH = '_www/__uniappopenlocation.html' + +export function openLocation (params) { + const statusBarStyle = plus.navigator.getStatusBarStyle() + const webview = plus.webview.create( + OPEN_LOCATION_PATH, + MAP_ID, { + titleNView: { + autoBackButton: true, + titleColor: '#ffffff', + titleText: '', + titleSize: '17px', + type: 'transparent' + }, + popGesture: 'close', + scrollIndicator: 'none' + }, { + __uniapp_type: 'map', + __uniapp_statusbar_style: statusBarStyle, + 'uni-app': 'none' + } + ) + if (statusBarStyle === 'light') { + plus.navigator.setStatusBarStyle('dark') + webview.addEventListener('popGesture', ({ + type, + result + }) => { + if (type === 'start') { + plus.navigator.setStatusBarStyle('light') + } else if (type === 'end' && !result) { + plus.navigator.setStatusBarStyle('dark') + } + }) + } + webview.show(ANI_SHOW, ANI_DURATION, () => { + webview.evalJS(`__openLocation__(${JSON.stringify(params)})`) + }) + + return { + errMsg: 'openLocation:ok' + } +} diff --git a/src/platforms/app-plus/service/api/location/open-location-weex.js b/src/platforms/app-plus/service/api/location/open-location-weex.js new file mode 100644 index 000000000..4ee503842 --- /dev/null +++ b/src/platforms/app-plus/service/api/location/open-location-weex.js @@ -0,0 +1,18 @@ +import { + showPage +} from '../page.js' + +export function openLocation (data) { + showPage({ + url: '__uniappopenlocation', + data, + style: { + titleNView: { + type: 'transparent' + } + } + }) + return { + errMsg: 'openLocation:ok' + } +} diff --git a/src/platforms/app-plus/service/api/location/open-location.js b/src/platforms/app-plus/service/api/location/open-location.js index 6e8f90a76..20240fc95 100644 --- a/src/platforms/app-plus/service/api/location/open-location.js +++ b/src/platforms/app-plus/service/api/location/open-location.js @@ -1,18 +1,7 @@ -import { - showPage -} from '../page.js' +import * as webview from './open-location-webview' +import * as weex from './open-location-weex' -export function openLocation(data) { - showPage({ - url: '__uniappopenlocation', - data, - style: { - titleNView: { - type: "transparent" - } - } - }) - return { - errMsg: 'openLocation:ok' - } -} +export function openLocation (...array) { + const api = __uniConfig.nvueCompiler === 'uni-app' ? weex : webview + return api.openLocation(...array) +} diff --git a/src/platforms/app-plus/service/api/page.js b/src/platforms/app-plus/service/api/page.js index 3c4201cdb..255a9a058 100644 --- a/src/platforms/app-plus/service/api/page.js +++ b/src/platforms/app-plus/service/api/page.js @@ -4,27 +4,27 @@ let uni_ let runtime -function getRuntime() { +function getRuntime () { return runtime || (runtime = typeof window === 'object' && typeof navigator === 'object' && typeof document === - 'object' ? - 'webview' : 'v8') + 'object' + ? 'webview' : 'v8') } -function setRuntime(value) { +function setRuntime (value) { runtime = value } -function getPageId() { +function getPageId () { return plus_.webview.currentWebview().id } let initedEventListener = false const callbacks = {} -function addEventListener(pageId, callback) { +function addEventListener (pageId, callback) { const runtime = getRuntime() - function onPlusMessage(res) { + function onPlusMessage (res) { const message = res.data && res.data.__message if (!message || !message.__page) { return @@ -51,10 +51,10 @@ function addEventListener(pageId, callback) { } class Page { - constructor(webview) { + constructor (webview) { this.webview = webview } - sendMessage(data) { + sendMessage (data) { const runtime = getRuntime() const message = { __message: { @@ -69,12 +69,12 @@ class Page { plus_.webview.postMessageToUniNView(message, this.webview.id) } } - close() { + close () { this.webview.close() } } -export function showPage({ +export function showPage ({ context, runtime, url, diff --git a/src/platforms/app-plus/service/api/plugin/payment.js b/src/platforms/app-plus/service/api/plugin/payment.js index 39ebab11c..525d15d75 100644 --- a/src/platforms/app-plus/service/api/plugin/payment.js +++ b/src/platforms/app-plus/service/api/plugin/payment.js @@ -14,9 +14,8 @@ export function requestPayment (params, callbackId) { }) } else { plus.payment.request(service, params.orderInfo, res => { - invoke(callbackId, { - errMsg: 'requestPayment:ok' - }) + res.errMsg = 'requestPayment:ok' + invoke(callbackId, res) }, err => { invoke(callbackId, { errMsg: 'requestPayment:fail:' + err.message -- GitLab