提交者
Gitee
防止get请求参数值为false或0等特殊值会导致无法正确的传参
Merge pull request !146 from wugh/hotfix/gitee-issue_I2A5FU
正在显示
1 个修改的文件
包含
103 行增加
和
103 行删除
| 1 | -import axios from 'axios' | ||
| 2 | -import { Notification, MessageBox, Message } from 'element-ui' | ||
| 3 | -import store from '@/store' | ||
| 4 | -import { getToken } from '@/utils/auth' | ||
| 5 | -import errorCode from '@/utils/errorCode' | ||
| 6 | - | ||
| 7 | -axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' | ||
| 8 | -// 创建axios实例 | ||
| 9 | -const service = axios.create({ | ||
| 10 | - // axios中请求配置有baseURL选项,表示请求URL公共部分 | ||
| 11 | - baseURL: process.env.VUE_APP_BASE_API, | ||
| 12 | - // 超时 | ||
| 13 | - timeout: 10000 | ||
| 14 | -}) | ||
| 15 | -// request拦截器 | ||
| 16 | -service.interceptors.request.use(config => { | ||
| 17 | - // 是否需要设置 token | ||
| 18 | - const isToken = (config.headers || {}).isToken === false | ||
| 19 | - if (getToken() && !isToken) { | ||
| 20 | - config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 | ||
| 21 | - } | ||
| 22 | - // get请求映射params参数 | ||
| 23 | - if (config.method === 'get' && config.params) { | ||
| 24 | - let url = config.url + '?'; | ||
| 25 | - for (const propName of Object.keys(config.params)) { | ||
| 26 | - const value = config.params[propName]; | ||
| 27 | - var part = encodeURIComponent(propName) + "="; | ||
| 28 | - if (value && typeof(value) !== "undefined") { | ||
| 29 | - if (typeof value === 'object') { | ||
| 30 | - for (const key of Object.keys(value)) { | ||
| 31 | - let params = propName + '[' + key + ']'; | ||
| 32 | - var subPart = encodeURIComponent(params) + "="; | ||
| 33 | - url += subPart + encodeURIComponent(value[key]) + "&"; | ||
| 34 | - } | ||
| 35 | - } else { | ||
| 36 | - url += part + encodeURIComponent(value) + "&"; | ||
| 37 | - } | ||
| 38 | - } | ||
| 39 | - } | ||
| 40 | - url = url.slice(0, -1); | ||
| 41 | - config.params = {}; | ||
| 42 | - config.url = url; | ||
| 43 | - } | ||
| 44 | - return config | ||
| 45 | -}, error => { | ||
| 46 | - console.log(error) | ||
| 47 | - Promise.reject(error) | ||
| 48 | -}) | ||
| 49 | - | ||
| 50 | -// 响应拦截器 | ||
| 51 | -service.interceptors.response.use(res => { | ||
| 52 | - // 未设置状态码则默认成功状态 | ||
| 53 | - const code = res.data.code || 200; | ||
| 54 | - // 获取错误信息 | ||
| 55 | - const msg = errorCode[code] || res.data.msg || errorCode['default'] | ||
| 56 | - if (code === 401) { | ||
| 57 | - MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { | ||
| 58 | - confirmButtonText: '重新登录', | ||
| 59 | - cancelButtonText: '取消', | ||
| 60 | - type: 'warning' | ||
| 61 | - } | ||
| 62 | - ).then(() => { | ||
| 63 | - store.dispatch('LogOut').then(() => { | ||
| 64 | - location.href = '/index'; | ||
| 65 | - }) | ||
| 66 | - }) | ||
| 67 | - } else if (code === 500) { | ||
| 68 | - Message({ | ||
| 69 | - message: msg, | ||
| 70 | - type: 'error' | ||
| 71 | - }) | ||
| 72 | - return Promise.reject(new Error(msg)) | ||
| 73 | - } else if (code !== 200) { | ||
| 74 | - Notification.error({ | ||
| 75 | - title: msg | ||
| 76 | - }) | ||
| 77 | - return Promise.reject('error') | ||
| 78 | - } else { | ||
| 79 | - return res.data | ||
| 80 | - } | ||
| 81 | - }, | ||
| 82 | - error => { | ||
| 83 | - console.log('err' + error) | ||
| 84 | - let { message } = error; | ||
| 85 | - if (message == "Network Error") { | ||
| 86 | - message = "后端接口连接异常"; | ||
| 87 | - } | ||
| 88 | - else if (message.includes("timeout")) { | ||
| 89 | - message = "系统接口请求超时"; | ||
| 90 | - } | ||
| 91 | - else if (message.includes("Request failed with status code")) { | ||
| 92 | - message = "系统接口" + message.substr(message.length - 3) + "异常"; | ||
| 93 | - } | ||
| 94 | - Message({ | ||
| 95 | - message: message, | ||
| 96 | - type: 'error', | ||
| 97 | - duration: 5 * 1000 | ||
| 98 | - }) | ||
| 99 | - return Promise.reject(error) | ||
| 100 | - } | ||
| 101 | -) | ||
| 102 | - | ||
| 103 | -export default service | 1 | +import axios from 'axios' |
| 2 | +import { Notification, MessageBox, Message } from 'element-ui' | ||
| 3 | +import store from '@/store' | ||
| 4 | +import { getToken } from '@/utils/auth' | ||
| 5 | +import errorCode from '@/utils/errorCode' | ||
| 6 | + | ||
| 7 | +axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' | ||
| 8 | +// 创建axios实例 | ||
| 9 | +const service = axios.create({ | ||
| 10 | + // axios中请求配置有baseURL选项,表示请求URL公共部分 | ||
| 11 | + baseURL: process.env.VUE_APP_BASE_API, | ||
| 12 | + // 超时 | ||
| 13 | + timeout: 10000 | ||
| 14 | +}) | ||
| 15 | +// request拦截器 | ||
| 16 | +service.interceptors.request.use(config => { | ||
| 17 | + // 是否需要设置 token | ||
| 18 | + const isToken = (config.headers || {}).isToken === false | ||
| 19 | + if (getToken() && !isToken) { | ||
| 20 | + config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 | ||
| 21 | + } | ||
| 22 | + // get请求映射params参数 | ||
| 23 | + if (config.method === 'get' && config.params) { | ||
| 24 | + let url = config.url + '?'; | ||
| 25 | + for (const propName of Object.keys(config.params)) { | ||
| 26 | + const value = config.params[propName]; | ||
| 27 | + var part = encodeURIComponent(propName) + "="; | ||
| 28 | + if (value !== null && typeof(value) !== "undefined") { | ||
| 29 | + if (typeof value === 'object') { | ||
| 30 | + for (const key of Object.keys(value)) { | ||
| 31 | + let params = propName + '[' + key + ']'; | ||
| 32 | + var subPart = encodeURIComponent(params) + "="; | ||
| 33 | + url += subPart + encodeURIComponent(value[key]) + "&"; | ||
| 34 | + } | ||
| 35 | + } else { | ||
| 36 | + url += part + encodeURIComponent(value) + "&"; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + url = url.slice(0, -1); | ||
| 41 | + config.params = {}; | ||
| 42 | + config.url = url; | ||
| 43 | + } | ||
| 44 | + return config | ||
| 45 | +}, error => { | ||
| 46 | + console.log(error) | ||
| 47 | + Promise.reject(error) | ||
| 48 | +}) | ||
| 49 | + | ||
| 50 | +// 响应拦截器 | ||
| 51 | +service.interceptors.response.use(res => { | ||
| 52 | + // 未设置状态码则默认成功状态 | ||
| 53 | + const code = res.data.code || 200; | ||
| 54 | + // 获取错误信息 | ||
| 55 | + const msg = errorCode[code] || res.data.msg || errorCode['default'] | ||
| 56 | + if (code === 401) { | ||
| 57 | + MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { | ||
| 58 | + confirmButtonText: '重新登录', | ||
| 59 | + cancelButtonText: '取消', | ||
| 60 | + type: 'warning' | ||
| 61 | + } | ||
| 62 | + ).then(() => { | ||
| 63 | + store.dispatch('LogOut').then(() => { | ||
| 64 | + location.href = '/index'; | ||
| 65 | + }) | ||
| 66 | + }) | ||
| 67 | + } else if (code === 500) { | ||
| 68 | + Message({ | ||
| 69 | + message: msg, | ||
| 70 | + type: 'error' | ||
| 71 | + }) | ||
| 72 | + return Promise.reject(new Error(msg)) | ||
| 73 | + } else if (code !== 200) { | ||
| 74 | + Notification.error({ | ||
| 75 | + title: msg | ||
| 76 | + }) | ||
| 77 | + return Promise.reject('error') | ||
| 78 | + } else { | ||
| 79 | + return res.data | ||
| 80 | + } | ||
| 81 | + }, | ||
| 82 | + error => { | ||
| 83 | + console.log('err' + error) | ||
| 84 | + let { message } = error; | ||
| 85 | + if (message == "Network Error") { | ||
| 86 | + message = "后端接口连接异常"; | ||
| 87 | + } | ||
| 88 | + else if (message.includes("timeout")) { | ||
| 89 | + message = "系统接口请求超时"; | ||
| 90 | + } | ||
| 91 | + else if (message.includes("Request failed with status code")) { | ||
| 92 | + message = "系统接口" + message.substr(message.length - 3) + "异常"; | ||
| 93 | + } | ||
| 94 | + Message({ | ||
| 95 | + message: message, | ||
| 96 | + type: 'error', | ||
| 97 | + duration: 5 * 1000 | ||
| 98 | + }) | ||
| 99 | + return Promise.reject(error) | ||
| 100 | + } | ||
| 101 | +) | ||
| 102 | + | ||
| 103 | +export default service |
-
请 注册 或 登录 后发表评论