tui-network-alert.vue
2.43 KB
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
<template>
<view class="tui-network__alert"
:style="{padding:padding,background:background,marginTop:marginTop+'rpx',marginBottom:marginBottom+'rpx'}"
v-if="!isConnected && visible" @tap.stop="onTap">
<slot></slot>
<text class="tui-network__text" :style="{color:color,fontSize:size+'rpx'}">{{text}}</text>
<slot name="right"></slot>
</view>
</template>
<script>
let listener;
export default {
name: "tui-network-alert",
emits: ['click', 'change'],
props: {
text: {
type: String,
default: '当前网络不可用,请检查你的网络设置'
},
background: {
type: String,
default: '#fee3e6'
},
size: {
type: [Number, String],
default: 28
},
color: {
type: String,
default: '#f74d54'
},
padding: {
type: String,
default: '24rpx 30rpx'
},
visible: {
type: Boolean,
default: true
},
marginTop: {
type: [Number, String],
default: 0
},
marginBottom: {
type: [Number, String],
default: 0
}
},
data() {
return {
isConnected: true,
networkType: ''
};
},
watch: {
networkType(newValue, oldValue) {
this.$emit('change', {
isConnected: newValue,
networkType: this.networkType
})
}
},
created() {
listener = (res) => {
this.networkType = res.networkType;
// #ifdef MP-TOUTIAO
this.isConnected = res.networkType != 'none'
// #endif
// #ifndef MP-TOUTIAO
this.isConnected = res.isConnected;
// #endif
}
listener && uni.onNetworkStatusChange(listener);
},
mounted() {
uni.getNetworkType({
success: (res) => {
// console.log(res)
this.networkType = res.networkType;
this.isConnected = res.networkType != 'none'
}
})
},
// #ifndef VUE3
beforeDestroy() {
// #ifdef APP || MP-WEIXIN || H5 || MP-KUAISHOU
listener && uni.offNetworkStatusChange(listener)
// #endif
},
// #endif
// #ifdef VUE3
beforeUnmount() {
// #ifdef APP || MP-WEIXIN || H5 || MP-KUAISHOU
listener && uni.offNetworkStatusChange(listener)
// #endif
},
// #endif
methods: {
onTap() {
this.$emit('click')
}
}
}
</script>
<style>
.tui-network__alert {
/* #ifndef APP-NVUE */
width: 100%;
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
box-sizing: border-box;
}
.tui-network__text {
flex: 1;
/* #ifndef APP-NVUE */
word-break: break-word;
box-sizing: border-box;
/* #endif */
}
</style>