-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.vue
219 lines (216 loc) · 4.85 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
<script>
const db = uniCloud.database()
import Vue from 'vue'
export default {
async onLaunch() {
this.checkUpdate()
this.autoLogin()
},
onShow: function() {},
onHide: function() {},
watch: {
hasLogin(val) {
if(val) {
this.getBaseInfo()
this.checkMsg()
}
},
baseInfo(newVal, oldVal) {
if(newVal && newVal._id !== oldVal._id) {
this.getBaseInfo()
}
}
},
methods: {
async autoLogin() {
// #ifdef MP-WEIXIN
try{
uni.showLoading({
title: '登录中'
})
const code = await new Promise(resolve => {
uni.login({
provider: 'weixin',
success: ({code}) => {
resolve(code)
},
fail: err => {
this.$u.toast(err)
}
})
})
const { userInfo, token, tokenExpired } = await this.$request('user-center','loginByWeixin', { code })
uni.setStorageSync('uni_id_token', token)
uni.setStorageSync('uni_id_token_expired', tokenExpired)
if(userInfo.gender) {
this.login(userInfo)
}
uni.hideLoading()
}catch(e) {
this.logout()
uni.showToast({
title: e,
icon: 'none'
})
}
// #endif
// #ifndef MP-WEIXIN
if(!uni.getStorageSync('uni_id_token'))
return
try {
uni.showLoading({
title: '登录中'
})
const { token, tokenExpired, userInfo = {} } = await this.$request('user-center','checkToken')
if(token) {
uni.setStorageSync('uni_id_token', token)
uni.setStorageSync('uni_id_token_expired', tokenExpired)
}
this.login(userInfo)
uni.hideLoading()
}catch(e){
console.log(e)
uni.showToast({
title: e,
icon: 'none'
})
this.logout()
}
// #endif
},
async getBaseInfo() {
if(this.userInfo.base_id && this.userInfo.subscribes.includes(this.userInfo.base_id)) {
const baseInfo = uni.getStorageSync('base_info') || {}
this.enter(baseInfo)
try {
const { result: { data = {} } } = await db.collection('bases').doc(this.userInfo.base_id).get({
getOne: true
})
this.enter(data)
}catch(e){
this.exit()
}
}
},
checkMsg() {
db.collection('messages').where(`touid=='${this.userInfo._id}'&&is_read==false`).count().then(res => {
const { total } = res.result
if(total > 0) {
uni.setTabBarBadge({
index: 3,
text: total + ''
})
}
})
},
checkUpdate() {
// #ifdef MP
const updateManager = uni.getUpdateManager();
updateManager.onUpdateReady(function (res) {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
// #endif
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, ({version}) => {
this.$request('check-update', '', {
version
}).then(res => {
if (res.update) {
uni.showModal({
title: '检测到新版本',
content: res.version + '\n' + res.note,
confirmText: '立即更新',
cancelText: '以后再说',
success: ({ confirm }) => {
if (confirm) {
const downloadTask = uni.downloadFile({
url: res.url,
success: (res) => {
if (res.statusCode === 200) {
plus.nativeUI.closeWaiting()
plus.runtime.install(res.tempFilePath, {
force: false
}, (e) => {
uni.showToast({
title: '更新成功',
icon: 'none',
duration: 1000
})
setTimeout(() => {
plus.runtime.restart()
},1000)
}, (e) => {
uni.showToast({
title: e.message,
icon: 'none'
})
})
}
}
})
plus.nativeUI.showWaiting()
}
}
})
}
})
});
// #endif
}
}
}
</script>
<style lang="scss">
/* #ifndef APP-PLUS-NVUE */
@import "colorui/main.css";
@import "colorui/icon.css";
.fullscreen {
width: 100vw;
height: calc(100vh - var(--window-bottom));
}
.cu-form-group .title {
min-width: calc(4em + 15px);
}
.gender {
color: #87CEEB;
}
.gender.female {
color: #FFC0CB;
}
.btn-transparent {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
background: transparent;
border: none;
}
.btn-transparent::after {
border: none;
}
.btn-float {
position: fixed;
right: 0;
bottom: var(--window-bottom);
opacity: .9;
line-height: 80rpx;
}
.cu-form-group.required .title::after {
content: '*';
display: inline-block;
margin-left: 6rpx;
color: #e54d42;
font-size: 34rpx;
}
/* #endif */
</style>