@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.
@@ -1,6 +1,10 @@
1
1
  import { processOpenApi } from '../utils';
2
2
  // 扫码
3
- export const scanCode = processOpenApi('scanQRCode', { needResult: 1 }, res => ({
4
- errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
5
- result: res.resultStr
6
- }));
3
+ export const scanCode = processOpenApi({
4
+ name: 'scanQRCode',
5
+ defaultOptions: { needResult: 1 },
6
+ formatResult: res => ({
7
+ errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
8
+ result: res.resultStr
9
+ })
10
+ });
@@ -0,0 +1,64 @@
1
+ import { processOpenApi, shouldBeObject } from '../utils';
2
+ import { MethodHandler } from '../utils/handler';
3
+ const getLocationByW3CApi = (options) => {
4
+ var _a;
5
+ // 断言 options 必须是 Object
6
+ const isObject = shouldBeObject(options);
7
+ if (!isObject.flag) {
8
+ const res = { errMsg: `getLocation:fail ${isObject.msg}` };
9
+ console.error(res.errMsg);
10
+ return Promise.reject(res);
11
+ }
12
+ // 解构回调函数
13
+ const { success, fail, complete } = options;
14
+ const handle = new MethodHandler({ name: 'getLocation', success, fail, complete });
15
+ // const defaultMaximumAge = 5 * 1000
16
+ const positionOptions = {
17
+ enableHighAccuracy: options.isHighAccuracy || (options.altitude != null),
18
+ // maximumAge: defaultMaximumAge, // 允许取多久以内的缓存位置
19
+ timeout: options.highAccuracyExpireTime // 高精度定位超时时间
20
+ };
21
+ // Web端API实现暂时仅支持GPS坐标系
22
+ if (((_a = options.type) === null || _a === void 0 ? void 0 : _a.toUpperCase()) !== 'WGS84') {
23
+ return handle.fail({
24
+ errMsg: 'This coordinate system type is not temporarily supported'
25
+ });
26
+ }
27
+ // 判断当前浏览器是否支持位置API
28
+ const geolocationSupported = navigator.geolocation;
29
+ if (!geolocationSupported) {
30
+ return handle.fail({
31
+ errMsg: 'The current browser does not support this feature'
32
+ });
33
+ }
34
+ // 开始获取位置
35
+ return new Promise((resolve, reject) => {
36
+ navigator.geolocation.getCurrentPosition((position) => {
37
+ const result = {
38
+ /** 位置的精确度 */
39
+ accuracy: position.coords.accuracy,
40
+ /** 高度,单位 m */
41
+ altitude: position.coords.altitude,
42
+ /** 水平精度,单位 m */
43
+ horizontalAccuracy: position.coords.accuracy,
44
+ /** 纬度,范围为 -90~90,负数表示南纬 */
45
+ latitude: position.coords.latitude,
46
+ /** 经度,范围为 -180~180,负数表示西经 */
47
+ longitude: position.coords.longitude,
48
+ /** 速度,单位 m/s */
49
+ speed: position.coords.speed,
50
+ /** 垂直精度,单位 m(Android 无法获取,返回 0) */
51
+ verticalAccuracy: position.coords.altitudeAccuracy || 0,
52
+ /** 调用结果,自动补充 */
53
+ errMsg: ''
54
+ };
55
+ handle.success(result, resolve);
56
+ }, (error) => {
57
+ handle.fail({ errMsg: error.message }, reject);
58
+ }, positionOptions);
59
+ });
60
+ };
61
+ export const getLocation = processOpenApi({
62
+ name: 'getLocation',
63
+ standardMethod: getLocationByW3CApi
64
+ });
@@ -3,11 +3,14 @@ import { processOpenApi, temporarilyNotSupport } from '../utils/index';
3
3
  export const stopLocationUpdate = temporarilyNotSupport('stopLocationUpdate');
4
4
  export const startLocationUpdateBackground = temporarilyNotSupport('startLocationUpdateBackground');
5
5
  export const startLocationUpdate = temporarilyNotSupport('startLocationUpdate');
6
- export const openLocation = processOpenApi('openLocation', { scale: 18 });
6
+ export const openLocation = processOpenApi({
7
+ name: 'openLocation',
8
+ defaultOptions: { scale: 18 }
9
+ });
7
10
  export const onLocationChangeError = temporarilyNotSupport('onLocationChangeError');
8
11
  export const onLocationChange = temporarilyNotSupport('onLocationChange');
9
12
  export const offLocationChangeError = temporarilyNotSupport('offLocationChangeError');
10
13
  export const offLocationChange = temporarilyNotSupport('offLocationChange');
11
- export const getLocation = processOpenApi('getLocation');
14
+ export { getLocation } from './getLocation';
12
15
  export const choosePoi = temporarilyNotSupport('choosePoi');
13
- export * from './chooseLocation';
16
+ export { chooseLocation } from './chooseLocation';
@@ -90,7 +90,7 @@ export function temporarilyNotSupport(apiName) {
90
90
  }
91
91
  export function weixinCorpSupport(apiName) {
92
92
  return () => {
93
- const errMsg = `h5端仅在微信公众号中支持 API ${apiName}`;
93
+ const errMsg = `h5端当前仅在微信公众号JS-SDK环境下支持此 API ${apiName}`;
94
94
  if (process.env.NODE_ENV !== 'production') {
95
95
  console.error(errMsg);
96
96
  return Promise.reject({
@@ -129,31 +129,36 @@ const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
129
129
  export const isValidColor = (color) => {
130
130
  return VALID_COLOR_REG.test(color);
131
131
  };
132
- export function processOpenApi(apiName, defaultOptions, formatResult = res => res, formatParams = options => options) {
133
- // @ts-ignore
134
- if (!window.wx) {
135
- return weixinCorpSupport(apiName);
136
- }
137
- return options => {
138
- options = options || {};
139
- const obj = Object.assign({}, defaultOptions, options);
140
- const p = new Promise((resolve, reject) => {
141
- ['fail', 'success', 'complete'].forEach(k => {
142
- obj[k] = oriRes => {
143
- const res = formatResult(oriRes);
144
- options[k] && options[k](res);
145
- if (k === 'success') {
146
- resolve(res);
147
- }
148
- else if (k === 'fail') {
149
- reject(res);
150
- }
151
- };
132
+ export function processOpenApi({ name, defaultOptions, standardMethod, formatOptions = options => options, formatResult = res => res }) {
133
+ const notSupported = weixinCorpSupport(name);
134
+ return (options = {}) => {
135
+ var _a;
136
+ // @ts-ignore
137
+ const targetApi = (_a = window === null || window === void 0 ? void 0 : window.wx) === null || _a === void 0 ? void 0 : _a[name];
138
+ const opts = formatOptions(Object.assign({}, defaultOptions, options));
139
+ if (typeof targetApi === 'function') {
140
+ return new Promise((resolve, reject) => {
141
+ ['fail', 'success', 'complete'].forEach(k => {
142
+ opts[k] = preRef => {
143
+ const res = formatResult(preRef);
144
+ options[k] && options[k](res);
145
+ if (k === 'success') {
146
+ resolve(res);
147
+ }
148
+ else if (k === 'fail') {
149
+ reject(res);
150
+ }
151
+ };
152
+ return targetApi(opts);
153
+ });
152
154
  });
153
- // @ts-ignore
154
- wx[apiName](formatParams(obj));
155
- });
156
- return p;
155
+ }
156
+ else if (typeof standardMethod === 'function') {
157
+ return standardMethod(opts);
158
+ }
159
+ else {
160
+ return notSupported();
161
+ }
157
162
  };
158
163
  }
159
164
  /**
@@ -1 +1,3 @@
1
- export declare const scanCode: (options: any) => Promise<unknown>;
1
+ export declare const scanCode: (options?: Partial<{
2
+ needResult: number;
3
+ }>) => Promise<any>;
@@ -0,0 +1,2 @@
1
+ import Taro from '@tarojs/api';
2
+ export declare const getLocation: (options?: Partial<Taro.getLocation.Option>) => Promise<Taro.getLocation.SuccessCallbackResult>;
@@ -7,7 +7,9 @@ export declare const startLocationUpdateBackground: () => Promise<{
7
7
  export declare const startLocationUpdate: () => Promise<{
8
8
  errMsg: string;
9
9
  }>;
10
- export declare const openLocation: (options: any) => Promise<unknown>;
10
+ export declare const openLocation: (options?: Partial<{
11
+ scale: number;
12
+ }>) => Promise<any>;
11
13
  export declare const onLocationChangeError: () => Promise<{
12
14
  errMsg: string;
13
15
  }>;
@@ -20,8 +22,8 @@ export declare const offLocationChangeError: () => Promise<{
20
22
  export declare const offLocationChange: () => Promise<{
21
23
  errMsg: string;
22
24
  }>;
23
- export declare const getLocation: (options: any) => Promise<unknown>;
25
+ export { getLocation } from './getLocation';
24
26
  export declare const choosePoi: () => Promise<{
25
27
  errMsg: string;
26
28
  }>;
27
- export * from './chooseLocation';
29
+ export { chooseLocation } from './chooseLocation';
@@ -28,7 +28,14 @@ export declare function permanentlyNotSupport(apiName: any): () => Promise<{
28
28
  }>;
29
29
  export declare function isFunction(obj: any): boolean;
30
30
  export declare const isValidColor: (color: any) => boolean;
31
- export declare function processOpenApi(apiName: string, defaultOptions?: Record<string, unknown>, formatResult?: (res: any) => any, formatParams?: (options: any) => any): (options: any) => Promise<unknown>;
31
+ interface IProcessOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any> {
32
+ name: string;
33
+ defaultOptions?: TOptions;
34
+ standardMethod?: (opts: TOptions) => Promise<TResult>;
35
+ formatOptions?: (opts: TOptions) => TOptions;
36
+ formatResult?: (res: TResult) => TResult;
37
+ }
38
+ export declare function processOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any>({ name, defaultOptions, standardMethod, formatOptions, formatResult }: IProcessOpenApi<TOptions, TResult>): (options?: Partial<TOptions>) => Promise<TResult>;
32
39
  /**
33
40
  * ease-in-out的函数
34
41
  * @param t 0-1的数字
package/dist/index.cjs.js CHANGED
@@ -108,7 +108,7 @@ function temporarilyNotSupport(apiName) {
108
108
  }
109
109
  function weixinCorpSupport(apiName) {
110
110
  return () => {
111
- const errMsg = `h5端仅在微信公众号中支持 API ${apiName}`;
111
+ const errMsg = `h5端当前仅在微信公众号JS-SDK环境下支持此 API ${apiName}`;
112
112
  if (process.env.NODE_ENV !== 'production') {
113
113
  console.error(errMsg);
114
114
  return Promise.reject({
@@ -147,31 +147,36 @@ const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
147
147
  const isValidColor = (color) => {
148
148
  return VALID_COLOR_REG.test(color);
149
149
  };
150
- function processOpenApi(apiName, defaultOptions, formatResult = res => res, formatParams = options => options) {
151
- // @ts-ignore
152
- if (!window.wx) {
153
- return weixinCorpSupport(apiName);
154
- }
155
- return options => {
156
- options = options || {};
157
- const obj = Object.assign({}, defaultOptions, options);
158
- const p = new Promise((resolve, reject) => {
159
- ['fail', 'success', 'complete'].forEach(k => {
160
- obj[k] = oriRes => {
161
- const res = formatResult(oriRes);
162
- options[k] && options[k](res);
163
- if (k === 'success') {
164
- resolve(res);
165
- }
166
- else if (k === 'fail') {
167
- reject(res);
168
- }
169
- };
150
+ function processOpenApi({ name, defaultOptions, standardMethod, formatOptions = options => options, formatResult = res => res }) {
151
+ const notSupported = weixinCorpSupport(name);
152
+ return (options = {}) => {
153
+ var _a;
154
+ // @ts-ignore
155
+ const targetApi = (_a = window === null || window === void 0 ? void 0 : window.wx) === null || _a === void 0 ? void 0 : _a[name];
156
+ const opts = formatOptions(Object.assign({}, defaultOptions, options));
157
+ if (typeof targetApi === 'function') {
158
+ return new Promise((resolve, reject) => {
159
+ ['fail', 'success', 'complete'].forEach(k => {
160
+ opts[k] = preRef => {
161
+ const res = formatResult(preRef);
162
+ options[k] && options[k](res);
163
+ if (k === 'success') {
164
+ resolve(res);
165
+ }
166
+ else if (k === 'fail') {
167
+ reject(res);
168
+ }
169
+ };
170
+ return targetApi(opts);
171
+ });
170
172
  });
171
- // @ts-ignore
172
- wx[apiName](formatParams(obj));
173
- });
174
- return p;
173
+ }
174
+ else if (typeof standardMethod === 'function') {
175
+ return standardMethod(opts);
176
+ }
177
+ else {
178
+ return notSupported();
179
+ }
175
180
  };
176
181
  }
177
182
  /**
@@ -1478,10 +1483,14 @@ const makePhoneCall = (options) => {
1478
1483
  };
1479
1484
 
1480
1485
  // 扫码
1481
- const scanCode = processOpenApi('scanQRCode', { needResult: 1 }, res => ({
1482
- errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
1483
- result: res.resultStr
1484
- }));
1486
+ const scanCode = processOpenApi({
1487
+ name: 'scanQRCode',
1488
+ defaultOptions: { needResult: 1 },
1489
+ formatResult: res => ({
1490
+ errMsg: res.errMsg === 'scanQRCode:ok' ? 'scanCode:ok' : res.errMsg,
1491
+ result: res.resultStr
1492
+ })
1493
+ });
1485
1494
 
1486
1495
  // 屏幕
1487
1496
  const setVisualEffectOnCapture = temporarilyNotSupport('setVisualEffectOnCapture');
@@ -1559,6 +1568,69 @@ const getApp = function () {
1559
1568
  // 自定义组件
1560
1569
  const getCurrentInstance = Taro__default["default"].getCurrentInstance;
1561
1570
 
1571
+ const getLocationByW3CApi = (options) => {
1572
+ var _a;
1573
+ // 断言 options 必须是 Object
1574
+ const isObject = shouldBeObject(options);
1575
+ if (!isObject.flag) {
1576
+ const res = { errMsg: `getLocation:fail ${isObject.msg}` };
1577
+ console.error(res.errMsg);
1578
+ return Promise.reject(res);
1579
+ }
1580
+ // 解构回调函数
1581
+ const { success, fail, complete } = options;
1582
+ const handle = new MethodHandler({ name: 'getLocation', success, fail, complete });
1583
+ // const defaultMaximumAge = 5 * 1000
1584
+ const positionOptions = {
1585
+ enableHighAccuracy: options.isHighAccuracy || (options.altitude != null),
1586
+ // maximumAge: defaultMaximumAge, // 允许取多久以内的缓存位置
1587
+ timeout: options.highAccuracyExpireTime // 高精度定位超时时间
1588
+ };
1589
+ // Web端API实现暂时仅支持GPS坐标系
1590
+ if (((_a = options.type) === null || _a === void 0 ? void 0 : _a.toUpperCase()) !== 'WGS84') {
1591
+ return handle.fail({
1592
+ errMsg: 'This coordinate system type is not temporarily supported'
1593
+ });
1594
+ }
1595
+ // 判断当前浏览器是否支持位置API
1596
+ const geolocationSupported = navigator.geolocation;
1597
+ if (!geolocationSupported) {
1598
+ return handle.fail({
1599
+ errMsg: 'The current browser does not support this feature'
1600
+ });
1601
+ }
1602
+ // 开始获取位置
1603
+ return new Promise((resolve, reject) => {
1604
+ navigator.geolocation.getCurrentPosition((position) => {
1605
+ const result = {
1606
+ /** 位置的精确度 */
1607
+ accuracy: position.coords.accuracy,
1608
+ /** 高度,单位 m */
1609
+ altitude: position.coords.altitude,
1610
+ /** 水平精度,单位 m */
1611
+ horizontalAccuracy: position.coords.accuracy,
1612
+ /** 纬度,范围为 -90~90,负数表示南纬 */
1613
+ latitude: position.coords.latitude,
1614
+ /** 经度,范围为 -180~180,负数表示西经 */
1615
+ longitude: position.coords.longitude,
1616
+ /** 速度,单位 m/s */
1617
+ speed: position.coords.speed,
1618
+ /** 垂直精度,单位 m(Android 无法获取,返回 0) */
1619
+ verticalAccuracy: position.coords.altitudeAccuracy || 0,
1620
+ /** 调用结果,自动补充 */
1621
+ errMsg: ''
1622
+ };
1623
+ handle.success(result, resolve);
1624
+ }, (error) => {
1625
+ handle.fail({ errMsg: error.message }, reject);
1626
+ }, positionOptions);
1627
+ });
1628
+ };
1629
+ const getLocation = processOpenApi({
1630
+ name: 'getLocation',
1631
+ standardMethod: getLocationByW3CApi
1632
+ });
1633
+
1562
1634
  function styleInject(css, ref) {
1563
1635
  if ( ref === void 0 ) ref = {};
1564
1636
  var insertAt = ref.insertAt;
@@ -1694,12 +1766,14 @@ const chooseLocation = ({ success, fail, complete, mapOpts } = {}) => {
1694
1766
  const stopLocationUpdate = temporarilyNotSupport('stopLocationUpdate');
1695
1767
  const startLocationUpdateBackground = temporarilyNotSupport('startLocationUpdateBackground');
1696
1768
  const startLocationUpdate = temporarilyNotSupport('startLocationUpdate');
1697
- const openLocation = processOpenApi('openLocation', { scale: 18 });
1769
+ const openLocation = processOpenApi({
1770
+ name: 'openLocation',
1771
+ defaultOptions: { scale: 18 }
1772
+ });
1698
1773
  const onLocationChangeError = temporarilyNotSupport('onLocationChangeError');
1699
1774
  const onLocationChange = temporarilyNotSupport('onLocationChange');
1700
1775
  const offLocationChangeError = temporarilyNotSupport('offLocationChangeError');
1701
1776
  const offLocationChange = temporarilyNotSupport('offLocationChange');
1702
- const getLocation = processOpenApi('getLocation');
1703
1777
  const choosePoi = temporarilyNotSupport('choosePoi');
1704
1778
 
1705
1779
  class InnerAudioContext {