@tarojs/taro-h5 3.4.4 → 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.
Files changed (153) hide show
  1. package/dist/api/device/scan.js +8 -4
  2. package/dist/api/location/chooseLocation.js +16 -7
  3. package/dist/api/location/getLocation.js +64 -0
  4. package/dist/api/location/index.js +6 -3
  5. package/dist/api/media/image/chooseImage.js +12 -1
  6. package/dist/api/media/image/getImageInfo.js +16 -1
  7. package/dist/api/network/request/index.js +4 -0
  8. package/dist/api/utils/index.js +30 -25
  9. package/dist/dist/api/ad/index.d.ts +6 -0
  10. package/dist/dist/api/ai/facial.d.ts +9 -0
  11. package/dist/dist/api/ai/index.d.ts +2 -0
  12. package/dist/dist/api/ai/visual.d.ts +6 -0
  13. package/dist/dist/api/alipay/index.d.ts +3 -0
  14. package/dist/dist/api/base/crypto.d.ts +3 -0
  15. package/dist/dist/api/base/debug.d.ts +9 -0
  16. package/dist/dist/api/base/index.d.ts +12 -0
  17. package/dist/dist/api/base/performance.d.ts +6 -0
  18. package/dist/dist/api/base/system.d.ts +25 -0
  19. package/dist/dist/api/base/update.d.ts +6 -0
  20. package/dist/dist/api/base/weapp/app-event.d.ts +48 -0
  21. package/dist/dist/api/base/weapp/life-cycle.d.ts +6 -0
  22. package/dist/dist/api/canvas/CanvasContext.d.ts +104 -0
  23. package/dist/dist/api/canvas/canvasGetImageData.d.ts +5 -0
  24. package/dist/dist/api/canvas/canvasPutImageData.d.ts +6 -0
  25. package/dist/dist/api/canvas/canvasToTempFilePath.d.ts +6 -0
  26. package/dist/dist/api/canvas/createCanvasContext.d.ts +5 -0
  27. package/dist/dist/api/canvas/index.d.ts +12 -0
  28. package/dist/dist/api/cloud/index.d.ts +31 -0
  29. package/dist/dist/api/data-analysis/index.d.ts +12 -0
  30. package/dist/dist/api/device/accelerometer.d.ts +17 -0
  31. package/dist/dist/api/device/accessibility.d.ts +3 -0
  32. package/dist/dist/api/device/battery.d.ts +5 -0
  33. package/dist/dist/api/device/bluetooth-ble.d.ts +48 -0
  34. package/dist/dist/api/device/bluetooth-peripheral.d.ts +9 -0
  35. package/dist/dist/api/device/bluetooth.d.ts +39 -0
  36. package/dist/dist/api/device/calendar.d.ts +6 -0
  37. package/dist/dist/api/device/clipboard.d.ts +15 -0
  38. package/dist/dist/api/device/compass.d.ts +17 -0
  39. package/dist/dist/api/device/contact.d.ts +6 -0
  40. package/dist/dist/api/device/crypto.d.ts +3 -0
  41. package/dist/dist/api/device/gyroscope.d.ts +12 -0
  42. package/dist/dist/api/device/iBeacon.d.ts +21 -0
  43. package/dist/dist/api/device/index.d.ts +23 -0
  44. package/dist/dist/api/device/keyboard.d.ts +12 -0
  45. package/dist/dist/api/device/memory.d.ts +6 -0
  46. package/dist/dist/api/device/motion.d.ts +17 -0
  47. package/dist/dist/api/device/network.d.ts +20 -0
  48. package/dist/dist/api/device/nfc.d.ts +21 -0
  49. package/dist/dist/api/device/phone.d.ts +2 -0
  50. package/dist/dist/api/device/scan.d.ts +3 -0
  51. package/dist/dist/api/device/screen.d.ts +18 -0
  52. package/dist/dist/api/device/vibrate.d.ts +9 -0
  53. package/dist/dist/api/device/wifi.d.ts +33 -0
  54. package/dist/dist/api/ext/index.d.ts +6 -0
  55. package/dist/dist/api/files/index.d.ts +24 -0
  56. package/dist/dist/api/framework/index.d.ts +4 -0
  57. package/dist/dist/api/index.d.ts +24 -0
  58. package/dist/dist/api/location/chooseLocation.d.ts +6 -0
  59. package/dist/dist/api/location/getLocation.d.ts +2 -0
  60. package/dist/dist/api/location/index.d.ts +29 -0
  61. package/dist/dist/api/media/EditorContext.d.ts +42 -0
  62. package/dist/dist/api/media/audio/InnerAudioContext.d.ts +58 -0
  63. package/dist/dist/api/media/audio/index.d.ts +29 -0
  64. package/dist/dist/api/media/background-audio/BackgroundAudioManager.d.ts +71 -0
  65. package/dist/dist/api/media/background-audio/index.d.ts +29 -0
  66. package/dist/dist/api/media/camera.d.ts +3 -0
  67. package/dist/dist/api/media/image/chooseImage.d.ts +5 -0
  68. package/dist/dist/api/media/image/getImageInfo.d.ts +5 -0
  69. package/dist/dist/api/media/image/index.d.ts +15 -0
  70. package/dist/dist/api/media/image/previewImage.d.ts +8 -0
  71. package/dist/dist/api/media/index.d.ts +12 -0
  72. package/dist/dist/api/media/live.d.ts +6 -0
  73. package/dist/dist/api/media/map.d.ts +3 -0
  74. package/dist/dist/api/media/media-recorder.d.ts +3 -0
  75. package/dist/dist/api/media/recorder.d.ts +9 -0
  76. package/dist/dist/api/media/video/index.d.ts +24 -0
  77. package/dist/dist/api/media/video-decoder.d.ts +3 -0
  78. package/dist/dist/api/media/video-processing.d.ts +3 -0
  79. package/dist/dist/api/media/voip.d.ts +42 -0
  80. package/dist/dist/api/navigate/index.d.ts +12 -0
  81. package/dist/dist/api/network/download.d.ts +6 -0
  82. package/dist/dist/api/network/index.d.ts +7 -0
  83. package/dist/dist/api/network/mdns.d.ts +30 -0
  84. package/dist/dist/api/network/request/index.d.ts +4 -0
  85. package/dist/dist/api/network/tcp.d.ts +3 -0
  86. package/dist/dist/api/network/udp.d.ts +3 -0
  87. package/dist/dist/api/network/upload.d.ts +5 -0
  88. package/dist/dist/api/network/utils.d.ts +44 -0
  89. package/dist/dist/api/network/websocket/index.d.ts +7 -0
  90. package/dist/dist/api/network/websocket/socketTask.d.ts +25 -0
  91. package/dist/dist/api/open-api/account.d.ts +3 -0
  92. package/dist/dist/api/open-api/address.d.ts +3 -0
  93. package/dist/dist/api/open-api/authorize.d.ts +6 -0
  94. package/dist/dist/api/open-api/card.d.ts +6 -0
  95. package/dist/dist/api/open-api/channels-live.d.ts +18 -0
  96. package/dist/dist/api/open-api/customer-service.d.ts +3 -0
  97. package/dist/dist/api/open-api/facial.d.ts +12 -0
  98. package/dist/dist/api/open-api/favorites.d.ts +6 -0
  99. package/dist/dist/api/open-api/group.d.ts +3 -0
  100. package/dist/dist/api/open-api/index.d.ts +18 -0
  101. package/dist/dist/api/open-api/invoice.d.ts +6 -0
  102. package/dist/dist/api/open-api/license-plate.d.ts +3 -0
  103. package/dist/dist/api/open-api/login.d.ts +9 -0
  104. package/dist/dist/api/open-api/red-package.d.ts +3 -0
  105. package/dist/dist/api/open-api/settings.d.ts +6 -0
  106. package/dist/dist/api/open-api/soter.d.ts +9 -0
  107. package/dist/dist/api/open-api/subscribe-message.d.ts +3 -0
  108. package/dist/dist/api/open-api/user-info.d.ts +6 -0
  109. package/dist/dist/api/open-api/werun.d.ts +6 -0
  110. package/dist/dist/api/payment/index.d.ts +6 -0
  111. package/dist/dist/api/route/index.d.ts +1 -0
  112. package/dist/dist/api/share/index.d.ts +30 -0
  113. package/dist/dist/api/storage/background-fetch.d.ts +12 -0
  114. package/dist/dist/api/storage/index.d.ts +18 -0
  115. package/dist/dist/api/swan/index.d.ts +63 -0
  116. package/dist/dist/api/taro.d.ts +12 -0
  117. package/dist/dist/api/ui/animation/index.d.ts +2 -0
  118. package/dist/dist/api/ui/background.d.ts +6 -0
  119. package/dist/dist/api/ui/custom-component.d.ts +1 -0
  120. package/dist/dist/api/ui/fonts.d.ts +2 -0
  121. package/dist/dist/api/ui/index.d.ts +12 -0
  122. package/dist/dist/api/ui/interaction/actionSheet.d.ts +61 -0
  123. package/dist/dist/api/ui/interaction/index.d.ts +14 -0
  124. package/dist/dist/api/ui/interaction/modal.d.ts +69 -0
  125. package/dist/dist/api/ui/interaction/toast.d.ts +85 -0
  126. package/dist/dist/api/ui/menu.d.ts +3 -0
  127. package/dist/dist/api/ui/navigation-bar/index.d.ts +15 -0
  128. package/dist/dist/api/ui/pull-down-refresh.d.ts +9 -0
  129. package/dist/dist/api/ui/scroll/index.d.ts +5 -0
  130. package/dist/dist/api/ui/sticky.d.ts +3 -0
  131. package/dist/dist/api/ui/tab-bar.d.ts +34 -0
  132. package/dist/dist/api/ui/window.d.ts +15 -0
  133. package/dist/dist/api/utils/handler.d.ts +48 -0
  134. package/dist/dist/api/utils/index.d.ts +45 -0
  135. package/dist/dist/api/worker/index.d.ts +3 -0
  136. package/dist/dist/api/wxml/index.d.ts +5 -0
  137. package/dist/dist/api/wxml/nodesRef.d.ts +13 -0
  138. package/dist/dist/api/wxml/selectorQuery.d.ts +24 -0
  139. package/dist/dist/index.d.ts +4 -0
  140. package/dist/dist/types/type.d.ts +29 -0
  141. package/dist/index.cjs.js +4675 -8471
  142. package/dist/index.cjs.js.map +1 -0
  143. package/dist/taroApis.js +1 -0
  144. package/dist/taroApis.js.map +1 -0
  145. package/package.json +12 -9
  146. package/src/api/device/scan.ts +8 -4
  147. package/src/api/location/chooseLocation.ts +14 -4
  148. package/src/api/location/getLocation.ts +80 -0
  149. package/src/api/location/index.ts +6 -3
  150. package/src/api/media/image/chooseImage.ts +18 -1
  151. package/src/api/media/image/getImageInfo.ts +16 -1
  152. package/src/api/network/request/index.ts +4 -0
  153. package/src/api/utils/index.ts +39 -23
@@ -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'
@@ -15,12 +15,21 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
15
15
  return Promise.reject(res)
16
16
  }
17
17
 
18
- const { count = 1, success, fail, complete, imageId = 'taroChooseImage' } = options
18
+ const {
19
+ count = 1,
20
+ success,
21
+ fail,
22
+ complete,
23
+ imageId = 'taroChooseImage',
24
+ sourceType = ['album', 'camera']
25
+ } = options
19
26
  const handle = new MethodHandler({ name: 'chooseImage', success, fail, complete })
20
27
  const res: Partial<Taro.chooseImage.SuccessCallbackResult> = {
21
28
  tempFilePaths: [],
22
29
  tempFiles: []
23
30
  }
31
+ const sourceTypeString = sourceType && sourceType.toString()
32
+ const acceptableSourceType = ['user', 'environment', 'camera']
24
33
 
25
34
  if (count && typeof count !== 'number') {
26
35
  res.errMsg = getParameterError({
@@ -39,6 +48,9 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
39
48
  if (count > 1) {
40
49
  obj.setAttribute('multiple', 'multiple')
41
50
  }
51
+ if (acceptableSourceType.indexOf(sourceTypeString) > -1) {
52
+ obj.setAttribute('capture', sourceTypeString)
53
+ }
42
54
  obj.setAttribute('accept', 'image/*')
43
55
  obj.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;')
44
56
  document.body.appendChild(obj)
@@ -49,6 +61,11 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
49
61
  } else {
50
62
  el.removeAttribute('multiple')
51
63
  }
64
+ if (acceptableSourceType.indexOf(sourceTypeString) > -1) {
65
+ el.setAttribute('capture', sourceTypeString)
66
+ } else {
67
+ el.removeAttribute('capture')
68
+ }
52
69
  }
53
70
 
54
71
  return new Promise(resolve => {
@@ -15,14 +15,29 @@ export const getImageInfo: typeof Taro.getImageInfo = (options) => {
15
15
  return Promise.reject(res)
16
16
  }
17
17
 
18
+ const getBase64Image = (image: HTMLImageElement) => {
19
+ try {
20
+ const canvas = document.createElement('canvas')
21
+ canvas.width = image.width
22
+ canvas.height = image.height
23
+ const ctx = canvas.getContext('2d')
24
+ ctx?.drawImage(image, 0, 0, image.width, image.height)
25
+ return canvas.toDataURL('image/png')
26
+ } catch (e) {
27
+ console.error('getImageInfo:get base64 fail', e)
28
+ }
29
+ }
30
+
18
31
  const { src, success, fail, complete } = options
19
32
  const handle = new MethodHandler({ name: 'getImageInfo', success, fail, complete })
20
33
  return new Promise((resolve, reject) => {
21
34
  const image = new Image()
35
+ image.crossOrigin = ''
22
36
  image.onload = () => {
23
37
  handle.success({
24
38
  width: image.naturalWidth,
25
- height: image.naturalHeight
39
+ height: image.naturalHeight,
40
+ path: getBase64Image(image) || src
26
41
  }, resolve)
27
42
  }
28
43
  image.onerror = (e: any) => {
@@ -86,6 +86,10 @@ function _request (options) {
86
86
  params.credentials = options.credentials
87
87
  return fetch(url, params)
88
88
  .then(response => {
89
+ if (!response) {
90
+ const errorResponse = { ok: false }
91
+ throw errorResponse
92
+ }
89
93
  res.statusCode = response.status
90
94
  res.header = {}
91
95
  for (const key of response.headers.keys()) {
@@ -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