【更新】兼容更多的请求method,增加moduleRequest,更方便添加前缀

pull/98/head
xlzy 2023-03-26 12:49:14 +08:00
parent 363c9b7b2b
commit 9db45c2297
2 changed files with 43 additions and 25 deletions

View File

@ -8,9 +8,9 @@
* 5.不可二次分发开源参与同类竞品如有想法可联系团队xiaonuobase@qq.com商议合作
* 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip
*/
import { baseRequest } from '@/utils/request'
import { moduleRequest } from '@/utils/request'
const request = (url, ...arg) => baseRequest(`/auth/b/${url}`, ...arg)
const request = moduleRequest(`/auth/b/`)
/**
* 登录
*

View File

@ -10,6 +10,7 @@
*/
// 统一的请求发送
import axios from 'axios'
import qs from 'qs'
import { Modal, message, notification } from 'ant-design-vue'
import sysConfig from '@/config/index'
import tool from '@/utils/tool'
@ -108,8 +109,25 @@ service.interceptors.response.use(
} else {
// 统一成功提示
const responseUrl = response.config.url
const apiNameArray = ['add', 'edit', 'delete', 'update', 'grant', 'reset', 'start', 'stop',
'pass', 'disable', 'enable', 'revoke', 'suspend', 'active', 'turn', 'adjust', 'reject']
const apiNameArray = [
'add',
'edit',
'delete',
'update',
'grant',
'reset',
'start',
'stop',
'pass',
'disable',
'enable',
'revoke',
'suspend',
'active',
'turn',
'adjust',
'reject'
]
apiNameArray.forEach((apiName) => {
if (responseUrl.includes(apiName)) {
message.success(data.msg)
@ -131,37 +149,37 @@ service.interceptors.response.use(
}
)
// 适配器, 用于适配不同的请求方式
export const baseRequest = (url, value = {}, method = 'post', options = {}) => {
url = sysConfig.API_URL + url
if (method === 'post') {
return service.post(url, value, options)
} else if (method === 'get') {
return service.get(url, {
params: value,
...options
})
return service.get(url, { params: value, ...options })
} else if (method === 'formdata') {
return service({
method: 'post',
url,
data: value,
// 转换数据的方法
transformRequest: [
function (data) {
let ret = ''
for (const it in data) {
ret += `${encodeURIComponent(it)}=${encodeURIComponent(data[it])}&`
}
ret = ret.substring(0, ret.length - 1)
return ret
}
],
// 设置请求头
// form-data表单提交的方式
return service.post(url, qs.stringify(value), {
headers: {
'Content-Type': 'multipart/form-data'
}
},
...options
})
} else {
// 其他请求方式例如put、delete
return service({
method: method,
url: url,
data: value,
...options
})
}
}
// 模块内的请求, 会自动加上模块的前缀
export const moduleRequest =
(moduleUrl) =>
(url, ...arg) => {
return baseRequest(moduleUrl + url, ...arg)
}
export default service