-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (166 loc) · 4.39 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
158
159
160
161
162
163
164
165
166
//app.js
App({
onLaunch: function () {
var _this = this;
//读取缓存
try {
var data = wx.getStorageInfoSync();
if (data && data.keys.length) {
data.keys.forEach(function (key) {
var value = wx.getStorageSync(key);
if (value) {
_this.cache[key] = value;
}
});
_this.parseData(_this.cache);
}
} catch (e) {
console.warn('获取缓存失败');
}
// 登陆检查
if (!_this.isLogin) {
_this.login();
}
},
parseData: function(data) {
this.user.avatarUrl = data.avatarUrl || '';
this.user.userToken = data.userToken || '';
this.user.hasRegister = data.hasRegister || false;
this.isLogin = data.isLogin || false;
},
//保存缓存
saveCache: function (key, value) {
if (!key || !value) { return; }
var _this = this;
_this.cache[key] = value;
wx.setStorage({
key: key,
data: value
});
},
//清除缓存
removeCache: function (key) {
if (!key) { return; }
var _this = this;
_this.cache[key] = '';
wx.removeStorage({
key: key
});
},
removeAllCache: function () {
var _this = this;
wx.clearStorage();
_this.cache = {};
_this.user = {};
_this.isLogin = false;
},
setUserInfo: function (e) {
this.saveCache('avatarUrl', e.detail.userInfo.avatarUrl);
this.saveCache('nickName', e.detail.userInfo.nickName);
},
handleResult: function(res) {
var _this = this;
if (res.data && res.statusCode === 200) {
if (res.data.code == '0000') {
return res.data.data;
} if (res.data.code == 'B001') {
// "未登陆/登陆过期"
console.log("not login.....")
_this.login();
} else {
console.log(res);
wx.showModal({
content: res.data.message
})
}
} else {
console.log(res);
wx.showModal({
content: '网络繁忙,请稍后重试'
})
}
return null;
},
login: function() {
var _this = this;
var result = false;
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
url: _this.apiHost + "/user/login",
method: 'POST',
data: {
"wxCode": res.code
},
success: function (res) {
// console.log(res);
var result = _this.handleResult(res);
if (result) {
_this.saveCache('userToken', result.userToken);
_this.saveCache('hasRegister', result.hasRegister);
_this.saveCache('isLogin', true);
_this.user.userToken = result.userToken;
_this.user.hasRegister = result.hasRegister;
_this.isLogin = true;
if (_this.loginReadyCallback) {
_this.loginReadyCallback(result);
}
}
},
fail: function (res) {
console.warn(res);
}
});
}
})
}
});
return result;
},
register: function (userName, avatarUrl) {
var _this = this;
wx.request({
url: _this.apiHost + "/user/register",
method: 'POST',
data: {
"userToken": _this.user.userToken,
"userName": userName,
"avatarUrl": avatarUrl
},
success: function (res) {
console.log(res);
var result = _this.handleResult(res);
if (result) {
_this.saveCache('hasRegister', true);
_this.user.hasRegister = true;
// callback
if (_this.registerCallback) {
_this.registerCallback(result);
}
}
}
});
},
globalData: {},
user:{
avatarUrl: null,
userToken: null,
hasRegister:false
},
cache:{
avatarUrl:null,
nickName:null,
userToken:null,
hasRegister: false,
isLogin:false
},
isLogin: false,
// apiHost:'http://localhost:8080',
apiHost: 'https://wk.iujs.cn',
util: require('./utils/util')
})