-
Notifications
You must be signed in to change notification settings - Fork 12
/
App.vue
287 lines (284 loc) · 7.77 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<script>
import { WebSocketURL } from './utils/helper'
function newspmentStatus() {
let parame = {
topic: 'announcementWindowsUnReadNum',
token: uni.getStorageSync('token')
}
uni.sendSocketMessage({
data: parame
})
}
export default {
data() {
return {
socketTask: null,
// 确保websocket是打开状态
is_open_socket: false,
timer: null
}
},
onLaunch: function () {
console.log('App Launch')
},
onShow: function () {
this.connectSocketInit()
// this.timer = setInterval(this.heartbeat, 5000);
},
onHide: function () {
console.log('App Hide')
},
onLoad() {},
beforeDestroy() {
window.clearInterval(this.timer)
this.closeSocket()
},
methods: {
// 消息的websocket
announcement() {
let param = {
topic: 'announcementWindowsUnReadNum',
token: uni.getStorageSync('token')
}
this.socketTask.send({
data: JSON.stringify(param),
async success() {
console.log('消息发送成功')
}
})
},
// 心跳的websocket
heartbeat() {
let param = {
topic: 'connect',
token: uni.getStorageSync('token'),
data: {
token: window.localStorage.getItem('token')
}
}
this.socketTask.send({
data: JSON.stringify(param),
async success() {
console.log('消息发送成功')
}
})
},
// 获取消息的websocket
connectSocketInit() {
let that = this
let param = {
topic: 'connect',
token: uni.getStorageSync('token'),
data: {
// token: window.localStorage.getItem('token')
token: uni.getStorageSync('token')
}
}
// 创建一个this.socketTask对象【发送、接收、关闭socket都由这个对象操作】
this.socketTask = uni.connectSocket({
// 【非常重要】必须确保你的服务器是成功的,如果是手机测试千万别使用ws://127.0.0.1:9099【特别容易犯的错误】
url: `${WebSocketURL}`, // 线上
// url: 'ws://192.168.0.42:30502/ws', //周金滕
success(data) {
console.log('websocket连接成功')
}
})
this.socketTask.onError((res) => {
console.log('异常抛出')
})
// 消息的发送和接收必须在正常连接打开中,才能发送或接收【否则会失败】
this.socketTask.onOpen((res) => {
console.log('WebSocket连接正常打开中...!')
this.is_open_socket = true
// 注:只有连接正常打开中 ,才能正常成功发送消息
this.socketTask.send({
data: JSON.stringify(param),
async success() {
console.log('消息发送成功')
}
})
this.announcement() // ws 消息
// 注:只有连接正常打开中 ,才能正常收到消息
this.socketTask.onMessage((res) => {
let data = JSON.parse(res.data)
if (data.topic == 'device_identify') {
console.log(data, '11')
that.$store.commit('SET_DEVICEIDENTIFY', data.data)
} else if (data.topic == 'announcementWindowsUnReadNum') {
that.$store.commit('SET_NEWMESSAGE', data.data)
console.log(data.data, '消息')
}
})
})
// 这里仅是事件监听【如果socket关闭了会执行】
this.socketTask.onClose(() => {
console.log('已经被关闭了')
that.connectSocketInit()
})
},
// 关闭websocket【离开这个页面的时候执行关闭】
closeSocket() {
this.socketTask.close({
success(res) {
this.is_open_socket = false
console.log('关闭成功', res)
},
fail(err) {
console.log('关闭失败', err)
}
})
},
clickRequest() {
if (this.is_open_socket) {
// websocket的服务器的原理是:发送一次消息,同时返回一组数据【否则服务器会进去死循环崩溃】
this.socketTask.send({
data: '请求一次发送一次message',
async success() {
console.log('消息发送成功')
}
})
}
}
}
}
</script>
<style lang="scss">
uni-body {
color: $uni-color-form;
}
@import '@/uni_modules/uview-ui/index.scss';
@font-face {
font-family: 'iconfont'; /* Project id 1903545 */
src: url('//at.alicdn.com/t/font_1903545_bov3uaiph8o.woff2?t=1639533677959') format('woff2'),
url('//at.alicdn.com/t/font_1903545_bov3uaiph8o.woff?t=1639533677959') format('woff'),
url('//at.alicdn.com/t/font_1903545_bov3uaiph8o.ttf?t=1639533677959') format('truetype');
}
.mt10 {
margin-top: 20upx;
}
.mt16 {
margin-top: 32upx;
}
.input_class {
height: 48px;
background: #f5f6f8;
border-radius: 12px;
padding: 0 20upx;
font-size: 28upx;
}
.posireave {
position: relative;
width: 100%;
}
/*验证码 start*/
.verification_code {
padding-right: 50%;
box-sizing: border-box;
}
.auth_code {
position: absolute;
top: 15px;
right: 10px;
font-size: 14px;
}
/*验证码 end*/
.mt44 {
margin-top: 44px;
font-weight: 700;
}
.password_area {
margin-top: 16vh;
}
.button_btn {
height: 48px;
line-height: 48px;
background: $uni-color-btn;
border-radius: 16px;
color: $uni-bg-color;
font-size: 14px;
font-weight: bold;
}
.mt24 {
margin-top: 48upx;
}
.view_con {
display: flex;
flex-direction: column;
height: 100vh;
}
.view_main {
flex: 1;
background-color: $uni-bg-color-main;
padding: 32upx;
box-sizing: border-box;
}
/*页面列表样式 start */
.list_main {
padding: 0 32upx;
box-sizing: border-box;
border-radius: 24upx;
overflow: hidden;
background-color: $uni-bg-color;
}
.list_content {
display: flex;
justify-content: space-between;
padding: 36upx 0;
box-sizing: border-box;
border-bottom: 1px solid $uni-color-borders;
font-size: 26upx;
}
.list_content:last-child {
border-bottom: 0;
}
.ml16 {
margin-left: 16px;
}
.ico_right {
color: $uni-color-tbr;
margin-left: 20upx;
}
.contend_left {
color: $uni-color-form;
font-size: 26upx;
}
.contend_right {
color: $uni-text-color-bo;
font-size: 26upx;
}
::v-deep {
.uni-input-placeholder {
font-size: 26upx;
color: $uni-text-color-bo;
}
.ico_right .u-icon__icon {
font-weight: 700 !important;
color: $uni-color-ico !important;
}
.u-navbar--fixed {
padding-top: 44px;
background-color: #fff;
}
}
.Verify_word {
color: $uni-color-form;
}
.dis_lines {
display: flex;
align-items: center;
}
::v-deep {
.seach-input .uni-input-input {
font-size: 28upx;
}
}
.over_space {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.mt88 {
margin-top: 176upx;
}
/*页面列表样式 end */
</style>