fix(备份域名): 修复code2session的备份域名逻辑,支持配置多个备份域名列表 (#73)
This commit is contained in:
2
build/interface.d.ts
vendored
2
build/interface.d.ts
vendored
@@ -24,7 +24,7 @@ export interface IInitOption {
|
||||
errorHandler?: Function | null;
|
||||
beforeSend?: Function | null;
|
||||
systemErrorHandler?: Function | null;
|
||||
backupDomain?: string;
|
||||
backupDomainList?: IAnyObject;
|
||||
backupDomainEnableCallback?: Function;
|
||||
}
|
||||
export interface ICodeToSessionOptions {
|
||||
|
||||
2
build/module/requestHandler.d.ts
vendored
2
build/module/requestHandler.d.ts
vendored
@@ -2,9 +2,11 @@ import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function format(originUrl: string): string;
|
||||
declare function request<TResp>(obj: IRequestOption): Promise<TResp>;
|
||||
declare function uploadFile(obj: IUploadFileOption): any;
|
||||
declare function enableBackupDomain(url?: string): void;
|
||||
declare const _default: {
|
||||
format: typeof format;
|
||||
request: typeof request;
|
||||
uploadFile: typeof uploadFile;
|
||||
enableBackupDomain: typeof enableBackupDomain;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
4
build/util/url.d.ts
vendored
4
build/util/url.d.ts
vendored
@@ -1,7 +1,9 @@
|
||||
declare function setParams(url: string | undefined, params: object): string;
|
||||
declare function replaceDomain(url?: string, domain?: string): string;
|
||||
declare function replaceDomain(url?: string): string;
|
||||
declare function isInBackupDomainList(url?: string): boolean;
|
||||
declare const _default: {
|
||||
setParams: typeof setParams;
|
||||
replaceDomain: typeof replaceDomain;
|
||||
isInBackupDomainList: typeof isInBackupDomainList;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
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
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "we-request",
|
||||
"version": "1.7.0",
|
||||
"version": "1.7.1",
|
||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||
"keywords": [
|
||||
"登录态",
|
||||
|
||||
@@ -57,7 +57,7 @@ export interface IInitOption {
|
||||
/* 自定义系统错误处理函数 */
|
||||
systemErrorHandler?: Function | null;
|
||||
/* 备用域名 */
|
||||
backupDomain?: string;
|
||||
backupDomainList?: IAnyObject;
|
||||
/* 备用域名启用时回调函数 */
|
||||
backupDomainEnableCallback?: Function;
|
||||
}
|
||||
|
||||
@@ -88,9 +88,7 @@ function initializeRequestObj(obj: IRequestOption) {
|
||||
}
|
||||
|
||||
// 备用域名逻辑
|
||||
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||
}
|
||||
obj.url = url.replaceDomain(obj.url);
|
||||
|
||||
durationReporter.start(obj);
|
||||
|
||||
@@ -131,9 +129,7 @@ function initializeUploadFileObj(obj: IUploadFileOption) {
|
||||
}
|
||||
|
||||
// 备用域名逻辑
|
||||
if (status.isEnableBackupDomain && config.backupDomain) {
|
||||
obj.url = url.replaceDomain(obj.url, config.backupDomain);
|
||||
}
|
||||
obj.url = url.replaceDomain(obj.url);
|
||||
|
||||
durationReporter.start(obj);
|
||||
|
||||
@@ -163,9 +159,9 @@ function doRequest(obj: IRequestOption) {
|
||||
},
|
||||
fail(res) {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && config.backupDomain && obj.url.indexOf(config.backupDomain) < 0) {
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && url.isInBackupDomainList(obj.url)) {
|
||||
// 开启备份域名
|
||||
enableBackupDomain();
|
||||
enableBackupDomain(obj.url);
|
||||
// 重试一次
|
||||
return doRequest(obj).then((res)=> resolve(res));
|
||||
}
|
||||
@@ -196,9 +192,9 @@ function doUploadFile(obj: IUploadFileOption) {
|
||||
},
|
||||
fail(res) {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && config.backupDomain && obj.url.indexOf(config.backupDomain) < 0) {
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && url.isInBackupDomainList(obj.url)) {
|
||||
// 开启备份域名
|
||||
enableBackupDomain();
|
||||
enableBackupDomain(obj.url);
|
||||
// 重试一次
|
||||
return doUploadFile(obj).then((res)=> resolve(res));
|
||||
}
|
||||
@@ -270,11 +266,11 @@ function uploadFile(obj: IUploadFileOption): any {
|
||||
})
|
||||
}
|
||||
|
||||
function enableBackupDomain() {
|
||||
function enableBackupDomain(url: string = "") {
|
||||
if (!status.isEnableBackupDomain) {
|
||||
status.isEnableBackupDomain = true;
|
||||
if (typeof config.backupDomainEnableCallback === 'function') {
|
||||
config.backupDomainEnableCallback();
|
||||
config.backupDomainEnableCallback(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -282,5 +278,6 @@ function enableBackupDomain() {
|
||||
export default {
|
||||
format,
|
||||
request,
|
||||
uploadFile
|
||||
uploadFile,
|
||||
enableBackupDomain
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ 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";
|
||||
|
||||
/* 生命周期内只做一次的checkSession */
|
||||
@@ -156,6 +157,9 @@ async function code2Session(code: string) {
|
||||
obj = config.beforeSend(obj);
|
||||
}
|
||||
|
||||
// 备用域名逻辑
|
||||
obj.url = url.replaceDomain(obj.url);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let start = new Date().getTime();
|
||||
wx.request({
|
||||
@@ -201,6 +205,13 @@ async function code2Session(code: string) {
|
||||
complete() {
|
||||
},
|
||||
fail: (res) => {
|
||||
// 如果主域名不可用,且配置了备份域名,且本次请求未使用备份域名
|
||||
if (res?.errMsg?.indexOf('CONNECTION_REFUSED') >= 0 && url.isInBackupDomainList(obj.url)) {
|
||||
// 开启备份域名
|
||||
requestHandler.enableBackupDomain(obj.url);
|
||||
// 重试一次
|
||||
return code2Session(code).then((res)=> resolve(res));
|
||||
}
|
||||
return reject({type: "system-error", res});
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import config from '../store/config'
|
||||
import status from '../store/status'
|
||||
|
||||
function setParams(url: string = "", params: object) {
|
||||
const queryStringIndex: number = url.indexOf("?");
|
||||
let kvp: any = {};
|
||||
@@ -25,13 +28,33 @@ function setParams(url: string = "", params: object) {
|
||||
}
|
||||
}
|
||||
|
||||
function replaceDomain(url: string = "", domain: string = "") {
|
||||
// 保证domain只包含域名,没有 http(s) 前缀 和 / 后缀
|
||||
domain = domain.replace(/^http(s)?:\/\//, '').replace(/\/$/, '');
|
||||
return url.replace(/^http(s)?:\/\/(.*?)\//, `https://${domain}/`);
|
||||
function replaceDomain(url: string = "") {
|
||||
if (status.isEnableBackupDomain && config.backupDomainList && typeof config.backupDomainList === 'object') {
|
||||
for(const origin in config.backupDomainList) {
|
||||
if (url.indexOf(origin) >= 0) {
|
||||
url = url.replace(origin, config.backupDomainList[origin]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
function isInBackupDomainList(url: string = "") {
|
||||
let res = false;
|
||||
if (config.backupDomainList && typeof config.backupDomainList === 'object') {
|
||||
for(const origin in config.backupDomainList) {
|
||||
if (url.indexOf(origin) >= 0) {
|
||||
res = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export default {
|
||||
setParams,
|
||||
replaceDomain,
|
||||
isInBackupDomainList
|
||||
};
|
||||
|
||||
@@ -12,16 +12,4 @@ describe("setParams", () => {
|
||||
sample = url.setParams(sample, { test2: 2 });
|
||||
expect(sample).toBe("https://www.example.com?test=1&test2=2");
|
||||
});
|
||||
|
||||
test("replace domain with host", () => {
|
||||
let sample = "https://example.com/dosomething";
|
||||
sample = url.replaceDomain(sample, "example2.com");
|
||||
expect(sample).toBe("https://example2.com/dosomething");
|
||||
})
|
||||
|
||||
test("replace domain with origin", () => {
|
||||
let sample = "https://example.com/dosomething";
|
||||
sample = url.replaceDomain(sample, "https://example2.com/");
|
||||
expect(sample).toBe("https://example2.com/dosomething");
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user