From 0fd2e39a44e96e3f037f071cb59cf68495bfbef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=98=B3?= Date: Sun, 28 Apr 2024 19:52:34 +0800 Subject: [PATCH] =?UTF-8?q?oschannel=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E4=B8=9A=E5=8A=A1=E4=BB=A3=E6=80=81?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/api/apis/NativeApi.ts | 57 ++- .../apis/base/getAppAuthorizeSetting/index.ts | 8 + .../base/getAppAuthorizeSetting/native.ts | 35 ++ .../base/getAppAuthorizeSetting/oschannel.ts | 94 +++++ .../api/apis/base/getSystemSetting/index.ts | 8 + .../api/apis/base/getSystemSetting/native.ts | 28 ++ .../apis/base/getSystemSetting/oschannel.ts | 55 +++ .../src/api/apis/base/index.ts | 2 + .../src/api/apis/base/system.ts | 57 +-- .../device/keyBoard/hideKeyboard/index.ts | 9 + .../native.ts} | 6 +- .../device/keyBoard/hideKeyboard/oschannel.ts | 28 ++ .../src/api/apis/device/phone/index.ts | 9 +- .../apis/device/phone/{phone.ts => native.ts} | 0 .../src/api/apis/device/phone/oschannel.ts | 33 ++ .../src/api/apis/network/request/index.ts | 2 +- .../src/api/apis/osChannelApi.ts | 397 ------------------ 17 files changed, 341 insertions(+), 487 deletions(-) create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/index.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/native.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/oschannel.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/index.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/native.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/oschannel.ts create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/index.ts rename packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/{hideKeyboard.ts => hideKeyboard/native.ts} (87%) create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/oschannel.ts rename packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/{phone.ts => native.ts} (100%) create mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/oschannel.ts delete mode 100644 packages/taro-platform-harmony-hybrid/src/api/apis/osChannelApi.ts diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts index 328acf435fdb..ae40b8be8ca6 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts @@ -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) // @ts-ignore @@ -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 diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/index.ts new file mode 100644 index 000000000000..c9f1e258ab6f --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/index.ts @@ -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() +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/native.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/native.ts new file mode 100644 index 000000000000..830424abb04a --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/native.ts @@ -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 +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/oschannel.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/oschannel.ts new file mode 100644 index 000000000000..19cda2740e0b --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getAppAuthorizeSetting/oschannel.ts @@ -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 +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/index.ts new file mode 100644 index 000000000000..56f0bd35e1ba --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/index.ts @@ -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() +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/native.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/native.ts new file mode 100644 index 000000000000..8e442b9d648b --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/native.ts @@ -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 +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/oschannel.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/oschannel.ts new file mode 100644 index 000000000000..cbdfd5977672 --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/getSystemSetting/oschannel.ts @@ -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, + } +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/index.ts index e9fe0082dd1c..88219be8a83f 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/base/index.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/index.ts @@ -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' diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/base/system.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/base/system.ts index 71d042f173ed..cde36e4cf3b8 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/base/system.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/base/system.ts @@ -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' /** * 跳转系统蓝牙设置页 @@ -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 -} - /** * 获取设备基础信息 * @@ -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的同步版本 diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/index.ts new file mode 100644 index 000000000000..b7c4bdb62eef --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/index.ts @@ -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) +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/native.ts similarity index 87% rename from packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard.ts rename to packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/native.ts index efc5f26b5a5f..7e49d3956e6f 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/native.ts @@ -1,8 +1,8 @@ import Taro from '@tarojs/taro' -import native from '../../NativeApi' -import { shouldBeObject } from '../../utils' -import { MethodHandler } from '../../utils/handler' +import native from '../../../NativeApi' +import { shouldBeObject } from '../../../utils' +import { MethodHandler } from '../../../utils/handler' /** * 在input、textarea等focus拉起键盘之后,手动调用此接口收起键盘 diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/oschannel.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/oschannel.ts new file mode 100644 index 000000000000..08277736e4e9 --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/device/keyBoard/hideKeyboard/oschannel.ts @@ -0,0 +1,28 @@ +import Taro from '@tarojs/taro' + +let inputMethod + +try { + // @ts-ignore + inputMethod = requireNapi('inputMethod') +} catch (error) {} + +export const hideKeyboard:typeof Taro.hideKeyboard = () => { + return new Promise((resolve, reject) => { + try { + const inputMethodController = inputMethod.getController() + inputMethodController + .hideTextInput() + .then(() => { + resolve({ errMsg: 'ok' }) + }) + .catch((err: any) => { + console.error('[OsChannel] Failed to hideSoftKeyboard: ' + JSON.stringify(err)) + reject(err) + }) + } catch (exception) { + console.error('[OsChannel] Failed to get inputMethod Controller. Cause: ' + JSON.stringify(exception)) + reject(exception) + } + }) +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/index.ts index c6457786ea96..5aafae102e13 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/index.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/index.ts @@ -1 +1,8 @@ -export * from './phone' +import Taro from '@tarojs/api' + +import { makePhoneCall as nativeMakePhoneCall } from './native' +import { makePhoneCall as osChannelMakePhoneCall } from './oschannel' + +export const makePhoneCall: typeof Taro.makePhoneCall = (options, useNativeImpl = false) => { + return useNativeImpl ? nativeMakePhoneCall(options) : osChannelMakePhoneCall(options) +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/phone.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/native.ts similarity index 100% rename from packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/phone.ts rename to packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/native.ts diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/oschannel.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/oschannel.ts new file mode 100644 index 000000000000..9173963f44cd --- /dev/null +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/device/phone/oschannel.ts @@ -0,0 +1,33 @@ +import Taro from '@tarojs/api' + +let call + +try { + // @ts-ignore + call = requireNapi('telephony.call') +} catch (error) {} + +const ErrorCode = { + PARAMETER_ERROR: 202, +} + +export const makePhoneCall: typeof Taro.makePhoneCall = (options) => { + const { phoneNumber, success, fail } = options + if (!phoneNumber) { + return new Promise((_resolve, reject) => { + // eslint-disable-next-line prefer-promise-reject-errors + reject(['param is invalid.', ErrorCode.PARAMETER_ERROR]) + }) + } + return call.makeCall(phoneNumber).then( + () => { + // @ts-ignore + success({ errMsg: 'ok' }) + }, + (err: any) => { + console.error(`[OsChannel] start makePhoneCall fail`) + // @ts-ignore + fail({ errMsg: JSON.stringify(err) }) + } + ) +} diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts index 0b7154c990fe..3507247cfb54 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts @@ -8,7 +8,7 @@ import { request as nativeReuqest } from './nativeRequest' * @param options 请求选项 * @param useNativeRequest 默认使用true */ -export function request (options: any, useNativeRequest: boolean = false) { +export function request (options: any, useNativeRequest: boolean = true) { return useNativeRequest ? nativeReuqest(options) : h5Request(options) } diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/osChannelApi.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/osChannelApi.ts deleted file mode 100644 index caa6eea322c0..000000000000 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/osChannelApi.ts +++ /dev/null @@ -1,397 +0,0 @@ -// let http -let display -let wifiManager -let call -let bluetooth -let inputMethod -let bundleManager -let abilityAccessCtrl -let geoLocationManager - -try { - // @ts-ignore - // http = requireNapi('net.http') - // @ts-ignore - display = requireNapi('display') - // @ts-ignore - wifiManager = requireNapi('wifi') - // @ts-ignore - call = requireNapi('telephony.call') - // @ts-ignore - bluetooth = requireNapi('bluetooth') - // @ts-ignore - inputMethod = requireNapi('inputMethod') - // @ts-ignore - bundleManager = requireNapi('bundle.bundleManager') - // @ts-ignore - abilityAccessCtrl = requireNapi('abilityAccessCtrl') - // @ts-ignore - geoLocationManager = requireNapi('geoLocationManager') -} catch (error) {} - -let bundleInfoForSelf = null - -try { - // @ts-ignore - bundleManager - .getBundleInfoForSelf( - bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO | - bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION - ) - .then((bundleInfoFor) => { - bundleInfoForSelf = bundleInfoFor - }) -} catch (e) {} - -// const errMsgMap = new Map([ -// [401, 'Parameter error'], -// [201, 'Permission denied'], -// [3, 'URL using bad/illegal format or missing URL'], -// [7, "Couldn't connect to server"], -// [23, 'Failed writing received data to disk/application'], -// [25, 'Upload failed'], -// [26, 'Failed to open/read local data from file/application'], -// [28, 'Timeout was reached'], -// [73, 'Remote file already exists'], -// [78, 'Remote file not found'], -// [999, 'Unknown Other Error'], -// ]) - -const ErrorCode = { - PARAMETER_ERROR: 202, -} - -// class RequestTask { -// private abortFlag: boolean -// private readonly fail: (arg0: any) => any -// private readonly complete: (arg0: any) => any -// private readonly httpRequest: any -// private headersCallback: Map -// private result: { data?: any, statusCode?: any, header?: any, cookies?: any, errMsg: string | undefined } -// private res: { errMsg: string } -// -// constructor (object: any) { -// const { url, header, method = 'GET', timeout, responseType, enableCache } = object || {} -// let { data } = object || {} -// const { success, fail, complete } = object || {} -// this.abortFlag = false -// this.fail = fail -// this.complete = complete -// this.httpRequest = http.createHttp() -// this.headersCallback = new Map() -// if (!object) { -// console.error('[OsChannel] request error: params illegal') -// return -// } -// let isFormUrlEncoded = false -// for (const key in header) { -// if (key.toLowerCase() === 'content-type') { -// if (header[key].toLowerCase().includes('application/x-www-form-urlencoded')) { -// isFormUrlEncoded = true -// } -// break -// } -// } -// -// // data为Object类型时,属性的值类型如果是number, request请求时信息会丢失. 故将data转成string类型进行规避 -// if (data && (isFormUrlEncoded || ['GET', 'OPTIONS', 'DELETE', 'TRACE', 'CONNECT'].includes(method))) { -// const dataArray = [] -// for (const key in data) { -// // @ts-ignore -// dataArray.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])) -// } -// data = dataArray.join('&') -// } -// // header的属性的值类型如果是number, request请求时信息会丢失. 故将各个属性转成string类型 -// if (header) { -// for (const key in header) { -// header[key] = `${header[key]}` -// } -// } -// const httpRequestOptions = { -// method: method, -// extraData: data || {}, -// header: header, -// connectTimeout: timeout, -// expectDataType: -// responseType && responseType === 'arraybuffer' ? http.HttpDataType.ARRAY_BUFFER : http.HttpDataType.STRING, -// usingCache: enableCache || false, -// } -// this.httpRequest -// .request(typeof url === 'string' ? url : '', httpRequestOptions) -// .then((data) => { -// if (success && !this.abortFlag) { -// let result = data.result -// const { dataType } = object || {} -// if (dataType && dataType !== 'json') { -// if (typeof data.result === 'object') { -// result = JSON.stringify(data.result) -// } -// } else if (typeof data.result === 'string') { -// try { -// result = JSON.parse(result) -// } catch (err) { -// /* empty */ -// } -// } -// const res = { -// data: result, -// statusCode: data.responseCode, -// header: data.header, -// cookies: typeof data.cookies === 'string' ? (data.cookies ? [data.cookies] : []) : data.cookies, -// errMsg: 'request:ok', -// } -// this.result = res -// success(res) -// } -// }) -// .catch((err: any) => { -// console.error('[OsChannel] request error: ' + JSON.stringify(err)) -// if (fail && !this.abortFlag) { -// const res = { -// errMsg: errMsgMap.has(err.code) ? errMsgMap.get(err.code) : 'Unknown Error', -// } -// this.result = res -// fail(res) -// } -// }) -// .finally(() => { -// if (complete && !this.abortFlag) { -// complete(this.result) -// } -// }) -// } -// -// /** -// * interrupt request task -// */ -// abort () { -// this.httpRequest.destroy() -// this.abortFlag = true -// this.res = { -// errMsg: 'request:fail abort', -// } -// this.fail && this.fail(this.res) -// this.complete && this.complete(this.res) -// } -// -// /** -// * subscribe HTTP Response Header event -// * @callback params header {Object}: HTTP Response Header -// */ -// onHeadersReceived (callback: any) { -// const taskCallback = (header: any) => { -// !this.abortFlag && -// callback({ -// header, -// }) -// } -// if (!callback) { -// console.error('[OsChannel] Invalid, callback is null') -// return -// } -// if (!this.headersCallback.has(callback)) { -// this.headersCallback.set(callback, taskCallback) -// if (this.httpRequest) { -// this.httpRequest.on('headersReceive', taskCallback) -// } -// } -// } -// -// /** -// * unsubscribe HTTP Response Header event -// * remove all if callback is null, otherwise remove the specialized callback -// */ -// offHeadersReceived (callback: any) { -// if (!callback) { -// this.headersCallback.clear() -// if (this.httpRequest) { -// this.httpRequest.off('headersReceive') -// } -// } else if (this.headersCallback.has(callback)) { -// if (this.httpRequest) { -// this.httpRequest.off('headersReceive', this.headersCallback.get(callback)) -// } -// this.headersCallback.delete(callback) -// } else { -// /* empty */ -// } -// } -// } - -const hideKeyboard = () => { - return new Promise((resolve, reject) => { - try { - const inputMethodController = inputMethod.getController() - inputMethodController - .hideTextInput() - .then(() => { - resolve({ errMsg: 'ok' }) - }) - .catch((err: any) => { - console.error('[OsChannel] Failed to hideSoftKeyboard: ' + JSON.stringify(err)) - reject(err) - }) - } catch (exception) { - console.error('[OsChannel] Failed to get inputMethod Controller. Cause: ' + JSON.stringify(exception)) - reject(exception) - } - }) -} - -const makePhoneCall = (options) => { - const { phoneNumber, success, fail } = options - if (!phoneNumber) { - return new Promise((_resolve, reject) => { - // eslint-disable-next-line prefer-promise-reject-errors - reject(['param is invalid.', ErrorCode.PARAMETER_ERROR]) - }) - } - return call.makeCall(phoneNumber).then( - () => { - // @ts-ignore - success({ errMsg: 'ok' }) - }, - (err: any) => { - console.error(`[OsChannel] start makePhoneCall fail`) - // @ts-ignore - fail({ errMsg: JSON.stringify(err) }) - } - ) -} - -const 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, - } -} - -const 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 -} - -// const request = (options: any) => { -// const { success, fail, complete } = options -// const task = new RequestTask(options) -// // if success / fail / complete, return requestTask otherwise return Promise -// if (options && !success && !fail && !complete) { -// success(task) -// return -// } -// if (!options) { -// fail(['illegal params', -1]) -// } -// return task -// } - -const osChannelApi = { - getSystemSetting, - hideKeyboard, - makePhoneCall, - getAppAuthorizeSetting, - // request, -} - -export default osChannelApi