網站首頁 編程語言 正文
安裝
利用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-11-06 Android小工具自定義view課表_Android
- 2022-05-06 pyecharts的Tab和Legend布局詳情_python
- 2023-02-03 C++中的HTTP協議問題_C 語言
- 2024-01-14 三種線程安全的List
- 2022-12-12 C語言scanf語句吃掉回車或者空格問題及解決_C 語言
- 2023-11-20 【ROS】用roslibpy庫在windows上用python 連接Ubuntu ROS
- 2022-06-22 Android實現精美的聊天界面_Android
- 2022-07-07 Asp.Net上傳文件并配置可上傳大文件的方法_基礎應用
- 最近更新
-
- 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同步修改后的遠程分支