@tarojs/taro-h5 3.4.6 → 3.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/taro-h5",
3
- "version": "3.4.6",
3
+ "version": "3.4.7",
4
4
  "description": "Taro h5 framework",
5
5
  "main:h5": "dist/index.js",
6
6
  "main": "dist/index.cjs.js",
@@ -16,7 +16,7 @@
16
16
  "test:ci": "jest -i --coverage false",
17
17
  "test:dev": "jest --watch",
18
18
  "test:coverage": "jest --coverage",
19
- "copy:assets": "copy src/**/*.css dist",
19
+ "copy:assets": "copy-cli src/**/*.css dist",
20
20
  "build": "rollup -c && tsc",
21
21
  "dev": "tsc -w",
22
22
  "prebuild": "npm run copy:assets",
@@ -32,15 +32,15 @@
32
32
  "author": "O2Team",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@tarojs/api": "3.4.6",
36
- "@tarojs/router": "3.4.6",
37
- "@tarojs/runtime": "3.4.6",
38
- "@tarojs/taro": "3.4.6",
35
+ "@tarojs/api": "3.4.7",
36
+ "@tarojs/router": "3.4.7",
37
+ "@tarojs/runtime": "3.4.7",
38
+ "@tarojs/taro": "3.4.7",
39
39
  "base64-js": "^1.3.0",
40
40
  "jsonp-retry": "^1.0.3",
41
41
  "mobile-detect": "^1.4.2",
42
42
  "query-string": "^7.1.1",
43
43
  "whatwg-fetch": "^3.4.0"
44
44
  },
45
- "gitHead": "c12d65079a197af709bfeec2498588291160dfb3"
45
+ "gitHead": "bb62ad5d4f099940c8c4d6dfcba9b743bc3d2acf"
46
46
  }
@@ -1,7 +1,11 @@
1
1
  import { processOpenApi } from '../utils'
2
2
 
3
3
  // 扫码
4
- export const scanCode = processOpenApi('scanQRCode', { needResult: 1 }, res => ({
5
- errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
6
- result: res.resultStr
7
- }))
4
+ export const scanCode = processOpenApi({
5
+ name: 'scanQRCode',
6
+ defaultOptions: { needResult: 1 },
7
+ formatResult: res => ({
8
+ errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
9
+ result: res.resultStr
10
+ })
11
+ })
@@ -0,0 +1,80 @@
1
+ import Taro from '@tarojs/api'
2
+ import { processOpenApi, shouldBeObject } from '../utils'
3
+ import { MethodHandler } from '../utils/handler'
4
+
5
+ const getLocationByW3CApi: (options: Taro.getLocation.Option) => Promise<Taro.getLocation.SuccessCallbackResult> = (options: Taro.getLocation.Option): Promise<Taro.getLocation.SuccessCallbackResult> => {
6
+ // 断言 options 必须是 Object
7
+ const isObject = shouldBeObject(options)
8
+ if (!isObject.flag) {
9
+ const res = { errMsg: `getLocation:fail ${isObject.msg}` }
10
+ console.error(res.errMsg)
11
+ return Promise.reject(res)
12
+ }
13
+
14
+ // 解构回调函数
15
+ const { success, fail, complete } = options
16
+
17
+ const handle = new MethodHandler({ name: 'getLocation', success, fail, complete })
18
+
19
+ // const defaultMaximumAge = 5 * 1000
20
+
21
+ const positionOptions: PositionOptions = {
22
+ enableHighAccuracy: options.isHighAccuracy || (options.altitude != null), // 海拔定位需要高精度
23
+ // maximumAge: defaultMaximumAge, // 允许取多久以内的缓存位置
24
+ timeout: options.highAccuracyExpireTime // 高精度定位超时时间
25
+ }
26
+
27
+ // Web端API实现暂时仅支持GPS坐标系
28
+ if (options.type?.toUpperCase() !== 'WGS84') {
29
+ return handle.fail({
30
+ errMsg: 'This coordinate system type is not temporarily supported'
31
+ })
32
+ }
33
+
34
+ // 判断当前浏览器是否支持位置API
35
+ const geolocationSupported = navigator.geolocation
36
+
37
+ if (!geolocationSupported) {
38
+ return handle.fail({
39
+ errMsg: 'The current browser does not support this feature'
40
+ })
41
+ }
42
+
43
+ // 开始获取位置
44
+ return new Promise<Taro.getLocation.SuccessCallbackResult>(
45
+ (resolve, reject) => {
46
+ navigator.geolocation.getCurrentPosition(
47
+ (position) => {
48
+ const result: Taro.getLocation.SuccessCallbackResult = {
49
+ /** 位置的精确度 */
50
+ accuracy: position.coords.accuracy,
51
+ /** 高度,单位 m */
52
+ altitude: position.coords.altitude!,
53
+ /** 水平精度,单位 m */
54
+ horizontalAccuracy: position.coords.accuracy,
55
+ /** 纬度,范围为 -90~90,负数表示南纬 */
56
+ latitude: position.coords.latitude,
57
+ /** 经度,范围为 -180~180,负数表示西经 */
58
+ longitude: position.coords.longitude,
59
+ /** 速度,单位 m/s */
60
+ speed: position.coords.speed!,
61
+ /** 垂直精度,单位 m(Android 无法获取,返回 0) */
62
+ verticalAccuracy: position.coords.altitudeAccuracy || 0,
63
+ /** 调用结果,自动补充 */
64
+ errMsg: ''
65
+ }
66
+ handle.success(result, resolve)
67
+ },
68
+ (error) => {
69
+ handle.fail({ errMsg: error.message }, reject)
70
+ },
71
+ positionOptions
72
+ )
73
+ }
74
+ )
75
+ }
76
+
77
+ export const getLocation = processOpenApi({
78
+ name: 'getLocation',
79
+ standardMethod: getLocationByW3CApi
80
+ })
@@ -5,15 +5,18 @@ export const stopLocationUpdate = temporarilyNotSupport('stopLocationUpdate')
5
5
  export const startLocationUpdateBackground = temporarilyNotSupport('startLocationUpdateBackground')
6
6
  export const startLocationUpdate = temporarilyNotSupport('startLocationUpdate')
7
7
 
8
- export const openLocation = processOpenApi('openLocation', { scale: 18 })
8
+ export const openLocation = processOpenApi({
9
+ name: 'openLocation',
10
+ defaultOptions: { scale: 18 }
11
+ })
9
12
 
10
13
  export const onLocationChangeError = temporarilyNotSupport('onLocationChangeError')
11
14
  export const onLocationChange = temporarilyNotSupport('onLocationChange')
12
15
  export const offLocationChangeError = temporarilyNotSupport('offLocationChangeError')
13
16
  export const offLocationChange = temporarilyNotSupport('offLocationChange')
14
17
 
15
- export const getLocation = processOpenApi('getLocation')
18
+ export { getLocation } from './getLocation'
16
19
 
17
20
  export const choosePoi = temporarilyNotSupport('choosePoi')
18
21
 
19
- export * from './chooseLocation'
22
+ export { chooseLocation } from './chooseLocation'
@@ -104,7 +104,7 @@ export function temporarilyNotSupport (apiName) {
104
104
 
105
105
  export function weixinCorpSupport (apiName) {
106
106
  return () => {
107
- const errMsg = `h5端仅在微信公众号中支持 API ${apiName}`
107
+ const errMsg = `h5端当前仅在微信公众号JS-SDK环境下支持此 API ${apiName}`
108
108
  if (process.env.NODE_ENV !== 'production') {
109
109
  console.error(errMsg)
110
110
  return Promise.reject({
@@ -146,30 +146,46 @@ export const isValidColor = (color) => {
146
146
  return VALID_COLOR_REG.test(color)
147
147
  }
148
148
 
149
- export function processOpenApi (apiName: string, defaultOptions?: Record<string, unknown>, formatResult = res => res, formatParams = options => options) {
150
- // @ts-ignore
151
- if (!window.wx) {
152
- return weixinCorpSupport(apiName)
153
- }
154
- return options => {
155
- options = options || {}
156
- const obj = Object.assign({}, defaultOptions, options)
157
- const p = new Promise((resolve, reject) => {
158
- ['fail', 'success', 'complete'].forEach(k => {
159
- obj[k] = oriRes => {
160
- const res = formatResult(oriRes)
161
- options[k] && options[k](res)
162
- if (k === 'success') {
163
- resolve(res)
164
- } else if (k === 'fail') {
165
- reject(res)
149
+ interface IProcessOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any> {
150
+ name: string
151
+ defaultOptions?: TOptions
152
+ standardMethod?: (opts: TOptions) => Promise<TResult>
153
+ formatOptions?: (opts: TOptions) => TOptions
154
+ formatResult?: (res: TResult) => TResult
155
+ }
156
+
157
+ export function processOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any> ({
158
+ name,
159
+ defaultOptions,
160
+ standardMethod,
161
+ formatOptions = options => options,
162
+ formatResult = res => res
163
+ }: IProcessOpenApi<TOptions, TResult>) {
164
+ const notSupported = weixinCorpSupport(name)
165
+ return (options: Partial<TOptions> = {}): Promise<TResult> => {
166
+ // @ts-ignore
167
+ const targetApi = window?.wx?.[name]
168
+ const opts = formatOptions(Object.assign({}, defaultOptions, options))
169
+ if (typeof targetApi === 'function') {
170
+ return new Promise<TResult>((resolve, reject) => {
171
+ ['fail', 'success', 'complete'].forEach(k => {
172
+ opts[k] = preRef => {
173
+ const res = formatResult(preRef)
174
+ options[k] && options[k](res)
175
+ if (k === 'success') {
176
+ resolve(res)
177
+ } else if (k === 'fail') {
178
+ reject(res)
179
+ }
166
180
  }
167
- }
181
+ return targetApi(opts)
182
+ })
168
183
  })
169
- // @ts-ignore
170
- wx[apiName](formatParams(obj))
171
- })
172
- return p
184
+ } else if (typeof standardMethod === 'function') {
185
+ return standardMethod(opts)
186
+ } else {
187
+ return notSupported() as Promise<TResult>
188
+ }
173
189
  }
174
190
  }
175
191