Skip to content

Commit 58a876a

Browse files
committed
更新2.0.10
- 调试mock参数,支持@phone生成随机手机号 - 调试mock参数,支持@idcard生成随机身份证号码 - config.js支持配置 WITHCREDENTIALS,调试接口时请求是否携带cookie
1 parent 954047b commit 58a876a

File tree

7 files changed

+111
-11
lines changed

7 files changed

+111
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apidoc",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",

public/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const config = {
2727
DELETE: "#ff4d4f",
2828
PATCH: "#802feb",
2929
},
30+
WITHCREDENTIALS: false,
3031
// 多语言
3132
LANG: [
3233
{

src/store/modules/App/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface FeConfigState {
3333
HTTP: FeConfigHTTPState;
3434
PUBLICPATH?: string;
3535
MENU?: FeConfigMenuState;
36+
WITHCREDENTIALS?: boolean;
3637
}
3738

3839
export interface FeConfigMenuState {

src/utils/helper/codeHelper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { trim } from "../index";
22
import Mock from "mockjs";
3+
import "./mockExtend";
34

45
export function renderCodeJsonByParams<T>(params: T[], isMock?: boolean): objectState {
56
let json: objectState = {};

src/utils/helper/mockExtend.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* mock数据自定义生成规则
3+
*/
4+
import Mock from "mockjs";
5+
import { createIdcard } from "../index";
6+
7+
Mock.Random.extend({
8+
phone: function () {
9+
let phonePrefixs = [
10+
"130",
11+
"131",
12+
"132",
13+
"133",
14+
"135",
15+
"137",
16+
"138",
17+
"152",
18+
"155",
19+
"157",
20+
"159",
21+
"170",
22+
"177",
23+
"180",
24+
"182",
25+
"183",
26+
"185",
27+
"187",
28+
"188",
29+
"189",
30+
"191",
31+
];
32+
return this.pick(phonePrefixs) + Mock.mock(/\d{8}/);
33+
},
34+
idcard: function () {
35+
return createIdcard();
36+
},
37+
});

src/utils/http/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ let apidocConfig: FeConfigState = {
1919
HTTP: {},
2020
};
2121

22-
let timeout = 30000;
23-
let baseURL = "/";
22+
let axiosOptions: AxiosRequestConfig = {
23+
baseURL: "",
24+
timeout: 3000,
25+
};
26+
2427
if (localStorage.APIDOC_CONFIG) {
2528
apidocConfig = JSON.parse(localStorage.APIDOC_CONFIG);
2629
if (apidocConfig.HTTP) {
27-
timeout = apidocConfig.HTTP.TIMEOUT ? apidocConfig.HTTP.TIMEOUT : timeout;
30+
axiosOptions.timeout = apidocConfig.HTTP.TIMEOUT ? apidocConfig.HTTP.TIMEOUT : 3000;
2831
}
2932
const cacheHost = Cache.get("HOST");
3033
if (cacheHost) {
31-
baseURL = cacheHost;
34+
axiosOptions.baseURL = cacheHost;
3235
} else if (apidocConfig.HTTP && apidocConfig.HTTP.HOSTS && isArray(apidocConfig.HTTP.HOSTS)) {
33-
baseURL = apidocConfig.HTTP.HOSTS[0] && apidocConfig.HTTP.HOSTS[0].host;
36+
axiosOptions.baseURL = apidocConfig.HTTP.HOSTS[0] && apidocConfig.HTTP.HOSTS[0].host;
37+
}
38+
if (apidocConfig.WITHCREDENTIALS) {
39+
axiosOptions.withCredentials = apidocConfig.WITHCREDENTIALS;
3440
}
3541
}
3642

37-
const service = axios.create({
38-
baseURL: baseURL,
39-
timeout: timeout,
40-
withCredentials: true,
41-
});
43+
const service = axios.create(axiosOptions);
4244

4345
// 请求拦截器
4446
service.interceptors.request.use(

src/utils/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,61 @@ export const replaceKeepAlivePageComma = (key: string, escape?: boolean): string
227227
}
228228
return key.replace(/,/g, KEEPALIVE_PAGE_COMMA);
229229
};
230+
231+
// 生成随机数字,max限制最大值,min限制最小值
232+
export const getRandomNumber = (max: number, min?: number): number => {
233+
return Math.floor(Math.random() * max + (min ? min : 0));
234+
};
235+
236+
// 根据前17位生成末位
237+
export const createIdcardEndNumber = (idcard: string) => {
238+
let arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 加权因子
239+
let arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; // 校验码
240+
let sum = 0;
241+
for (let j = 0; j < 17; j++) {
242+
// 对前17位数字与权值乘积求和
243+
sum += parseInt(idcard[j], 10) * arrExp[j];
244+
}
245+
return arrValid[sum % 11];
246+
};
247+
248+
// 生成身份证号
249+
export const createIdcard = () => {
250+
let cityPrefixs = [
251+
11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52,
252+
53, 54, 61, 62, 63, 64, 65, 71, 81, 82,
253+
];
254+
let idcard = cityPrefixs[getRandomNumber(cityPrefixs.length)] + "";
255+
256+
for (let i = 2; i < 18; i++) {
257+
if (i < 6) {
258+
idcard += getRandomNumber(10);
259+
} else if (i == 6) {
260+
idcard += getRandomNumber(2, 1); //年份第一位仅支持1和2
261+
} else if (i == 7) {
262+
idcard += idcard[6] == "1" ? 9 : 0; //两位年份规则,仅支持19和20
263+
} else if (i == 8) {
264+
idcard += idcard[6] == "1" ? getRandomNumber(7, 3) : getRandomNumber(2); //三位年份规则,仅支持193-199、200、201这些值
265+
} else if (i == 9) {
266+
idcard += getRandomNumber(10); //四位年份规则,0-9
267+
} else if (i == 10) {
268+
idcard += getRandomNumber(2); //首位月份规则
269+
} else if (i == 11) {
270+
idcard += idcard[10] == "0" ? getRandomNumber(9, 1) : getRandomNumber(3); //末位月份规则
271+
} else if (i == 12) {
272+
let maxDays = new Date(
273+
Number(idcard.substr(6, 4)),
274+
Number(idcard.substr(10, 2)),
275+
0
276+
).getDate(); // 获取当月最大天数
277+
let day = getRandomNumber(maxDays, 1);
278+
idcard += day < 10 ? "0" + day : day;
279+
i++;
280+
} else if (i > 13 && i < 17) {
281+
idcard += getRandomNumber(10);
282+
} else if (i == 17) {
283+
idcard += createIdcardEndNumber(idcard);
284+
}
285+
}
286+
return idcard;
287+
};

0 commit comments

Comments
 (0)