Commit 80bb979a by limin

调整http拦截逻辑

1 parent 746f7f08
.DS_Store .DS_Store
node_modules node_modules
/dist
# local env files # local env files
......
This diff could not be displayed because it is too large.
{ {
"name": "glsx-common", "name": "glsx-common",
"version": "1.3.3", "version": "1.3.7",
"description": "glsx common", "description": "glsx common",
"main": "./dist/glsx-common.js", "main": "./dist/glsx-common.js",
"private": false, "private": false,
......
...@@ -38,9 +38,10 @@ Http.prototype.create = function () { ...@@ -38,9 +38,10 @@ Http.prototype.create = function () {
let _configSign = this.customer.sign || {} let _configSign = this.customer.sign || {}
data = createSign(Object.assign({}, _configSign, data), this.customer.secret, noSignAttr, this.customer.signWay) data = createSign(Object.assign({}, _configSign, data), this.customer.secret, noSignAttr, this.customer.signWay)
} }
if (config.method.toLocaleLowerCase() == 'get') { const method = config.method.toLocaleLowerCase()
if (method == 'get') {
config.params = data config.params = data
} else if (config.method.toLocaleLowerCase() == 'post') { } else if (method == 'post') {
config.data = qs.stringify(data) config.data = qs.stringify(data)
} }
return config return config
...@@ -48,24 +49,27 @@ Http.prototype.create = function () { ...@@ -48,24 +49,27 @@ Http.prototype.create = function () {
error => Promise.reject(error) error => Promise.reject(error)
) )
this.service.interceptors.response.use( this.service.interceptors.response.use(
response => { ({ data: res, config: { returnAll } }) => {
const { response: result } = this.customer const {
const res = response.data response: { code_key = 'returnCode', errId = 'errId', code_success_value = '0', data_key = 'data' },
let code = res[result.code_key || 'returnCode'] alert
let errId = res[result.errId || 'errId'] } = this.customer
let code = res[code_key]
// let errId = res[errId]
// 特殊的code处理,可以增加 // 特殊的code处理,可以增加
switch (code + '') { switch (code + '') {
case '200': case code_success_value + '':
// 请求成功,不弹提示框,成功code可以更改 // 请求成功,不弹提示框,成功code可以更改
break break
default: default:
this.customer.alert && this.customer.alert(res.message || res.msg || '出错了,请联系管理员') alert && alert(res.message || res.msg || '出错了,请联系管理员')
break break
} }
return response.config.returnAll ? res : res[result.data_key || 'data'] return returnAll ? res : res[data_key]
}, },
error => { error => {
this.customer.alert && this.customer.alert(error.message || '出错了,请联系管理员') const { alert } = this.customer
alert && alert(error.message || '出错了,请联系管理员')
// 返回接口返回的错误信息 // 返回接口返回的错误信息
return Promise.reject(error) return Promise.reject(error)
} }
......
...@@ -153,6 +153,7 @@ export const Util = { ...@@ -153,6 +153,7 @@ export const Util = {
// 创建签名 // 创建签名
createSign: function (params, secret, noSignAttr = [], way = 'sha1') { createSign: function (params, secret, noSignAttr = [], way = 'sha1') {
delete params.sign delete params.sign
params.timestamp = new Date().getTime()
var strQs = qs.stringify(params, { arrayFormat: 'indices', encode: false, allowDots: true }) var strQs = qs.stringify(params, { arrayFormat: 'indices', encode: false, allowDots: true })
const objQs = strQs.split('&') const objQs = strQs.split('&')
const collect = objQs.reduce((collect, item) => { const collect = objQs.reduce((collect, item) => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!