diff --git a/build/rollup.config.app.js b/build/rollup.config.app.js index 9832fd4152aeb487b4824d71de1674b093a67ba6..0c8c141fbe64defa8e07e923791af8c5c4a6b0f9 100644 --- a/build/rollup.config.app.js +++ b/build/rollup.config.app.js @@ -38,7 +38,7 @@ module.exports = { requireContext(), alias({ 'uni-core': path.resolve(__dirname, '../src/core'), - 'uni-platform': path.resolve(__dirname, '../src/platforms/app-plus'), + 'uni-platform': path.resolve(__dirname, '../src/platforms/' + process.env.UNI_PLATFORM), 'uni-platforms': path.resolve(__dirname, '../src/platforms'), 'uni-shared': path.resolve(__dirname, '../src/shared/util.js'), 'uni-helpers': path.resolve(__dirname, '../src/core/helpers') diff --git a/packages/uni-app-plus-nvue/dist/index.legacy.js b/packages/uni-app-plus-nvue/dist/index.legacy.js index f2a4b2653a78c940aa97b865bcd71a66e26e5210..b0a33bd7214185e7c5432a3762a9010d7b5e8b00 100644 --- a/packages/uni-app-plus-nvue/dist/index.legacy.js +++ b/packages/uni-app-plus-nvue/dist/index.legacy.js @@ -518,11 +518,11 @@ class MapContext { function createMapContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createMapContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createMapContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm); if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new MapContext(id, elm) } @@ -576,11 +576,11 @@ class VideoContext { function createVideoContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createVideoContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createVideoContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm); if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new VideoContext(id, elm) } @@ -650,11 +650,11 @@ class LivePusherContext { function createLivePusherContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createLivePusherContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createLivePusherContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm); if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new LivePusherContext(id, elm) } diff --git a/packages/uni-app-plus-nvue/dist/uni.js b/packages/uni-app-plus-nvue/dist/uni.js index e7c22d200d71dad2fbd6bbeb9a1ba6a1fb542785..d0cb593c1befe49b3fa275390dad0fa1d3db13d0 100644 --- a/packages/uni-app-plus-nvue/dist/uni.js +++ b/packages/uni-app-plus-nvue/dist/uni.js @@ -1816,7 +1816,7 @@ function invokeCallbackHandler (invokeCallbackId, res) { } function wrapperUnimplemented (name) { - return function (args) { + return function unimplemented (args) { console.error('API `' + name + '` is not yet implemented'); } } @@ -6415,7 +6415,7 @@ function showTabBar$2 ({ -var api$1 = /*#__PURE__*/Object.freeze({ +var appApi = /*#__PURE__*/Object.freeze({ startPullDownRefresh: startPullDownRefresh$1, stopPullDownRefresh: stopPullDownRefresh$1, chooseImage: chooseImage$1, @@ -6454,8 +6454,6 @@ var api$1 = /*#__PURE__*/Object.freeze({ startBeaconDiscovery: startBeaconDiscovery, stopBeaconDiscovery: stopBeaconDiscovery, makePhoneCall: makePhoneCall$1, - SCAN_ID: SCAN_ID, - SCAN_PATH: SCAN_PATH, scanCode: scanCode, getSystemInfo: getSystemInfo, vibrateLong: vibrateLong, @@ -6522,16 +6520,312 @@ var api$1 = /*#__PURE__*/Object.freeze({ showTabBar: showTabBar$2 }); -const api$2 = Object.create(null); +const SUCCESS = 'success'; +const FAIL = 'fail'; +const COMPLETE = 'complete'; +const CALLBACKS = [SUCCESS, FAIL, COMPLETE]; -Object.assign(api$2, api); -Object.assign(api$2, api$1); +/** + * 调用无参数,或仅一个参数且为 callback 的 API + * @param {Object} vm + * @param {Object} method + * @param {Object} args + * @param {Object} extras + */ +function invokeVmMethodWithoutArgs (vm, method, args, extras) { + if (!vm) { + return + } + if (typeof args === 'undefined') { + return vm[method]() + } + const [, callbacks] = normalizeArgs(args, extras); + if (!Object.keys(callbacks).length) { + return vm[method]() + } + return vm[method](normalizeCallback(method, callbacks)) +} +/** + * 调用两个参数(第一个入参为普通参数,第二个入参为 callback) API + * @param {Object} vm + * @param {Object} method + * @param {Object} args + * @param {Object} extras + */ +function invokeVmMethod (vm, method, args, extras) { + if (!vm) { + return + } + const [pureArgs, callbacks] = normalizeArgs(args, extras); + if (!Object.keys(callbacks).length) { + return vm[method](pureArgs) + } + return vm[method](pureArgs, normalizeCallback(method, callbacks)) +} + +function findElmById (id, vm) { + return findElmByVNode(id, vm._vnode) +} + +function findElmByVNode (id, vnode) { + if (!id || !vnode) { + return + } + if ( + vnode.data && + vnode.data.attrs && + vnode.data.attrs.id === id + ) { + return vnode.elm + } + const children = vnode.children; + if (!children) { + return + } + for (let i = 0, len = children.length; i < len; i++) { + const elm = findElmByVNode(id, children[i]); + if (elm) { + return elm + } + } +} + +function normalizeArgs (args = {}, extras) { + const callbacks = Object.create(null); + + const iterator = function iterator (name) { + const callback = args[name]; + if (isFn(callback)) { + callbacks[name] = callback; + delete args[name]; + } + }; + + CALLBACKS.forEach(iterator); + + extras && extras.forEach(iterator); + + return [args, callbacks] +} + +function normalizeCallback (method, callbacks) { + return function weexCallback (ret) { + const type = ret.type; + delete ret.type; + const callback = callbacks[type]; + + if (type === SUCCESS) { + ret.errMsg = `${method}:ok`; + } else if (type === FAIL) { + ret.errMsg = method + ':fail' + (ret.msg ? (' ' + ret.msg) : ''); + } + + delete ret.code; + delete ret.msg; + + isFn(callback) && callback(ret); + + if (type === SUCCESS || type === FAIL) { + const complete = callbacks['complete']; + isFn(complete) && complete(ret); + } + } +} + +class LivePusherContext { + constructor (id, ctx) { + this.id = id; + this.ctx = ctx; + } + + start (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'start', cbs) + } + + stop (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'stop', cbs) + } + + pause (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'pause', cbs) + } + + resume (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'resume', cbs) + } + + switchCamera (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'switchCamera', cbs) + } + + snapshot (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'snapshot', cbs) + } + + toggleTorch (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'toggleTorch', cbs) + } + + playBGM (args) { + return invokeVmMethod(this.ctx, 'playBGM', args) + } + + stopBGM (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'stopBGM', cbs) + } + + pauseBGM (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'pauseBGM', cbs) + } + + resumeBGM (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'resumeBGM', cbs) + } + + setBGMVolume (cbs) { + return invokeVmMethod(this.ctx, 'setBGMVolume', cbs) + } + + startPreview (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'startPreview', cbs) + } + + stopPreview (args) { + return invokeVmMethodWithoutArgs(this.ctx, 'stopPreview', args) + } +} + +function createLivePusherContext (id, vm) { + if (!vm) { + return console.warn('uni.createLivePusherContext 必须传入第二个参数,即当前 vm 对象(this)') + } + const elm = findElmById(id, vm); + if (!elm) { + return console.warn('Can not find `' + id + '`') + } + return new LivePusherContext(id, elm) +} + +class MapContext { + constructor (id, ctx) { + this.id = id; + this.ctx = ctx; + } + + getCenterLocation (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'getCenterLocation', cbs) + } + + moveToLocation () { + return invokeVmMethodWithoutArgs(this.ctx, 'moveToLocation') + } + + translateMarker (args) { + return invokeVmMethod(this.ctx, 'translateMarker', args, ['animationEnd']) + } + + includePoints (args) { + return invokeVmMethod(this.ctx, 'includePoints', args) + } + + getRegion (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'getRegion', cbs) + } + + getScale (cbs) { + return invokeVmMethodWithoutArgs(this.ctx, 'getScale', cbs) + } +} + +function createMapContext$1 (id, vm) { + if (!vm) { + return console.warn('uni.createMapContext 必须传入第二个参数,即当前 vm 对象(this)') + } + const elm = findElmById(id, vm); + if (!elm) { + return console.warn('Can not find `' + id + '`') + } + return new MapContext(id, elm) +} + +class VideoContext { + constructor (id, ctx) { + this.id = id; + this.ctx = ctx; + } + + play () { + return invokeVmMethodWithoutArgs(this.ctx, 'play') + } + + pause () { + return invokeVmMethodWithoutArgs(this.ctx, 'pause') + } + + seek (args) { + return invokeVmMethod(this.ctx, 'seek', args) + } + + stop () { + return invokeVmMethodWithoutArgs(this.ctx, 'stop') + } + + sendDanmu (args) { + return invokeVmMethod(this.ctx, 'sendDanmu', args) + } + + playbackRate (args) { + return invokeVmMethod(this.ctx, 'playbackRate', args) + } + + requestFullScreen (args) { + return invokeVmMethod(this.ctx, 'requestFullScreen', args) + } + + exitFullScreen () { + return invokeVmMethodWithoutArgs(this.ctx, 'exitFullScreen') + } + + showStatusBar () { + return invokeVmMethodWithoutArgs(this.ctx, 'showStatusBar') + } + + hideStatusBar () { + return invokeVmMethodWithoutArgs(this.ctx, 'hideStatusBar') + } +} + +function createVideoContext$1 (id, vm) { + if (!vm) { + return console.warn('uni.createVideoContext 必须传入第二个参数,即当前 vm 对象(this)') + } + const elm = findElmById(id, vm); + if (!elm) { + return console.warn('Can not find `' + id + '`') + } + return new VideoContext(id, elm) +} + + + +var nvueApi = /*#__PURE__*/Object.freeze({ + createLivePusherContext: createLivePusherContext, + createMapContext: createMapContext$1, + createVideoContext: createVideoContext$1 +}); + +var platformApi = Object.assign({}, appApi, nvueApi); + +const api$1 = Object.create(null); + +Object.assign(api$1, api); +Object.assign(api$1, platformApi); const uni$1 = Object.create(null); apis.forEach(name => { - if (api$2[name]) { - uni$1[name] = promisify(name, wrapper(name, api$2[name])); + if (api$1[name]) { + uni$1[name] = promisify(name, wrapper(name, api$1[name])); } else { uni$1[name] = wrapperUnimplemented(name); } diff --git a/src/core/helpers/api.js b/src/core/helpers/api.js index 8790b2b800e9df366f5c9880df6a152899f15747..61865eb2826a99dc9e6241029cc61c7c5232279e 100644 --- a/src/core/helpers/api.js +++ b/src/core/helpers/api.js @@ -234,7 +234,7 @@ export function invokeCallbackHandler (invokeCallbackId, res) { } export function wrapperUnimplemented (name) { - return function (args) { + return function unimplemented (args) { console.error('API `' + name + '` is not yet implemented') } } diff --git a/src/platforms/app-plus-nvue/service/api.js b/src/platforms/app-plus-nvue/service/api.js index 9585c1c5348150587464ba6b089507971ac6fd48..897ecfd0420394c93ef2a339e529a7ed328d3107 100644 --- a/src/platforms/app-plus-nvue/service/api.js +++ b/src/platforms/app-plus-nvue/service/api.js @@ -1,4 +1,4 @@ import * as appApi from '../../app-plus/service/api/index' import * as nvueApi from './api/index' -export default Object.assign(appApi, nvueApi) +export default Object.assign({}, appApi, nvueApi) diff --git a/src/platforms/app-plus-nvue/service/api/context/live-pusher.js b/src/platforms/app-plus-nvue/service/api/context/live-pusher.js index 4d1e1d0e8178c1e82d5625685e4e5429354d2385..9771ef98dbc44a9c2e358060ba67ecf00864c811 100644 --- a/src/platforms/app-plus-nvue/service/api/context/live-pusher.js +++ b/src/platforms/app-plus-nvue/service/api/context/live-pusher.js @@ -69,11 +69,11 @@ class LivePusherContext { export function createLivePusherContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createLivePusherContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createLivePusherContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm) if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new LivePusherContext(id, elm) } diff --git a/src/platforms/app-plus-nvue/service/api/context/map.js b/src/platforms/app-plus-nvue/service/api/context/map.js index 0c3978943ae2dc55c3d49bc5854aa17a894c8040..7e13d1e60fddde7c191a0550fe09505dcaa3e5dd 100644 --- a/src/platforms/app-plus-nvue/service/api/context/map.js +++ b/src/platforms/app-plus-nvue/service/api/context/map.js @@ -37,11 +37,11 @@ class MapContext { export function createMapContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createMapContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createMapContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm) if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new MapContext(id, elm) } diff --git a/src/platforms/app-plus-nvue/service/api/context/video.js b/src/platforms/app-plus-nvue/service/api/context/video.js index 036ffbc816c8d449c653cd8c433a4ddb6987bf58..4e704bb2cd4ee634e9263283c9e98dba933abd45 100644 --- a/src/platforms/app-plus-nvue/service/api/context/video.js +++ b/src/platforms/app-plus-nvue/service/api/context/video.js @@ -53,11 +53,11 @@ class VideoContext { export function createVideoContext (id, vm) { if (!vm) { - return global.nativeLog('uni.createVideoContext 必须传入第二个参数,即当前 vm 对象(this)', '__WARN') + return console.warn('uni.createVideoContext 必须传入第二个参数,即当前 vm 对象(this)') } const elm = findElmById(id, vm) if (!elm) { - return global.nativeLog('Can not find `' + id + '`', '__WARN') + return console.warn('Can not find `' + id + '`') } return new VideoContext(id, elm) } 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 b14998b9669ed3bcdf220b3526782ec3983bbb47..26cb4b56faaf988b6f1461a19dd4b85635ec0767 100644 --- a/src/platforms/app-plus/service/api/device/scan-code.js +++ b/src/platforms/app-plus/service/api/device/scan-code.js @@ -15,8 +15,8 @@ import { registerPlusMessage } from '../../framework/plus-message' -export const SCAN_ID = '__UNIAPP_SCAN' -export const SCAN_PATH = '_www/__uniappscan.html' +const SCAN_ID = '__UNIAPP_SCAN' +const SCAN_PATH = '_www/__uniappscan.html' const MESSAGE_TYPE = 'scanCode'