vue2-client 1.17.34 → 1.17.35

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,215 +1,215 @@
1
- import { getSystemVersion, METHOD, request } from '@vue2-client/utils/request'
2
- import { ACCESS_TOKEN, V4_ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
3
-
4
- /**
5
- * GET请求
6
- * @param url 请求地址
7
- * @param parameter 路径参数
8
- * @returns {Promise<AxiosResponse<T>>}
9
- */
10
- function get (url, parameter) {
11
- return request(url, METHOD.GET, parameter)
12
- }
13
-
14
- /**
15
- * POST请求
16
- * @param url 请求地址
17
- * @param parameter 请求参数
18
- * @param config
19
- * @returns {Promise<AxiosResponse<T>>}
20
- */
21
- function post (url, parameter, config = {}) {
22
- return request(url, METHOD.POST, parameter, config)
23
- }
24
-
25
- /**
26
- * POST请求
27
- * @param url 请求地址
28
- * @param parameter 请求参数
29
- * @param serviceName
30
- * @param isDev
31
- * @param config
32
- * @returns {Promise<AxiosResponse<T>>}
33
- */
34
- function postByServiceName (url, parameter, serviceName = process.env.VUE_APP_SYSTEM_NAME, isDev, config = {}) {
35
- if (url.startsWith('api/') || url.startsWith('/api/')) {
36
- return request(url, METHOD.POST, parameter, config)
37
- }
38
- let apiPre = '/api/'
39
- if (isDev) {
40
- apiPre = '/devApi/'
41
- }
42
- if (!url.startsWith('/')) {
43
- url = '/' + url
44
- }
45
- url = apiPre + serviceName + url
46
- return request(url, METHOD.POST, parameter, config)
47
- }
48
-
49
- /**
50
- * DELETE请求
51
- * @param url 请求地址
52
- * @param parameter 请求参数
53
- * @param config
54
- * @returns {Promise<AxiosResponse<T>>}
55
- */
56
- function del (url, parameter, config = {}) {
57
- return request(url, METHOD.DELETE, parameter, config)
58
- }
59
-
60
- /**
61
- * PUT请求
62
- * @param url 请求地址
63
- * @param parameter 请求参数
64
- * @param config
65
- * @returns {Promise<AxiosResponse<T>>}
66
- */
67
- function put (url, parameter, config = {}) {
68
- return request(url, METHOD.PUT, parameter, config)
69
- }
70
-
71
- /**
72
- * 封装 EventSource 请求函数
73
- * @param {string} url - 请求地址
74
- * @param {Object} options - 请求选项(包括 method, headers, body 等)
75
- * @param {Function} onDataUpdate - 数据更新回调,每次接收到新的数据都会调用
76
- * @param {Function} onError - 错误处理回调
77
- * @returns {Function} - 用于取消请求的函数
78
- */
79
- function startEventStreamPOST (url, params, headers = {}, onDataUpdate, onError, serviceName = process.env.VUE_APP_SYSTEM_NAME, method = 'GET') {
80
- return startEventStream(url, params, headers, onDataUpdate, onError, serviceName, 'POST')
81
- }
82
- function startEventStream (url, params, headers = {}, onDataUpdate, onError, serviceName = process.env.VUE_APP_SYSTEM_NAME, method = 'GET') {
83
- // let isStopped = false
84
- let isStopped = false
85
- let requestCount = 0
86
-
87
- if (!(url.startsWith('api/') || url.startsWith('/api/') || url.startsWith('http'))) {
88
- if (!url.startsWith('/')) {
89
- url = '/' + url
90
- }
91
- url = '/api/' + serviceName + url
92
- }
93
-
94
- console.log(`[EventStream] 开始新的请求: ${url}`, {
95
- params,
96
- headers,
97
- method
98
- })
99
-
100
- const controller = new AbortController()
101
- const { signal } = controller
102
- const token = localStorage.getItem(ACCESS_TOKEN)
103
- // 增加 V4 登录请求头
104
- if (token) {
105
- const compatible = getSystemVersion()
106
- if (compatible === 'V4') {
107
- // V4 环境则添加 V4请求头
108
- headers[V4_ACCESS_TOKEN] = token
109
- } else {
110
- headers[ACCESS_TOKEN] = token
111
- }
112
- }
113
- fetch(url, {
114
- method: method,
115
- headers: {
116
- ...headers,
117
- Accept: 'text/event-stream',
118
- 'Cache-Control': 'no-cache',
119
- Connection: 'keep-alive'
120
- },
121
- body: JSON.stringify(params),
122
- signal
123
- }).then(response => {
124
- if (!response.ok) {
125
- throw new Error(`HTTP error! status: ${response.status}`)
126
- }
127
-
128
- requestCount++
129
- console.log(`[EventStream] 请求已打开 (第${requestCount}次): ${url}`, {
130
- status: response.status,
131
- statusText: response.statusText
132
- })
133
-
134
- const reader = response.body.getReader()
135
- const decoder = new TextDecoder()
136
- let buffer = ''
137
-
138
- function processStream () {
139
- if (isStopped) {
140
- console.log(`[EventStream] 请求已停止,停止读取流: ${url}`)
141
- return
142
- }
143
-
144
- reader.read().then(({ done, value }) => {
145
- if (done) {
146
- console.log(`[EventStream] 流读取完成: ${url}`)
147
- return
148
- }
149
-
150
- const chunk = decoder.decode(value, { stream: true })
151
- buffer += chunk
152
-
153
- // 处理接收到的数据
154
- const lines = buffer.split('\n')
155
- buffer = lines.pop() // 保留最后一个不完整的行
156
-
157
- let currentEvent = null
158
- let currentData = null
159
-
160
- for (const line of lines) {
161
- if (line.startsWith('event: ')) {
162
- currentEvent = line.slice(7)
163
- } else if (line.startsWith('data: ')) {
164
- currentData = line.slice(6)
165
- try {
166
- const parsedData = JSON.parse(currentData)
167
- if (currentEvent === 'additionalInfo') {
168
- // 触发 additionalInfo 回调
169
- onDataUpdate?.(parsedData, 'additionalInfo')
170
- } else {
171
- // 普通消息数据
172
- onDataUpdate?.(parsedData, 'sourceInfo')
173
- }
174
- } catch (e) {
175
- if (currentEvent === 'sourceInfo') {
176
- // 触发 sourceInfo 回调
177
- onDataUpdate?.(currentData, 'sourceInfo')
178
- } else {
179
- onDataUpdate?.(currentData, 'data')
180
- }
181
- }
182
- // 重置当前事件和数据
183
- currentEvent = null
184
- currentData = null
185
- }
186
- }
187
-
188
- processStream()
189
- }).catch(error => {
190
- console.error(`[EventStream] 流读取错误: ${url}`, error)
191
- if (!isStopped) {
192
- isStopped = true
193
- onError?.(error)
194
- }
195
- })
196
- }
197
-
198
- processStream()
199
- }).catch(error => {
200
- console.error(`[EventStream] 请求错误: ${url}`, error)
201
- if (!isStopped) {
202
- isStopped = true
203
- onError?.(error)
204
- }
205
- })
206
-
207
- // 返回停止函数
208
- return () => {
209
- isStopped = true
210
- controller.abort()
211
- console.log('连接已关闭')
212
- }
213
- }
214
-
215
- export { get, post, del, put, postByServiceName, startEventStream, startEventStreamPOST }
1
+ import { getSystemVersion, METHOD, request } from '@vue2-client/utils/request'
2
+ import { ACCESS_TOKEN, V4_ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
3
+
4
+ /**
5
+ * GET请求
6
+ * @param url 请求地址
7
+ * @param parameter 路径参数
8
+ * @returns {Promise<AxiosResponse<T>>}
9
+ */
10
+ function get (url, parameter) {
11
+ return request(url, METHOD.GET, parameter)
12
+ }
13
+
14
+ /**
15
+ * POST请求
16
+ * @param url 请求地址
17
+ * @param parameter 请求参数
18
+ * @param config
19
+ * @returns {Promise<AxiosResponse<T>>}
20
+ */
21
+ function post (url, parameter, config = {}) {
22
+ return request(url, METHOD.POST, parameter, config)
23
+ }
24
+
25
+ /**
26
+ * POST请求
27
+ * @param url 请求地址
28
+ * @param parameter 请求参数
29
+ * @param serviceName
30
+ * @param isDev
31
+ * @param config
32
+ * @returns {Promise<AxiosResponse<T>>}
33
+ */
34
+ function postByServiceName (url, parameter, serviceName = process.env.VUE_APP_SYSTEM_NAME, isDev, config = {}) {
35
+ if (url.startsWith('api/') || url.startsWith('/api/')) {
36
+ return request(url, METHOD.POST, parameter, config)
37
+ }
38
+ let apiPre = '/api/'
39
+ if (isDev) {
40
+ apiPre = '/devApi/'
41
+ }
42
+ if (!url.startsWith('/')) {
43
+ url = '/' + url
44
+ }
45
+ url = apiPre + serviceName + url
46
+ return request(url, METHOD.POST, parameter, config)
47
+ }
48
+
49
+ /**
50
+ * DELETE请求
51
+ * @param url 请求地址
52
+ * @param parameter 请求参数
53
+ * @param config
54
+ * @returns {Promise<AxiosResponse<T>>}
55
+ */
56
+ function del (url, parameter, config = {}) {
57
+ return request(url, METHOD.DELETE, parameter, config)
58
+ }
59
+
60
+ /**
61
+ * PUT请求
62
+ * @param url 请求地址
63
+ * @param parameter 请求参数
64
+ * @param config
65
+ * @returns {Promise<AxiosResponse<T>>}
66
+ */
67
+ function put (url, parameter, config = {}) {
68
+ return request(url, METHOD.PUT, parameter, config)
69
+ }
70
+
71
+ /**
72
+ * 封装 EventSource 请求函数
73
+ * @param {string} url - 请求地址
74
+ * @param {Object} options - 请求选项(包括 method, headers, body 等)
75
+ * @param {Function} onDataUpdate - 数据更新回调,每次接收到新的数据都会调用
76
+ * @param {Function} onError - 错误处理回调
77
+ * @returns {Function} - 用于取消请求的函数
78
+ */
79
+ function startEventStreamPOST (url, params, headers = {}, onDataUpdate, onError, serviceName = process.env.VUE_APP_SYSTEM_NAME, method = 'GET') {
80
+ return startEventStream(url, params, headers, onDataUpdate, onError, serviceName, 'POST')
81
+ }
82
+ function startEventStream (url, params, headers = {}, onDataUpdate, onError, serviceName = process.env.VUE_APP_SYSTEM_NAME, method = 'GET') {
83
+ // let isStopped = false
84
+ let isStopped = false
85
+ let requestCount = 0
86
+
87
+ if (!(url.startsWith('api/') || url.startsWith('/api/') || url.startsWith('http'))) {
88
+ if (!url.startsWith('/')) {
89
+ url = '/' + url
90
+ }
91
+ url = '/api/' + serviceName + url
92
+ }
93
+
94
+ console.log(`[EventStream] 开始新的请求: ${url}`, {
95
+ params,
96
+ headers,
97
+ method
98
+ })
99
+
100
+ const controller = new AbortController()
101
+ const { signal } = controller
102
+ const token = localStorage.getItem(ACCESS_TOKEN)
103
+ // 增加 V4 登录请求头
104
+ if (token) {
105
+ const compatible = getSystemVersion()
106
+ if (compatible === 'V4') {
107
+ // V4 环境则添加 V4请求头
108
+ headers[V4_ACCESS_TOKEN] = token
109
+ } else {
110
+ headers[ACCESS_TOKEN] = token
111
+ }
112
+ }
113
+ fetch(url, {
114
+ method: method,
115
+ headers: {
116
+ ...headers,
117
+ Accept: 'text/event-stream',
118
+ 'Cache-Control': 'no-cache',
119
+ Connection: 'keep-alive'
120
+ },
121
+ body: JSON.stringify(params),
122
+ signal
123
+ }).then(response => {
124
+ if (!response.ok) {
125
+ throw new Error(`HTTP error! status: ${response.status}`)
126
+ }
127
+
128
+ requestCount++
129
+ console.log(`[EventStream] 请求已打开 (第${requestCount}次): ${url}`, {
130
+ status: response.status,
131
+ statusText: response.statusText
132
+ })
133
+
134
+ const reader = response.body.getReader()
135
+ const decoder = new TextDecoder()
136
+ let buffer = ''
137
+
138
+ function processStream () {
139
+ if (isStopped) {
140
+ console.log(`[EventStream] 请求已停止,停止读取流: ${url}`)
141
+ return
142
+ }
143
+
144
+ reader.read().then(({ done, value }) => {
145
+ if (done) {
146
+ console.log(`[EventStream] 流读取完成: ${url}`)
147
+ return
148
+ }
149
+
150
+ const chunk = decoder.decode(value, { stream: true })
151
+ buffer += chunk
152
+
153
+ // 处理接收到的数据
154
+ const lines = buffer.split('\n')
155
+ buffer = lines.pop() // 保留最后一个不完整的行
156
+
157
+ let currentEvent = null
158
+ let currentData = null
159
+
160
+ for (const line of lines) {
161
+ if (line.startsWith('event: ')) {
162
+ currentEvent = line.slice(7)
163
+ } else if (line.startsWith('data: ')) {
164
+ currentData = line.slice(6)
165
+ try {
166
+ const parsedData = JSON.parse(currentData)
167
+ if (currentEvent === 'additionalInfo') {
168
+ // 触发 additionalInfo 回调
169
+ onDataUpdate?.(parsedData, 'additionalInfo')
170
+ } else {
171
+ // 普通消息数据
172
+ onDataUpdate?.(parsedData, 'sourceInfo')
173
+ }
174
+ } catch (e) {
175
+ if (currentEvent === 'sourceInfo') {
176
+ // 触发 sourceInfo 回调
177
+ onDataUpdate?.(currentData, 'sourceInfo')
178
+ } else {
179
+ onDataUpdate?.(currentData, 'data')
180
+ }
181
+ }
182
+ // 重置当前事件和数据
183
+ currentEvent = null
184
+ currentData = null
185
+ }
186
+ }
187
+
188
+ processStream()
189
+ }).catch(error => {
190
+ console.error(`[EventStream] 流读取错误: ${url}`, error)
191
+ if (!isStopped) {
192
+ isStopped = true
193
+ onError?.(error)
194
+ }
195
+ })
196
+ }
197
+
198
+ processStream()
199
+ }).catch(error => {
200
+ console.error(`[EventStream] 请求错误: ${url}`, error)
201
+ if (!isStopped) {
202
+ isStopped = true
203
+ onError?.(error)
204
+ }
205
+ })
206
+
207
+ // 返回停止函数
208
+ return () => {
209
+ isStopped = true
210
+ controller.abort()
211
+ console.log('连接已关闭')
212
+ }
213
+ }
214
+
215
+ export { get, post, del, put, postByServiceName, startEventStream, startEventStreamPOST }
@@ -1,47 +1,47 @@
1
- import AMapLoader from '@amap/amap-jsapi-loader'
2
- let Amap
3
- async function GetGDMap (secretKey, key) {
4
- if (!Amap) {
5
- window._AMapSecurityConfig = {
6
- securityJsCode: secretKey
7
- }
8
- // 解决高德地图加载报错 ---> 禁止多种API加载方式混用
9
- AMapLoader.reset()
10
- Amap = await AMapLoader.load({
11
- key: key, // 申请好的Web端开发者Key,首次调用 load 时必填
12
- version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
13
- plugins: ['AMap.IndexCluster', 'AMP.MarkerCluster', 'AMap.InfoWindow', 'AMap.HeatMap', 'AMap.HawkEye', 'AMap.DistrictSearch',
14
- 'AMap.ToolBar', 'AMap.Geolocation', 'AMap.MouseTool',
15
- 'AMap.Geocoder', 'AMap.MarkerClusterer', 'AMap.AutoComplete', 'AMap.Scale'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
16
- AMapUI: {
17
- version: '1.1', // AMapUI 缺省 1.1
18
- plugins: ['misc/PositionPicker'] // 需要加载的 AMapUI ui插件
19
- }
20
- })
21
- }
22
- return Amap
23
- }
24
-
25
- async function getGDMap (address) {
26
- new (await GetGDMap()).Geocoder({
27
- radius: 500 // 范围,默认:500
28
- }).getLocation(address, function (status, result) {
29
- if (status === 'complete' && result.geocodes.length) {
30
- return ({ lng: result.geocodes[0].location.lng, lat: result.geocodes[0].location.lat })
31
- } else {
32
- // eslint-disable-next-line prefer-promise-reject-errors
33
- throw new Error('根据经纬度查询地址失败')
34
- }
35
- })
36
- }
37
-
38
- async function GetLocation (address) {
39
- return new Promise((resolve, reject) => {
40
- try {
41
- resolve(getGDMap(address))
42
- } catch (e) {
43
- reject(e)
44
- }
45
- })
46
- }
47
- export { GetGDMap, GetLocation }
1
+ import AMapLoader from '@amap/amap-jsapi-loader'
2
+ let Amap
3
+ async function GetGDMap (secretKey, key) {
4
+ if (!Amap) {
5
+ window._AMapSecurityConfig = {
6
+ securityJsCode: secretKey
7
+ }
8
+ // 解决高德地图加载报错 ---> 禁止多种API加载方式混用
9
+ AMapLoader.reset()
10
+ Amap = await AMapLoader.load({
11
+ key: key, // 申请好的Web端开发者Key,首次调用 load 时必填
12
+ version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
13
+ plugins: ['AMap.IndexCluster', 'AMP.MarkerCluster', 'AMap.InfoWindow', 'AMap.HeatMap', 'AMap.HawkEye', 'AMap.DistrictSearch',
14
+ 'AMap.ToolBar', 'AMap.Geolocation', 'AMap.MouseTool',
15
+ 'AMap.Geocoder', 'AMap.MarkerClusterer', 'AMap.AutoComplete', 'AMap.Scale'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
16
+ AMapUI: {
17
+ version: '1.1', // AMapUI 缺省 1.1
18
+ plugins: ['misc/PositionPicker'] // 需要加载的 AMapUI ui插件
19
+ }
20
+ })
21
+ }
22
+ return Amap
23
+ }
24
+
25
+ async function getGDMap (address) {
26
+ new (await GetGDMap()).Geocoder({
27
+ radius: 500 // 范围,默认:500
28
+ }).getLocation(address, function (status, result) {
29
+ if (status === 'complete' && result.geocodes.length) {
30
+ return ({ lng: result.geocodes[0].location.lng, lat: result.geocodes[0].location.lat })
31
+ } else {
32
+ // eslint-disable-next-line prefer-promise-reject-errors
33
+ throw new Error('根据经纬度查询地址失败')
34
+ }
35
+ })
36
+ }
37
+
38
+ async function GetLocation (address) {
39
+ return new Promise((resolve, reject) => {
40
+ try {
41
+ resolve(getGDMap(address))
42
+ } catch (e) {
43
+ reject(e)
44
+ }
45
+ })
46
+ }
47
+ export { GetGDMap, GetLocation }