vue2-client 1.16.32 → 1.16.33

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.
@@ -0,0 +1,14 @@
1
+ 发生sendMessage请求
2
+ ```json
3
+ {
4
+ "event": "16", // 进度数据, 取值 0~100
5
+ "userList": [
6
+ "ceshiCRUD-t_f_operator_id-419879409793761282-1-18",// 数据webSocket 唯一标识 (配置名称-字段名称-用户id-页码-数据行id)
7
+ "ceshiCRUD-t_f_type-419879409793761282-1-18",
8
+ "ceshiCRUD-t_f_type-419879409793761282-1-19",
9
+ "ceshiCRUD-t_f_type-419879409793761282-1-20",
10
+ "ceshiCRUD-t_f_operator_id-419879409793761282-1-21",
11
+ "ceshiCRUD-t_f_operator_id-419879409793761282-1-22"
12
+ ]
13
+ }
14
+ ```
@@ -0,0 +1,252 @@
1
+ <template>
2
+ <div class="websocket-progress">
3
+ <a-progress
4
+ :percent="progressValue"
5
+ status="active"
6
+ style="padding-right: 20px;" />
7
+
8
+ <!-- 连接状态指示 -->
9
+ <div class="connection-status">
10
+ <a-tooltip :title="connectionStatusText">
11
+ <a-badge
12
+ :status="connectionBadgeStatus"
13
+ :text="connectionStatusText"
14
+ />
15
+ </a-tooltip>
16
+ </div>
17
+ </div>
18
+ </template>
19
+
20
+ <script>
21
+ import { useWebSocket } from '@vue2-client/utils/websocket/websocket'
22
+
23
+ export default {
24
+ name: 'XWebSocketProgress',
25
+ components: {
26
+ },
27
+ props: {
28
+ // WebSocket 唯一标识
29
+ websocketId: {
30
+ type: String,
31
+ required: true,
32
+ },
33
+ // 初始进度值
34
+ initialValue: {
35
+ type: Number,
36
+ default: 0
37
+ },
38
+ // 自动重连
39
+ autoReconnect: {
40
+ type: Boolean,
41
+ default: true
42
+ },
43
+ // 重连间隔(毫秒)
44
+ reconnectInterval: {
45
+ type: Number,
46
+ default: 3000
47
+ },
48
+ // 最大重连次数
49
+ maxReconnectAttempts: {
50
+ type: Number,
51
+ default: 1
52
+ },
53
+ },
54
+ data () {
55
+ return {
56
+ progressValue: this.initialValue,
57
+ connectionState: {
58
+ isConnected: false,
59
+ reconnectAttempts: 0
60
+ },
61
+ websocketUrl: '',
62
+ }
63
+ },
64
+ computed: {
65
+ // 连接状态文本
66
+ connectionStatusText () {
67
+ if (this.connectionState.isConnected) {
68
+ return '已连接'
69
+ } else if (this.connectionState.reconnectAttempts > 0) {
70
+ return `重连中 (${this.connectionState.reconnectAttempts})`
71
+ } else {
72
+ return '未连接'
73
+ }
74
+ },
75
+
76
+ // 连接状态徽章状态
77
+ connectionBadgeStatus () {
78
+ if (this.connectionState.isConnected) {
79
+ return 'success'
80
+ } else if (this.connectionState.reconnectAttempts > 0) {
81
+ return 'processing'
82
+ } else {
83
+ return 'default'
84
+ }
85
+ }
86
+ },
87
+ mounted () {
88
+ this.initWebSocket()
89
+ },
90
+ beforeDestroy () {
91
+ this.cleanup()
92
+ },
93
+ watch: {
94
+ // 初始值变更时重置当前进度显示
95
+ initialValue (val) {
96
+ const num = parseFloat(val)
97
+ this.progressValue = isNaN(num) ? 0 : Math.max(0, Math.min(100, num))
98
+ },
99
+ // 标识变更时,断开旧连接并按新标识重连
100
+ websocketId (newId, oldId) {
101
+ if (newId === oldId) return
102
+ this.cleanup()
103
+ // 重置进度为新的初始值,确保界面先渲染初始值
104
+ const num = parseFloat(this.initialValue)
105
+ this.progressValue = isNaN(num) ? 0 : Math.max(0, Math.min(100, num))
106
+ // 重新建立连接
107
+ this.$nextTick(() => {
108
+ this.initWebSocket()
109
+ })
110
+ }
111
+ },
112
+ methods: {
113
+ /**
114
+ * 初始化 WebSocket
115
+ */
116
+ initWebSocket () {
117
+ if (!this.websocketId) {
118
+ return
119
+ }
120
+
121
+ this.websocketUrl = `ws:${window.location.host}/socket/af-system/sendMessage?userId=${this.websocketId}`
122
+
123
+ const wsOptions = {
124
+ autoReconnect: this.autoReconnect,
125
+ reconnectInterval: this.reconnectInterval,
126
+ maxReconnectAttempts: this.maxReconnectAttempts
127
+ }
128
+
129
+ // 使用 WebSocket 工具
130
+ const wsManager = useWebSocket(wsOptions)
131
+
132
+ // 保存方法引用
133
+ this.wsConnect = wsManager.connect
134
+ this.wsDisconnect = wsManager.disconnect
135
+ this.wsOn = wsManager.on
136
+ // 绑定事件监听器
137
+ this.setupEventListeners()
138
+
139
+ // 开始连接
140
+ this.wsConnect(this.websocketUrl, wsOptions)
141
+ },
142
+ /**
143
+ * 手动重置为初始值(供父组件可选调用)
144
+ */
145
+ resetToInitial () {
146
+ const num = parseFloat(this.initialValue)
147
+ this.progressValue = isNaN(num) ? 0 : Math.max(0, Math.min(100, num))
148
+ },
149
+
150
+ /**
151
+ * 设置事件监听器
152
+ */
153
+ setupEventListeners () {
154
+ // 连接成功
155
+ this.wsOn('connected', (data) => {
156
+ this.connectionState.isConnected = true
157
+ this.connectionState.reconnectAttempts = 0
158
+ this.$emit('websocket-connected', data)
159
+ })
160
+
161
+ // 连接中
162
+ this.wsOn('connecting', (data) => {
163
+ this.$emit('websocket-connecting', data)
164
+ })
165
+
166
+ // 消息接收
167
+ this.wsOn('message', (data) => {
168
+ this.handleWebSocketMessage(data)
169
+ this.$emit('websocket-message', data)
170
+ })
171
+
172
+ // 连接关闭
173
+ this.wsOn('closed', (data) => {
174
+ this.connectionState.isConnected = false
175
+ this.$emit('websocket-closed', data)
176
+ })
177
+
178
+ // 连接错误
179
+ this.wsOn('error', (data) => {
180
+ this.connectionState.isConnected = false
181
+ this.$emit('websocket-error', data)
182
+ })
183
+
184
+ // 重连中
185
+ this.wsOn('reconnecting', (data) => {
186
+ this.connectionState.reconnectAttempts = data.attempts
187
+ this.$emit('websocket-reconnecting', data)
188
+ })
189
+
190
+ // 重连失败
191
+ this.wsOn('reconnect-failed', (data) => {
192
+ this.connectionState.reconnectAttempts = data.attempts
193
+ this.$emit('websocket-reconnect-failed', data)
194
+ })
195
+
196
+ // 断开连接
197
+ this.wsOn('disconnected', (data) => {
198
+ this.connectionState.isConnected = false
199
+ this.connectionState.reconnectAttempts = 0
200
+ this.$emit('websocket-disconnected', data)
201
+ })
202
+ },
203
+
204
+ /**
205
+ * 处理 WebSocket 消息
206
+ * @param {Object} data 消息数据
207
+ */
208
+ handleWebSocketMessage (data) {
209
+ const messageData = data.data
210
+
211
+ // 更新进度值
212
+ this.updateProgress(parseFloat(messageData))
213
+ },
214
+
215
+ /**
216
+ * 更新进度值
217
+ * @param {number} value 进度值
218
+ */
219
+ updateProgress (value) {
220
+ const numValue = parseFloat(value)
221
+ if (!isNaN(numValue)) {
222
+ this.progressValue = Math.max(0, Math.min(100, numValue))
223
+ this.$emit('progress-updated', {
224
+ value: this.progressValue,
225
+ originalValue: value
226
+ })
227
+ }
228
+ },
229
+
230
+ /**
231
+ * 清理资源
232
+ */
233
+ cleanup () {
234
+ if (this.wsDisconnect) {
235
+ this.wsDisconnect()
236
+ }
237
+ }
238
+ }
239
+ }
240
+ </script>
241
+
242
+ <style scoped lang="less">
243
+ .websocket-progress {
244
+ display: flex;
245
+ align-items: center;
246
+ gap: 8px;
247
+
248
+ .connection-status {
249
+ flex-shrink: 0;
250
+ }
251
+ }
252
+ </style>