Files
FirstUI-vue/components/firstui/fui-request/fui-common.js
2023-08-17 21:28:49 +08:00

120 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 本文件由FirstUI授权予新疆天衡创新研究院有限公司手机号1 86 1 4 07 2 549身份证尾号5A07X5专用请尊重知识产权勿私下传播违者追究法律责任。
const base = {
//所有配置项,未设置则使用默认值
config() {
return {
//接口域名https://firstui.cn如果host为空则直接使用传入的目标地址url
host: '',
// 接口地址:/order/getList如果host为空直接传入https://firstui.cn/order/getList
url: '',
//参数
data: {},
//请求头
header: {
/*
* content-type:
* application/x-www-form-urlencoded
* application/json
*/
'content-type': 'application/json'
},
//必须大写
method: 'POST',
//大于0时才生效否则使用全局配置或者默认值
timeout: 0,
dataType: 'json',
//是否阻止拦截重复的请求重复请求地址url + method + 参数data 一致)
prevent: false,
//Array<String> 参数data中的keyprevent为true时有效进行重复请求判断时移除keys中相关参数如时间戳、随机数等.
keys: [],
//是否仅返回简要数据true-仅返回接口数据datafalse-返回包含header、statusCode、errMsg、data等数据
brief: false,
//String 请求标记用于中断该请求不同请求不可重复只可包含数字、字母、下划线firstui_001
cancelToken: '',
showLoading: true,
//加载中提示文本showLoading为true时有效
loadingText: '',
errorMsg: '网络不给力,请检查网络设置!',
//跨域请求时是否携带凭证cookies仅H5支持HBuilderX 2.6.15+
withCredentials: false,
//DNS解析时优先使用ipv4,仅 App-Android 支持 (HBuilderX 2.8.0+)
firstIpv4: false,
//get请求时参数值为数组时处理方式可选值comma-值逗号拼接repeat-重复参数名brackets-带中括号参数名indices-数组下标参数名
arrayFormat: 'comma'
}
},
getOptions(config) {
let options = {
...config
};
['host', 'timeout', 'prevent', 'keys', 'brief', 'cancelToken', 'showLoading', 'loadingText', 'errorMsg',
'arrayFormat'
]
.forEach(item => {
delete options[item];
})
return options;
},
getParamsHandle(config, arrayFormat) {
const options = {
...config
}
const idx = ['indices', 'repeat', 'brackets'].indexOf(arrayFormat)
if (options.data && Object.keys(options.data).length > 0 && idx !== -1) {
let params = []
Object.entries(options.data).forEach(([key, value]) => {
if (Array.isArray(value)) {
let _key = key;
if (arrayFormat === 'brackets') {
_key = `${key}[]`
}
value.forEach((item, index) => {
if (arrayFormat === 'indices') {
_key = `${key}[${index}]`
}
const val = Object.prototype.toString.call(item) === '[object Object]' ?
JSON.stringify(item) : item;
params.push(`${_key}=${val}`)
})
} else {
params.push(`${key}=${value}`)
}
})
options.url = `${options.url}?${params.join('&')}`
options.data = {}
}
return options;
},
merge(a, b) {
return Object.assign({}, a, b);
},
mergeConfig(defaultConfig, config, init) {
let header = base.merge(defaultConfig.header, config.header || {});
let params = base.merge(defaultConfig, config)
params.header = header;
if (!init) {
let url = base.combineURLs(params.host, params.url)
params.url = url;
}
return params;
},
//如果host为空则直接使用传入的目标地址
combineURLs(host, target) {
return host ? host.replace(/\s+/g, '') + '/' + target.replace(/\s+/g, '').replace(/^\/+/, '') : target;
},
toast(text, duration, success) {
text && uni.showToast({
title: text,
icon: success ? 'success' : 'none',
duration: duration || 2000
})
},
showLoading(title, mask = true) {
uni.showLoading({
mask: mask,
title: title || '请稍候...'
})
}
}
export default base