提交 ba98c592 编写于 作者: D DCloud_LXH

feat(App-nvue): createLivePusherContext、createVideoContext、createMapContext

上级 51111f73
......@@ -34,3 +34,13 @@ export const CreateCanvasContextProtocol: ProtocolOptions<String | Object>[] = [
export const API_CREATE_INNER_AUDIO_CONTEXT = 'createInnerAudioContext'
export type API_TYPE_CREATEE_INNER_AUDIO_CONTEXT =
typeof uni.createInnerAudioContext
export const API_CREATE_LIVE_PUSHER_CONTEXT = 'createLivePusherContext'
export type API_TYPE_CREATE_LIVE_PUSHER_CONTEXT = (
id: string,
componentInstance: any
) => ReturnType<typeof uni.createLivePusherContext> | void
export const CreateLivePusherContextProtocol = validator.concat({
name: 'componentInstance',
type: Object,
})
import {
API_CREATE_LIVE_PUSHER_CONTEXT,
API_TYPE_CREATE_LIVE_PUSHER_CONTEXT,
CreateLivePusherContextProtocol,
defineSyncApi,
} from '@dcloudio/uni-api'
import {
findElmById,
invokeVmMethod,
invokeVmMethodWithoutArgs,
CallBacks as cb,
} from '../util'
type CallBacks = Partial<cb>
class LivePusherContext implements UniApp.LivePusherContext {
id: string
ctx: any
constructor(id: string, ctx: any) {
this.id = id
this.ctx = ctx
}
start(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'start', option)
}
stop(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'stop', option)
}
pause(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'pause', option)
}
resume(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'resume', option)
}
switchCamera(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'switchCamera', option)
}
snapshot(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'snapshot', option)
}
toggleTorch(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'toggleTorch', option)
}
playBGM(option?: CallBacks) {
return invokeVmMethod(this.ctx, 'playBGM', option)
}
stopBGM(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'stopBGM', option)
}
pauseBGM(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'pauseBGM', option)
}
resumeBGM(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'resumeBGM', option)
}
setBGMVolume(option?: CallBacks) {
return invokeVmMethod(this.ctx, 'setBGMVolume', option)
}
startPreview(option?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'startPreview', option)
}
stopPreview(args?: CallBacks) {
return invokeVmMethodWithoutArgs(this.ctx, 'stopPreview', args)
}
'setMICVolume': () => {}
}
export const createLivePusherContext =
defineSyncApi<API_TYPE_CREATE_LIVE_PUSHER_CONTEXT>(
API_CREATE_LIVE_PUSHER_CONTEXT,
(id, vm) => {
if (!vm) {
return console.warn(
'uni.createLivePusherContext: 2 arguments required, but only 1 present'
)
}
const elm = findElmById(id, vm)
if (!elm) {
return console.warn('Can not find `' + id + '`')
}
return new LivePusherContext(id, elm)
},
CreateLivePusherContextProtocol
)
import { ComponentPublicInstance } from 'vue'
import { findElmById, invokeVmMethod, invokeVmMethodWithoutArgs } from '../util'
type Methords = Record<string, (ctx: any, args: any) => void>
const METHODS: Methords = {
getCenterLocation(ctx, cbs) {
return invokeVmMethodWithoutArgs(ctx, 'getCenterLocation', cbs)
},
moveToLocation(ctx, args) {
return invokeVmMethod(ctx, 'moveToLocation', args)
},
translateMarker(ctx, args) {
return invokeVmMethod(ctx, 'translateMarker', args, ['animationEnd'])
},
includePoints(ctx, args) {
return invokeVmMethod(ctx, 'includePoints', args)
},
getRegion(ctx, cbs) {
return invokeVmMethodWithoutArgs(ctx, 'getRegion', cbs)
},
getScale(ctx, cbs) {
return invokeVmMethodWithoutArgs(ctx, 'getScale', cbs)
},
addCustomLayer(ctx, args) {
return invokeVmMethod(ctx, 'addCustomLayer', args)
},
removeCustomLayer(ctx, args) {
return invokeVmMethod(ctx, 'removeCustomLayer', args)
},
addGroundOverlay(ctx, args) {
return invokeVmMethod(ctx, 'addGroundOverlay', args)
},
removeGroundOverlay(ctx, args) {
return invokeVmMethod(ctx, 'removeGroundOverlay', args)
},
updateGroundOverlay(ctx, args) {
return invokeVmMethod(ctx, 'updateGroundOverlay', args)
},
initMarkerCluster(ctx, args) {
return invokeVmMethod(ctx, 'initMarkerCluster', args)
},
addMarkers(ctx, args) {
return invokeVmMethod(ctx, 'addMarkers', args)
},
removeMarkers(ctx, args) {
return invokeVmMethod(ctx, 'removeMarkers', args)
},
moveAlong(ctx, args) {
return invokeVmMethod(ctx, 'moveAlong', args)
},
openMapApp(ctx, args) {
return invokeVmMethod(ctx, 'openMapApp', args)
},
}
export function operateMap(
id: string,
pageId: number,
......@@ -5,6 +61,14 @@ export function operateMap(
data?: unknown,
operateMapCallback?: (res: any) => void
) {
const page = getCurrentPages().find((page) => page.$page.id === pageId)
if (page?.$page.meta.isNVue) {
const pageVm = (page as any).$vm as ComponentPublicInstance
return METHODS[type as keyof typeof METHODS](
findElmById(id, pageVm),
data as any
)
}
UniServiceJSBridge.invokeViewMethod(
'map.' + id,
{
......
import { ComponentPublicInstance } from 'vue'
import { findElmById, invokeVmMethod, invokeVmMethodWithoutArgs } from '../util'
const METHODS = {
play(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'play')
},
pause(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'pause')
},
seek(ctx: any, args: { position: number }) {
return invokeVmMethod(ctx, 'seek', args.position)
},
stop(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'stop')
},
sendDanmu(ctx: any, args: WechatMiniprogram.Danmu) {
return invokeVmMethod(ctx, 'sendDanmu', args)
},
playbackRate(ctx: any, args: { rate: number }) {
return invokeVmMethod(ctx, 'playbackRate', args.rate)
},
requestFullScreen(
ctx: any,
args: WechatMiniprogram.VideoContextRequestFullScreenOption = {}
) {
return invokeVmMethod(ctx, 'requestFullScreen', args)
},
exitFullScreen(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'exitFullScreen')
},
showStatusBar(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'showStatusBar')
},
hideStatusBar(ctx: any) {
return invokeVmMethodWithoutArgs(ctx, 'hideStatusBar')
},
}
export function operateVideoPlayer(
videoId: string,
pageId: number,
type: string,
data?: unknown
) {
const page = getCurrentPages().find((page) => page.$page.id === pageId)
if (page?.$page.meta.isNVue) {
const pageVm = (page as any).$vm as ComponentPublicInstance
return METHODS[type as keyof typeof METHODS](
findElmById(videoId, pageVm),
data as any
)
}
UniServiceJSBridge.invokeViewMethod(
'video.' + videoId,
{
......
......@@ -38,6 +38,7 @@ export * from './network/uploadFile'
export * from './context/createInnerAudioContext'
export * from './context/getBackgroundAudioManager'
export * from './context/createLivePusherContext'
export * from './location/getLocation'
export * from './location/chooseLocation'
......
import { isFunction } from '@vue/shared'
import { ComponentPublicInstance } from 'vue'
const SUCCESS = 'success'
const FAIL = 'fail'
const COMPLETE = 'complete'
const CALLBACKS: CallBackName[] = [SUCCESS, FAIL, COMPLETE]
type CallBackName = typeof SUCCESS | typeof FAIL | typeof COMPLETE
export type CallBacks = Record<CallBackName, Function>
/**
* 调用无参数,或仅一个参数且为 callback 的 API
* @param {Object} vm
* @param {Object} method
* @param {Object} args
* @param {Object} extras
*/
export function invokeVmMethodWithoutArgs(
vm: any,
method: string,
args?: any,
extras?: [any]
) {
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
*/
export function invokeVmMethod(
vm: any,
method: string,
args: any,
extras?: [any]
) {
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))
}
export function findElmById(id: string, vm: ComponentPublicInstance) {
const elm = findRefByElm(id, vm.$el)
if (!elm) {
return console.error('Can not find `' + id + '`')
}
return elm
}
function findRefByElm(id: string, elm: any): any {
if (!id || !elm) {
return
}
if (elm.attr && elm.attr.id === id) {
return elm
}
const children = elm.children
if (!children) {
return
}
for (let i = 0, len = children.length; i < len; i++) {
const elm = findRefByElm(id, children[i])
if (elm) {
return elm
}
}
}
function normalizeArgs(args: any = {}, extras?: [any]) {
const callbacks = Object.create(null)
const iterator = function iterator(name: string) {
const callback = args[name]
if (isFunction(callback)) {
callbacks[name] = callback
delete args[name]
}
}
CALLBACKS.forEach(iterator)
extras && extras.forEach(iterator)
return [args, callbacks]
}
function normalizeCallback(method: string, callbacks: CallBacks) {
return function weexCallback(ret: any) {
const type = ret.type as CallBackName
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
isFunction(callback) && callback(ret)
if (type === SUCCESS || type === FAIL) {
const complete = callbacks.complete
isFunction(complete) && complete(ret)
}
}
}
......@@ -233,6 +233,9 @@ export default /*#__PURE__*/ defineBuiltInComponent({
case 'playbackRate':
options = data.rate
break
case 'requestFullScreen':
options = data.direction
break
}
if (video) {
video[type as Method](options)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册