Fix missing sid and jscode (#85)
* fix: 修复同时缺失sid和jscode的部分场景;优化登录过程失败的处理逻辑 * 1.7.7-beta * build: 1.8.0 ---------
This commit is contained in:
3
build/module/sessionManager.d.ts
vendored
3
build/module/sessionManager.d.ts
vendored
@@ -1,7 +1,6 @@
|
|||||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
|
||||||
declare function setSession(session: string): void;
|
declare function setSession(session: string): void;
|
||||||
declare function delSession(): void;
|
declare function delSession(): void;
|
||||||
declare function main(relatedRequestObj?: IRequestOption | IUploadFileOption): Promise<void>;
|
declare function main(): Promise<void>;
|
||||||
declare const _default: {
|
declare const _default: {
|
||||||
main: typeof main;
|
main: typeof main;
|
||||||
setSession: typeof setSession;
|
setSession: typeof setSession;
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
4
build/weRequest.min.js
vendored
4
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "we-request",
|
"name": "we-request",
|
||||||
"version": "1.7.3",
|
"version": "1.7.7-beta",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "we-request",
|
"name": "we-request",
|
||||||
"version": "1.7.5",
|
"version": "1.8.0",
|
||||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"登录态",
|
"登录态",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { IInitOption } from '../interface'
|
|||||||
|
|
||||||
export default (params: IInitOption) => {
|
export default (params: IInitOption) => {
|
||||||
Object.assign(config, params);
|
Object.assign(config, params);
|
||||||
console.log(config.errorTitle);
|
|
||||||
try {
|
try {
|
||||||
status.session = wx.getStorageSync(config.sessionName!) || '';
|
status.session = wx.getStorageSync(config.sessionName!) || '';
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|||||||
@@ -147,6 +147,10 @@ function getGlobalData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function doRequest(obj: IRequestOption) {
|
function doRequest(obj: IRequestOption) {
|
||||||
|
// 真正发请求时,再次判断一次是否有登陆态
|
||||||
|
if(!status.session) {
|
||||||
|
return request(obj) as Promise<WechatMiniprogram.RequestSuccessCallbackResult>;
|
||||||
|
}
|
||||||
obj = initializeRequestObj(obj);
|
obj = initializeRequestObj(obj);
|
||||||
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
||||||
obj = config.beforeSend(obj, status.session);
|
obj = config.beforeSend(obj, status.session);
|
||||||
@@ -191,6 +195,10 @@ function doRequest(obj: IRequestOption) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function doUploadFile(obj: IUploadFileOption) {
|
function doUploadFile(obj: IUploadFileOption) {
|
||||||
|
// 真正发请求时,再次判断一次是否有登陆态
|
||||||
|
if(!status.session) {
|
||||||
|
return uploadFile(obj) as Promise<WechatMiniprogram.UploadFileSuccessCallbackResult>;
|
||||||
|
}
|
||||||
obj = initializeUploadFileObj(obj);
|
obj = initializeUploadFileObj(obj);
|
||||||
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
||||||
obj = config.beforeSend(obj, status.session);
|
obj = config.beforeSend(obj, status.session);
|
||||||
@@ -250,7 +258,7 @@ function request<TResp>(obj: IRequestOption): Promise<TResp> {
|
|||||||
cacheManager.get(obj);
|
cacheManager.get(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionManager.main(obj).then(() => {
|
sessionManager.main().then(() => {
|
||||||
return doRequest(obj)
|
return doRequest(obj)
|
||||||
}).then((res: WechatMiniprogram.RequestSuccessCallbackResult) => {
|
}).then((res: WechatMiniprogram.RequestSuccessCallbackResult) => {
|
||||||
let response = responseHandler.responseForRequest(res, obj);
|
let response = responseHandler.responseForRequest(res, obj);
|
||||||
@@ -275,7 +283,7 @@ function uploadFile(obj: IUploadFileOption): any {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionManager.main(obj).then(() => {
|
sessionManager.main().then(() => {
|
||||||
return doUploadFile(obj)
|
return doUploadFile(obj)
|
||||||
}).then((res: WechatMiniprogram.UploadFileSuccessCallbackResult) => {
|
}).then((res: WechatMiniprogram.UploadFileSuccessCallbackResult) => {
|
||||||
let response = responseHandler.responseForUploadFile(res, obj);
|
let response = responseHandler.responseForUploadFile(res, obj);
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import status from '../store/status'
|
import status from '../store/status'
|
||||||
import config from '../store/config'
|
import config from '../store/config'
|
||||||
import errorHandler from './errorHandler'
|
|
||||||
import durationReporter from './durationReporter'
|
import durationReporter from './durationReporter'
|
||||||
import requestHandler from './requestHandler'
|
import requestHandler from './requestHandler'
|
||||||
import loading from '../util/loading'
|
import loading from '../util/loading'
|
||||||
import request from '../api/request'
|
|
||||||
import url from '../util/url'
|
import url from '../util/url'
|
||||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
import { IErrorObject } from "../interface"
|
||||||
|
|
||||||
/* 生命周期内只做一次的checkSession */
|
/* 生命周期内只做一次的checkSession */
|
||||||
let checkSessionPromise: any = null;
|
let checkSessionPromise: any = null;
|
||||||
@@ -178,25 +176,11 @@ async function code2Session(code: string) {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof s === 'string') {
|
if (typeof s === 'string' && s) {
|
||||||
status.session = s;
|
setSession(s);
|
||||||
// 换回来的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,
|
|
||||||
data: status.session
|
|
||||||
});
|
|
||||||
return resolve(s);
|
return resolve(s);
|
||||||
} else {
|
} else {
|
||||||
return reject(errorHandler.getErrorMsg(res));
|
return reject({type: "logic-error", res});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return reject({type: "http-error", res});
|
return reject({type: "http-error", res});
|
||||||
@@ -230,23 +214,14 @@ function delSession() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main(relatedRequestObj?: IRequestOption | IUploadFileOption) {
|
function main() {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
let retry = !relatedRequestObj
|
|
||||||
// 如果没有关联的请求,重试即调用自身
|
|
||||||
? () => main().then(resolve).catch(reject)
|
|
||||||
// 如果有关联的请求,重试即调用所关联的请求
|
|
||||||
: () => request(relatedRequestObj).then(relatedRequestObj._resolve).catch(relatedRequestObj._reject);
|
|
||||||
return checkLogin().then(() => {
|
return checkLogin().then(() => {
|
||||||
return config.doNotCheckSession ? Promise.resolve() : checkSession()
|
return config.doNotCheckSession ? Promise.resolve() : checkSession()
|
||||||
}, ({title, content}) => {
|
|
||||||
errorHandler.doError(title, content, retry);
|
|
||||||
return reject({title, content});
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return resolve();
|
return resolve();
|
||||||
}, ({title, content})=> {
|
}).catch((e: IErrorObject) => {
|
||||||
errorHandler.doError(title, content, retry);
|
return reject(e);
|
||||||
return reject({title, content});
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user