Skip to content

Commit

Permalink
Merge pull request #77 from xiaoyan428820/release-3.6.8-app
Browse files Browse the repository at this point in the history
fix: 修复网络相关接口实现及加速计接口桥接问题
  • Loading branch information
likailong180 authored Aug 7, 2023
2 parents 0077322 + 1a2ada3 commit 16091c6
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 93 deletions.
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

0 comments on commit 16091c6

Please sign in to comment.