123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- // import axios from 'axios'
- // import Router from '@/router/index'
- import store from '@/store'
- import {
- getToken
- } from '@/utils/auth'
- const header = {
- // "X-Requested-With": "XMLHttpRequest",
- // "X-Frame-Options": "DENY", // 告诉浏览器不要(DENY)把这个网页放在iFrame内,通常的目的就是要帮助用户对抗点击劫持。
- // "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
- }
- window.ajaxTimeout = 20000
- export default async(
- url = '',
- data = {},
- type = 'GET',
- params = {},
- headers = header,
- onUploadProgress
- ) => {
- type = url.method.toLowerCase()
- // console.log(url)
- const obj = {
- method: type,
- baseURL: '',
- url: url.url,
- data: url.data,
- params,
- // // `onUploadProgress` 允许为上传处理进度事件
- // onUploadProgress: function(progressEvent) {
- // // 对原生进度事件的处理
- // },
- // // `onDownloadProgress` 允许为下载处理进度事件
- // onDownloadProgress: function(progressEvent) {
- // // 对原生进度事件的处理
- // },
- // `cancelToken` 指定用于取消请求的 cancel token
- // (查看后面的 Cancellation 这节了解更多)
- // cancelToken: new CancelToken(function (cancel) {
- // }),
- processData: true, // 告诉axios不要去处理发送的数据(重要参数)
- timeout: window.ajaxTimeout,
- headers,
- onUploadProgress
- }
- if (onUploadProgress && typeof onUploadProgress === 'function') {
- obj.onUploadProgress = onUploadProgress
- }
- // 请求拦截器
- axios.interceptors.request.use(
- config => {
- if (!config.data.token) {
- config.data.token = getToken()
- }
- const { noRelation } = config.data
- // 决定传递关联公司:非超管用户且单独的请求中data中没有设置noRelation
- const isAllowRelaComNo = !store.state.user.isSupertube && !noRelation
- config.data.relaComNo = isAllowRelaComNo ? store.state.user.currentCompany : ''
- return config
- },
- () => {
- // 错误抛到业务代码
- return Promise.reject(new Error('服务器异常,请联系管理员!'))
- }
- )
- // 添加响应拦截器
- axios.interceptors.response.use(
- async response => {
- return response
- },
- error => {
- return Promise.reject(error.response.data)
- }
- )
- return new Promise((resolve, reject) => {
- axios(obj)
- .then(response => {
- resolve(response.data)
- })
- .catch(res => {
- reject(res)
- })
- })
- }
|