From 263fc61cabb84d244bd72c4114d5ee23f39c2db9 Mon Sep 17 00:00:00 2001 From: codexu <461229187@qq.com> Date: Wed, 14 Sep 2022 18:55:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(h5):=20=E4=BD=8D=E7=BD=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- packages/uni-api/src/index.ts | 1 + .../src/protocols/location/locationChange.ts | 33 +++++++ packages/uni-h5-vite/lib/api.json | 6 ++ packages/uni-h5/src/service/api/index.ts | 1 + .../service/api/location/locationChange.ts | 86 +++++++++++++++++++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 packages/uni-api/src/protocols/location/locationChange.ts create mode 100644 packages/uni-h5/src/service/api/location/locationChange.ts diff --git a/package.json b/package.json index 1b734534e..670b683f4 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "devDependencies": { "@babel/core": "^7.17.10", "@babel/preset-env": "^7.16.11", - "@dcloudio/types": "^3.0.14", + "@dcloudio/types": "^3.0.15", "@dcloudio/uni-api": "3.0.0-alpha-3060120220907003", "@dcloudio/uni-app": "3.0.0-alpha-3060120220907003", "@jest/types": "^27.0.2", diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 534828561..0de90475a 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -79,6 +79,7 @@ export * from './protocols/network/uploadFile' export * from './protocols/network/socket' export * from './protocols/location/getLocation' +export * from './protocols/location/locationChange' export * from './protocols/route/route' diff --git a/packages/uni-api/src/protocols/location/locationChange.ts b/packages/uni-api/src/protocols/location/locationChange.ts new file mode 100644 index 000000000..202c4326c --- /dev/null +++ b/packages/uni-api/src/protocols/location/locationChange.ts @@ -0,0 +1,33 @@ +export const API_START_LOCATION_UPDATE = 'startLocationUpdate' +export type API_TYPE_START_LOCATION_UPDATE = typeof uni.startLocationUpdate +export const API_ON_LOCATION_CHANGE = 'onLocationChange' +export type API_TYPE_ON_LOCATION_CHANGE = typeof uni.onLocationChange +export const API_STOP_LOCATION_UPDATE = 'stopLocationUpdate' +export type API_TYPE_STOP_LOCATION_UPDATE = typeof uni.stopLocationUpdate +export const API_OFF_LOCATION_CHANGE = 'offLocationChange' +export type API_TYPE_OFF_LOCATION_CHANGE = typeof uni.offLocationChange +export const API_OFF_LOCATION_CHANGE_ERROR = 'offLocationChangeError' +export type API_TYPE_OFF_LOCATION_CHANGE_ERROR = typeof uni.offLocationChangeError +export const API_ON_LOCATION_CHANGE_ERROR = 'onLocationChangeError' +export type API_TYPE_ON_LOCATION_CHANGE_ERROR = typeof uni.onLocationChangeError + +const coordTypes = ['wgs84', 'gcj02'] + +export const StartLocationUpdateProtocol: ApiProtocol = + { + type: String + } + +export const StartLocationUpdateOptions: ApiOptions = + { + formatArgs: { + type(value, params) { + value = (value || '').toLowerCase() + if (coordTypes.indexOf(value) === -1) { + params.type = coordTypes[0] + } else { + params.type = value + } + } + } + } \ No newline at end of file diff --git a/packages/uni-h5-vite/lib/api.json b/packages/uni-h5-vite/lib/api.json index 632a64bcc..608314f66 100644 --- a/packages/uni-h5-vite/lib/api.json +++ b/packages/uni-h5-vite/lib/api.json @@ -82,6 +82,8 @@ "offAppShow", "offCompassChange", "offError", + "offLocationChange", + "offLocationChangeError", "offNetworkStatusChange", "offPageNotFound", "offPushMessage", @@ -94,6 +96,8 @@ "onError", "onGyroscopeChange", "onLocaleChange", + "onLocationChange", + "onLocationChangeError", "onMemoryWarning", "onNetworkStatusChange", "onPageNotFound", @@ -152,10 +156,12 @@ "startAccelerometer", "startCompass", "startGyroscope", + "startLocationUpdate", "startPullDownRefresh", "stopAccelerometer", "stopCompass", "stopGyroscope", + "stopLocationUpdate", "stopPullDownRefresh", "switchTab", "uploadFile", diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index 3ee5b8c99..e1a4824d5 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -45,6 +45,7 @@ export * from './network/socket' export * from './location/getLocation' export * from './location/openLocation' export * from './location/chooseLocation' +export * from './location/locationChange' export * from './route/navigateBack' export * from './route/navigateTo' diff --git a/packages/uni-h5/src/service/api/location/locationChange.ts b/packages/uni-h5/src/service/api/location/locationChange.ts new file mode 100644 index 000000000..1ce0e085f --- /dev/null +++ b/packages/uni-h5/src/service/api/location/locationChange.ts @@ -0,0 +1,86 @@ +import { + defineAsyncApi, + defineOnApi, + defineOffApi, + API_START_LOCATION_UPDATE, + API_TYPE_START_LOCATION_UPDATE, + StartLocationUpdateProtocol, + StartLocationUpdateOptions, + API_ON_LOCATION_CHANGE, + API_TYPE_ON_LOCATION_CHANGE, + API_TYPE_STOP_LOCATION_UPDATE, + API_STOP_LOCATION_UPDATE, + API_TYPE_OFF_LOCATION_CHANGE, + API_OFF_LOCATION_CHANGE, + API_TYPE_OFF_LOCATION_CHANGE_ERROR, + API_OFF_LOCATION_CHANGE_ERROR, + API_TYPE_ON_LOCATION_CHANGE_ERROR, + API_ON_LOCATION_CHANGE_ERROR +} from '@dcloudio/uni-api' +import { + translateGeo, +} from '../../../helpers/location' + +let watchId: number = 0; + +/** + * 开始更新定位 + */ +export const startLocationUpdate = defineAsyncApi( + API_START_LOCATION_UPDATE, + (_, { resolve, reject }) => { + if (navigator.geolocation && watchId === 0) { + watchId = navigator.geolocation.watchPosition( + (res) => { + translateGeo(_?.type, res.coords) + .then((coords) => { + UniServiceJSBridge.invokeOnCallback( + API_ON_LOCATION_CHANGE, + coords + ) + resolve() + }) + .catch((error) => { + reject(error.message) + }) + }, + (error) => { + reject(error.message) + } + ) + } + resolve() + }, + StartLocationUpdateProtocol, + StartLocationUpdateOptions +) + +export const onLocationChange = ( + defineOnApi(API_ON_LOCATION_CHANGE, () => {}) +) + +export const stopLocationUpdate = ( + defineAsyncApi(API_STOP_LOCATION_UPDATE, (_, { resolve, reject }) => { + if (watchId) { + navigator.geolocation.clearWatch(watchId) + watchId = 0 + resolve() + } else { + reject('stopLocationUpdate:fail') + } + }) +) + +export const offLocationChange = ( + defineOffApi(API_OFF_LOCATION_CHANGE, () => { + stopLocationUpdate() + }) +) + +export const onLocationChangeError = ( + defineOnApi(API_ON_LOCATION_CHANGE_ERROR, () => {}) +) + +export const offLocationChangeError = ( + defineOnApi(API_OFF_LOCATION_CHANGE_ERROR, () => {}) +) -- GitLab