Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
6ced4d4d
U
uni-app
项目概览
DCloud
/
uni-app
4 个月 前同步成功
通知
730
Star
38706
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6ced4d4d
编写于
10月 28, 2020
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: uni.offAccelerometerChange
上级
deca0508
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
133 addition
and
131 deletion
+133
-131
lib/apis.js
lib/apis.js
+8
-7
src/core/helpers/api.js
src/core/helpers/api.js
+14
-7
src/core/service/api/device/accelerometer.js
src/core/service/api/device/accelerometer.js
+0
-48
src/core/service/api/ui/window.js
src/core/service/api/ui/window.js
+2
-4
src/platforms/app-plus/service/api/device/accelerometer.js
src/platforms/app-plus/service/api/device/accelerometer.js
+46
-38
src/platforms/h5/service/api/device/accelerometer.js
src/platforms/h5/service/api/device/accelerometer.js
+63
-27
未找到文件。
lib/apis.js
浏览文件 @
6ced4d4d
...
...
@@ -73,6 +73,7 @@ const device = [
'
getNetworkType
'
,
'
onNetworkStatusChange
'
,
'
onAccelerometerChange
'
,
'
offAccelerometerChange
'
,
'
startAccelerometer
'
,
'
stopAccelerometer
'
,
'
onCompassChange
'
,
...
...
@@ -162,12 +163,12 @@ const ui = [
'
createSelectorQuery
'
,
'
createIntersectionObserver
'
,
'
createMediaQueryObserver
'
,
'
getMenuButtonBoundingClientRect
'
,
'
showTopWindow
'
,
'
showLeftWindow
'
,
'
showRightWindow
'
,
'
hideTopWindow
'
,
'
hideLeftWindow
'
,
'
getMenuButtonBoundingClientRect
'
,
'
showTopWindow
'
,
'
showLeftWindow
'
,
'
showRightWindow
'
,
'
hideTopWindow
'
,
'
hideLeftWindow
'
,
'
hideRightWindow
'
,
]
...
...
@@ -224,7 +225,7 @@ const third = [
]
const
ad
=
[
'
createRewardedVideoAd
'
,
'
createRewardedVideoAd
'
,
'
createFullScreenVideoAd
'
]
...
...
src/core/helpers/api.js
浏览文件 @
6ced4d4d
...
...
@@ -106,18 +106,25 @@ function createKeepAliveApiCallback (apiName, callback) {
const
callbackId
=
invokeCallbackId
++
const
invokeCallbackName
=
'
api.
'
+
apiName
+
'
.
'
+
callbackId
const
invokeCallback
=
function
(
res
,
extras
)
{
callback
(
res
,
extras
)
}
invokeCallbacks
[
callbackId
]
=
{
name
:
invokeCallbackName
,
keepAlive
:
true
,
callback
:
invokeCallback
callback
}
return
callbackId
}
function
getKeepAliveApiCallback
(
apiName
,
callback
)
{
for
(
const
key
in
invokeCallbacks
)
{
const
item
=
invokeCallbacks
[
key
]
if
(
item
.
name
.
startsWith
(
'
api.
'
+
apiName
.
replace
(
/^off/
,
'
on
'
))
&&
item
.
callback
===
callback
)
{
delete
invokeCallbacks
[
key
]
return
Number
(
key
)
}
}
return
'
fail
'
}
function
createApiCallback
(
apiName
,
params
=
{},
extras
=
{})
{
if
(
!
isPlainObject
(
params
))
{
return
{
...
...
@@ -294,7 +301,7 @@ export function wrapper (name, invokeMethod, extras = {}) {
}
}
else
if
(
isCallbackApi
(
name
))
{
if
(
validateParams
(
name
,
args
,
-
1
))
{
return
invokeMethod
(
createKeepAliveApiCallback
(
name
,
args
[
0
]))
return
invokeMethod
(
(
name
.
startsWith
(
'
off
'
)
?
getKeepAliveApiCallback
:
createKeepAliveApiCallback
)
(
name
,
args
[
0
]))
}
}
else
{
let
argsObj
=
{}
...
...
@@ -322,4 +329,4 @@ export function wrapper (name, invokeMethod, extras = {}) {
}
}
}
}
}
src/core/service/api/device/accelerometer.js
已删除
100644 → 0
浏览文件 @
deca0508
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
})
}
src/core/service/api/ui/window.js
浏览文件 @
6ced4d4d
...
...
@@ -17,9 +17,7 @@ export function onWindowResize (callbackId) {
callbacks
.
push
(
callbackId
)
}
export
function
offWindowResize
(
callbackId
)
{
// TODO 目前 on 和 off 即使传入同一个 function,获取到的 callbackId 也不会一致,导致不能 off 掉指定
// 后续修复
export
function
offWindowResize
(
callbackId
)
{
// 此处和微信平台一致查询不到去掉最后一个
callbacks
.
splice
(
callbacks
.
indexOf
(
callbackId
),
1
)
}
}
src/platforms/app-plus/service/api/device/accelerometer.js
浏览文件 @
6ced4d4d
...
...
@@ -3,53 +3,61 @@ import {
}
from
'
../constants
'
import
{
getLastWebview
}
from
'
../util
'
import
{
publish
invoke
}
from
'
../../bridge
'
let
watchAccelerationId
=
false
let
isWatchAcceleration
=
false
let
listener
const
clearWatchAcceleration
=
()
=>
{
if
(
watchAccelerationId
)
{
plus
.
accelerometer
.
clearWatch
(
watchAccelerationId
)
watchAccelerationId
=
false
}
}
const
callbackIds
=
[]
export
function
enableAccelerometer
({
enable
})
{
if
(
enable
)
{
// 启用监听
clearWatchAcceleration
()
watchAccelerationId
=
plus
.
accelerometer
.
watchAcceleration
((
res
)
=>
{
publish
(
'
onAccelerometerChange
'
,
{
export
function
startAccelerometer
(
options
,
callbackId
)
{
listener
=
listener
||
plus
.
accelerometer
.
watchAcceleration
((
res
)
=>
{
callbackIds
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
{
x
:
res
.
xAxis
,
y
:
res
.
yAxis
,
z
:
res
.
zAxis
,
errMsg
:
'
enableAccelerometer:ok
'
z
:
res
.
zAxis
})
},
(
e
)
=>
{
publish
(
'
onAccelerometerChange
'
,
{
errMsg
:
'
enableAccelerometer:fail
'
})
},
{
frequency
:
DEVICE_FREQUENCY
})
if
(
!
isWatchAcceleration
)
{
isWatchAcceleration
=
true
const
webview
=
getLastWebview
()
if
(
webview
)
{
webview
.
addEventListener
(
'
close
'
,
clearWatchAcceleration
)
}
},
err
=>
{
listener
=
null
invoke
(
callbackId
,
{
errMsg
:
`startAccelerometer:fail
${
err
.
message
}
`
})
},
{
frequency
:
DEVICE_FREQUENCY
})
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
startAccelerometer:ok
'
})
},
DEVICE_FREQUENCY
)
}
export
function
stopAccelerometer
()
{
if
(
listener
)
{
plus
.
accelerometer
.
clearWatch
(
listener
)
listener
=
null
}
return
{}
}
export
function
onAccelerometerChange
(
callbackId
)
{
if
(
!
callbackIds
.
length
)
{
startAccelerometer
()
}
callbackIds
.
push
(
callbackId
)
}
export
function
offAccelerometerChange
(
callbackId
)
{
// 暂不支持移除所有监听
if
(
callbackId
)
{
const
index
=
callbackIds
.
indexOf
(
callbackId
)
if
(
index
>=
0
)
{
callbackIds
.
splice
(
index
,
1
)
}
}
else
{
clearWatchAcceleration
()
}
return
{
errMsg
:
'
enableAccelerometer:ok
'
if
(
!
callbackIds
.
length
)
{
stopAccelerometer
()
}
}
src/platforms/h5/service/api/device/accelerometer.js
浏览文件 @
6ced4d4d
import
{
publish
}
from
'
../../bridge
'
let
listener
export
function
enableAccelerometer
({
enable
})
{
return
enable
?
startAccelerometer
()
:
stopAccelerometer
()
}
const
callbackIds
=
[]
/**
* 开始监听加速度数据
*/
function
startAccelerometer
()
{
if
(
window
.
DeviceMotionEvent
)
{
export
function
startAccelerometer
(
options
,
callbackId
)
{
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
if
(
!
window
.
DeviceMotionEvent
)
{
return
{
errMsg
:
'
startAccelerometer:fail
'
}
}
function
addEventListener
()
{
listener
=
function
(
event
)
{
const
acceleration
=
event
.
acceleration
||
event
.
accelerationIncludingGravity
publish
(
'
onAccelerometerChange
'
,
{
x
:
acceleration
.
x
||
0
,
y
:
acceleration
.
y
||
0
,
z
:
acceleration
.
z
||
0
,
errMsg
:
'
onAccelerometerChange:ok
'
callbackIds
.
forEach
(
callbackId
=>
{
invoke
(
callbackId
,
{
x
:
acceleration
.
x
||
0
,
y
:
acceleration
.
y
||
0
,
z
:
acceleration
.
z
||
0
})
})
}
window
.
addEventListener
(
'
devicemotion
'
,
listener
,
false
)
return
{}
}
else
{
throw
new
Error
(
'
device nonsupport devicemotion
'
)
}
if
(
!
listener
)
{
if
(
DeviceMotionEvent
.
requestPermission
)
{
DeviceMotionEvent
.
requestPermission
().
then
((
res
)
=>
{
if
(
res
===
'
granted
'
)
{
addEventListener
()
invoke
(
callbackId
,
{
errMsg
:
'
startAccelerometer:ok
'
})
}
else
{
invoke
(
callbackId
,
{
errMsg
:
`startAccelerometer:fail
${
res
}
`
})
}
}).
catch
(
error
=>
{
invoke
(
callbackId
,
{
errMsg
:
`startAccelerometer:fail
${
error
}
`
})
})
return
}
addEventListener
()
}
return
{}
}
/**
* 停止监听加速度数据
*/
function
stopAccelerometer
()
{
export
function
stopAccelerometer
()
{
if
(
listener
)
{
window
.
removeEventListener
(
'
devicemotion
'
,
listener
,
false
)
listener
=
null
}
return
{}
}
}
export
function
onAccelerometerChange
(
callbackId
)
{
if
(
!
callbackIds
.
length
)
{
startAccelerometer
()
}
callbackIds
.
push
(
callbackId
)
}
export
function
offAccelerometerChange
(
callbackId
)
{
// 暂不支持移除所有监听
if
(
callbackId
)
{
const
index
=
callbackIds
.
indexOf
(
callbackId
)
if
(
index
>=
0
)
{
callbackIds
.
splice
(
index
,
1
)
}
}
if
(
!
callbackIds
.
length
)
{
stopAccelerometer
()
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录