提交 ec066665 编写于 作者: fxy060608's avatar fxy060608

refactor accelerometer

上级 e0039c51
...@@ -159,6 +159,15 @@ function createApiCallback (apiName, params = {}, extras = {}) { ...@@ -159,6 +159,15 @@ function createApiCallback (apiName, params = {}, extras = {}) {
const invokeCallback = function (res) { const invokeCallback = function (res) {
res.errMsg = res.errMsg || apiName + ':ok' res.errMsg = res.errMsg || apiName + ':ok'
// 部分 api 可能返回的 errMsg 的 api 名称部分不一致,格式化为正确的
if (res.errMsg.indexOf(':ok') !== -1) {
res.errMsg = apiName + ':ok'
} else if (res.errMsg.indexOf(':cancel') !== -1) {
res.errMsg = apiName + ':cancel'
} else if (res.errMsg.indexOf(':fail') !== -1) {
res.errMsg = apiName + ':fail'
}
const errMsg = res.errMsg const errMsg = res.errMsg
if (errMsg.indexOf(apiName + ':ok') === 0) { if (errMsg.indexOf(apiName + ':ok') === 0) {
......
...@@ -207,4 +207,4 @@ const apis = [ ...@@ -207,4 +207,4 @@ const apis = [
...third ...third
] ]
export default apis module.exports = apis
export const upx2px = [{ export const upx2px = [{
name: 'upx', name: 'upx',
type: Number, type: [Number, String],
required: true required: true
}] }]
import {
invoke
} from 'uni-core/service/bridge'
import {
onMethod,
invokeMethod
} from '../../platform'
const callbacks = []
onMethod('onAccelerometerChange', function (res) {
callbacks.forEach(callbackId => {
invoke(callbackId, res)
})
})
let isEnable = false
/**
* 监听加速度
* @param {*} callbackId
*/
export function onAccelerometerChange (callbackId) {
// TODO 当没有 start 时,添加 on 需要主动 start?
callbacks.push(callbackId)
if (!isEnable) {
startAccelerometer()
}
}
export function startAccelerometer ({
interval // TODO
} = {}) {
if (isEnable) {
return
}
isEnable = true
return invokeMethod('enableAccelerometer', {
enable: true
})
}
export function stopAccelerometer () {
isEnable = false
return invokeMethod('enableAccelerometer', {
enable: false
})
}
const callbacks = [] import {
var listener publish
/** } from '../../bridge'
* 监听加速度
* @param {*} callbackId let listener
*/
export function onAccelerometerChange (callbackId) { export function enableAccelerometer ({
callbacks.push(callbackId) enable
if (!listener) { }) {
startAccelerometer() return enable ? startAccelerometer() : stopAccelerometer()
}
} }
/** /**
* 开始监听加速度数据 * 开始监听加速度数据
*/ */
export function startAccelerometer () { function startAccelerometer () {
const {
invokeCallbackHandler: invoke
} = UniServiceJSBridge
if (window.DeviceMotionEvent) { if (window.DeviceMotionEvent) {
listener = function (event) { listener = function (event) {
callbacks.forEach(callbackId => {
const acceleration = event.acceleration || event.accelerationIncludingGravity const acceleration = event.acceleration || event.accelerationIncludingGravity
invoke(callbackId, { publish('onAccelerometerChange', {
x: acceleration.x || 0, x: acceleration.x || 0,
y: acceleration.y || 0, y: acceleration.y || 0,
z: acceleration.z || 0, z: acceleration.z || 0,
errMsg: 'onAccelerometerChange:ok' errMsg: 'onAccelerometerChange:ok'
}) })
})
} }
window.addEventListener('devicemotion', listener, false) window.addEventListener('devicemotion', listener, false)
return {} return {}
...@@ -38,7 +33,7 @@ export function startAccelerometer () { ...@@ -38,7 +33,7 @@ export function startAccelerometer () {
/** /**
* 停止监听加速度数据 * 停止监听加速度数据
*/ */
export function stopAccelerometer () { function stopAccelerometer () {
if (listener) { if (listener) {
window.removeEventListener('devicemotion', listener, false) window.removeEventListener('devicemotion', listener, false)
listener = null listener = null
......
/**
* 触发 service 层,与 onMethod 对应
*/
export function publish (name, res) {
return UniServiceJSBridge.emit('api.' + name, res)
}
export function publishHandler (event, args, pageId) { export function publishHandler (event, args, pageId) {
// h5 平台直接调用UniViewJSBridge // h5 平台直接调用UniViewJSBridge
global.UniViewJSBridge.subscribeHandler(event, args, pageId) global.UniViewJSBridge.subscribeHandler(event, args, pageId)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册