forked from dongweiming/wechat-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwechat.js
More file actions
66 lines (61 loc) · 2.01 KB
/
wechat.js
File metadata and controls
66 lines (61 loc) · 2.01 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
import Vue from 'vue'
function checkStatus(res, options = {}) {
let { r, msg } = res.data;
let type, message;
if (r !== 0) {
type = 'error';
message = msg;
} else {
type = 'success';
message = '提交成功';
}
this.$message({
message: message,
type: type
});
}
function eventSourceListener() {
let source = new EventSource('/stream');
let self = this;
source.addEventListener('login', function(event) {
let data = JSON.parse(event.data);
if (data.type == 'scan_qr_code') {
self.uuid = data.uuid;
self.qrCode = `data:image/png;base64,${data.extra}`;
} else if (data.type == 'confirm_login') {
self.sub_title = 'Scan successful';
self.sub_desc = 'Confirm login on mobile WeChat';
self.qrCode = data.extra;
} else if (data.type == 'logged_in') {
sessionStorage.setItem('user', JSON.stringify(data.user));
self.$router.push({ path: '/main' });
} else if (data.type == 'logged_out') {
sessionStorage.removeItem('user');
self.$router.push('/login');
}
}, false);
source.addEventListener('notification', function(event) {
let data = JSON.parse(event.data);
self.notificationCount = data.count;
}, false);
source.addEventListener('error', function(event) {
console.log("Failed to connect to event stream");
}, false);
}
export default {
install(Vue, defaultOptions = {}) {
Vue.mixin({
data: function() {
return {
uuid: '',
qrCode: `/static/img/qr_code.gif`,
sub_title: 'Scan to log in to WeChat',
sub_desc: 'Log in on phone to use WeChat on Web',
notificationCount: 0
}
}
}),
Vue.prototype.$checkStatus = checkStatus;
Vue.prototype.$eventSourceListener = eventSourceListener;
}
}