Commit 8bfc5b04 by zqm

update: 捕捉请求

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