@tarojs/taro 3.3.11 → 3.3.15

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 (97) hide show
  1. package/package.json +5 -5
  2. package/types/api/ad/index.d.ts +34 -28
  3. package/types/api/alipay/index.d.ts +11 -7
  4. package/types/api/base/debug.d.ts +60 -54
  5. package/types/api/base/env.d.ts +12 -8
  6. package/types/api/base/index.d.ts +87 -83
  7. package/types/api/base/system.d.ts +59 -54
  8. package/types/api/base/update.d.ts +37 -33
  9. package/types/api/base/weapp/app-event.d.ts +147 -131
  10. package/types/api/base/weapp/life-cycle.d.ts +14 -10
  11. package/types/api/canvas/index.d.ts +115 -110
  12. package/types/api/cloud/index.d.ts +99 -94
  13. package/types/api/data-analysis/index.d.ts +68 -64
  14. package/types/api/device/accelerometer.d.ts +57 -50
  15. package/types/api/device/battery.d.ts +19 -15
  16. package/types/api/device/ble.d.ts +241 -228
  17. package/types/api/device/bluetooth.d.ts +207 -194
  18. package/types/api/device/clipboard.d.ts +48 -43
  19. package/types/api/device/compass.d.ts +55 -48
  20. package/types/api/device/contact.d.ts +13 -9
  21. package/types/api/device/gyroscope.d.ts +41 -35
  22. package/types/api/device/iBeacon.d.ts +79 -73
  23. package/types/api/device/motion.d.ts +42 -37
  24. package/types/api/device/network.d.ts +80 -42
  25. package/types/api/device/nfc.d.ts +105 -96
  26. package/types/api/device/performance.d.ts +32 -18
  27. package/types/api/device/phone.d.ts +20 -15
  28. package/types/api/device/scan.d.ts +31 -26
  29. package/types/api/device/screen.d.ts +74 -67
  30. package/types/api/device/vibrate.d.ts +31 -25
  31. package/types/api/device/wifi.d.ts +151 -140
  32. package/types/api/ext/index.d.ts +44 -39
  33. package/types/api/files/index.d.ts +219 -209
  34. package/types/api/framework/index.d.ts +25 -20
  35. package/types/api/location/index.d.ts +140 -136
  36. package/types/api/media/audio.d.ts +135 -127
  37. package/types/api/media/background-audio.d.ts +135 -126
  38. package/types/api/media/camera.d.ts +30 -26
  39. package/types/api/media/editor.d.ts +35 -33
  40. package/types/api/media/image.d.ts +161 -150
  41. package/types/api/media/live.d.ts +88 -82
  42. package/types/api/media/map.d.ts +47 -43
  43. package/types/api/media/recorder.d.ts +90 -84
  44. package/types/api/media/video-processing.d.ts +10 -6
  45. package/types/api/media/video.d.ts +63 -58
  46. package/types/api/network/download.d.ts +34 -30
  47. package/types/api/network/mdns.d.ts +104 -91
  48. package/types/api/network/request.d.ts +83 -79
  49. package/types/api/network/udp.d.ts +20 -16
  50. package/types/api/network/upload.d.ts +60 -56
  51. package/types/api/network/websocket.d.ts +201 -194
  52. package/types/api/open-api/account.d.ts +18 -14
  53. package/types/api/open-api/address.d.ts +27 -23
  54. package/types/api/open-api/authorize.d.ts +30 -26
  55. package/types/api/open-api/card.d.ts +55 -51
  56. package/types/api/open-api/customer-service.d.ts +21 -17
  57. package/types/api/open-api/facial.d.ts +28 -24
  58. package/types/api/open-api/invoice.d.ts +38 -34
  59. package/types/api/open-api/login.d.ts +56 -52
  60. package/types/api/open-api/navigate.d.ts +79 -75
  61. package/types/api/open-api/payment.d.ts +34 -30
  62. package/types/api/open-api/settings.d.ts +49 -45
  63. package/types/api/open-api/soter.d.ts +92 -86
  64. package/types/api/open-api/subscribe-message.d.ts +24 -20
  65. package/types/api/open-api/user-info.d.ts +60 -57
  66. package/types/api/open-api/werun.d.ts +50 -46
  67. package/types/api/route/index.d.ts +122 -118
  68. package/types/api/share/index.d.ts +68 -64
  69. package/types/api/storage/background-fetch.d.ts +40 -35
  70. package/types/api/storage/index.d.ts +267 -262
  71. package/types/api/swan/index.d.ts +47 -42
  72. package/types/api/ui/animation.d.ts +21 -17
  73. package/types/api/ui/background.d.ts +38 -34
  74. package/types/api/ui/custom-component.d.ts +22 -18
  75. package/types/api/ui/fonts.d.ts +26 -21
  76. package/types/api/ui/interaction.d.ts +167 -121
  77. package/types/api/ui/keyboard.d.ts +59 -55
  78. package/types/api/ui/menu.d.ts +10 -6
  79. package/types/api/ui/navigation-bar.d.ts +67 -63
  80. package/types/api/ui/pull-down-refresh.d.ts +33 -29
  81. package/types/api/ui/scroll.d.ts +30 -26
  82. package/types/api/ui/sticky.d.ts +22 -19
  83. package/types/api/ui/tab-bar.d.ts +101 -97
  84. package/types/api/ui/window.d.ts +24 -18
  85. package/types/api/worker/index.d.ts +28 -24
  86. package/types/api/wxml/index.d.ts +42 -38
  87. package/types/global.d.ts +390 -0
  88. package/types/index.d.ts +12 -98
  89. package/types/taro.api.d.ts +94 -0
  90. package/types/taro.component.d.ts +10 -57
  91. package/types/taro.config.d.ts +24 -6
  92. package/types/taro.extend.d.ts +33 -75
  93. package/types/taro.hooks.d.ts +68 -67
  94. package/types/taro.lifecycle.d.ts +47 -28
  95. package/types/api/ad/index.ts +0 -6
  96. package/types/api/alipay/index.ts +0 -7
  97. package/types/api/index.d.ts +0 -352
@@ -1,4 +1,6 @@
1
- declare namespace Taro {
1
+ import Taro from '../../index'
2
+
3
+ declare module '../../index' {
2
4
  namespace uploadFile {
3
5
  interface Option {
4
6
  /** 开发者服务器地址 */
@@ -8,9 +10,9 @@ declare namespace Taro {
8
10
  /** 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容 */
9
11
  name: string
10
12
  /** HTTP 请求 Header,Header 中不能设置 Referer */
11
- header?: General.IAnyObject
13
+ header?: TaroGeneral.IAnyObject
12
14
  /** HTTP 请求中其他额外的 form data */
13
- formData?: General.IAnyObject
15
+ formData?: TaroGeneral.IAnyObject
14
16
  /** 超时时间,单位为毫秒 */
15
17
  timeout?: number
16
18
  /** 上传的文件名
@@ -18,15 +20,15 @@ declare namespace Taro {
18
20
  */
19
21
  fileName?: string
20
22
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
21
- complete?: (res: General.CallbackResult) => void
23
+ complete?: (res: TaroGeneral.CallbackResult) => void
22
24
  /** 接口调用失败的回调函数 */
23
- fail?: (res: General.CallbackResult) => void
25
+ fail?: (res: TaroGeneral.CallbackResult) => void
24
26
  /** 接口调用成功的回调函数 */
25
27
  success?: (
26
28
  result: SuccessCallbackResult,
27
29
  ) => void
28
30
  }
29
- interface SuccessCallbackResult extends General.CallbackResult {
31
+ interface SuccessCallbackResult extends TaroGeneral.CallbackResult {
30
32
  /** 开发者服务器返回的数据 */
31
33
  data: string
32
34
  /** 开发者服务器返回的 HTTP 状态码 */
@@ -39,11 +41,11 @@ declare namespace Taro {
39
41
  namespace UploadTask {
40
42
  /** HTTP Response Header 事件的回调函数 */
41
43
  type OffHeadersReceivedCallback = (
42
- res: General.CallbackResult,
44
+ res: TaroGeneral.CallbackResult,
43
45
  ) => void
44
46
  /** 上传进度变化事件的回调函数 */
45
47
  type OffProgressUpdateCallback = (
46
- res: General.CallbackResult,
48
+ res: TaroGeneral.CallbackResult,
47
49
  ) => void
48
50
  /** HTTP Response Header 事件的回调函数 */
49
51
  type OnHeadersReceivedCallback = (
@@ -55,7 +57,7 @@ declare namespace Taro {
55
57
  ) => void
56
58
  interface OnHeadersReceivedCallbackResult {
57
59
  /** 开发者服务器返回的 HTTP Response Header */
58
- header: General.IAnyObject
60
+ header: TaroGeneral.IAnyObject
59
61
  }
60
62
  interface OnProgressUpdateCallbackResult {
61
63
  /** 上传进度百分比 */
@@ -67,53 +69,6 @@ declare namespace Taro {
67
69
  }
68
70
  }
69
71
 
70
- /** 将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 `content-type` 为 `multipart/form-data`。使用前请注意阅读[相关说明](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html)。
71
- * @supported weapp, swan, alipay, h5
72
- * @example
73
- * ```tsx
74
- * Taro.chooseImage({
75
- * success (res) {
76
- * const tempFilePaths = res.tempFilePaths
77
- * Taro.uploadFile({
78
- * url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
79
- * filePath: tempFilePaths[0],
80
- * name: 'file',
81
- * formData: {
82
- * 'user': 'test'
83
- * },
84
- * success (res){
85
- * const data = res.data
86
- * //do something
87
- * }
88
- * })
89
- * }
90
- * })
91
- * ```
92
- * @example
93
- * ```tsx
94
- * const uploadTask = Taro.uploadFile({
95
- * url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
96
- * filePath: tempFilePaths[0],
97
- * name: 'file',
98
- * formData:{
99
- * 'user': 'test'
100
- * },
101
- * success: function (res){
102
- * var data = res.data
103
- * //do something
104
- * }
105
- * })
106
- * uploadTask.progress((res) => {
107
- * console.log('上传进度', res.progress)
108
- * console.log('已经上传的数据长度', res.totalBytesSent)
109
- * console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
110
- * })
111
- * uploadTask.abort() // 取消上传任务
112
- * ```
113
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html
114
- */
115
- function uploadFile(option: uploadFile.Option): Promise<uploadFile.SuccessCallbackResult & UploadTask> & UploadTask
116
-
117
72
  /** 一个可以监听上传进度变化事件,以及取消上传任务的对象
118
73
  * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/UploadTask.html
119
74
  */
@@ -164,4 +119,53 @@ declare namespace Taro {
164
119
  callback: UploadTask.OnProgressUpdateCallback,
165
120
  ): void
166
121
  }
122
+
123
+ interface TaroStatic {
124
+ /** 将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 `content-type` 为 `multipart/form-data`。使用前请注意阅读[相关说明](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html)。
125
+ * @supported weapp, swan, alipay, h5
126
+ * @example
127
+ * ```tsx
128
+ * Taro.chooseImage({
129
+ * success (res) {
130
+ * const tempFilePaths = res.tempFilePaths
131
+ * Taro.uploadFile({
132
+ * url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
133
+ * filePath: tempFilePaths[0],
134
+ * name: 'file',
135
+ * formData: {
136
+ * 'user': 'test'
137
+ * },
138
+ * success (res){
139
+ * const data = res.data
140
+ * //do something
141
+ * }
142
+ * })
143
+ * }
144
+ * })
145
+ * ```
146
+ * @example
147
+ * ```tsx
148
+ * const uploadTask = Taro.uploadFile({
149
+ * url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
150
+ * filePath: tempFilePaths[0],
151
+ * name: 'file',
152
+ * formData:{
153
+ * 'user': 'test'
154
+ * },
155
+ * success: function (res){
156
+ * var data = res.data
157
+ * //do something
158
+ * }
159
+ * })
160
+ * uploadTask.progress((res) => {
161
+ * console.log('上传进度', res.progress)
162
+ * console.log('已经上传的数据长度', res.totalBytesSent)
163
+ * console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
164
+ * })
165
+ * uploadTask.abort() // 取消上传任务
166
+ * ```
167
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html
168
+ */
169
+ uploadFile(option: uploadFile.Option): Promise<uploadFile.SuccessCallbackResult & UploadTask> & UploadTask
170
+ }
167
171
  }
@@ -1,72 +1,27 @@
1
- declare namespace Taro {
1
+ import Taro from '../../index'
2
+
3
+ declare module '../../index' {
2
4
  namespace sendSocketMessage {
3
5
  interface Option {
4
6
  /** 需要发送的内容 */
5
7
  data: string | ArrayBuffer
6
8
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
7
- complete?: (res: General.CallbackResult) => void
9
+ complete?: (res: TaroGeneral.CallbackResult) => void
8
10
  /** 接口调用失败的回调函数 */
9
- fail?: (res: General.CallbackResult) => void
11
+ fail?: (res: TaroGeneral.CallbackResult) => void
10
12
  /** 接口调用成功的回调函数 */
11
- success?: (res: General.CallbackResult) => void
13
+ success?: (res: TaroGeneral.CallbackResult) => void
12
14
  }
13
15
  }
14
16
 
15
- /** 通过 WebSocket 连接发送数据。需要先 Taro.connectSocket,并在 Taro.onSocketOpen 回调之后才能发送。
16
- * @supported weapp, alipay, swan
17
- * @example
18
- * ```tsx
19
- * let socketOpen = false
20
- * const socketMsgQueue = []
21
- * Taro.connectSocket({
22
- * url: 'test.php'
23
- * })
24
- * Taro.onSocketOpen(function(res) {
25
- * socketOpen = true
26
- * for (let i = 0; i < socketMsgQueue.length; i++){
27
- * sendSocketMessage(socketMsgQueue[i])
28
- * }
29
- * socketMsgQueue = []
30
- * })
31
- * function sendSocketMessage(msg) {
32
- * if (socketOpen) {
33
- * Taro.sendSocketMessage({
34
- * data:msg
35
- * })
36
- * } else {
37
- * socketMsgQueue.push(msg)
38
- * }
39
- * }
40
- * ```
41
- @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.sendSocketMessage.html
42
- */
43
- function sendSocketMessage(option: sendSocketMessage.Option): Promise<General.CallbackResult>
44
-
45
17
  namespace onSocketOpen {
46
18
  /** WebSocket 连接打开事件的回调函数 */
47
19
  type Callback = (result: OpenCallbackResult) => void
48
20
  interface OpenCallbackResult {
49
21
  /** 连接成功的 HTTP 响应 Header */
50
- header: General.IAnyObject
22
+ header: TaroGeneral.IAnyObject
51
23
  }
52
24
  }
53
- /** 监听 WebSocket 连接打开事件
54
- * @supported weapp, alipay, swan
55
- * @example
56
- * ```tsx
57
- * Taro.connectSocket({
58
- * url: 'test.php'
59
- * })
60
- * Taro.onSocketOpen(function (res) {
61
- * console.log('WebSocket连接已打开!')
62
- * })
63
- * ```
64
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketOpen.html
65
- */
66
- function onSocketOpen(
67
- /** WebSocket 连接打开事件的回调函数 */
68
- callback: onSocketOpen.Callback
69
- ): void
70
25
 
71
26
  namespace onSocketMessage {
72
27
  /** WebSocket 接受到服务器的消息事件的回调函数 */
@@ -77,52 +32,14 @@ declare namespace Taro {
77
32
  }
78
33
  }
79
34
 
80
- /** 监听 WebSocket 接受到服务器的消息事件
81
- * @supported weapp, alipay, swan
82
- * @example
83
- * ```tsx
84
- * Taro.connectSocket({
85
- * url: 'test.php'
86
- * })
87
- * Taro.onSocketMessage(function (res) {
88
- * console.log('收到服务器内容:' + res.data)
89
- * })
90
- * ```
91
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketMessage.html
92
- */
93
- function onSocketMessage<T = any>(
94
- /** WebSocket 接受到服务器的消息事件的回调函数 */
95
- callback: onSocketMessage.Callback<T>
96
- ): void
97
-
98
35
  namespace onSocketError {
99
36
  /** WebSocket 错误事件的回调函数 */
100
37
  type Callback = (result: CallbackResult) => void
101
- interface CallbackResult extends General.CallbackResult {
38
+ interface CallbackResult extends TaroGeneral.CallbackResult {
102
39
  /** 错误信息 */
103
40
  errMsg: string
104
41
  }
105
42
  }
106
- /** 监听 WebSocket 错误事件
107
- * @supported weapp, alipay, swan
108
- * @example
109
- * ```tsx
110
- * Taro.connectSocket({
111
- * url: 'test.php'
112
- * })
113
- * Taro.onSocketOpen(function (res){
114
- * console.log('WebSocket连接已打开!')
115
- * })
116
- * Taro.onSocketError(function (res){
117
- * console.log('WebSocket连接打开失败,请检查!')
118
- * })
119
- * ```
120
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketError.html
121
- */
122
- function onSocketError(
123
- /** WebSocket 错误事件的回调函数 */
124
- callback: (result: onSocketError.Callback) => void
125
- ): void
126
43
 
127
44
  namespace onSocketClose {
128
45
  /** WebSocket 连接关闭事件的回调函数 */
@@ -134,141 +51,53 @@ declare namespace Taro {
134
51
  reason: string
135
52
  }
136
53
  }
137
- /** 监听 WebSocket 连接关闭事件
138
- * @supported weapp, alipay, swan
139
- * @example
140
- * ```tsx
141
- * Taro.connectSocket({
142
- * url: 'test.php'
143
- * })
144
- * //注意这里有时序问题,
145
- * //如果 Taro.connectSocket 还没回调 Taro.onSocketOpen,而先调用 Taro.closeSocket,那么就做不到关闭 WebSocket 的目的。
146
- * //必须在 WebSocket 打开期间调用 Taro.closeSocket 才能关闭。
147
- * Taro.onSocketOpen(function () {
148
- * Taro.closeSocket()
149
- * })
150
- * Taro.onSocketClose(function (res) {
151
- * console.log('WebSocket 已关闭!')
152
- * })
153
- * ```
154
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketClose.html
155
- */
156
- function onSocketClose(
157
- /** WebSocket 连接关闭事件的回调函数 */
158
- callback: onSocketClose.Callback
159
- ): void
160
54
 
161
55
  namespace connectSocket {
162
56
  interface Option {
163
57
  /** 开发者服务器 wss 接口地址 */
164
58
  url: string
165
59
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
166
- complete?: (res: General.CallbackResult) => void
60
+ complete?: (res: TaroGeneral.CallbackResult) => void
167
61
  /** 接口调用失败的回调函数 */
168
- fail?: (res: General.CallbackResult) => void
62
+ fail?: (res: TaroGeneral.CallbackResult) => void
169
63
  /** HTTP Header,Header 中不能设置 Referer */
170
- header?: General.IAnyObject
64
+ header?: TaroGeneral.IAnyObject
171
65
  /** 子协议数组 */
172
66
  protocols?: string[]
173
67
  /** 接口调用成功的回调函数 */
174
- success?: (res: General.CallbackResult) => void
68
+ success?: (res: TaroGeneral.CallbackResult) => void
175
69
  /** 建立 TCP 连接的时候的 TCP_NODELAY 设置 */
176
70
  tcpNoDelay?: boolean
177
71
  }
178
72
  }
179
73
 
180
- /** 创建一个 WebSocket 连接。使用前请注意阅读[相关说明](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html)。
181
- *
182
- * **并发数**
183
- * - 1.7.0 及以上版本,最多可以同时存在 5 个 WebSocket 连接。
184
- * - 1.7.0 以下版本,一个小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。
185
- * @supported weapp, h5, rn, alipay, swan
186
- * @example
187
- * ```tsx
188
- * Taro.connectSocket({
189
- * url: 'wss://example.qq.com',
190
- * header:{
191
- * 'content-type': 'application/json'
192
- * },
193
- * protocols: ['protocol1']
194
- * })
195
- * ```
196
- * @example
197
- * ```tsx
198
- * Taro.connectSocket({
199
- * url: 'ws://echo.websocket.org/echo',
200
- * success: function () {
201
- * console.log('connect success')
202
- * }
203
- * }).then(task => {
204
- * task.onOpen(function () {
205
- * console.log('onOpen')
206
- * task.send({ data: 'xxx' })
207
- * })
208
- * task.onMessage(function (msg) {
209
- * console.log('onMessage: ', msg)
210
- * task.close()
211
- * })
212
- * task.onError(function () {
213
- * console.log('onError')
214
- * })
215
- * task.onClose(function (e) {
216
- * console.log('onClose: ', e)
217
- * })
218
- * })
219
- * ```
220
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.connectSocket.html
221
- */
222
- function connectSocket(option: connectSocket.Option): Promise<SocketTask>
223
-
224
74
  namespace closeSocket {
225
75
  interface Option {
226
76
  /** 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。 */
227
77
  code?: number
228
78
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
229
- complete?: (res: General.CallbackResult) => void
79
+ complete?: (res: TaroGeneral.CallbackResult) => void
230
80
  /** 接口调用失败的回调函数 */
231
- fail?: (res: General.CallbackResult) => void
81
+ fail?: (res: TaroGeneral.CallbackResult) => void
232
82
  /** 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于 123 字节的 UTF-8 文本(不是字符)。 */
233
83
  reason?: string
234
84
  /** 接口调用成功的回调函数 */
235
- success?: (res: General.CallbackResult) => void
85
+ success?: (res: TaroGeneral.CallbackResult) => void
236
86
  }
237
87
  }
238
88
 
239
- /** 关闭 WebSocket 连接
240
- * @supported weapp, alipay, swan
241
- * @example
242
- * ```tsx
243
- * Taro.connectSocket({
244
- * url: 'test.php'
245
- * })
246
- * //注意这里有时序问题,
247
- * //如果 Taro.connectSocket 还没回调 Taro.onSocketOpen,而先调用 Taro.closeSocket,那么就做不到关闭 WebSocket 的目的。
248
- * //必须在 WebSocket 打开期间调用 Taro.closeSocket 才能关闭。
249
- * Taro.onSocketOpen(function() {
250
- * Taro.closeSocket()
251
- * })
252
- * Taro.onSocketClose(function(res) {
253
- * console.log('WebSocket 已关闭!')
254
- * })
255
- * ```
256
- * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.closeSocket.html
257
- */
258
- function closeSocket(option?: closeSocket.Option): Promise<General.CallbackResult>
259
-
260
89
  namespace SocketTask {
261
90
  interface CloseOption {
262
91
  /** 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。 */
263
92
  code?: number
264
93
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
265
- complete?: (res: General.CallbackResult) => void
94
+ complete?: (res: TaroGeneral.CallbackResult) => void
266
95
  /** 接口调用失败的回调函数 */
267
- fail?: (res: General.CallbackResult) => void
96
+ fail?: (res: TaroGeneral.CallbackResult) => void
268
97
  /** 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于 123 字节的 UTF-8 文本(不是字符)。 */
269
98
  reason?: string
270
99
  /** 接口调用成功的回调函数 */
271
- success?: (res: General.CallbackResult) => void
100
+ success?: (res: TaroGeneral.CallbackResult) => void
272
101
  }
273
102
  /** WebSocket 连接关闭事件的回调函数 */
274
103
  type OnCloseCallback = (result: OnCloseCallbackResult) => void
@@ -280,7 +109,7 @@ declare namespace Taro {
280
109
  }
281
110
  /** WebSocket 错误事件的回调函数 */
282
111
  type OnErrorCallback = (result: OnErrorCallbackResult) => void
283
- interface OnErrorCallbackResult extends General.CallbackResult {
112
+ interface OnErrorCallbackResult extends TaroGeneral.CallbackResult {
284
113
  /** 错误信息 */
285
114
  errMsg: string
286
115
  }
@@ -294,17 +123,17 @@ declare namespace Taro {
294
123
  type OnOpenCallback = (result: OnOpenCallbackResult) => void
295
124
  interface OnOpenCallbackResult {
296
125
  /** 连接成功的 HTTP 响应 Header*/
297
- header: General.IAnyObject
126
+ header: TaroGeneral.IAnyObject
298
127
  }
299
128
  interface SendOption {
300
129
  /** 需要发送的内容 */
301
130
  data: string | ArrayBuffer
302
131
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
303
- complete?: (res: General.CallbackResult) => void
132
+ complete?: (res: TaroGeneral.CallbackResult) => void
304
133
  /** 接口调用失败的回调函数 */
305
- fail?: (res: General.CallbackResult) => void
134
+ fail?: (res: TaroGeneral.CallbackResult) => void
306
135
  /** 接口调用成功的回调函数 */
307
- success?: (res: General.CallbackResult) => void
136
+ success?: (res: TaroGeneral.CallbackResult) => void
308
137
  }
309
138
  }
310
139
 
@@ -372,4 +201,182 @@ declare namespace Taro {
372
201
  /** 浏览器 websocket 实例。(h5 端独有) */
373
202
  readonly ws: WebSocket
374
203
  }
204
+
205
+ interface TaroStatic {
206
+ /** 通过 WebSocket 连接发送数据。需要先 Taro.connectSocket,并在 Taro.onSocketOpen 回调之后才能发送。
207
+ * @supported weapp, alipay, swan
208
+ * @example
209
+ * ```tsx
210
+ * let socketOpen = false
211
+ * const socketMsgQueue = []
212
+ * Taro.connectSocket({
213
+ * url: 'test.php'
214
+ * })
215
+ * Taro.onSocketOpen(function(res) {
216
+ * socketOpen = true
217
+ * for (let i = 0; i < socketMsgQueue.length; i++){
218
+ * sendSocketMessage(socketMsgQueue[i])
219
+ * }
220
+ * socketMsgQueue = []
221
+ * })
222
+ * function sendSocketMessage(msg) {
223
+ * if (socketOpen) {
224
+ * Taro.sendSocketMessage({
225
+ * data:msg
226
+ * })
227
+ * } else {
228
+ * socketMsgQueue.push(msg)
229
+ * }
230
+ * }
231
+ * ```
232
+ @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.sendSocketMessage.html
233
+ */
234
+ sendSocketMessage(option: sendSocketMessage.Option): Promise<TaroGeneral.CallbackResult>
235
+
236
+ /** 监听 WebSocket 连接打开事件
237
+ * @supported weapp, alipay, swan
238
+ * @example
239
+ * ```tsx
240
+ * Taro.connectSocket({
241
+ * url: 'test.php'
242
+ * })
243
+ * Taro.onSocketOpen(function (res) {
244
+ * console.log('WebSocket连接已打开!')
245
+ * })
246
+ * ```
247
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketOpen.html
248
+ */
249
+ onSocketOpen(
250
+ /** WebSocket 连接打开事件的回调函数 */
251
+ callback: onSocketOpen.Callback
252
+ ): void
253
+
254
+ /** 监听 WebSocket 接受到服务器的消息事件
255
+ * @supported weapp, alipay, swan
256
+ * @example
257
+ * ```tsx
258
+ * Taro.connectSocket({
259
+ * url: 'test.php'
260
+ * })
261
+ * Taro.onSocketMessage(function (res) {
262
+ * console.log('收到服务器内容:' + res.data)
263
+ * })
264
+ * ```
265
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketMessage.html
266
+ */
267
+ onSocketMessage<T = any>(
268
+ /** WebSocket 接受到服务器的消息事件的回调函数 */
269
+ callback: onSocketMessage.Callback<T>
270
+ ): void
271
+
272
+ /** 监听 WebSocket 错误事件
273
+ * @supported weapp, alipay, swan
274
+ * @example
275
+ * ```tsx
276
+ * Taro.connectSocket({
277
+ * url: 'test.php'
278
+ * })
279
+ * Taro.onSocketOpen(function (res){
280
+ * console.log('WebSocket连接已打开!')
281
+ * })
282
+ * Taro.onSocketError(function (res){
283
+ * console.log('WebSocket连接打开失败,请检查!')
284
+ * })
285
+ * ```
286
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketError.html
287
+ */
288
+ onSocketError(
289
+ /** WebSocket 错误事件的回调函数 */
290
+ callback: (result: onSocketError.Callback) => void
291
+ ): void
292
+
293
+ /** 监听 WebSocket 连接关闭事件
294
+ * @supported weapp, alipay, swan
295
+ * @example
296
+ * ```tsx
297
+ * Taro.connectSocket({
298
+ * url: 'test.php'
299
+ * })
300
+ * //注意这里有时序问题,
301
+ * //如果 Taro.connectSocket 还没回调 Taro.onSocketOpen,而先调用 Taro.closeSocket,那么就做不到关闭 WebSocket 的目的。
302
+ * //必须在 WebSocket 打开期间调用 Taro.closeSocket 才能关闭。
303
+ * Taro.onSocketOpen(function () {
304
+ * Taro.closeSocket()
305
+ * })
306
+ * Taro.onSocketClose(function (res) {
307
+ * console.log('WebSocket 已关闭!')
308
+ * })
309
+ * ```
310
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.onSocketClose.html
311
+ */
312
+ onSocketClose(
313
+ /** WebSocket 连接关闭事件的回调函数 */
314
+ callback: onSocketClose.Callback
315
+ ): void
316
+
317
+ /** 创建一个 WebSocket 连接。使用前请注意阅读[相关说明](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html)。
318
+ *
319
+ * **并发数**
320
+ * - 1.7.0 及以上版本,最多可以同时存在 5 个 WebSocket 连接。
321
+ * - 1.7.0 以下版本,一个小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。
322
+ * @supported weapp, h5, rn, alipay, swan
323
+ * @example
324
+ * ```tsx
325
+ * Taro.connectSocket({
326
+ * url: 'wss://example.qq.com',
327
+ * header:{
328
+ * 'content-type': 'application/json'
329
+ * },
330
+ * protocols: ['protocol1']
331
+ * })
332
+ * ```
333
+ * @example
334
+ * ```tsx
335
+ * Taro.connectSocket({
336
+ * url: 'ws://echo.websocket.org/echo',
337
+ * success: function () {
338
+ * console.log('connect success')
339
+ * }
340
+ * }).then(task => {
341
+ * task.onOpen(function () {
342
+ * console.log('onOpen')
343
+ * task.send({ data: 'xxx' })
344
+ * })
345
+ * task.onMessage(function (msg) {
346
+ * console.log('onMessage: ', msg)
347
+ * task.close()
348
+ * })
349
+ * task.onError(function () {
350
+ * console.log('onError')
351
+ * })
352
+ * task.onClose(function (e) {
353
+ * console.log('onClose: ', e)
354
+ * })
355
+ * })
356
+ * ```
357
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.connectSocket.html
358
+ */
359
+ connectSocket(option: connectSocket.Option): Promise<SocketTask>
360
+
361
+ /** 关闭 WebSocket 连接
362
+ * @supported weapp, alipay, swan
363
+ * @example
364
+ * ```tsx
365
+ * Taro.connectSocket({
366
+ * url: 'test.php'
367
+ * })
368
+ * //注意这里有时序问题,
369
+ * //如果 Taro.connectSocket 还没回调 Taro.onSocketOpen,而先调用 Taro.closeSocket,那么就做不到关闭 WebSocket 的目的。
370
+ * //必须在 WebSocket 打开期间调用 Taro.closeSocket 才能关闭。
371
+ * Taro.onSocketOpen(function() {
372
+ * Taro.closeSocket()
373
+ * })
374
+ * Taro.onSocketClose(function(res) {
375
+ * console.log('WebSocket 已关闭!')
376
+ * })
377
+ * ```
378
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/websocket/wx.closeSocket.html
379
+ */
380
+ closeSocket(option?: closeSocket.Option): Promise<TaroGeneral.CallbackResult>
381
+ }
375
382
  }