-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.js
More file actions
77 lines (70 loc) · 2.18 KB
/
fetch.js
File metadata and controls
77 lines (70 loc) · 2.18 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
import axios from "./config";
// 根据 ./apiModules文件夹中 生成fetchCfg -- 实现方法 webpack-require.context()
// fetchCfg = {
// account,
// colony
// };
const fetchCfg = {};
const requireContext = require.context("./apiModules", false, /\.js$/);
requireContext.keys().forEach(path => {
let module = path.replace(".js", "").replace("./", "");
fetchCfg[module] = requireContext(path).default;
});
// console.log("接口总数统计:", Object.values(fetchCfg).reduce((sum, item) => sum + Object.keys(item).length, 0))
/**
* 解析参数
* @param {String} param
*/
const fetchParam = param => {
var valid = /[a-z]+(\.[a-z])+/.test(param);
if (!valid) {
throw new Error("[Error in fetch]: fetch 参数格式为 moduleName.apiName");
} else {
return {
moduleName: param.split(".")[0],
apiName: param.split(".")[1]
};
}
};
class Fetch {
// 给Vue提供安装接口
install(vue) {
Object.assign(vue.prototype, {
$fetch: this.fetch
});
}
/**
* 对axios封装通用fetch方法
* 会根据传入的下列参数自动寻找 method 和路径
* @param {*} module 对应 fetch配置的名字
* @param {*} apiName 该模块下的某个请求api名称
*/
fetch(moduleInfo, payload) {
let prefix = "/api";
let moduleName = fetchParam(moduleInfo)["moduleName"];
let apiName = fetchParam(moduleInfo)["apiName"];
// 判断API模块存在情况
if (!this.fetchCfg) {
throw new Error(`[Error in fetch]: 未找到api配置文件`);
}
if (!this.fetchCfg.hasOwnProperty(moduleName)) {
throw new Error(`[Error in fetch]: api配置中未找到模块 -> ${moduleName}`);
}
if (!this.fetchCfg[moduleName].hasOwnProperty(apiName)) {
throw new Error(`[Error in fetch]: api模块${moduleName}中未找到接口 -> ${apiName}`);
}
let fetchInfo = fetchCfg[moduleName][apiName];
let method = fetchInfo["method"];
let url = `${prefix}/${fetchInfo["url"]}`;
if (method && url) {
if (method === "get") {
return axios[method](url, {
params: payload
});
} else {
return axios[method](url, payload);
}
}
}
}
export default new Fetch();