http({
url: http.adornUrl("/admin/user/setUserLevel"),
method: "post",
data:http.adornData({}),
params: http.adornParams({
userId: dataForm.value.userId || undefined,
userLevel: dataForm.value.userLevel,
teamName: dataForm.value.teamName
}),
}).then(() => {
ElMessage({
message: "操作成功",
type: "success",
duration: 1500,
onClose: () => {
visible.value = false;
emit("refreshDataList", page);
},
});
});
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
const defaults = {
t: Date.now()
}
data = openDefultdata ? merge(defaults, data) : data
return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
}
http.adornParams = (params = {}, openDefultParams = true) => {
const defaults = {
t: Date.now()
}
return openDefultParams ? merge(defaults, params) : params
}
/**
* 请求拦截
*/
http.interceptors.request.use(
config => {
config.headers.Authorization = cookie.get('Authorization') // 请求头带上token
// 只针对get方式进行序列化
if (config.method === 'get' || config.method === 'GET') {
config.paramsSerializer = function (params) {
return qs.stringify(params, { arrayFormat: 'repeat' })
}
}
return config
},
error => {
return Promise.reject(error)
}
)
/**
* 响应拦截
*/
http.interceptors.response.use(
response => {
// blob 格式处理
if (response.request.responseType === 'blob') {
return response
}
const res = response.data
// 00000 请求成功
if (res.code === '00000' || res.code === 'A00002') {
return res
}
// A00001 用于直接显示提示用户的错误,内容由输入决定
if (res.code === 'A00001') {
ElMessage({
message: res.msg || res.data || 'Error',
type: 'error',
duration: 1.5 * 1000
})
return Promise.reject(res)
}
// A00004 未授权
if (res.code === 'A00004') {
clearLoginInfo()
router.push({ name: 'login' })
}
// A00005 服务器异常
if (res.code === 'A00005') {
// eslint-disable-next-line no-console
console.error('============== 请求异常 ==============', '\n', `接口地址: ${response.config.url.replace(import.meta.env.VITE_APP_BASE_API, '')}`, '\n', `异常信息: ${res}`, '\n', '============== 请求异常 end ==========')
ElMessage({
message: '服务器出了点小差,请稍后再试',
type: 'error',
duration: 1.5 * 1000,
customClass: 'element-error-message-zindex'
})
return Promise.reject(res)
}
},
error => {
// eslint-disable-next-line no-console
console.log('========请求失败========', '\n', error.response, '\n', '========请求失败 end========')
switch (error.response.status) {
case 400:
ElMessage({
message: error.response.data,
type: 'error',
duration: 1500,
customClass: 'element-error-message-zindex'
})
break
case 401:
clearLoginInfo()
router.push({ name: 'login' })
break
case 405:
ElMessage({
message: 'http请求方式有误',
type: 'error',
duration: 1500,
customClass: 'element-error-message-zindex'
})
break
case 500:
ElMessage({
message: '服务器出了点小差,请稍后再试',
type: 'error',
duration: 1500,
customClass: 'element-error-message-zindex'
})
break
case 501:
ElMessage({
message: '服务器不支持当前请求所需要的某个功能',
type: 'error',
duration: 1500,
customClass: 'element-error-message-zindex'
})
break
}
return Promise.reject(error)
}
)
params方式传递参数
@PostMapping("/setUserLevel")
public ServerResponseEntity<Boolean> setUserLevel(String teamName,String userId,String userLevel) {}
data方式传递参数
@PostMapping
public ServerResponseEntity<String> save(@Valid @RequestBody SysUser user){}