【前端】云工厂的纸掌柜app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.2 KiB

import Http from './http.js'
// 请求封装文件
const urlEnv = process.env.NODE_ENV === 'production' ? '-test' : ''
const xappid = '503258978847966403'
// 请求url列表
const prefixList = {
'/yyt-uec': `https://api-client-yyt${urlEnv}.qniao.cn`,
'/base-paper-trading': `https://api-client-yyt${urlEnv}.qniao.cn`,
'/uec': `https://api-client-uec${urlEnv}.qniao.cn`
}
const config = {
baseUrl: '',
header: {
'content-type': 'application/json;charset=UTF-8'
},
requestOption:{
// 是否需要隐藏loading
hideLoading:false,
// 是否返回原始数据
isReturnNativeResponse:false,
// 需要对返回数据进行处理
isTransformResponse:true
}
}
// 请求拦截
const reqInterceptor = (config, options) => {
// 添加token
const token = uni.getStorageSync('Qn_token')
config.header = {
...config.header,
'Authorization': token || 'iHP4V/g6O5DXHixyNrf7tm/UsBwShEYjzGx1kBBPitXOsbrMnv5z4DHjhbgWwrgz/eyAbzsk0APv+gBprZdnNQ==',
'X-APP-ID': xappid
}
// 改变url
let newUrl = ''
const {url = ''} = config
if (url.startsWith('http')) {
newUrl = url
} else {
for (let key in prefixList) {
if (url.startsWith(key)) {
newUrl = prefixList[key] + url
break
}
}
}
config.url = newUrl
if (!options.hideLoading) {
uni.showLoading({
title: '加载中...'
})
}
return config
}
// 响应拦截
const resInterceptor = (response,options) => {
if(options.isReturnNativeResponse) {
return response
}
// 直接返回 {code,data,message}
if(!options.isTransformResponse) {
return response.data
}
const {statusCode} = response
const res = response.data
if (statusCode >= 200 && statusCode < 300) {
uni.hideLoading()
return res
} else {
uni.showToast({
title:'服务器错误,请联系客服'
})
// 后期可以加记录
return null
}
}
const tranformConfig = (ins, config) => {
let conf = {
...config
}
conf.baseUrl = config.baseUrl || ins.baseUrl || ''
conf.header = config.header || ins.header
conf.method = config.method || 'GET'
conf.data = config.data || config.params || {}
conf.dataType = config.dataType || 'json'
conf.responseType = config.responseType || 'text'
return conf
}
const http = new Http(config, {
reqInterceptor,
resInterceptor,
tranformConfig
})
export default http