Skip to content

Commit

Permalink
oschannel实现优化,支持业务代态动态选择
Browse files Browse the repository at this point in the history
  • Loading branch information
58liuyang committed Apr 28, 2024
1 parent 6c930de commit 0fd2e39
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 487 deletions.
57 changes: 27 additions & 30 deletions packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import osChannelApi from './osChannelApi'
// import { RequestTask } from './request'

// @ts-ignore
const syncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations

Check warning on line 2 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Unexpected mix of '&&' and '||'. Use parentheses to clarify the intended order of operations
// @ts-ignore
Expand Down Expand Up @@ -879,33 +876,33 @@ class AsyncToSyncProxy {
}
}

class HybridProxy {
// private readonly useAxios: boolean
private readonly useOsChannel: boolean
private readonly cacheProxy: any
// private readonly requestApi = 'request'

constructor (useOsChannel: boolean, nativeApi: NativeApi) {
// this.useAxios = useAxios
this.useOsChannel = useOsChannel
this.cacheProxy = new Proxy(nativeApi, new CacheStorageProxy(nativeApi))
}

get (_target: any, prop: string) {
return (...args: any) => {
// if (this.useAxios && prop === this.requestApi) {
// judgeUseAxios = this.useAxios
// // @ts-ignore
// return new RequestTask(...args)
// }
if (this.useOsChannel && osChannelApi.hasOwnProperty(prop)) {
return osChannelApi[prop](...args)
}
return this.cacheProxy[prop](...args)
}
}
}
// class HybridProxy {
// // private readonly useAxios: boolean
// private readonly useOsChannel: boolean
// private readonly cacheProxy: any
// // private readonly requestApi = 'request'
//
// constructor (useOsChannel: boolean, nativeApi: NativeApi) {
// // this.useAxios = useAxios
// this.useOsChannel = useOsChannel
// this.cacheProxy = new Proxy(nativeApi, new CacheStorageProxy(nativeApi))
// }
//
// get (_target: any, prop: string) {
// return (...args: any) => {
// // if (this.useAxios && prop === this.requestApi) {
// // judgeUseAxios = this.useAxios
// // // @ts-ignore
// // return new RequestTask(...args)
// // }
// if (this.useOsChannel && osChannelApi.hasOwnProperty(prop)) {
// return osChannelApi[prop](...args)
// }
// return this.cacheProxy[prop](...args)
// }
// }
// }

const nativeApi = new NativeApi()
const native = new Proxy(nativeApi, new HybridProxy(false, nativeApi)) // 第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
const native = new Proxy(nativeApi, new CacheStorageProxy(nativeApi)) // 第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
export default native
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Taro from '@tarojs/api'

import { getAppAuthorizeSetting as nativeGetAppAuthorizeSetting } from './native'
import { getAppAuthorizeSetting as osChannelGetAppAuthorizeSetting } from './oschannel'

export const getAppAuthorizeSetting: typeof Taro.getAppAuthorizeSetting = (useNativeImpl: boolean = false) => {
return useNativeImpl ? nativeGetAppAuthorizeSetting() : osChannelGetAppAuthorizeSetting()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Taro from '@tarojs/api'

import native from '../../NativeApi'

/**
* 获取APP授权设置
*
* @canUse getAppAuthorizeSetting
* @__return
* [albumAuthorized, bluetoothAuthorized, cameraAuthorized, locationAuthorized, locationReducedAccuracy,\
* microphoneAuthorized, notificationAuthorized, phoneCalendarAuthorized]
*/
export const getAppAuthorizeSetting: typeof Taro.getAppAuthorizeSetting = () => {
const info = native.getAppAuthorizeSetting()
// @ts-ignore
const appAuthorizeSetting: Taro.getAppAuthorizeSetting.Result = {
/** 允许微信使用相册的开关(仅 iOS 有效) */
albumAuthorized: info.albumAuthorized,
/** 允许微信使用蓝牙的开关(仅 iOS 有效) */
bluetoothAuthorized: info.bluetoothAuthorized,
/** 允许微信使用摄像头的开关 */
cameraAuthorized: info.cameraAuthorized,
/** 允许微信使用定位的开关 */
locationAuthorized: info.locationAuthorized,
/** 定位准确度。true 表示模糊定位,false 表示精确定位(仅 iOS 有效) */
locationReducedAccuracy: info.locationAccuracy === 'reduced',
/** 允许微信使用麦克风的开关 */
microphoneAuthorized: info.microphoneAuthorized,
/** 允许微信通知的开关 */
notificationAuthorized: info.notificationAuthorized,
/** 允许微信读写日历的开关 */
phoneCalendarAuthorized: info.phoneCalendarAuthorized,
}
return appAuthorizeSetting
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import Taro from '@tarojs/api'


let abilityAccessCtrl

try {
// @ts-ignore
abilityAccessCtrl = requireNapi('abilityAccessCtrl')
} catch (error) {
}

// @ts-ignore
export const getAppAuthorizeSetting: typeof Taro.getAppAuthorizeSetting = () => {
const permissionsList = {
album: 'ohos.permission.WRITE_IMAGEVIDEO',
bluetooth: 'ohos.permission.USE_BLUETOOTH',
camera: 'ohos.permission.CAMERA',
location: 'ohos.permission.LOCATION',
locationAccuracy: 'ohos.permission.APPROXIMATELY_LOCATION',
microphone: 'ohos.permission.MICROPHONE',
notification: 'ohos.permission.NOTIFICATION_CONTROLLER',
phoneCalendar: 'ohos.permission.READ_CALENDAR',
}
const atManager = abilityAccessCtrl.createAtManager()
// @ts-ignore
const tokenID = bundleInfoForSelf.appInfo.accessTokenId
const grantStatus = (flag) => {
if (flag === -1) {
return 'denied'
} else if (flag === 0) {
return 'authorized'
}
return 'config error'
}
let albumAuthorized = 'not determined'
try {
albumAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.album)
albumAuthorized = grantStatus(albumAuthorized)
} catch (e) {
}
let bluetoothAuthorized = 'not determined'
try {
bluetoothAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.bluetooth)
bluetoothAuthorized = grantStatus(bluetoothAuthorized)
} catch (e) {
}
let cameraAuthorized = 'not determined'
try {
cameraAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.camera)
cameraAuthorized = grantStatus(cameraAuthorized)
} catch (e) {
}
let locationAuthorized = 'not determined'
try {
locationAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.location)
locationAuthorized = grantStatus(locationAuthorized)
} catch (e) {
}
let locationAccuracy = 'not determined'
try {
locationAccuracy =
atManager.checkAccessTokenSync(tokenID, permissionsList.locationAccuracy) === 0 ? 'full' : 'reduced'
} catch (e) {
}
let microphoneAuthorized = 'not determined'
try {
microphoneAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.microphone)
microphoneAuthorized = grantStatus(microphoneAuthorized)
} catch (e) {
}
let notificationAuthorized = 'not determined'
try {
notificationAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.notification)
notificationAuthorized = grantStatus(notificationAuthorized)
} catch (e) {
}
let phoneCalendarAuthorized = 'not determined'
try {
phoneCalendarAuthorized = atManager.checkAccessTokenSync(tokenID, permissionsList.phoneCalendar)
phoneCalendarAuthorized = grantStatus(phoneCalendarAuthorized)
} catch (e) {
}
const result = {
albumAuthorized,
bluetoothAuthorized,
cameraAuthorized,
locationAuthorized,
locationAccuracy,
microphoneAuthorized,
notificationAuthorized,
phoneCalendarAuthorized,
}
return result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Taro from '@tarojs/api'

import { getSystemSetting as nativeGetSystemSetting } from './native'
import { getSystemSetting as osChannelGetSystemSetting } from './oschannel'

export const getSystemSetting: typeof Taro.getSystemSetting = (useNativeImpl: boolean = false) => {
return useNativeImpl ? nativeGetSystemSetting() : osChannelGetSystemSetting()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Taro from '@tarojs/api'

import native from '../../NativeApi'


const lastSystemSettingResult: Taro.getSystemSetting.Result = {}
let lastGetSystemSettingTime = 0

/**
* 获取设备设置
*
* @canUse getSystemSetting
* @__return [bluetoothEnabled, locationEnabled, wifiEnabled, deviceOrientation[portrait, landscape]]
*/
export const getSystemSetting: typeof Taro.getSystemSetting = () => {
const currentTime = Date.now()
if (currentTime - lastGetSystemSettingTime < 500) {
return lastSystemSettingResult
}
// @ts-ignore
const info = native.getSystemSetting()
lastSystemSettingResult.bluetoothEnabled = info.bluetoothEnabled
lastSystemSettingResult.locationEnabled = info.locationEnabled
lastSystemSettingResult.wifiEnabled = info.wifiEnabled
lastSystemSettingResult.deviceOrientation = info.deviceOrientation
lastGetSystemSettingTime = currentTime
return lastSystemSettingResult
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Taro from '@tarojs/api'


let bluetooth
let geoLocationManager
let wifiManager

try {
// @ts-ignore
bluetooth = requireNapi('bluetooth')

// @ts-ignore
geoLocationManager = requireNapi('geoLocationManager')

// @ts-ignore
wifiManager = requireNapi('wifi')
} catch (error) {}

// @ts-ignore
export const getSystemSetting: typeof Taro.getSystemSetting = () => {
let bluetoothEnabled: number | boolean = false
let locationEnabled = false
let wifiEnabled = false
let bluetoothError = ''
let locationError = ''
try {
bluetoothEnabled = bluetooth.getState()
bluetoothEnabled = bluetoothEnabled === 2 || bluetoothEnabled === 5
} catch (err) {
console.error('errCode:' + err.code + ',errMessage:' + err.message)
bluetoothError = err.message
}
try {
locationEnabled = geoLocationManager.isLocationEnabled()
} catch (err) {
console.error('errCode:' + err.code + ',errMessage:' + err.message)
locationError = err.message
}
try {
wifiEnabled = wifiManager.isWifiActive()
} catch (err) {
console.error('errCode:' + err.code + ',errMessage:' + err.message)
}
// @ts-ignore
const { rotation } = display.getDefaultDisplaySync()
const deviceOrientation = rotation === 1 || rotation === 3 ? 'landscape' : 'portrait'
return {
bluetoothEnabled,
bluetoothError,
locationEnabled,
locationError,
wifiEnabled,
deviceOrientation,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const env = {
*/

export * from './debug/index'
export * from './getAppAuthorizeSetting'
export * from './getSystemSetting'
export * from './system'
export * from './update'
export * from './weapp/life-cycle'
57 changes: 2 additions & 55 deletions packages/taro-platform-harmony-hybrid/src/api/apis/base/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Taro from '@tarojs/api'
import native from '../NativeApi'
import { shouldBeObject } from '../utils'
import { MethodHandler } from '../utils/handler'
import { getAppAuthorizeSetting } from './getAppAuthorizeSetting'
import { getSystemSetting } from './getSystemSetting'

/**
* 跳转系统蓝牙设置页
Expand Down Expand Up @@ -80,30 +82,6 @@ export const getWindowInfo: typeof Taro.getWindowInfo = () => {
return windowInfo
}

const lastSystemSettingResult: Taro.getSystemSetting.Result = {}
let lastGetSystemSettingTime = 0

/**
* 获取设备设置
*
* @canUse getSystemSetting
* @__return [bluetoothEnabled, locationEnabled, wifiEnabled, deviceOrientation[portrait, landscape]]
*/
export const getSystemSetting: typeof Taro.getSystemSetting = () => {
const currentTime = Date.now()
if (currentTime - lastGetSystemSettingTime < 500) {
return lastSystemSettingResult
}
// @ts-ignore
const info = native.getSystemSetting()
lastSystemSettingResult.bluetoothEnabled = info.bluetoothEnabled
lastSystemSettingResult.locationEnabled = info.locationEnabled
lastSystemSettingResult.wifiEnabled = info.wifiEnabled
lastSystemSettingResult.deviceOrientation = info.deviceOrientation
lastGetSystemSettingTime = currentTime
return lastSystemSettingResult
}

/**
* 获取设备基础信息
*
Expand Down Expand Up @@ -151,37 +129,6 @@ export const getAppBaseInfo: typeof Taro.getAppBaseInfo = () => {
return appBaseInfo
}

/**
* 获取APP授权设置
*
* @canUse getAppAuthorizeSetting
* @__return
* [albumAuthorized, bluetoothAuthorized, cameraAuthorized, locationAuthorized, locationReducedAccuracy,\
* microphoneAuthorized, notificationAuthorized, phoneCalendarAuthorized]
*/
export const getAppAuthorizeSetting: typeof Taro.getAppAuthorizeSetting = () => {
const info = native.getAppAuthorizeSetting()
// @ts-ignore
const appAuthorizeSetting: Taro.getAppAuthorizeSetting.Result = {
/** 允许微信使用相册的开关(仅 iOS 有效) */
albumAuthorized: info.albumAuthorized,
/** 允许微信使用蓝牙的开关(仅 iOS 有效) */
bluetoothAuthorized: info.bluetoothAuthorized,
/** 允许微信使用摄像头的开关 */
cameraAuthorized: info.cameraAuthorized,
/** 允许微信使用定位的开关 */
locationAuthorized: info.locationAuthorized,
/** 定位准确度。true 表示模糊定位,false 表示精确定位(仅 iOS 有效) */
locationReducedAccuracy: info.locationAccuracy === 'reduced',
/** 允许微信使用麦克风的开关 */
microphoneAuthorized: info.microphoneAuthorized,
/** 允许微信通知的开关 */
notificationAuthorized: info.notificationAuthorized,
/** 允许微信读写日历的开关 */
phoneCalendarAuthorized: info.phoneCalendarAuthorized,
}
return appAuthorizeSetting
}

/**
* 获取系统信息Taro.getSystemInfo的同步版本
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Taro from '@tarojs/taro'

import { hideKeyboard as nativeHideKeyboard } from './native'
import { hideKeyboard as osChannelHideKeyboard } from './oschannel'


export const hideKeyboard: typeof Taro.hideKeyboard = (options, useNativeImpl: boolean = false) => {
return useNativeImpl ? nativeHideKeyboard(options) : osChannelHideKeyboard(options)
}
Loading

0 comments on commit 0fd2e39

Please sign in to comment.