request.js
4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* @Author: limin
* @Date: 2019-08-26 18:02:41
* @Last Modified by: chenwq
* @Last Modified time: 2021-06-30 14:30:41
*/
import axiosPackage from "axios";
import util from "@/libs/util.js";
import { baseConfig } from "@/main";
// import axios from "@/plugin/axios"
import axios from "@/plugin/axios/index_code";
import RequestMethods from "./request.methods";
import sign from "./sign";
function request(
params = {},
method = RequestMethods.GET,
noSignAttr = [],
options = {},
) {
// 兼容线上dj和scrm appkey和secret不一样的情况
let apiName = "";
if (params.method.includes("dj.api.smartcarlife")) {
apiName = "dj";
} else if (params.method.includes("dj.api.script")) {
apiName = "script";
} else if (params.method.includes("dj.staff.api")) {
apiName = "staff";
} else if (params.method.includes("dj.smartcarlife.stock")) {
apiName = "dj.smartcarlife";
} else if (params.method.includes("risk.manage.api")) {
apiName = "riskmanage";
} else if (params.method.includes("dj.api.strategy")) {
apiName = "strategy";
} else if (params.method.includes("dj.api.supervisor")) {
apiName = "supervisor";
} else if (params.method.includes("dj.pad.api")) {
// rop-dj-pad 等方法名含 dj.pad,split(".api") 会得到 dj.pad 无 mapping;签名与 appKey 沿用 dj
apiName = "dj";
} else {
// console.log(params.method.split(".api"))
apiName = params.method.split(".api")[0];
}
// console.log("params.method", params.method)
// console.log("apiName", apiName)
// 为空报错
if (!apiName) {
throw new Error("apiName is null");
}
const mappingName = {
"dj.smartcarlife": "axios",
dj: "axios",
scrm: "axios2",
merchants: "axios3",
reportExport: "axios4",
"glsx.accounts.biz.common.pictureUpload": "common",
clue: "axios5",
"dj.businessSchool": "axios6",
script: "axios7",
staff: "axios9",
riskmanage: "axios10",
strategy: "axios_strategy",
supervisor: "axios_supervisor",
};
const baseApiName = mappingName[apiName];
// console.log(baseApiName, "baseApiName")
// params.appKey = baseConfig[baseApiName].appkey
if (baseApiName == "common") {
params.app_key = baseConfig[baseApiName].appkey;
} else {
params.appKey = baseConfig[baseApiName].appkey;
}
if (util.cookies.get("uuid")) {
params.accountId = util.cookies.get("uuid");
}
params.timestamp = new Date().getTime();
params.v = "1.0.0";
params.format = "json";
params.locale = "zh_CN";
if (params.method !== "dj.api.user.login") {
params.sessionId = util.cookies.get("token");
}
/**
* mock - url = params.method
* 调后端接口 - url = ''
*/
const url = baseConfig[baseApiName].baseURL;
// const url = "/api"
/**
* 调后端接口,请使用以下代码。
*/
const signWay = {
"dj.smartcarlife": "sha1",
dj: "sha1",
scrm: "sha1",
glsx: "md5",
"glsx.accounts.biz.common.pictureUpload": "md5",
clue: "sha1",
script: "sha1",
staff: "sha1",
riskmanage: "sha1",
};
// console.log(signWay[apiName], "baseApiName")
const secret = baseConfig[baseApiName].secret;
const CancelToken = axiosPackage.CancelToken;
let reqData =
method === RequestMethods.POST
? {
baseURL: "/",
data: sign(params, noSignAttr, secret, signWay[apiName]),
}
: { params: sign(params, noSignAttr, secret, signWay[apiName]) };
reqData = Object.assign(reqData, options, {});
// 请求进度
if (params?.onProcess) {
reqData.onUploadProgress = params.onProcess;
}
// 取消请求
if (params?.cancelFun) {
reqData.cancelToken = new CancelToken((c) => {
params.cancelFun.cancel = c;
});
}
// console.log(method === RequestMethods.POST)
// console.log(url)
// console.log(options)
// console.log(reqData)
let axiosParams = {
url,
method,
...reqData,
};
if (options.returnAll) axiosParams.headers = { "return-all": true };
return axios(axiosParams);
/**
* mock,请使用以下代码。
*/
// return axios({
// url,
// method,
// params: sign(params, noSignAttr)
// })
}
export default request;