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 delSession(): void;
|
||||
declare function main(relatedRequestObj?: IRequestOption | IUploadFileOption): Promise<void>;
|
||||
declare function main(): Promise<void>;
|
||||
declare const _default: {
|
||||
main: typeof main;
|
||||
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",
|
||||
"version": "1.7.3",
|
||||
"version": "1.7.7-beta",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "we-request",
|
||||
"version": "1.7.5",
|
||||
"version": "1.8.0",
|
||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||
"keywords": [
|
||||
"登录态",
|
||||
|
||||
@@ -4,7 +4,6 @@ import { IInitOption } from '../interface'
|
||||
|
||||
export default (params: IInitOption) => {
|
||||
Object.assign(config, params);
|
||||
console.log(config.errorTitle);
|
||||
try {
|
||||
status.session = wx.getStorageSync(config.sessionName!) || '';
|
||||
} catch (e) {}
|
||||
|
||||
@@ -147,6 +147,10 @@ function getGlobalData() {
|
||||
}
|
||||
|
||||
function doRequest(obj: IRequestOption) {
|
||||
// 真正发请求时,再次判断一次是否有登陆态
|
||||
if(!status.session) {
|
||||
return request(obj) as Promise<WechatMiniprogram.RequestSuccessCallbackResult>;
|
||||
}
|
||||
obj = initializeRequestObj(obj);
|
||||
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
||||
obj = config.beforeSend(obj, status.session);
|
||||
@@ -191,6 +195,10 @@ function doRequest(obj: IRequestOption) {
|
||||
}
|
||||
|
||||
function doUploadFile(obj: IUploadFileOption) {
|
||||
// 真正发请求时,再次判断一次是否有登陆态
|
||||
if(!status.session) {
|
||||
return uploadFile(obj) as Promise<WechatMiniprogram.UploadFileSuccessCallbackResult>;
|
||||
}
|
||||
obj = initializeUploadFileObj(obj);
|
||||
if (obj.reLoginCount === 0 && typeof config.beforeSend === "function") {
|
||||
obj = config.beforeSend(obj, status.session);
|
||||
@@ -250,7 +258,7 @@ function request<TResp>(obj: IRequestOption): Promise<TResp> {
|
||||
cacheManager.get(obj);
|
||||
}
|
||||
|
||||
sessionManager.main(obj).then(() => {
|
||||
sessionManager.main().then(() => {
|
||||
return doRequest(obj)
|
||||
}).then((res: WechatMiniprogram.RequestSuccessCallbackResult) => {
|
||||
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)
|
||||
}).then((res: WechatMiniprogram.UploadFileSuccessCallbackResult) => {
|
||||
let response = responseHandler.responseForUploadFile(res, obj);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import status from '../store/status'
|
||||
import config from '../store/config'
|
||||
import errorHandler from './errorHandler'
|
||||
import durationReporter from './durationReporter'
|
||||
import requestHandler from './requestHandler'
|
||||
import loading from '../util/loading'
|
||||
import request from '../api/request'
|
||||
import url from '../util/url'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
import { IErrorObject } from "../interface"
|
||||
|
||||
/* 生命周期内只做一次的checkSession */
|
||||
let checkSessionPromise: any = null;
|
||||
@@ -178,25 +176,11 @@ async function code2Session(code: string) {
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
if (typeof s === 'string') {
|
||||
status.session = 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
|
||||
});
|
||||
if (typeof s === 'string' && s) {
|
||||
setSession(s);
|
||||
return resolve(s);
|
||||
} else {
|
||||
return reject(errorHandler.getErrorMsg(res));
|
||||
return reject({type: "logic-error", res});
|
||||
}
|
||||
} else {
|
||||
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) => {
|
||||
let retry = !relatedRequestObj
|
||||
// 如果没有关联的请求,重试即调用自身
|
||||
? () => main().then(resolve).catch(reject)
|
||||
// 如果有关联的请求,重试即调用所关联的请求
|
||||
: () => request(relatedRequestObj).then(relatedRequestObj._resolve).catch(relatedRequestObj._reject);
|
||||
return checkLogin().then(() => {
|
||||
return config.doNotCheckSession ? Promise.resolve() : checkSession()
|
||||
}, ({title, content}) => {
|
||||
errorHandler.doError(title, content, retry);
|
||||
return reject({title, content});
|
||||
}).then(() => {
|
||||
return resolve();
|
||||
}, ({title, content})=> {
|
||||
errorHandler.doError(title, content, retry);
|
||||
return reject({title, content});
|
||||
}).catch((e: IErrorObject) => {
|
||||
return reject(e);
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user