hookehuyr

✨ feat(axios模块): post请求判断是否使用qs状态使用封装函数

......@@ -2,13 +2,14 @@
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-28 10:17:40
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-30 00:10:50
* @LastEditTime: 2022-06-30 01:16:40
* @FilePath: /tswj/src/utils/axios.js
* @Description:
*/
import axios from 'axios';
import router from '@/router';
import qs from 'Qs'
import { strExist } from '@/utils/tools'
// import { parseQueryString } from '@/utils/tools'
axios.defaults.params = {
......@@ -23,15 +24,13 @@ axios.interceptors.request.use(
// const url_params = parseQueryString(location.href);
// GET请求默认打上时间戳,避免从缓存中拿数据。
const timestamp = config.method === 'get' ? (new Date()).valueOf() : '';
// 上传相关接口需要屏蔽掉封装, 不能序列化数据会报错。
if (
config.method === 'post' &&
(config.url.indexOf('a=upload') === -1 && config.url.indexOf('upload.qiniup.com') === -1)
)
{
if (config.method === 'post') {
// 上传相关接口需要屏蔽掉封装, 不能序列化,数据会报错。
if (!strExist(['a=upload', 'upload.qiniup.com'], config.url)) {
// POST PHP需要修改数据格式
config.data = qs.stringify(config.data)
}
}
// 绑定默认请求头
config.params = { ...config.params, timestamp }
return config;
......
/*
* @Date: 2022-04-18 15:59:42
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-29 22:35:51
* @LastEditTime: 2022-06-30 01:15:33
* @FilePath: /tswj/src/utils/tools.js
* @Description: 文件描述
*/
......@@ -58,4 +58,17 @@ const parseQueryString = url => {
return json;
}
export { formatDate, wxInfo, hasEllipsis, parseQueryString };
/**
* 字符串包含字符数组中字符的状态
* @param {*} array 字符数组
* @param {*} str 字符串
* @returns 包含状态
*/
const strExist = (array, str) => {
const exist = array.filter(arr => {
if (str.indexOf(arr) >= 0) return str;
})
return exist.length > 0
}
export { formatDate, wxInfo, hasEllipsis, parseQueryString, strExist };
......