@tarojs/taro-h5 3.3.14 → 3.3.18

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 (175) hide show
  1. package/dist/index.cjs.js +6601 -8371
  2. package/dist/index.js +9493 -0
  3. package/dist/taroApis.js +1 -1
  4. package/package.json +9 -9
  5. package/src/api/ad/index.ts +5 -0
  6. package/src/api/ai/facial.ts +6 -0
  7. package/src/api/ai/index.ts +2 -0
  8. package/src/api/ai/visual.ts +4 -0
  9. package/src/api/alipay/index.ts +4 -0
  10. package/src/api/base/crypto.ts +4 -0
  11. package/src/api/base/debug.ts +5 -0
  12. package/src/api/base/index.ts +23 -0
  13. package/src/api/base/performance.ts +5 -0
  14. package/src/api/base/system/index.ts +2 -0
  15. package/src/api/base/system/info.ts +64 -0
  16. package/src/api/base/system/network.ts +70 -0
  17. package/src/api/base/update.ts +5 -0
  18. package/src/api/base/weapp/app-event.ts +19 -0
  19. package/src/api/base/weapp/life-cycle.ts +5 -0
  20. package/src/api/canvas/CanvasContext.ts +230 -0
  21. package/src/api/canvas/canvasGetImageData.ts +27 -0
  22. package/src/api/canvas/canvasPutImageData.ts +24 -0
  23. package/src/api/canvas/canvasToTempFilePath.ts +24 -0
  24. package/src/api/canvas/createCanvasContext.ts +18 -0
  25. package/src/api/canvas/index.ts +10 -0
  26. package/src/api/cloud/index.ts +27 -0
  27. package/src/api/data-analysis/index.ts +6 -0
  28. package/src/api/device/accelerometer.ts +87 -0
  29. package/src/api/device/accessibility.ts +4 -0
  30. package/src/api/device/battery.ts +5 -0
  31. package/src/api/device/bluetooth-ble.ts +19 -0
  32. package/src/api/device/bluetooth-peripheral.ts +6 -0
  33. package/src/api/device/bluetooth.ts +16 -0
  34. package/src/api/device/calendar.ts +5 -0
  35. package/src/api/device/clipboard.ts +67 -0
  36. package/src/api/device/compass.ts +67 -0
  37. package/src/api/device/contact.ts +5 -0
  38. package/src/api/device/crypto.ts +4 -0
  39. package/src/api/device/gyroscope.ts +7 -0
  40. package/src/api/device/iBeacon.ts +10 -0
  41. package/src/api/device/index.ts +22 -0
  42. package/src/api/device/keyboard.ts +7 -0
  43. package/src/api/device/memory.ts +5 -0
  44. package/src/api/device/motion.ts +85 -0
  45. package/src/api/device/nfc.ts +10 -0
  46. package/src/api/device/phone.ts +30 -0
  47. package/src/api/device/scan.ts +7 -0
  48. package/src/api/device/screen.ts +9 -0
  49. package/src/api/device/vibrate.ts +36 -0
  50. package/src/api/device/wifi.ts +13 -0
  51. package/src/api/ext/index.ts +5 -0
  52. package/src/api/files/index.ts +11 -0
  53. package/src/api/framework/index.ts +10 -0
  54. package/src/api/index.ts +25 -0
  55. package/src/api/location/chooseLocation.ts +106 -0
  56. package/src/api/location/index.ts +19 -0
  57. package/src/api/location/style.css +8 -8
  58. package/src/api/media/audio/index.ts +105 -0
  59. package/src/api/media/background-audio.ts +12 -0
  60. package/src/api/media/camera.ts +4 -0
  61. package/src/api/media/editor.ts +29 -0
  62. package/src/api/media/image/chooseImage.ts +78 -0
  63. package/src/api/media/image/getImageInfo.ts +35 -0
  64. package/src/api/media/image/index.ts +13 -0
  65. package/src/api/media/image/previewImage.ts +101 -0
  66. package/src/api/media/index.ts +13 -0
  67. package/src/api/media/live.ts +5 -0
  68. package/src/api/media/map.ts +4 -0
  69. package/src/api/media/media-recorder.ts +4 -0
  70. package/src/api/media/recorder.ts +6 -0
  71. package/src/api/media/video/index.ts +82 -0
  72. package/src/api/media/video-decoder.ts +4 -0
  73. package/src/api/media/video-processing.ts +4 -0
  74. package/src/api/media/voip.ts +17 -0
  75. package/src/api/navigate/index.ts +7 -0
  76. package/src/api/{fileTransfer/downloadFile.js → network/download.ts} +16 -27
  77. package/src/api/network/index.ts +7 -0
  78. package/src/api/network/mdns.ts +13 -0
  79. package/src/api/{request/index.js → network/request/index.ts} +9 -6
  80. package/src/api/network/tcp.ts +4 -0
  81. package/src/api/network/udp.ts +4 -0
  82. package/src/api/{fileTransfer/uploadFile.js → network/upload.ts} +28 -32
  83. package/src/api/{fileTransfer/utils.js → network/utils.ts} +0 -0
  84. package/src/api/network/websocket/index.ts +88 -0
  85. package/src/api/{webSocket/socketTask.js → network/websocket/socketTask.ts} +17 -7
  86. package/src/api/open-api/account.ts +4 -0
  87. package/src/api/open-api/address.ts +4 -0
  88. package/src/api/open-api/authorize.ts +5 -0
  89. package/src/api/open-api/card.ts +5 -0
  90. package/src/api/open-api/channels-live.ts +9 -0
  91. package/src/api/open-api/customer-service.ts +4 -0
  92. package/src/api/open-api/facial.ts +7 -0
  93. package/src/api/open-api/favorites.ts +5 -0
  94. package/src/api/open-api/group.ts +4 -0
  95. package/src/api/open-api/index.ts +18 -0
  96. package/src/api/open-api/invoice.ts +5 -0
  97. package/src/api/open-api/license-plate.ts +4 -0
  98. package/src/api/open-api/login.ts +6 -0
  99. package/src/api/open-api/red-package.ts +4 -0
  100. package/src/api/open-api/settings.ts +5 -0
  101. package/src/api/open-api/soter.ts +6 -0
  102. package/src/api/open-api/subscribe-message.ts +4 -0
  103. package/src/api/open-api/user-info.ts +5 -0
  104. package/src/api/open-api/werun.ts +5 -0
  105. package/src/api/payment/index.ts +5 -0
  106. package/src/api/route/index.ts +10 -0
  107. package/src/api/share/index.ts +13 -0
  108. package/src/api/storage/background-fetch.ts +7 -0
  109. package/src/api/storage/index.ts +181 -0
  110. package/src/api/swan/index.ts +31 -0
  111. package/src/{taro/index.js → api/taro.ts} +133 -149
  112. package/src/api/{createAnimation/index.js → ui/animation/index.ts} +51 -35
  113. package/src/api/ui/background.ts +5 -0
  114. package/src/api/ui/custom-component.ts +4 -0
  115. package/src/api/ui/fonts.ts +4 -0
  116. package/src/api/ui/index.ts +12 -0
  117. package/src/api/ui/interaction/actionSheet.ts +212 -0
  118. package/src/api/{interactive/index.js → ui/interaction/index.ts} +93 -77
  119. package/src/api/ui/interaction/modal.ts +237 -0
  120. package/src/api/{interactive/toast.js → ui/interaction/toast.ts} +35 -33
  121. package/src/api/ui/menu.ts +4 -0
  122. package/src/api/ui/navigation-bar/index.ts +52 -0
  123. package/src/api/ui/pull-down-refresh.ts +27 -0
  124. package/src/api/ui/scroll/index.ts +85 -0
  125. package/src/api/ui/sticky.ts +4 -0
  126. package/src/api/ui/tab-bar.ts +326 -0
  127. package/src/api/ui/window.ts +37 -0
  128. package/src/api/utils/handler.ts +105 -0
  129. package/src/api/utils/{index.js → index.ts} +36 -115
  130. package/src/api/worker/index.ts +4 -0
  131. package/src/api/wxml/index.ts +9 -0
  132. package/src/api/wxml/nodesRef.ts +49 -0
  133. package/src/api/{createSelectorQuery/index.js → wxml/selectorQuery.ts} +75 -94
  134. package/src/index.ts +6 -0
  135. package/src/types/api.d.ts +4 -0
  136. package/src/types/define.d.ts +2 -0
  137. package/{types → src/types}/index.d.ts +0 -0
  138. package/{types → src/types}/type.ts +1 -1
  139. package/src/api/accelerometer/index.js +0 -105
  140. package/src/api/audio/index.js +0 -137
  141. package/src/api/canvas/canvasGetImageData.js +0 -44
  142. package/src/api/canvas/canvasPutImageData.js +0 -45
  143. package/src/api/canvas/canvasToTempFilePath.js +0 -49
  144. package/src/api/canvas/createCanvasContext.js +0 -340
  145. package/src/api/canvas/index.js +0 -4
  146. package/src/api/clipboard/index.js +0 -113
  147. package/src/api/compass/index.js +0 -99
  148. package/src/api/deviceMotion/index.js +0 -105
  149. package/src/api/fileTransfer/index.js +0 -2
  150. package/src/api/image/chooseImage.js +0 -87
  151. package/src/api/image/getImageInfo.js +0 -74
  152. package/src/api/image/index.js +0 -7
  153. package/src/api/image/previewImage.js +0 -94
  154. package/src/api/index.js +0 -29
  155. package/src/api/interactive/actionSheet.js +0 -214
  156. package/src/api/interactive/modal.js +0 -256
  157. package/src/api/location/chooseLocation.js +0 -143
  158. package/src/api/location/index.js +0 -5
  159. package/src/api/navigationBar/index.js +0 -57
  160. package/src/api/open/index.js +0 -5
  161. package/src/api/others/index.js +0 -43
  162. package/src/api/pullDownRefresh/index.js +0 -51
  163. package/src/api/scroll/index.js +0 -99
  164. package/src/api/storage/index.js +0 -211
  165. package/src/api/system/index.js +0 -9
  166. package/src/api/system/info.js +0 -36
  167. package/src/api/system/network.js +0 -68
  168. package/src/api/tabBar/index.js +0 -424
  169. package/src/api/unsupportedApi/index.js +0 -278
  170. package/src/api/vibrate/index.js +0 -49
  171. package/src/api/video/index.js +0 -87
  172. package/src/api/webSocket/index.js +0 -103
  173. package/src/api/window/index.js +0 -38
  174. package/src/index.cjs.js +0 -11
  175. package/src/index.js +0 -6
@@ -0,0 +1,82 @@
1
+ import Taro from '@tarojs/api'
2
+
3
+ import { MethodHandler } from '../../utils/handler'
4
+ import { findDOM, shouldBeObject, temporarilyNotSupport } from '../../utils'
5
+
6
+ // 视频
7
+ export const saveVideoToPhotosAlbum = temporarilyNotSupport('saveVideoToPhotosAlbum')
8
+ export const openVideoEditor = temporarilyNotSupport('openVideoEditor')
9
+ export const getVideoInfo = temporarilyNotSupport('getVideoInfo')
10
+
11
+ /**
12
+ * 创建 video 上下文 VideoContext 对象。
13
+ */
14
+ export const createVideoContext: typeof Taro.createVideoContext = (id, inst) => {
15
+ const el = findDOM(inst) as HTMLVideoElement
16
+ // TODO HTMLVideoElement to VideoContext
17
+ return el?.querySelector(`taro-video-core[id=${id}]`) as unknown as Taro.VideoContext
18
+ }
19
+
20
+ export const compressVideo = temporarilyNotSupport('compressVideo')
21
+
22
+ /**
23
+ * 拍摄视频或从手机相册中选视频。
24
+ */
25
+ export const chooseVideo: typeof Taro.chooseVideo = (options) => {
26
+ // options must be an Object
27
+ const isObject = shouldBeObject(options)
28
+ if (!isObject.flag) {
29
+ const res = { errMsg: `chooseVideo:fail ${isObject.msg}` }
30
+ console.error(res.errMsg)
31
+ return Promise.reject(res)
32
+ }
33
+
34
+ const { success, fail, complete } = options
35
+ const handle = new MethodHandler<Taro.chooseVideo.SuccessCallbackResult>({ name: 'chooseVideo', success, fail, complete })
36
+ const res: Partial<Taro.chooseVideo.SuccessCallbackResult> = {
37
+ tempFilePath: '',
38
+ duration: 0,
39
+ size: 0,
40
+ height: 0,
41
+ width: 0
42
+ }
43
+
44
+ const inputEl = document.createElement('input')
45
+ inputEl.setAttribute('type', 'file')
46
+ inputEl.setAttribute('multiple', 'multiple')
47
+ inputEl.setAttribute('accept', 'video/*')
48
+ inputEl.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;')
49
+ document.body.appendChild(inputEl)
50
+
51
+ return new Promise<Taro.chooseVideo.SuccessCallbackResult>(resolve => {
52
+ const TaroMouseEvents = document.createEvent('MouseEvents')
53
+ TaroMouseEvents.initEvent('click', true, true)
54
+ inputEl.dispatchEvent(TaroMouseEvents)
55
+ inputEl.onchange = function (e) {
56
+ const target = e.target as HTMLInputElement
57
+ const file = target.files?.[0]
58
+ const reader = new FileReader()
59
+ reader.onload = function (event: ProgressEvent<FileReader>) {
60
+ const videoEl = document.createElement('video')
61
+ const url = event.target?.result as string
62
+ videoEl.preload = 'metadata'
63
+ videoEl.src = url
64
+ videoEl.onloadedmetadata = () => {
65
+ res.tempFilePath = url
66
+ res.duration = videoEl.duration
67
+ res.size = event.total
68
+ res.height = videoEl.videoHeight
69
+ res.width = videoEl.videoHeight
70
+ return handle.success(res, resolve)
71
+ }
72
+ }
73
+ if (file) {
74
+ reader.readAsDataURL(file)
75
+ }
76
+ }
77
+ }).finally(() => {
78
+ document.body.removeChild(inputEl)
79
+ })
80
+ }
81
+
82
+ export const chooseMedia = temporarilyNotSupport('chooseMedia')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 视频解码器
4
+ export const createVideoDecoder = temporarilyNotSupport('createVideoDecoder')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 音视频合成
4
+ export const createMediaContainer = temporarilyNotSupport('createMediaContainer')
@@ -0,0 +1,17 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 实时语音
4
+ export const updateVoIPChatMuteConfig = temporarilyNotSupport('updateVoIPChatMuteConfig')
5
+ export const subscribeVoIPVideoMembers = temporarilyNotSupport('subscribeVoIPVideoMembers')
6
+ export const setEnable1v1Chat = temporarilyNotSupport('setEnable1v1Chat')
7
+ export const onVoIPVideoMembersChanged = temporarilyNotSupport('onVoIPVideoMembersChanged')
8
+ export const onVoIPChatStateChanged = temporarilyNotSupport('onVoIPChatStateChanged')
9
+ export const onVoIPChatSpeakersChanged = temporarilyNotSupport('onVoIPChatSpeakersChanged')
10
+ export const onVoIPChatMembersChanged = temporarilyNotSupport('onVoIPChatMembersChanged')
11
+ export const onVoIPChatInterrupted = temporarilyNotSupport('onVoIPChatInterrupted')
12
+ export const offVoIPVideoMembersChanged = temporarilyNotSupport('offVoIPVideoMembersChanged')
13
+ export const offVoIPChatStateChanged = temporarilyNotSupport('offVoIPChatStateChanged')
14
+ export const offVoIPChatMembersChanged = temporarilyNotSupport('offVoIPChatMembersChanged')
15
+ export const offVoIPChatInterrupted = temporarilyNotSupport('offVoIPChatInterrupted')
16
+ export const joinVoIPChat = temporarilyNotSupport('joinVoIPChat')
17
+ export const exitVoIPChat = temporarilyNotSupport('exitVoIPChat')
@@ -0,0 +1,7 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 跳转
4
+ export const openEmbeddedMiniProgram = temporarilyNotSupport('openEmbeddedMiniProgram')
5
+ export const navigateToMiniProgram = temporarilyNotSupport('navigateToMiniProgram')
6
+ export const navigateBackMiniProgram = temporarilyNotSupport('navigateBackMiniProgram')
7
+ export const exitMiniProgram = temporarilyNotSupport('exitMiniProgram')
@@ -1,13 +1,14 @@
1
- import { createCallbackManager } from '../utils'
1
+ import Taro from '@tarojs/api'
2
+ import { CallbackManager } from '../utils/handler'
2
3
  import { NETWORK_TIMEOUT, setHeader, XHR_STATS } from './utils'
3
4
 
4
- const createDownloadTask = ({ url, header, success, error }) => {
5
+ const createDownloadTask = ({ url, header, success, error }): Taro.DownloadTask => {
5
6
  let timeout
6
7
  const apiName = 'downloadFile'
7
8
  const xhr = new XMLHttpRequest()
8
9
  const callbackManager = {
9
- headersReceived: createCallbackManager(),
10
- progressUpdate: createCallbackManager()
10
+ headersReceived: new CallbackManager(),
11
+ progressUpdate: new CallbackManager()
11
12
  }
12
13
 
13
14
  xhr.open('GET', url, true)
@@ -47,7 +48,7 @@ const createDownloadTask = ({ url, header, success, error }) => {
47
48
  })
48
49
  }
49
50
 
50
- xhr.onerror = e => {
51
+ xhr.onerror = (e: ProgressEvent<EventTarget> & { message?: string }) => {
51
52
  error({
52
53
  errMsg: `${apiName}:fail ${e.message}`
53
54
  })
@@ -111,41 +112,29 @@ const createDownloadTask = ({ url, header, success, error }) => {
111
112
  /**
112
113
  * 下载文件资源到本地。客户端直接发起一个 HTTPS GET 请求,返回文件的本地临时路径。使用前请注意阅读相关说明。
113
114
  * 注意:请在服务端响应的 header 中指定合理的 Content-Type 字段,以保证客户端正确处理文件类型。
114
- * @todo 未挂载 task.offHeadersReceived
115
- * @todo 未挂载 task.offProgressUpdate
116
- * @param {Object} object 参数
117
- * @param {string} object.url 下载资源的 url
118
- * @param {Object} [object.header] HTTP 请求的 Header,Header 中不能设置 Referer
119
- * @param {string} [object.filePath] *指定文件下载后存储的路径
120
- * @param {function} [object.success] 接口调用成功的回调函数
121
- * @param {function} [object.fail] 接口调用失败的回调函数
122
- * @param {function} [object.complete] 接口调用结束的回调函数(调用成功、失败都会执行)
123
- * @returns {DownloadTask}
124
115
  */
125
- const downloadFile = ({ url, header, success, fail, complete }) => {
126
- let task
127
- const promise = new Promise((resolve, reject) => {
116
+ export const downloadFile: typeof Taro.downloadFile = ({ url, header, success, fail, complete }) => {
117
+ let task: Taro.DownloadTask
118
+ const result: ReturnType<typeof Taro.downloadFile> = new Promise((resolve, reject) => {
128
119
  task = createDownloadTask({
129
120
  url,
130
121
  header,
131
122
  success: res => {
132
123
  success && success(res)
133
- complete && complete()
124
+ complete && complete(res)
134
125
  resolve(res)
135
126
  },
136
127
  error: res => {
137
128
  fail && fail(res)
138
- complete && complete()
129
+ complete && complete(res)
139
130
  reject(res)
140
131
  }
141
132
  })
142
- })
143
133
 
144
- promise.headersReceive = task.onHeadersReceived
145
- promise.progress = task.onProgressUpdate
146
- promise.abort = task.abort
134
+ result.headersReceive = task.onHeadersReceived
135
+ result.progress = task.onProgressUpdate
136
+ result.abort = task.abort
137
+ }) as any
147
138
 
148
- return promise
139
+ return result
149
140
  }
150
-
151
- export default downloadFile
@@ -0,0 +1,7 @@
1
+ export * from './download'
2
+ export * from './mdns'
3
+ export * from './request'
4
+ export * from './tcp'
5
+ export * from './udp'
6
+ export * from './upload'
7
+ export * from './websocket'
@@ -0,0 +1,13 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // mDNS
4
+ export const stopLocalServiceDiscovery = temporarilyNotSupport('stopLocalServiceDiscovery')
5
+ export const startLocalServiceDiscovery = temporarilyNotSupport('startLocalServiceDiscovery')
6
+ export const onLocalServiceResolveFail = temporarilyNotSupport('onLocalServiceResolveFail')
7
+ export const onLocalServiceLost = temporarilyNotSupport('onLocalServiceLost')
8
+ export const onLocalServiceFound = temporarilyNotSupport('onLocalServiceFound')
9
+ export const onLocalServiceDiscoveryStop = temporarilyNotSupport('onLocalServiceDiscoveryStop')
10
+ export const offLocalServiceResolveFail = temporarilyNotSupport('offLocalServiceResolveFail')
11
+ export const offLocalServiceLost = temporarilyNotSupport('offLocalServiceLost')
12
+ export const offLocalServiceFound = temporarilyNotSupport('offLocalServiceFound')
13
+ export const offLocalServiceDiscoveryStop = temporarilyNotSupport('offLocalServiceDiscoveryStop')
@@ -1,10 +1,13 @@
1
1
  import Taro from '@tarojs/api'
2
2
  import 'whatwg-fetch'
3
3
  import jsonpRetry from 'jsonp-retry'
4
- import { serializeParams } from '../utils'
4
+
5
+ import { serializeParams } from '../../utils'
6
+
7
+ // @ts-ignore
5
8
  const { Link } = Taro
6
9
 
7
- function generateRequestUrlWithParams (url, params) {
10
+ function generateRequestUrlWithParams (url: string, params?: unknown) {
8
11
  params = typeof params === 'string' ? params : serializeParams(params)
9
12
  if (params) {
10
13
  url += (~url.indexOf('?') ? '&' : '?') + params
@@ -13,6 +16,7 @@ function generateRequestUrlWithParams (url, params) {
13
16
  return url
14
17
  }
15
18
 
19
+ // FIXME 移除 any 标注
16
20
  function _request (options) {
17
21
  options = options || {}
18
22
  if (typeof options === 'string') {
@@ -22,8 +26,8 @@ function _request (options) {
22
26
  }
23
27
  const { success, complete, fail } = options
24
28
  let url = options.url
25
- const params = {}
26
- const res = {}
29
+ const params: any = {}
30
+ const res: any = {}
27
31
  if (options.jsonp) {
28
32
  Object.assign(params, options)
29
33
  params.params = options.data
@@ -122,6 +126,5 @@ function taroInterceptor (chain) {
122
126
 
123
127
  const link = new Link(taroInterceptor)
124
128
 
125
- /** @type {TaroH5.request} */
126
- export const request = link.request.bind(link)
129
+ export const request: typeof Taro.request = link.request.bind(link)
127
130
  export const addInterceptor = link.addInterceptor.bind(link)
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // TCP 通信
4
+ export const createTCPSocket = temporarilyNotSupport('createTCPSocket')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // UDP 通信
4
+ export const createUDPSocket = temporarilyNotSupport('createUDPSocket')
@@ -1,4 +1,6 @@
1
- import { createCallbackManager } from '../utils'
1
+ import Taro from '@tarojs/api'
2
+ import { temporarilyNotSupport } from '../utils'
3
+ import { CallbackManager } from '../utils/handler'
2
4
  import {
3
5
  convertObjectUrlToBlob,
4
6
  NETWORK_TIMEOUT,
@@ -6,15 +8,15 @@ import {
6
8
  XHR_STATS
7
9
  } from './utils'
8
10
 
9
- const createUploadTask = ({ url, filePath, formData, name, header, timeout, fileName, success, error }) => {
11
+ const createUploadTask = ({ url, filePath, formData, name, header, timeout, fileName, success, error }): Taro.UploadTask => {
10
12
  let timeoutInter
11
13
  let formKey
12
14
  const apiName = 'uploadFile'
13
15
  const xhr = new XMLHttpRequest()
14
16
  const form = new FormData()
15
17
  const callbackManager = {
16
- headersReceived: createCallbackManager(),
17
- progressUpdate: createCallbackManager()
18
+ headersReceived: new CallbackManager(),
19
+ progressUpdate: new CallbackManager()
18
20
  }
19
21
 
20
22
  xhr.open('POST', url)
@@ -57,7 +59,7 @@ const createUploadTask = ({ url, filePath, formData, name, header, timeout, file
57
59
  })
58
60
  }
59
61
 
60
- xhr.onerror = e => {
62
+ xhr.onerror = (e: ProgressEvent<EventTarget> & { message?: string }) => {
61
63
  clearTimeout(timeoutInter)
62
64
  error({
63
65
  errMsg: `${apiName}:fail ${e.message}`
@@ -88,8 +90,11 @@ const createUploadTask = ({ url, filePath, formData, name, header, timeout, file
88
90
  }
89
91
 
90
92
  convertObjectUrlToBlob(filePath)
91
- .then(fileObj => {
92
- form.append(name, fileObj, fileName || fileObj.name || `file-${Date.now()}`)
93
+ .then((fileObj: string | (Blob & { name?: string })) => {
94
+ if (!fileName) {
95
+ fileName = typeof fileObj !== 'string' && fileObj.name
96
+ }
97
+ form.append(name, fileObj, fileName || `file-${Date.now()}`)
93
98
  send()
94
99
  })
95
100
  .catch(e => {
@@ -120,33 +125,26 @@ const createUploadTask = ({ url, filePath, formData, name, header, timeout, file
120
125
  */
121
126
  const offProgressUpdate = callbackManager.progressUpdate.remove
122
127
 
128
+ const headersReceived = temporarilyNotSupport('UploadTask.headersReceived')
129
+ const progress = temporarilyNotSupport('UploadTask.progress')
130
+
123
131
  return {
124
132
  abort,
125
133
  onHeadersReceived,
126
134
  offHeadersReceived,
127
135
  onProgressUpdate,
128
- offProgressUpdate
136
+ offProgressUpdate,
137
+ headersReceived,
138
+ progress
129
139
  }
130
140
  }
131
141
 
132
142
  /**
133
143
  * 将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data。使用前请注意阅读相关说明。
134
- * @param {Object} object 参数
135
- * @param {string} object.url 开发者服务器地址
136
- * @param {string} object.filePath 要上传文件资源的路径
137
- * @param {string} object.name 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
138
- * @param {Object} [object.header] HTTP 请求 Header,Header 中不能设置 Referer
139
- * @param {Object} [object.formData] HTTP 请求中其他额外的 form data
140
- * @param {number} [object.timeout] 超时时间,单位为毫秒
141
- * @param {string} [object.fileName] 上传的文件名
142
- * @param {function} [object.success] 接口调用成功的回调函数
143
- * @param {function} [object.fail] 接口调用失败的回调函数
144
- * @param {function} [object.complete] 接口调用结束的回调函数(调用成功、失败都会执行)
145
- * @returns {UploadTask}
146
144
  */
147
- const uploadFile = ({ url, filePath, name, header, formData, timeout, fileName, success, fail, complete }) => {
148
- let task
149
- const promise = new Promise((resolve, reject) => {
145
+ export const uploadFile: typeof Taro.uploadFile = ({ url, filePath, name, header, formData, timeout, fileName, success, fail, complete }) => {
146
+ let task: Taro.UploadTask
147
+ const result: ReturnType<typeof Taro.uploadFile> = new Promise((resolve, reject) => {
150
148
  task = createUploadTask({
151
149
  url,
152
150
  header,
@@ -157,22 +155,20 @@ const uploadFile = ({ url, filePath, name, header, formData, timeout, fileName,
157
155
  fileName,
158
156
  success: res => {
159
157
  success && success(res)
160
- complete && complete()
158
+ complete && complete(res)
161
159
  resolve(res)
162
160
  },
163
161
  error: res => {
164
162
  fail && fail(res)
165
- complete && complete()
163
+ complete && complete(res)
166
164
  reject(res)
167
165
  }
168
166
  })
169
- })
170
167
 
171
- promise.headersReceive = task.onHeadersReceived
172
- promise.progress = task.onProgressUpdate
173
- promise.abort = task.abort
168
+ result.headersReceive = task.onHeadersReceived
169
+ result.progress = task.onProgressUpdate
170
+ result.abort = task.abort
171
+ }) as any
174
172
 
175
- return promise
173
+ return result
176
174
  }
177
-
178
- export default uploadFile
@@ -0,0 +1,88 @@
1
+ import { SocketTask } from './socketTask'
2
+ import { shouldBeObject, getParameterError } from '../../utils'
3
+ import { MethodHandler } from 'src/api/utils/handler'
4
+
5
+ let socketTasks: SocketTask[] = []
6
+ let socketsCounter = 1
7
+
8
+ export function sendSocketMessage () {
9
+ console.warn('Deprecated.Please use socketTask.send instead.')
10
+ }
11
+
12
+ export function onSocketOpen () {
13
+ console.warn('Deprecated.Please use socketTask.onOpen instead.')
14
+ }
15
+
16
+ export function onSocketMessage () {
17
+ console.warn('Deprecated.Please use socketTask.onMessage instead.')
18
+ }
19
+
20
+ export function onSocketError () {
21
+ console.warn('Deprecated.Please use socketTask.onError instead.')
22
+ }
23
+
24
+ export function onSocketClose () {
25
+ console.warn('Deprecated.Please use socketTask.onClose instead.')
26
+ }
27
+
28
+ export function connectSocket (options) {
29
+ const name = 'connectSocket'
30
+
31
+ return new Promise((resolve, reject) => {
32
+ // options must be an Object
33
+ const isObject = shouldBeObject(options)
34
+ if (!isObject.flag) {
35
+ const res = { errMsg: `${name}:fail ${isObject.msg}` }
36
+ console.error(res.errMsg)
37
+ return reject(res)
38
+ }
39
+ const { url, protocols, success, fail, complete } = options
40
+ const handle = new MethodHandler<{
41
+ socketTaskId?: number
42
+ }>({ name, success, fail, complete })
43
+
44
+ // options.url must be String
45
+ if (typeof url !== 'string') {
46
+ return handle.fail({
47
+ errMsg: getParameterError({
48
+ para: 'url',
49
+ correct: 'String',
50
+ wrong: url
51
+ })
52
+ }, reject)
53
+ }
54
+
55
+ // options.url must be invalid
56
+ if (!url.startsWith('ws://') && !url.startsWith('wss://')) {
57
+ return handle.fail({
58
+ errMsg: `request:fail invalid url "${url}"`
59
+ }, reject)
60
+ }
61
+
62
+ // protocols must be array
63
+ const _protocols = Array.isArray(protocols) ? protocols : null
64
+
65
+ // 2 connection at most
66
+ if (socketTasks.length > 1) {
67
+ return handle.fail({
68
+ errMsg: '同时最多发起 2 个 socket 请求,更多请参考文档。'
69
+ }, reject)
70
+ }
71
+
72
+ const task = new SocketTask(url, _protocols)
73
+ task._destroyWhenClose = function () {
74
+ socketTasks = socketTasks.filter(socketTask => socketTask !== this)
75
+ }
76
+ socketTasks.push(task)
77
+
78
+ handle.success({
79
+ socketTaskId: socketsCounter++
80
+ })
81
+
82
+ return resolve(task)
83
+ })
84
+ }
85
+
86
+ export function closeSocket () {
87
+ console.warn('Deprecated.Please use socketTask.close instead.')
88
+ }
@@ -1,4 +1,14 @@
1
- export default class SocketTask {
1
+ import Taro from '@tarojs/api'
2
+
3
+ export class SocketTask {
4
+ ws: WebSocket
5
+ CONNECTING: number
6
+ OPEN: number
7
+ CLOSING: number
8
+ CLOSED: number
9
+ closeDetail: { code: any; reason: any }
10
+ _destroyWhenClose?: () => void
11
+
2
12
  constructor (url, protocols) {
3
13
  if (protocols && protocols.length) {
4
14
  this.ws = new WebSocket(url, protocols)
@@ -15,10 +25,10 @@ export default class SocketTask {
15
25
  return this.ws.readyState
16
26
  }
17
27
 
18
- send (obj = {}) {
19
- if (typeof obj !== 'object' || !obj) obj = {}
28
+ send (opts: Partial<Taro.SocketTask.SendOption> = {}) {
29
+ if (typeof opts !== 'object' || !opts) opts = {}
20
30
 
21
- const { data = '', success, fail, complete } = obj
31
+ const { data = '', success, fail, complete } = opts
22
32
 
23
33
  if (this.readyState !== 1) {
24
34
  const res = { errMsg: 'SocketTask.send:fail SocketTask.readState is not OPEN' }
@@ -36,15 +46,15 @@ export default class SocketTask {
36
46
  return Promise.resolve(res)
37
47
  }
38
48
 
39
- close (obj = {}) {
40
- if (typeof obj !== 'object' || !obj) obj = {}
49
+ close (opts: Partial<Taro.SocketTask.CloseOption> = {}) {
50
+ if (typeof opts !== 'object' || !opts) opts = {}
41
51
 
42
52
  const {
43
53
  code = 1000,
44
54
  reason = 'server complete,close',
45
55
  success,
46
56
  complete
47
- } = obj
57
+ } = opts
48
58
 
49
59
  this.closeDetail = { code, reason }
50
60
  // 主动断开时需要重置链接数
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 帐号信息
4
+ export const getAccountInfoSync = temporarilyNotSupport('getAccountInfoSync')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 收货地址
4
+ export const chooseAddress = temporarilyNotSupport('chooseAddress')
@@ -0,0 +1,5 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 授权
4
+ export const authorizeForMiniProgram = temporarilyNotSupport('authorizeForMiniProgram')
5
+ export const authorize = temporarilyNotSupport('authorize')
@@ -0,0 +1,5 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 卡券
4
+ export const openCard = temporarilyNotSupport('openCard')
5
+ export const addCard = temporarilyNotSupport('addCard')
@@ -0,0 +1,9 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 视频号
4
+ export const reserveChannelsLive = temporarilyNotSupport('reserveChannelsLive')
5
+ export const openChannelsLive = temporarilyNotSupport('openChannelsLive')
6
+ export const openChannelsEvent = temporarilyNotSupport('openChannelsEvent')
7
+ export const openChannelsActivity = temporarilyNotSupport('openChannelsActivity')
8
+ export const getChannelsLiveNoticeInfo = temporarilyNotSupport('getChannelsLiveNoticeInfo')
9
+ export const getChannelsLiveInfo = temporarilyNotSupport('getChannelsLiveInfo')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 微信客服
4
+ export const openCustomerServiceChat = temporarilyNotSupport('openCustomerServiceChat')
@@ -0,0 +1,7 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 过往接口
4
+ export const checkIsSupportFacialRecognition = temporarilyNotSupport('checkIsSupportFacialRecognition')
5
+ export const startFacialRecognitionVerify = temporarilyNotSupport('startFacialRecognitionVerify')
6
+ export const startFacialRecognitionVerifyAndUploadVideo = temporarilyNotSupport('startFacialRecognitionVerifyAndUploadVideo')
7
+ export const faceVerifyForPay = temporarilyNotSupport('faceVerifyForPay')
@@ -0,0 +1,5 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 收藏
4
+ export const addVideoToFavorites = temporarilyNotSupport('addVideoToFavorites')
5
+ export const addFileToFavorites = temporarilyNotSupport('addFileToFavorites')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 微信群
4
+ export const getGroupEnterInfo = temporarilyNotSupport('getGroupEnterInfo')
@@ -0,0 +1,18 @@
1
+ export * from './account'
2
+ export * from './address'
3
+ export * from './authorize'
4
+ export * from './card'
5
+ export * from './channels-live'
6
+ export * from './customer-service'
7
+ export * from './facial'
8
+ export * from './favorites'
9
+ export * from './group'
10
+ export * from './invoice'
11
+ export * from './license-plate'
12
+ export * from './login'
13
+ export * from './red-package'
14
+ export * from './settings'
15
+ export * from './soter'
16
+ export * from './subscribe-message'
17
+ export * from './user-info'
18
+ export * from './werun'
@@ -0,0 +1,5 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 发票
4
+ export const chooseInvoiceTitle = temporarilyNotSupport('chooseInvoiceTitle')
5
+ export const chooseInvoice = temporarilyNotSupport('chooseInvoice')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 车牌
4
+ export const chooseLicensePlate = temporarilyNotSupport('chooseLicensePlate')
@@ -0,0 +1,6 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 帐号信息
4
+ export const pluginLogin = temporarilyNotSupport('pluginLogin')
5
+ export const login = temporarilyNotSupport('login')
6
+ export const checkSession = temporarilyNotSupport('checkSession')
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 微信红包
4
+ export const showRedPackage = temporarilyNotSupport('showRedPackage')
@@ -0,0 +1,5 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 设置
4
+ export const openSetting = temporarilyNotSupport('openSetting')
5
+ export const getSetting = temporarilyNotSupport('getSetting')