|
1 | 1 | import ams from '../ams'; |
2 | | -import { getQueryString, getType, isFn } from '../utils'; |
| 2 | +import { getQueryString, getType, isFn, responseHandler } from '../utils'; |
3 | 3 | import { httpRequestTypeExcludeGet } from '../ams/request'; |
4 | 4 |
|
5 | 5 | /** |
@@ -112,19 +112,14 @@ export const read = ams.createApiAction({ |
112 | 112 | }; |
113 | 113 | }, |
114 | 114 | success(res) { |
115 | | - const successCode = this.getConfig('resource.api.read.successCode') || this.getConfig('resource.api.successCode'); |
116 | | - if (res.data.code === successCode) { |
117 | | - const config = this.resource.api.read; |
118 | | - if (typeof config === 'object' && typeof config.transform === 'function') { |
119 | | - this.setBlockData(config.transform(res.data.data)); |
120 | | - } else if (typeof config === 'object' && typeof config.responseDataParse === 'function') { |
121 | | - this.setBlockData(config.responseDataParse(res.data)); |
122 | | - } else { |
123 | | - this.setBlockData(res.data.data); |
124 | | - } |
| 115 | + const { message, code, isSuccess, data } = responseHandler.call(this, res, 'read'); |
| 116 | + if (isSuccess) { |
| 117 | + const { transform, responseDataParse } = this.resource.api.read || {}; |
| 118 | + const handler = transform || responseDataParse || (data => data); |
| 119 | + this.setBlockData(handler(data)); |
125 | 120 | } else { |
126 | | - this.$message.error(`${res.data.msg}(${res.data.code})`); |
127 | | - throw '@read:' + res.data.code; |
| 121 | + this.$message.error(`${message}(${code})`); |
| 122 | + throw '@read:' + code; |
128 | 123 | } |
129 | 124 | return res; |
130 | 125 | } |
@@ -160,16 +155,14 @@ export const update = ams.createApiAction({ |
160 | 155 | }; |
161 | 156 | }, |
162 | 157 | success(res) { |
163 | | - // 默认successCode |
164 | | - const successCode = this.getConfig('resource.api.update.successCode') || this.getConfig('resource.api.successCode'); |
165 | | - if (res.data.code === successCode) { |
| 158 | + const { isSuccess, message, code, data } = responseHandler.call(this, res, 'update'); |
| 159 | + if (isSuccess) { |
166 | 160 | this.$message.success('更新成功'); |
167 | | - if (typeof this.on['update-success'] === 'function') { |
168 | | - this.on['update-success'](res.data); |
169 | | - } |
| 161 | + const onSuccess = this.on['update-success']; |
| 162 | + if (isFn(onSuccess)) onSuccess(data); |
170 | 163 | } else { |
171 | | - this.$message.error(`${res.data.msg}(${res.data.code})`); |
172 | | - throw '@update:' + res.data.code; |
| 164 | + this.$message.error(`${message}(${code})`); |
| 165 | + throw '@update:' + code; |
173 | 166 | } |
174 | 167 | return res; |
175 | 168 | } |
@@ -206,16 +199,14 @@ export const deleteAction = ams.createApiAction({ |
206 | 199 | }; |
207 | 200 | }, |
208 | 201 | success(res) { |
209 | | - // 默认successCode |
210 | | - const successCode = this.getConfig('resource.api.delete.successCode') || this.getConfig('resource.api.successCode'); |
211 | | - if (res.data.code === successCode) { |
| 202 | + const { isSuccess, message, code, data } = responseHandler.call(this, res, 'delete'); |
| 203 | + if (isSuccess) { |
212 | 204 | this.$message.success('删除成功'); |
213 | | - if (typeof this.on['delete-success'] === 'function') { |
214 | | - this.on['delete-success'](res.data); |
215 | | - } |
| 205 | + const onSuccess = this.on['delete-success']; |
| 206 | + if (isFn(onSuccess)) onSuccess(data); |
216 | 207 | } else { |
217 | | - this.$message.error(`${res.data.msg}(${res.data.code})`); |
218 | | - throw '@delete:' + res.data.code; |
| 208 | + this.$message.error(`${message}(${code})`); |
| 209 | + throw '@delete:' + code; |
219 | 210 | } |
220 | 211 | return res; |
221 | 212 | } |
@@ -245,21 +236,21 @@ export const create = ams.createApiAction({ |
245 | 236 | }; |
246 | 237 | }, |
247 | 238 | success(res) { |
248 | | - // 默认successCode |
249 | | - const successCode = this.getConfig('resource.api.create.successCode') || this.getConfig('resource.api.successCode'); |
250 | | - if (res.data.code === successCode) { |
| 239 | + const { isSuccess, message, code, data } = responseHandler.call(this, res, 'create'); |
| 240 | + if (isSuccess) { |
251 | 241 | this.$message.success('创建成功'); |
252 | | - if (typeof this.on['create-success'] === 'function') { |
253 | | - this.on['create-success'](res.data); |
254 | | - } |
| 242 | + const onSuccess = this.on['create-success']; |
| 243 | + if (isFn(onSuccess)) onSuccess(data); |
255 | 244 | } else { |
256 | | - this.$message.error(`${res.data.msg}(${res.data.code})`); |
257 | | - throw '@create code:' + res.data.code; |
| 245 | + this.$message.error(`${message}(${code})`); |
| 246 | + throw '@create code:' + code; |
258 | 247 | } |
259 | 248 | return res; |
260 | 249 | } |
261 | 250 | }); |
262 | 251 |
|
| 252 | +// https://github.com/vipshop/ams/blob/5c8e0112c3b8e42c4bed9ff658767bbdbcf9bbd4/src/ams/request.js#L162 |
| 253 | +// createApiAction -> src/ams/request.js |
263 | 254 | export const list = ams.createApiAction({ |
264 | 255 | getOptions(params) { |
265 | 256 | // 使用传入页数,如搜索使用 @list:1 将页数重置为1 |
@@ -336,35 +327,39 @@ export const list = ams.createApiAction({ |
336 | 327 | params: arg |
337 | 328 | }; |
338 | 329 | }, |
| 330 | + /** |
| 331 | + * |
| 332 | + * @param {*} res { status: statusCode, data: JSON.parse(xhr.responseText) } |
| 333 | + */ |
339 | 334 | success(res) { |
340 | | - // 默认successCode |
341 | | - const successCode = this.getConfig('resource.api.list.successCode') || this.getConfig('resource.api.successCode'); |
342 | | - if ( |
343 | | - res.data.code === successCode && |
344 | | - res.data.data |
345 | | - ) { |
346 | | - const config = this.resource.api.list; |
347 | | - this.data.total = res.data.data.total; |
| 335 | + const { message, code, isSuccess, data, getter } = responseHandler.call(this, res, 'list'); |
| 336 | + if (isSuccess) { |
| 337 | + const total = data[getter.totalPath]; |
| 338 | + const list = data[getter.listPath]; |
| 339 | + this.data.list = list; |
| 340 | + this.data.total = total; |
| 341 | + |
| 342 | + const { transform, responseDataParse } = this.resource.api.list || {}; |
| 343 | + if (isFn(transform)) { |
| 344 | + this.data.list = transform(data); |
348 | 345 |
|
349 | | - if (typeof config === 'object' && typeof config.transform === 'function') { |
350 | | - this.data.list = config.transform(res.data.data.list) || []; |
351 | | - } else if (typeof config === 'object' && typeof config.responseDataParse === 'function') { |
352 | | - const convertResponseDataParse = config.responseDataParse(res.data); |
353 | | - if (getType(convertResponseDataParse) !== 'object') { |
| 346 | + } else if (isFn(responseDataParse)) { |
| 347 | + const parsedData = responseDataParse(data); |
| 348 | + if (getType(parsedData) !== 'object') { |
354 | 349 | console.error('responseDataParse中需要返回object类型,如{ list: [] }'); |
355 | 350 | this.data.list = []; |
356 | 351 | } else { |
357 | | - this.data = { ...this.data, ...config.responseDataParse(res.data) }; |
| 352 | + this.data = { |
| 353 | + ...this.data, |
| 354 | + ...parsedData |
| 355 | + }; |
358 | 356 | } |
359 | | - } else { |
360 | | - this.data.list = res.data.data.list || []; |
361 | | - } |
362 | | - if (typeof this.on['list-success'] === 'function') { |
363 | | - this.on['list-success'](res.data); |
364 | 357 | } |
| 358 | + const onSuccess = this.on['list-success']; |
| 359 | + if (isFn(onSuccess)) onSuccess(data); |
365 | 360 | } else { |
366 | | - this.$message.error(`${res.data.msg}(${res.data.code})`); |
367 | | - throw '@list:' + res.data.code; |
| 361 | + this.$message.error(`${message}(${code})`); |
| 362 | + throw '@list:' + code; |
368 | 363 | } |
369 | 364 |
|
370 | 365 | return res; |
|
0 commit comments