-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.js
executable file
·157 lines (148 loc) · 4.82 KB
/
app.js
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
import Local from 'utils/local';
import Util from 'utils/util';
import Events from 'utils/events';
import _ from 'utils/underscore';
import wxService from 'utils/wxService';
import httpService from 'utils/httpService';
App({
version: 'v0.0.0',
onLaunch() {
//读取缓存
let data = this.local.infoSync();
if (data && data.keys.length) {
data.keys.forEach((key) => {
let value = this.local.getSync(key);
if (value) this.cache[key] = value;
});
if (this.cache.version !== this.version) {
this.cache = {};
this.local.clearSync();
this.local.set('version', this.version);
}
this.session_token = this.cache.session_token;
this._user = this.cache.ggInfo;
if (this.session_token) {
this.wxService.checkSession()
.then(() => {
console.log('checkSession success');
},
() => {
this.getUserInfo();
});
} else {
this.getUserInfo();
}
} else {
this.local.set('version', this.version);
this.getUserInfo();
}
},
getUserInfo(scb, ecb) {
this.wxService.login()
.then(res => {
return this.httpService.postUserLogin(res.code)
})
.then(data => {
if (data.code == 0) {
let user = data.result || {};
this.session_token = user.sessionToken;
this.local.set('session_token', user.sessionToken);
this._user = user;
if (user.avatarUrl) {
this.getWxInfo(user);
} else {
this.getWxInfo();
}
typeof scb === 'function' && scb();
} else {
typeof ecb === 'function' && ecb();
}
}, () => {
typeof ecb === 'function' && ecb();
});
},
// 调用 wx.getUserInfo 以及请求后端用户信息
getWxInfo(user, scb, ecb) {
let _this = this;
this.wxService.getUserInfo()
.then((res) => {
// 发请求
let { nickName, gender, language, city, province, country, avatarUrl } = res.userInfo;
let userParams = { nickName, gender, language, city, province, country, avatarUrl };
let gginfo = user || _this._user || {};
_.extend(gginfo, userParams);
_this._user = gginfo;
_this.local.set('ggInfo', gginfo);
typeof scb === 'function' && scb(gginfo);
if (!user) {
let params = {
encryptedData: res.encryptedData,
iv: res.iv
}
this.httpService.postUserInfo(params);
}
}, () => {
typeof ecb === 'function' && ecb();
});
},
// 首页获取用户信息
getWxUserInfo(cb) {
if (this._user) {
typeof cb === 'function' && cb(this._user);
} else {
this.getWxInfo(0, cb);
}
},
// 更新个人信息
updateWxInfo(cb) {
this.getWxInfo(0, (res) => {
typeof cb === 'function' && cb(res);
}, () => {
wx.openSetting()
.then(res => {
this.getWxInfo(0, cb);
});
});
},
// 提示登录后事件处理
loginTo(obj, cb) {
if (this.session_token) {
if (cb && typeof cb == "string") {
this.util.loadpage('new', cb);
} else {
typeof cb == "function" && cb();
}
} else {
let str = '未登录或登录失效,请重新登录';
this.util.showModal(str, 0, '', '知道了', () => {
this.getUserInfo(() => {
this.util.showOkToast('登录成功');
}, () => {
this.util.showTipToast('登录失败,请稍后重试');
});
});
}
},
//设置全局缓存
setCache(key, value) {
if (!key || !value) return;
this.cache[key] = value;
},
//清除全局缓存
removeCache(...keys) {
if (!keys) return;
let _this = this;
keys.forEach((key) => {
_this.cache[key] = '';
});
},
register: new Events().register,
local: new Local,
util: new Util,
wxService: new wxService,
httpService: new httpService,
cache: {},
ggPages: [],
session_token: null,
_user: null
});