forked from raul-sauco/minihiker-weapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
119 lines (109 loc) · 3.99 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
//app.js
const ProgramProvider = require('./helpers/programProvider.js');
const AccountInfoProvider = require('./helpers/accountInfoProvider.js');
const Logger = require('./helpers/logger.js');
const ENV = 'dev';
App({
onLaunch: function () {
this.globalData.accessToken = wx.getStorageSync('accessToken');
this.globalData.logger = new Logger(this.globalData.url, ENV);
if (this.globalData.accessToken) {
this.globalData.logger.setAccessToken(this.globalData.accessToken);
}
// Get an instance of AccountInfoProvider
this.globalData.accountInfoProvider = new AccountInfoProvider();
this.globalData.accountInfoProvider.setApiUrl(this.globalData.url);
this.globalData.accountInfoProvider.setLogger(this.globalData.logger);
if (this.globalData.accessToken) {
this.globalData.accountInfoProvider.setAccessToken(this.globalData.accessToken);
}
// Get an instance of ProgramProvider
this.globalData.programProvider = new ProgramProvider();
this.globalData.programProvider.setApiUrl(this.globalData.url);
if (this.globalData.accessToken) {
this.globalData.programProvider.setAccessToken(this.globalData.accessToken);
}
// Check if the user session is still valid
wx.checkSession({
success: res => {
// Check if the accountInfoProvider needs to fetch an auth_token
if (!this.globalData.accessToken) {
console.debug('User session is valid but no accessToken stored, requesting wx.login()');
this.requestLogin();
}
},
fail: res => {
// WX user session has expired, we need to renew the session_key
console.log('User session has expired, calling wx.login()');
this.requestLogin();
}
});
},
/**
* Request user login against the wx backend. We will obtain a
* temporary JS code that can be sent to the minihiker.com backend
* to obtain permanent user openid and temporary session_key.
*/
requestLogin: function () {
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
// Authenticate the user on the backend
this.loginUser(res.code);
},
fail: err => {
console.error('wx.login failed', err);
}
});
},
/** Send login information to the backend to authenticate user. */
loginUser: function (code) {
wx.request({
url: this.globalData.url + 'wx-auth',
method: 'POST',
data: { jsCode:code },
header: {'Content-Type':'application/json'},
success: res => {
this.setAccessToken(res.data.access_token);
// YHT
wx.setStorageSync('userId', res.data.user_id);
},
fail: err => { console.warn('Login user error', err); }
});
},
/** Set globally the access token */
setAccessToken: function (accessToken) {
wx.setStorage({
key: 'accessToken',
data: accessToken,
success: () => { console.debug('Saved user access token to storage.') },
fail: () => { console.error('Failed to save user access token to storage.') }
});
this.globalData.accessToken = accessToken;
this.globalData.programProvider.setAccessToken(accessToken);
this.globalData.accountInfoProvider.setAccessToken(accessToken);
this.globalData.logger.setAccessToken(accessToken);
},
/**
* @deprecated, clients should switch to calling log directly on the
* logger instance.
*/
log: function (data) {
console.warn('Calling deprecated method app.log(), please update your code to use logger.log()');
this.globalData.logger.log(data);
},
globalData: {
accessToken: null,
logger: null,
programProvider: null,
accountInfoProvider: null,
payments: null,
url: 'https://wxapiv1.minihiker.com/',
resUrl: 'https://static.minihiker.com/',
staticUrl: 'https://static.minihiker.com/',
webviewUrl: 'https://backend.minihiker.com/',
// url: 'http://wxapi.mh/',
// resUrl: 'http://static.mh/',
// staticUrl: 'http://static.mh/'
}
})