fix: checkSession返回失败后没有自动重新登录

This commit is contained in:
TENCENT\ivinwu
2019-03-22 14:47:54 +08:00
parent b2e1787389
commit 9b195301cd
7 changed files with 102 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
<p align="center"><img src="./image/logo.png" alt="weRequest" height="160"/></p> <p align="center"><img src="./image/logo.png" alt="weRequest" height="160"/></p>
<h2 align="center">v1.2.2</h2> <h2 align="center">v1.2.3</h2>
<p align="center"><b>解决繁琐的小程序会话管理,一款自带登录态管理的网络请求组件。</b></p> <p align="center"><b>解决繁琐的小程序会话管理,一款自带登录态管理的网络请求组件。</b></p>

View File

@@ -1,7 +1,9 @@
declare function setSession(session: string): void;
declare function delSession(): void; declare function delSession(): void;
declare function main(): Promise<{}>; declare function main(): Promise<{}>;
declare const _default: { declare const _default: {
main: typeof main; main: typeof main;
setSession: typeof setSession;
delSession: typeof delSession; delSession: typeof delSession;
}; };
export default _default; export default _default;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
{ {
"name": "we-request", "name": "we-request",
"version": "1.2.2", "version": "1.2.3",
"description": "本工具通过拓展小程序的wx.request让开发者通过简单的配置实现自动管理登录态等功能", "description": "本工具通过拓展小程序的wx.request让开发者通过简单的配置实现自动管理登录态等功能",
"keywords": [ "keywords": [
"登录态", "登录态",

View File

@@ -1,5 +1,5 @@
import status from '../store/status' import sessionManager from '../module/sessionManager'
export default (session: string) => { export default (session: string) => {
status.session = session; sessionManager.setSession(session);
} }

View File

@@ -9,7 +9,7 @@ let checkSessionPromise: any = null;
function checkSession() { function checkSession() {
if (!checkSessionPromise) { if (!checkSessionPromise) {
checkSessionPromise = new Promise((resolve) => { checkSessionPromise = new Promise((resolve, reject) => {
console.log("wx.checkSession()"); console.log("wx.checkSession()");
const start = new Date().getTime(); const start = new Date().getTime();
wx.checkSession({ wx.checkSession({
@@ -20,7 +20,11 @@ function checkSession() {
fail() { fail() {
// 登录态过期 // 登录态过期
delSession(); delSession();
return resolve(); return doLogin().then(() => {
return resolve();
}, (res: any)=>{
return reject(res);
});
}, },
complete() { complete() {
const end = new Date().getTime(); const end = new Date().getTime();
@@ -49,6 +53,8 @@ function isSessionExpireOrEmpty() {
function checkLogin() { function checkLogin() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (isSessionExpireOrEmpty()) { if (isSessionExpireOrEmpty()) {
// 没有登陆态不需要再checkSession
config.doNotCheckSession = true;
return doLogin().then(() => { return doLogin().then(() => {
return resolve(); return resolve();
}, (res: any)=>{ }, (res: any)=>{
@@ -106,6 +112,24 @@ function login() {
}) })
} }
function setSession(session: string) {
status.session = session;
// 换回来的session不需要再checkSession
config.doNotCheckSession = true;
// 如果有设置本地session过期时间
if (config.sessionExpireTime && config.sessionExpireKey) {
status.sessionExpire = new Date().getTime() + config.sessionExpireTime;
wx.setStorage({
key: config.sessionExpireKey,
data: String(status.sessionExpire)
})
}
wx.setStorage({
key: config.sessionName as string,
data: status.session
});
}
function code2Session(code: string) { function code2Session(code: string) {
let data: any; let data: any;
// codeToSession.data支持函数 // codeToSession.data支持函数
@@ -173,8 +197,14 @@ function code2Session(code: string) {
function delSession() { function delSession() {
status.session = ''; status.session = '';
wx.removeStorage({ wx.removeStorage({
key: config.sessionName key: config.sessionName as string
}) });
if (config.sessionExpireTime && config.sessionExpireKey) {
status.sessionExpire = Infinity;
wx.removeStorage({
key: config.sessionExpireKey
})
}
} }
function main() { function main() {
@@ -186,11 +216,15 @@ function main() {
return reject({title, content}); return reject({title, content});
}).then(() => { }).then(() => {
return resolve(); return resolve();
}, ({title, content})=> {
errorHandler.doError(title, content);
return reject({title, content});
}) })
}) })
} }
export default { export default {
main, main,
setSession,
delSession delSession
} }