123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import Vue from 'vue'
- import axios from 'axios'
- import router from '../router'
- import { getCookieValue } from '../utils'
- import { platform } from '../utils/platform'
- const request = axios.create({
- withCredentials: true,
- baseURL: process.env.API_DOMAIN,
- timeout: 50000,
- headers: {
- 'Content-Type': 'application/json;charset=UTF-8'
- }
- })
- let tingbangSource = 'h5'
- if (platform.isAlipay) {
- tingbangSource = 'alipay'
- } else if (platform.isWeixin) {
- tingbangSource = 'wechat'
- }
- // Add a request interceptor
- request.interceptors.request.use(request => {
- request.headers.token = getCookieValue('afhousetoken')
- request.headers.tingbangSource = tingbangSource
- if (platform.isWeixin) {
- request.headers.wechatToken = getCookieValue('afhousewechatToken')
- }
- return request
- }, error => {
- // Do something with request error
- return Promise.reject(error)
- })
- // Add a response interceptor
- request.interceptors.response.use(response => {
- // Do something with response data
- const { code, authorizeUrl } = response.data
- if (code === 301 && authorizeUrl) {
- Vue.cookie.delete('afhousewechatToken')
- location.replace(authorizeUrl)
- } else if (code === 403) {
- router.push('/login')
- }
- return response.data
- }, (error) => {
- // Do something with response error
- return Promise.reject(error)
- })
- export default request
|