Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复网络相关接口实现及加速计接口桥接问题 #77

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import ButtonList from '@/components/buttonList'
import { TestConsole } from '@/util/util'
import './index.scss'

/**
Expand All @@ -14,27 +14,54 @@ export default class Index extends React.Component {
list: [
{
id: 'stopAccelerometer',
func: null,
func: () => {
TestConsole.consoleTest('stopAccelerometer')
Taro.stopAccelerometer().then((res) => {
TestConsole.consoleReturn(res)
})
},
},
{
id: 'startAccelerometer',
func: null,
func: () => {
TestConsole.consoleTest('startAccelerometer')
Taro.startAccelerometer().then((res) => {
TestConsole.consoleReturn(res)
})
},
},
{
id: 'onAccelerometerChange',
func: null,
func: () => {
TestConsole.consoleTest('onAccelerometerChange')
Taro.onAccelerometerChange(this.callback)
},
},
{
id: 'offAccelerometerChange',
func: null,
func: () => {
TestConsole.consoleTest('offAccelerometerChange')
Taro.offAccelerometerChange()
},
},
],
}

callback = (res: any) => {
TestConsole.consoleNormal('AccelerometerChangeCallback', res)
}

render() {
const { list } = this.state
return (
<View className='api-page'>
<ButtonList buttonList={list} />
{this.state.list.map((item) => {
return (
<View key={item.id} className='api-page-btn' onClick={item.func == null ? () => {} : item.func}>
{item.id}
{item.func == null && <Text className='navigator-state tag'>未创建Demo</Text>}
</View>
)
})}
</View>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import ButtonList from '@/components/buttonList'
import { TestConsole } from '@/util/util'
import './index.scss'

/**
Expand All @@ -18,31 +18,81 @@ export default class Index extends React.Component {
},
{
id: 'onNetworkStatusChange',
func: null,
func: () => {
TestConsole.consoleTest('onNetworkStatusChange')
Taro.onNetworkStatusChange(this.callback)
},
},
{
id: 'offNetworkWeakChange',
func: null,
},
{
id: 'offNetworkStatusChange',
func: null,
func: () => {
TestConsole.consoleTest('offNetworkStatusChange')
Taro.offNetworkStatusChange(this.callback)
},
},
{
id: 'getNetworkType',
func: null,
func: () => {
TestConsole.consoleTest('getNetworkType')
Taro.getNetworkType({
success: (res) => {
TestConsole.consoleSuccess(res)
this.setState({
networkType: res.networkType
})
},
fail: (res) => {
TestConsole.consoleFail(res)
this.setState({
networkType: '获取失败'
})
},
complete: (res) => {
TestConsole.consoleComplete(res)
},
}).then((res) => {
TestConsole.consoleReturn(res)
})
},
},
{
id: 'getLocalIPAddress',
func: null,
},
],
networkType: '未获取',
networkState: null
}

callback = (res: any) => {
TestConsole.consoleSuccess(res)
this.setState({
networkState: res.isConnected,
networkType: res.networkType
})
}

render() {
const { list } = this.state
let { list, networkType, networkState } = this.state
return (
<View className='api-page'>
<ButtonList buttonList={list} />
<View style={{display:'inline-block'}}>
<View>网络类型:{networkType}</View>
<View hidden={networkState == null ? false : true}>网络状态:未获取</View>
<View hidden={networkState == null ? true : false}>网络状态:{networkState ? '已连接' : '已断开'}</View>
</View>
{list.map((item) => {
return (
<View key={item.id} className='api-page-btn' onClick={item.func == null ? () => {} : item.func}>
{item.id}
{item.func == null && <Text className='navigator-state tag'>未创建Demo</Text>}
</View>
)
})}
</View>
)
}
Expand Down
79 changes: 0 additions & 79 deletions packages/taro-mpharmony/src/api/device/accelerometer.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/taro-mpharmony/src/api/device/accelerometer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './offAccelerometerChange'
export * from './onAccelerometerChange'
export * from './startAccelerometer'
export * from './stopAccelerometer'
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Taro from '@tarojs/taro'
import { shouldBeFunction } from 'src/utils'

/**
* 取消监听加速度数据事件,参数为空,则取消所有的事件监听
*/
export const offAccelerometerChange: typeof Taro.offAccelerometerChange = (callback) => {
const name = 'offAccelerometerChange'

// callback must be an Function or undefined
const isFunction = shouldBeFunction(callback)
if (!isFunction.flag && typeof callback !== 'undefined') {
const res = { errMsg: `${name}:fail ${isFunction.msg}` }
console.error(res.errMsg)
return
}

// @ts-ignore
native.offAccelerometerChange(callback)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Taro from '@tarojs/taro'
import { shouldBeFunction } from 'src/utils'

/**
* 监听加速度数据事件。频率根据 Taro.startAccelerometer() 的 interval 参数。可使用 Taro.stopAccelerometer() 停止监听。
*/
export const onAccelerometerChange: typeof Taro.onAccelerometerChange = (callback) => {
const name = 'onAccelerometerChange'

// callback must be an Function
const isFunction = shouldBeFunction(callback)
if (!isFunction.flag) {
const res = { errMsg: `${name}:fail ${isFunction.msg}` }
console.error(res.errMsg)
return
}

// @ts-ignore
native.onAccelerometerChange(callback)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Taro from '@tarojs/taro'
import { shouldBeObject } from 'src/utils'
import { MethodHandler } from 'src/utils/handler'

/**
* 开始监听加速度数据。
*/
export const startAccelerometer: typeof Taro.startAccelerometer = (options) => {
const name = 'startAccelerometer'

return new Promise((resolve, reject) => {
if (typeof options === 'undefined') {
// @ts-ignore
native.startAccelerometer({
interval: 'normal',
success: (res: any) => {
resolve(res)
},
fail: (res: any) => {
resolve(res)
}
})
return
}

// options must be an Object
const isObject = shouldBeObject(options)
if (!isObject.flag) {
const res = { errMsg: `${name}:fail ${isObject.msg}` }
console.error(res.errMsg)
return Promise.reject(res)
}
const {
interval = 'normal',
success,
fail,
complete
} = options as Exclude<typeof options, undefined>

const handle = new MethodHandler<{
errMsg?: string
}>({ name, success, fail, complete })

// @ts-ignore
native.startAccelerometer({
interval: interval,
success: (res: any) => {
handle.success(res, { resolve, reject })
},
fail: (res: any) => {
handle.fail(res, { resolve, reject })
}
})
})
}
Loading