Commit 8bfc5b04 by zqm

update: 捕捉请求

parent b2ed08b0
Pipeline #24832 failed with stage
in 0 seconds
Showing with 33 additions and 36 deletions
......@@ -2,7 +2,7 @@
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>self-service</title>
<title>员工自助</title>
<meta name="format-detection" content="telephone=no"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<script src="miniapp://common/js/qing/qing.js"></script>
......
import axios from 'axios'
var originAdapter = axios.defaults.adapter
axios.defaults.adapter = function (config) {
axios.defaults.adapter = function(config) {
if (!/^https?/.test(config.url)) {
return originAdapter(config)
}
......@@ -23,7 +23,7 @@ function isFormData(fd) {
function formData2obj(fd) {
var data = {}
for (var p of fd.entries()) {
data[p[0]] = p[1]
data[ p[ 0 ] ] = p[ 1 ]
}
return data
}
......@@ -33,15 +33,12 @@ function params2object(url) {
if (typeof url === 'object') {
return url
} else if (typeof url === 'string') {
url
.replace(/^.*\?|#.*$/g, '')
.split('&')
.forEach((n) => {
n = n.split('=')
if (n.length === 2) {
params[n[0]] = decodeURIComponent(n[1])
}
})
url.replace(/^.*\?|#.*$/g, '').split('&').forEach((n) => {
n = n.split('=')
if (n.length === 2) {
params[ n[ 0 ] ] = decodeURIComponent(n[ 1 ])
}
})
}
return params
}
......@@ -65,33 +62,30 @@ function request(opt) {
opt.url = opt.url.replace(/^\s+|\s+$/g, '')
// 将params拼接到url中
opt.url =
opt.url.split('?')[0] +
opt.url.split('?')[ 0 ] +
'?' +
Object.keys(urlParams)
.map((key) => {
var val = urlParams[key]
if (isObject(val)) {
val = JSON.stringify(val)
val = encodeURIComponent(val)
} else if (/[^\x00-\xff]/.test(val)) {
val = encodeURIComponent(val)
}
return key + '=' + val
})
.join('&')
Object.keys(urlParams).map((key) => {
var val = urlParams[ key ]
if (isObject(val)) {
val = JSON.stringify(val)
val = encodeURIComponent(val)
} else if (/[^\x00-\xff]/.test(val)) {
val = encodeURIComponent(val)
}
return key + '=' + val
}).join('&')
// post非json即是form。
if (opt.method.toLowerCase() === 'post') {
var data = opt.data
const headers = opt.headers
const contentType = headers['content-type'] || headers['Content-Type'] || headers['CONTENT-TYPE'] || ''
const contentType = headers[ 'content-type' ] || headers[ 'Content-Type' ] || headers[ 'CONTENT-TYPE' ] || ''
if (contentType.indexOf('application/json') !== -1) {
serializer = 'json'
}
if ((serializer === 'form') && isFormData(data)) {
opt.data = formData2obj(data)
}
else if (isString(data)) {
} else if (isString(data)) {
if (serializer === 'json') {
try {
opt.data = JSON.parse(data)
......@@ -108,8 +102,8 @@ function request(opt) {
opt.headers = opt.headers || {}
const headers = {}
// headers字段值必须是字符串型
Object.keys(opt.headers).forEach(function (key) {
var val = opt.headers[key]
Object.keys(opt.headers).forEach(function(key) {
var val = opt.headers[ key ]
if (val) {
var type = typeof val
if (type !== 'string') {
......@@ -118,15 +112,14 @@ function request(opt) {
// 因content-type可能存在大小写不规范,这里先过滤掉,后面根据serializer统一添加
var _key = key.toLowerCase()
if (_key !== 'content-type') {
headers[_key] = val
headers[ _key ] = val
}
}
})
// 根据serializer统一添加content-type
headers['content-type'] = serializer === 'json' ? 'application/json' : 'application/x-www-form-urlencoded'
headers[ 'content-type' ] = serializer === 'json' ? 'application/json' : 'application/x-www-form-urlencoded'
opt.headers = headers
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
qing.call('request', {
url: opt.url,
method: opt.method.toUpperCase(),
......@@ -134,7 +127,9 @@ function request(opt) {
header: opt.headers,
data: opt.data || {},
dataType: opt.dataType,
success: function (resp) {
success: function(resp) {
console.log('qing success opt>>', opt)
console.log('qing success resp>>', resp)
var response = {
data: resp.data,
status: resp.statusCode,
......@@ -158,7 +153,9 @@ function request(opt) {
reject(error)
}
},
error: function (e) {
error: function(e) {
console.log('qing error opt>>', opt)
console.log('qing error e>>', e)
var error = new Error(e)
error.config = opt
reject(error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment