@hzab/data-model 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -0
- package/package.json +47 -0
- package/src/axios.js +75 -0
- package/src/data-model.js +324 -0
- package/src/index.js +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# @hzab/data-model
|
|
2
|
+
|
|
3
|
+
组件模板
|
|
4
|
+
|
|
5
|
+
- node@14.21
|
|
6
|
+
|
|
7
|
+
# 组件
|
|
8
|
+
|
|
9
|
+
## 示例
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
import DataModel from "@hzab/data-model";
|
|
13
|
+
|
|
14
|
+
const listDM = new DataModel({
|
|
15
|
+
// query 参数
|
|
16
|
+
query,
|
|
17
|
+
// 新增接口 post url
|
|
18
|
+
createApi,
|
|
19
|
+
// 新增接口 post 入参枚举函数
|
|
20
|
+
createMap,
|
|
21
|
+
// 详情接口 get url
|
|
22
|
+
getApi,
|
|
23
|
+
// 详情接口 get 出参枚举函数
|
|
24
|
+
getMap,
|
|
25
|
+
// 详情接口 get url
|
|
26
|
+
getListApi,
|
|
27
|
+
// 列表接口 get 出参枚举函数
|
|
28
|
+
getListMap,
|
|
29
|
+
// 列表请求函数
|
|
30
|
+
getListFunc,
|
|
31
|
+
// 编辑接口 put url
|
|
32
|
+
updateApi,
|
|
33
|
+
// 编辑接口 put 入参枚举函数
|
|
34
|
+
updateMap,
|
|
35
|
+
// 删除接口 delete url
|
|
36
|
+
deleteApi,
|
|
37
|
+
// 批量删除接口 delete url
|
|
38
|
+
multipleDeleteApi,
|
|
39
|
+
// axios 实例对象
|
|
40
|
+
axios,
|
|
41
|
+
// axios 相关配置
|
|
42
|
+
axiosConf,
|
|
43
|
+
// 列表接口 get 入参枚举函数
|
|
44
|
+
getListReqMap,
|
|
45
|
+
// 列表接口 get 出参枚举函数
|
|
46
|
+
getListResMap,
|
|
47
|
+
// 详情接口 get 入参枚举函数
|
|
48
|
+
getReqMap,
|
|
49
|
+
// 详情接口 get 出参枚举函数
|
|
50
|
+
getResMap,
|
|
51
|
+
// 新增接口 post 入参枚举函数
|
|
52
|
+
createReqMap,
|
|
53
|
+
// 新增接口 post 出参枚举函数
|
|
54
|
+
createResMap,
|
|
55
|
+
// 编辑接口 put 入参枚举函数
|
|
56
|
+
updateReqMap,
|
|
57
|
+
// 编辑接口 put 出参枚举函数
|
|
58
|
+
updateResMap,
|
|
59
|
+
// 删除接口 delete 入参枚举函数
|
|
60
|
+
deleteReqMap,
|
|
61
|
+
// 删除接口 delete 出参枚举函数
|
|
62
|
+
deleteResMap,
|
|
63
|
+
// 批量删除接口 delete 入参枚举函数
|
|
64
|
+
multipleDeleteReqMap,
|
|
65
|
+
// 批量删除接口 delete 出参枚举函数
|
|
66
|
+
multipleDeleteResMap,
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## API
|
|
71
|
+
|
|
72
|
+
### InfoPanel Attributes
|
|
73
|
+
|
|
74
|
+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
|
75
|
+
| ---- | ------ | ---- | ------ | ---- |
|
|
76
|
+
| | string | 否 | - | |
|
|
77
|
+
|
|
78
|
+
# 组件开发流程
|
|
79
|
+
|
|
80
|
+
- 在 config/webpack.config.js 中按需修改 library 配置的文件名
|
|
81
|
+
- 在 config/webpack.config.js 中按需修改 alias 配置的包名,便于本地调试
|
|
82
|
+
- 在 tsconfig.json 中按需修改 paths 配置的包名,解决 ts 报错问题
|
|
83
|
+
- npm run dev
|
|
84
|
+
|
|
85
|
+
## 文件目录
|
|
86
|
+
|
|
87
|
+
- example 本地开发测试代码
|
|
88
|
+
- src 组件源码
|
|
89
|
+
- lib 组件打包编译后的代码
|
|
90
|
+
|
|
91
|
+
## 命令
|
|
92
|
+
|
|
93
|
+
- Mac 执行该命令,设置 pre-commit 为可执行文件
|
|
94
|
+
|
|
95
|
+
- npm run mac-chmod
|
|
96
|
+
- chmod +x .husky && chmod +x .husky/pre-commit
|
|
97
|
+
|
|
98
|
+
- 生成文档:npm run docs
|
|
99
|
+
- 本地运行:npm run dev
|
|
100
|
+
- 打包编译:npm run build
|
|
101
|
+
|
|
102
|
+
## 发布
|
|
103
|
+
|
|
104
|
+
- 注意:示例代码生效,但发布之后未生效。确认是否执行了编译!!!
|
|
105
|
+
|
|
106
|
+
- 编译组件:npm run build
|
|
107
|
+
- 命令:npm publish --access public
|
|
108
|
+
- 发布目录:
|
|
109
|
+
- lib
|
|
110
|
+
- src
|
|
111
|
+
|
|
112
|
+
## 配置
|
|
113
|
+
|
|
114
|
+
### 配置文件
|
|
115
|
+
|
|
116
|
+
- 本地配置文件:config/config.js
|
|
117
|
+
|
|
118
|
+
### webpack 配置文件
|
|
119
|
+
|
|
120
|
+
- config/webpack.config.js
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hzab/data-model",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "data model",
|
|
5
|
+
"main": "src",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "npm run prepare && webpack serve -c ./config/webpack.config.js --env local",
|
|
8
|
+
"build": "webpack -c ./config/webpack.config.js --env production",
|
|
9
|
+
"publish-patch": "npm version patch && npm publish --access public",
|
|
10
|
+
"publish-minor": "npm version minor && npm publish --access public",
|
|
11
|
+
"publish-major": "npm version major && npm publish --access public",
|
|
12
|
+
"prepare": "husky install",
|
|
13
|
+
"mac-chmod": "chmod +x .husky && chmod +x .husky/pre-commit",
|
|
14
|
+
"docs": "typedoc"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [],
|
|
20
|
+
"author": "CaiYansong",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@hzab/permissions": "0.0.4",
|
|
24
|
+
"@hzab/webpack-config": "^0.0.15",
|
|
25
|
+
"@types/react": "^17.0.62",
|
|
26
|
+
"@types/react-dom": "^17.0.20",
|
|
27
|
+
"antd": "^4.14.0",
|
|
28
|
+
"axios": "^1.4.0",
|
|
29
|
+
"eslint": "^8.30.0",
|
|
30
|
+
"less": "^4.1.3",
|
|
31
|
+
"mobx": "^6.7.0",
|
|
32
|
+
"mobx-react": "^7.6.0",
|
|
33
|
+
"react": "^17.0.2",
|
|
34
|
+
"react-dom": "^17.0.2",
|
|
35
|
+
"react-router-dom": "^6.14.1",
|
|
36
|
+
"typedoc": "^0.24.8",
|
|
37
|
+
"typescript": "^4.9.4"
|
|
38
|
+
},
|
|
39
|
+
"directories": {
|
|
40
|
+
"lib": "lib"
|
|
41
|
+
},
|
|
42
|
+
"lint-staged": {
|
|
43
|
+
"**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
|
|
44
|
+
"prettier --write"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/axios.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
|
|
3
|
+
export const temp = { axiosList: [axios] };
|
|
4
|
+
|
|
5
|
+
export function setAxios(axiosList) {
|
|
6
|
+
temp.axiosList = [...temp.axiosList, ...axiosList];
|
|
7
|
+
return temp.axiosList;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* axios 守卫
|
|
12
|
+
* @param {Function} cb
|
|
13
|
+
*/
|
|
14
|
+
export function setAxRequest(cb) {
|
|
15
|
+
temp.axiosList?.forEach((_ax) => {
|
|
16
|
+
_ax.interceptors.request.use(cb);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 请求到结果的拦截处理
|
|
22
|
+
* @param {Function} cb
|
|
23
|
+
*/
|
|
24
|
+
export function setAxResponse(cb, errCb) {
|
|
25
|
+
temp.axiosList?.forEach((_ax) => {
|
|
26
|
+
_ax.interceptors.response.use(
|
|
27
|
+
cb,
|
|
28
|
+
errCb ||
|
|
29
|
+
((error) => {
|
|
30
|
+
console.error("Error axios response: ", error);
|
|
31
|
+
return Promise.reject(error);
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 设置 axios.defaults
|
|
39
|
+
* @param {string} key
|
|
40
|
+
* @param {*} val
|
|
41
|
+
*/
|
|
42
|
+
export function setAxDefaults(key, val) {
|
|
43
|
+
temp.axiosList?.forEach((_ax) => {
|
|
44
|
+
_ax.defaults[key] = val;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 设置 headers
|
|
50
|
+
* @param {string} key
|
|
51
|
+
* @param {*} val
|
|
52
|
+
*/
|
|
53
|
+
export function setAxHeaders(key, val) {
|
|
54
|
+
temp.axiosList?.forEach((_ax) => {
|
|
55
|
+
_ax.defaults.headers[key] = val;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 设置 timeout
|
|
61
|
+
* @param {number} timeout
|
|
62
|
+
*/
|
|
63
|
+
export function setAxTimeout(timeout = 5000) {
|
|
64
|
+
setAxDefaults("timeout", timeout);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 设置 token
|
|
69
|
+
* @param {string} token
|
|
70
|
+
*/
|
|
71
|
+
export function setAxToken(token) {
|
|
72
|
+
setAxTimeout("Authorization", token);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default axios;
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
|
|
4
|
+
const _$Temp = {};
|
|
5
|
+
|
|
6
|
+
class DataModel {
|
|
7
|
+
constructor(params) {
|
|
8
|
+
const {
|
|
9
|
+
ctx,
|
|
10
|
+
query,
|
|
11
|
+
createApi,
|
|
12
|
+
createMap,
|
|
13
|
+
getApi,
|
|
14
|
+
getMap,
|
|
15
|
+
getListApi,
|
|
16
|
+
getListMap,
|
|
17
|
+
getListFunc,
|
|
18
|
+
updateApi,
|
|
19
|
+
updateMap,
|
|
20
|
+
deleteApi,
|
|
21
|
+
multipleDeleteApi,
|
|
22
|
+
axios: as,
|
|
23
|
+
axiosConf,
|
|
24
|
+
} = params;
|
|
25
|
+
|
|
26
|
+
this.ctx = ctx || {};
|
|
27
|
+
this.query = query || {};
|
|
28
|
+
this.axios = as || _$Temp.axios || axios;
|
|
29
|
+
this.axiosConf = axiosConf || {};
|
|
30
|
+
|
|
31
|
+
this.createApi = createApi;
|
|
32
|
+
this.createMap = createMap;
|
|
33
|
+
this.getApi = getApi;
|
|
34
|
+
this.getMap = getMap;
|
|
35
|
+
this.getListApi = getListApi;
|
|
36
|
+
this.getListMap = getListMap;
|
|
37
|
+
this.getListFunc = getListFunc;
|
|
38
|
+
this.updateApi = updateApi;
|
|
39
|
+
this.updateMap = updateMap;
|
|
40
|
+
this.deleteApi = deleteApi;
|
|
41
|
+
this.multipleDeleteApi = multipleDeleteApi;
|
|
42
|
+
|
|
43
|
+
const {
|
|
44
|
+
getListReqMap,
|
|
45
|
+
getListResMap,
|
|
46
|
+
getReqMap,
|
|
47
|
+
getResMap,
|
|
48
|
+
createReqMap,
|
|
49
|
+
createResMap,
|
|
50
|
+
updateReqMap,
|
|
51
|
+
updateResMap,
|
|
52
|
+
deleteReqMap,
|
|
53
|
+
deleteResMap,
|
|
54
|
+
multipleDeleteReqMap,
|
|
55
|
+
multipleDeleteResMap,
|
|
56
|
+
} = params || {};
|
|
57
|
+
this.getListReqMap = getListReqMap;
|
|
58
|
+
this.getListResMap = getListResMap;
|
|
59
|
+
this.getReqMap = getReqMap;
|
|
60
|
+
this.getResMap = getResMap;
|
|
61
|
+
this.createReqMap = createReqMap;
|
|
62
|
+
this.createResMap = createResMap;
|
|
63
|
+
this.updateReqMap = updateReqMap;
|
|
64
|
+
this.updateResMap = updateResMap;
|
|
65
|
+
this.deleteReqMap = deleteReqMap;
|
|
66
|
+
this.deleteResMap = deleteResMap;
|
|
67
|
+
this.multipleDeleteReqMap = multipleDeleteReqMap;
|
|
68
|
+
this.multipleDeleteResMap = multipleDeleteResMap;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getApiUrl(api, record, ctx = this.ctx) {
|
|
72
|
+
if (!api) {
|
|
73
|
+
throw new Error("Error api 不能为空", api, record, ctx);
|
|
74
|
+
}
|
|
75
|
+
let apiUrl = api;
|
|
76
|
+
const params = _.merge({}, record, ctx);
|
|
77
|
+
_.each(params, (value, key) => {
|
|
78
|
+
if (!_.isString(value) || !_.isNumber(value) || _.isBoolean(value)) {
|
|
79
|
+
apiUrl = apiUrl.replace(`:${key}`, value);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return apiUrl;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get(q = {}, ctx = {}) {
|
|
86
|
+
let query = _.merge({}, this.query, q);
|
|
87
|
+
query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
|
|
88
|
+
|
|
89
|
+
if (this.getReqMap) {
|
|
90
|
+
query = this.getReqMap(query);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const apiUrl = this.getApiUrl(this.getApi, query, ctx);
|
|
95
|
+
this.axios
|
|
96
|
+
.get(apiUrl, {
|
|
97
|
+
...this.axiosConf,
|
|
98
|
+
params: query,
|
|
99
|
+
})
|
|
100
|
+
.then((response) => {
|
|
101
|
+
this.handleRes(
|
|
102
|
+
response,
|
|
103
|
+
(res) => {
|
|
104
|
+
if (this.getMap) {
|
|
105
|
+
res = this.getMap(res);
|
|
106
|
+
}
|
|
107
|
+
if (this.getResMap) {
|
|
108
|
+
res = this.getResMap(res);
|
|
109
|
+
}
|
|
110
|
+
resolve(res);
|
|
111
|
+
},
|
|
112
|
+
reject,
|
|
113
|
+
);
|
|
114
|
+
})
|
|
115
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async getList(q, ctx) {
|
|
120
|
+
let query = _.merge({}, this.query, q);
|
|
121
|
+
query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
|
|
122
|
+
|
|
123
|
+
if (this.getListReqMap) {
|
|
124
|
+
query = this.getListReqMap(query);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let resultList = null;
|
|
128
|
+
if (this.getListFunc) {
|
|
129
|
+
resultList = await this.getListFunc(query);
|
|
130
|
+
} else {
|
|
131
|
+
const getPro = new Promise((resolve, reject) => {
|
|
132
|
+
const apiUrl = this.getApiUrl(this.getListApi, q, ctx);
|
|
133
|
+
this.axios
|
|
134
|
+
.get(apiUrl, {
|
|
135
|
+
...this.axiosConf,
|
|
136
|
+
params: query,
|
|
137
|
+
})
|
|
138
|
+
.then((response) => {
|
|
139
|
+
this.handleRes(
|
|
140
|
+
response,
|
|
141
|
+
(res) => {
|
|
142
|
+
// data: { list: [], pagination: { current: 0, total: 1 } }
|
|
143
|
+
if (this.getListMap) {
|
|
144
|
+
res.list = res.list.map((record) => this.getListMap(record));
|
|
145
|
+
}
|
|
146
|
+
if (this.getListResMap) {
|
|
147
|
+
res.list = res.list.map((record) => this.getListResMap(record));
|
|
148
|
+
}
|
|
149
|
+
resolve(res);
|
|
150
|
+
},
|
|
151
|
+
reject,
|
|
152
|
+
);
|
|
153
|
+
})
|
|
154
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
155
|
+
});
|
|
156
|
+
resultList = await getPro;
|
|
157
|
+
}
|
|
158
|
+
return resultList;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
create(params, ctx) {
|
|
162
|
+
let _params = _.cloneDeep(params);
|
|
163
|
+
if (this.createReqMap) {
|
|
164
|
+
_params = this.createReqMap(_params);
|
|
165
|
+
}
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
const apiUrl = this.getApiUrl(this.createApi, _params, ctx);
|
|
168
|
+
const opt = { ...this.axiosConf };
|
|
169
|
+
if (_params instanceof FormData) {
|
|
170
|
+
opt.headers = { "Content-Type": "multipart/form-data" };
|
|
171
|
+
}
|
|
172
|
+
this.axios
|
|
173
|
+
.post(apiUrl, _params, opt)
|
|
174
|
+
.then((response) => {
|
|
175
|
+
this.handleRes(
|
|
176
|
+
response,
|
|
177
|
+
(res) => {
|
|
178
|
+
if (this.createResMap) {
|
|
179
|
+
res = this.createResMap(res);
|
|
180
|
+
}
|
|
181
|
+
resolve(res);
|
|
182
|
+
},
|
|
183
|
+
reject,
|
|
184
|
+
);
|
|
185
|
+
})
|
|
186
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
update(params, ctx) {
|
|
191
|
+
let _params = _.cloneDeep(params);
|
|
192
|
+
if (this.updateReqMap) {
|
|
193
|
+
_params = this.updateReqMap(_params);
|
|
194
|
+
}
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
const apiUrl = this.getApiUrl(this.updateApi, _params, ctx);
|
|
197
|
+
const opt = { ...this.axiosConf };
|
|
198
|
+
if (_params instanceof FormData) {
|
|
199
|
+
opt.headers = { "Content-Type": "multipart/form-data" };
|
|
200
|
+
}
|
|
201
|
+
this.axios
|
|
202
|
+
.put(apiUrl, _params, opt)
|
|
203
|
+
.then((response) => {
|
|
204
|
+
this.handleRes(
|
|
205
|
+
response,
|
|
206
|
+
(res) => {
|
|
207
|
+
if (this.updateResMap) {
|
|
208
|
+
res = this.updateResMap(res);
|
|
209
|
+
}
|
|
210
|
+
resolve(res);
|
|
211
|
+
},
|
|
212
|
+
reject,
|
|
213
|
+
);
|
|
214
|
+
})
|
|
215
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
delete(params, ctx) {
|
|
220
|
+
let _params = _.cloneDeep(params);
|
|
221
|
+
if (this.deleteReqMap) {
|
|
222
|
+
_params = this.deleteReqMap(_params);
|
|
223
|
+
}
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
const apiUrl = this.getApiUrl(this.deleteApi, _params, ctx);
|
|
226
|
+
this.axios
|
|
227
|
+
.delete(apiUrl, { ...this.axiosConf, ..._params })
|
|
228
|
+
.then((response) => {
|
|
229
|
+
this.handleRes(
|
|
230
|
+
response,
|
|
231
|
+
(res) => {
|
|
232
|
+
if (this.deleteResMap) {
|
|
233
|
+
res = this.deleteResMap(res);
|
|
234
|
+
}
|
|
235
|
+
resolve(res);
|
|
236
|
+
},
|
|
237
|
+
reject,
|
|
238
|
+
);
|
|
239
|
+
})
|
|
240
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
multipleDelete(params, ctx) {
|
|
245
|
+
let _params = _.cloneDeep(params);
|
|
246
|
+
if (this.multipleDeleteReqMap) {
|
|
247
|
+
_params = this.multipleDeleteReqMap(_params);
|
|
248
|
+
}
|
|
249
|
+
return new Promise((resolve, reject) => {
|
|
250
|
+
const apiUrl = this.getApiUrl(this.multipleDeleteApi, _params, ctx);
|
|
251
|
+
this.axios
|
|
252
|
+
.delete(apiUrl, { ...this.axiosConf, ..._params })
|
|
253
|
+
.then((response) => {
|
|
254
|
+
this.handleRes(
|
|
255
|
+
response,
|
|
256
|
+
(res) => {
|
|
257
|
+
if (this.multipleDeleteResMap) {
|
|
258
|
+
res = this.multipleDeleteResMap(res);
|
|
259
|
+
}
|
|
260
|
+
resolve(res);
|
|
261
|
+
},
|
|
262
|
+
reject,
|
|
263
|
+
);
|
|
264
|
+
})
|
|
265
|
+
.catch((err) => this.errorHandler(err, reject));
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
handleRes(response, resolve, reject) {
|
|
270
|
+
if (typeof response !== "object") {
|
|
271
|
+
reject(new Error("response not object"));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
let _res = response;
|
|
275
|
+
// 兼容 传入的是 response.data 的情况
|
|
276
|
+
if (_res.data && _res.headers && _res.request && typeof _res.data?.code === "number") {
|
|
277
|
+
_res = _res.data;
|
|
278
|
+
}
|
|
279
|
+
const { code, message, data, msg } = _res;
|
|
280
|
+
if (code === 200) {
|
|
281
|
+
let _data = data ?? {};
|
|
282
|
+
if (_.isObject(_data) && _data.message === undefined) {
|
|
283
|
+
// 前缀 _ 避免与 data 里已有的 message 冲突
|
|
284
|
+
_data._message = message || msg;
|
|
285
|
+
_data._msg = message || msg;
|
|
286
|
+
}
|
|
287
|
+
resolve(_data);
|
|
288
|
+
} else {
|
|
289
|
+
const error = new Error(message || msg);
|
|
290
|
+
error.code = code;
|
|
291
|
+
error.response = response;
|
|
292
|
+
error._message = message || msg;
|
|
293
|
+
error._msg = message || msg;
|
|
294
|
+
reject(error);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
errorHandler(err, reject) {
|
|
299
|
+
const response = err.response || err;
|
|
300
|
+
if (response) {
|
|
301
|
+
const message = (response.data && (response.data.message || response.data.msg)) || response.msg;
|
|
302
|
+
const error = new Error(message || response.statusText || "未知错误");
|
|
303
|
+
error.code = response.status;
|
|
304
|
+
error.response = response;
|
|
305
|
+
if (message) {
|
|
306
|
+
// 前缀 _ 避免与 data 里已有的 message 冲突
|
|
307
|
+
error._message = message;
|
|
308
|
+
error._msg = message;
|
|
309
|
+
}
|
|
310
|
+
return reject(error);
|
|
311
|
+
}
|
|
312
|
+
return reject(err);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function setDefaultAxios(ax) {
|
|
317
|
+
if (ax) {
|
|
318
|
+
_$Temp.axios = ax;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export { axios, DataModel, setDefaultAxios };
|
|
323
|
+
|
|
324
|
+
export default DataModel;
|