2018-12-19 19:26:38 +08:00
|
|
|
|
import status from '../store/status'
|
|
|
|
|
|
import config from '../store/config'
|
|
|
|
|
|
import errorHandler from './errorHandler'
|
|
|
|
|
|
import durationReporter from './durationReporter'
|
2019-01-10 22:46:22 +08:00
|
|
|
|
import requestHandler from './requestHandler'
|
2018-12-19 19:26:38 +08:00
|
|
|
|
|
2019-01-11 22:27:54 +08:00
|
|
|
|
/* 生命周期内只做一次的checkSession */
|
2019-01-10 22:46:22 +08:00
|
|
|
|
let checkSessionPromise: any = null;
|
2019-01-24 10:26:51 +08:00
|
|
|
|
|
2018-12-28 17:33:30 +08:00
|
|
|
|
function checkSession() {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
if (!checkSessionPromise) {
|
2019-01-24 10:26:51 +08:00
|
|
|
|
checkSessionPromise = new Promise((resolve) => {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
console.log("wx.checkSession()");
|
|
|
|
|
|
const start = new Date().getTime();
|
|
|
|
|
|
wx.checkSession({
|
|
|
|
|
|
success() {
|
|
|
|
|
|
// 登录态有效,且在本生命周期内无须再检验了
|
|
|
|
|
|
return resolve();
|
|
|
|
|
|
},
|
|
|
|
|
|
fail() {
|
|
|
|
|
|
// 登录态过期
|
2019-01-24 10:26:51 +08:00
|
|
|
|
delSession();
|
|
|
|
|
|
return resolve();
|
2019-01-11 22:27:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
complete() {
|
|
|
|
|
|
const end = new Date().getTime();
|
|
|
|
|
|
durationReporter.report('wx_checkSession', start, end);
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2019-01-10 22:46:22 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return checkSessionPromise;
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 22:27:54 +08:00
|
|
|
|
/* 判断session是否为空或已过期 */
|
|
|
|
|
|
function isSessionExpireOrEmpty() {
|
|
|
|
|
|
if (!status.session) {
|
|
|
|
|
|
// 如果缓存中没有session
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2019-01-24 10:26:51 +08:00
|
|
|
|
if (config.sessionExpireTime && new Date().getTime() > status.sessionExpire) {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
// 如果有设置本地session缓存时间,且缓存时间已到
|
|
|
|
|
|
delSession();
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-24 10:26:51 +08:00
|
|
|
|
function checkLogin() {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
if (isSessionExpireOrEmpty()) {
|
|
|
|
|
|
return doLogin().then(() => {
|
|
|
|
|
|
return resolve();
|
|
|
|
|
|
}, (res: any)=>{
|
|
|
|
|
|
return reject(res);
|
2019-01-11 22:27:54 +08:00
|
|
|
|
})
|
2018-12-19 19:26:38 +08:00
|
|
|
|
} else {
|
2019-01-24 10:26:51 +08:00
|
|
|
|
// 缓存中有session且未过期
|
|
|
|
|
|
return resolve();
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
2019-01-24 10:26:51 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 登陆流程的promise */
|
|
|
|
|
|
let loginPromise: any = null;
|
|
|
|
|
|
|
|
|
|
|
|
function doLogin() {
|
|
|
|
|
|
if (!loginPromise) {
|
|
|
|
|
|
loginPromise = new Promise((resolve, reject) => {
|
|
|
|
|
|
login().then(() => {
|
|
|
|
|
|
loginPromise = null;
|
|
|
|
|
|
return resolve();
|
|
|
|
|
|
}).catch((res) => {
|
|
|
|
|
|
loginPromise = null;
|
|
|
|
|
|
return reject(res);
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2018-12-28 17:33:30 +08:00
|
|
|
|
}
|
2019-01-24 10:26:51 +08:00
|
|
|
|
return loginPromise;
|
2018-12-28 17:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-24 10:26:51 +08:00
|
|
|
|
function login() {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
console.log('wx.login');
|
|
|
|
|
|
const start = new Date().getTime();
|
|
|
|
|
|
wx.login({
|
|
|
|
|
|
success(res) {
|
|
|
|
|
|
if (res.code) {
|
|
|
|
|
|
code2Session(res.code).then(() => {
|
|
|
|
|
|
return resolve();
|
2019-01-24 10:26:51 +08:00
|
|
|
|
}).catch((res) => {
|
|
|
|
|
|
return reject(res);
|
2019-01-11 22:27:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return reject({title: "登录失败", "content": "请稍后重试[code 获取失败]"});
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
complete() {
|
|
|
|
|
|
const end = new Date().getTime();
|
|
|
|
|
|
durationReporter.report('wx_login', start, end);
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(res) {
|
|
|
|
|
|
return reject({title: "登录失败", "content": res.errMsg});
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
2019-01-11 22:27:54 +08:00
|
|
|
|
})
|
2018-12-28 17:33:30 +08:00
|
|
|
|
})
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 19:32:53 +08:00
|
|
|
|
function code2Session(code: string) {
|
2018-12-28 17:33:30 +08:00
|
|
|
|
let data: any;
|
2018-12-19 19:26:38 +08:00
|
|
|
|
// codeToSession.data支持函数
|
|
|
|
|
|
if (typeof config.codeToSession.data === "function") {
|
|
|
|
|
|
data = config.codeToSession.data();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data = config.codeToSession.data || {};
|
|
|
|
|
|
}
|
2018-12-20 21:21:58 +08:00
|
|
|
|
data[config.codeToSession.codeName!] = code;
|
2018-12-19 19:26:38 +08:00
|
|
|
|
|
2019-01-11 22:27:54 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-10 22:46:22 +08:00
|
|
|
|
let start = new Date().getTime();
|
|
|
|
|
|
wx.request({
|
|
|
|
|
|
url: requestHandler.format(config.codeToSession.url),
|
2019-01-07 19:32:53 +08:00
|
|
|
|
data,
|
2018-12-28 17:33:30 +08:00
|
|
|
|
method: config.codeToSession.method || 'GET',
|
2019-01-11 22:27:54 +08:00
|
|
|
|
success(res: wx.RequestSuccessCallbackResult) {
|
2019-01-10 22:46:22 +08:00
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
|
// 耗时上报
|
2019-01-11 22:27:54 +08:00
|
|
|
|
if (config.codeToSession.report) {
|
2019-01-10 22:46:22 +08:00
|
|
|
|
let end = new Date().getTime();
|
|
|
|
|
|
durationReporter.report(config.codeToSession.report, start, end)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let s = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
s = config.codeToSession.success(res.data);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
|
|
status.session = s;
|
2019-01-11 22:27:54 +08:00
|
|
|
|
// 换回来的session,不需要再checkSession
|
|
|
|
|
|
config.doNotCheckSession = true;
|
2019-01-10 22:46:22 +08:00
|
|
|
|
// 如果有设置本地session过期时间
|
2019-01-24 10:26:51 +08:00
|
|
|
|
if (config.sessionExpireTime) {
|
|
|
|
|
|
status.sessionExpire = new Date().getTime() + config.sessionExpireTime;
|
2019-01-10 22:46:22 +08:00
|
|
|
|
wx.setStorage({
|
|
|
|
|
|
key: config.sessionExpireKey,
|
|
|
|
|
|
data: String(status.sessionExpire)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
wx.setStorage({
|
|
|
|
|
|
key: config.sessionName,
|
|
|
|
|
|
data: status.session
|
|
|
|
|
|
});
|
2019-01-11 22:27:54 +08:00
|
|
|
|
return resolve();
|
2019-01-10 22:46:22 +08:00
|
|
|
|
} else {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
return reject(errorHandler.getErrorMsg(res));
|
2019-01-10 22:46:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2019-01-11 22:27:54 +08:00
|
|
|
|
return reject({title: "登录失败", "content": "请稍后重试"});
|
2018-12-28 17:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2019-01-11 22:27:54 +08:00
|
|
|
|
complete() {
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: () => {
|
|
|
|
|
|
return reject({title: "登录失败", "content": "请稍后重试"});
|
2019-01-10 22:46:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2018-12-19 19:26:38 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 22:27:54 +08:00
|
|
|
|
/* 清空session */
|
|
|
|
|
|
function delSession() {
|
|
|
|
|
|
status.session = '';
|
|
|
|
|
|
wx.removeStorage({
|
|
|
|
|
|
key: config.sessionName
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-24 10:26:51 +08:00
|
|
|
|
function main() {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
return checkLogin().then(() => {
|
|
|
|
|
|
return config.doNotCheckSession ? Promise.resolve() : checkSession()
|
|
|
|
|
|
}, ({title, content}) => {
|
|
|
|
|
|
errorHandler.doError(title, content);
|
|
|
|
|
|
return reject({title, content});
|
|
|
|
|
|
}).then(() => {
|
|
|
|
|
|
return resolve();
|
2019-01-11 22:27:54 +08:00
|
|
|
|
})
|
2019-01-24 10:26:51 +08:00
|
|
|
|
})
|
2019-01-11 22:27:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
main,
|
|
|
|
|
|
delSession
|
2018-12-28 17:33:30 +08:00
|
|
|
|
}
|