From 1386b6f0c57cc83dbde5f3ffef9f1e1a03d46722 Mon Sep 17 00:00:00 2001 From: dengxiongfei Date: Mon, 27 Dec 2021 18:12:34 +0800 Subject: [PATCH] feat:xappid --- utils/http/index.js | 155 ++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 77 deletions(-) diff --git a/utils/http/index.js b/utils/http/index.js index 7fefdfb..8cc4749 100644 --- a/utils/http/index.js +++ b/utils/http/index.js @@ -2,100 +2,101 @@ import Http from './http.js' // 请求封装文件 const urlEnv = process.env.NODE_ENV === 'production' ? '-test' : '' -const xappid = '503258978847966403' +const xappid = '503258978847966404' // 请求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` + '/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 - } + 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 + // 添加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 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 + 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 + reqInterceptor, + resInterceptor, + tranformConfig }) - export default http