網站首頁 編程語言 正文
安裝
利用npm安裝npm install axios --save
封裝
request.js:
import axios from 'axios'
import { Message } from 'element-ui'
import store from '../vuex/index'
/* eslint-disable new-cap */
const service = new axios.create({
timeout: 20000
})
service.interceptors.request.use(
config => {
if (config.url.indexOf('token=') === -1) {
// 把token參數拼接在url后
config.url = `${config.url}?token=${window.sessionStorage.token.replace(/"/g, '')}`
}
return config
},
error => {
window.console.log(error)
Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
if (
response.request.responseType === 'blob' // 文件上傳,返回文件流
) {
return response
}
const res = response.data
if (res.baseRes) {
res.code = res.baseRes.code
res.desc = res.baseRes.desc
}
if (res.code !== 100) {
store.commit('updateLoading', false)
Message({
message: res.desc,
type: 'error',
duration: 3 * 1000
})
if (res.code === 919 || res.code === 918) {
// 登陸過期。
setTimeout(() => {
Message({
message: response.data.baseRes.desc,
type: 'error'
})
// 跳轉到登陸頁面
window.location.href = 'xxx'
}, 3000)
} else {
return response.data
}
} else {
return response.data
}
},
error => {
store.commit('updateLoading', false)
Message({
message: error.message,
type: 'error',
duration: 3 * 1000
})
return Promise.reject(error)
}
)
export default service
api/common/index.js:
import request from '@/utils/request'
export function getSystemMenu (params) {
return request({
method: 'get',
url: `${window.msBaseUrl}services/systemMenu.json`,
params
})
}
export function getMallCategoryList (params) {
return request({
url: `${window.msNewBaseUrl}config/list`,
method: 'post',
data: params
})
}
頁面使用:
import { getSystemMenu } from '@/api/common/index'
const params = {
skuId: this.formData.skuId,
platForm: 1
}
queryProdSkuId(params)
.then(
res => {
if (res.baseRes.code == 100) {
let data = res.baseRes.result
} else {
this.formData.skuId = null
}
},
err => {
}
)
.catch(err => {
})
原文鏈接:https://blog.csdn.net/HH18700418030/article/details/122713630
- 上一篇:TypeScript 變量聲明 —— 類型斷言
- 下一篇:go實現分布式鎖
相關推薦
- 2022-10-27 go?熔斷原理分析與源碼解讀_Golang
- 2022-03-16 .net6環境下使用RestSharp請求GBK編碼網頁亂碼的解決方案_實用技巧
- 2022-09-28 基于OpenCV(python)的實現文本分割之垂直投影法_python
- 2022-04-30 python的正則表達式和re模塊詳解_python
- 2022-07-25 詳解docker進行數據掛載的三種模式_docker
- 2022-07-10 初中級前端程序員必用且夠用的git命令同時推送到github/gitee及三種常用場景
- 2022-12-04 .NET?Core利用BsonDocumentProjectionDefinition和Lookup
- 2022-11-21 Nginx解決history模式下頁面刷新404問題示例_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支