|
- import Vue from 'vue'
- import axios from 'axios'
- import qs from 'qs'
- import 'vue2-toast/lib/toast.css'
- import Toast from 'vue2-toast'
- import store from '@/store'
- import {guid} from "./util";
- import md5 from 'js-md5';
-
- // import { Spin } from 'iview'
- Vue.use(Toast)
- class HttpRequest {
- constructor (baseUrl = baseURL) {
- this.baseUrl = baseUrl
- this.queue = {}
- }
- getInsideConfig () {
- const config = {
- baseURL: this.baseUrl,
- /*transformRequest: [function (data) {
- // 对 data 进行任意转换处理
- return qs.stringify(data, {arrayFormat: 'indices'})
- }],*/
- headers: {
- // 'Content-Type':'application/x-www-form-urlencoded'
- },
- paramsSerializer: function (params) {
- return qs.stringify(params, {arrayFormat: 'repeat'})
- }
- }
- return config
- }
- distroy (url) {
- delete this.queue[url]
- if (!Object.keys(this.queue).length) {
- // Spin.hide()
- }
- }
- interceptors (instance, url) {
- // 请求拦截
- instance.interceptors.request.use(config => {
- // 添加全局的loading...
- if (!Object.keys(this.queue).length) {
- // Spin.show() // 不建议开启,因为界面不友好
- }
- let accesstoken = 'aaaa09f1bc6305d06a7307c698ca2403'
- let timestamp = new Date().getTime()
- let nonce = guid()
- let secret = 'a7d1c64a82e5105e7a9e6713478a2a66'
- config.headers['x-dj-accesstoken'] = accesstoken
- config.headers['x-dj-timestamp'] = timestamp
- config.headers['x-dj-nonce'] = nonce
- config.headers['x-dj-signature'] = md5(accesstoken+secret+timestamp+nonce)
- config.headers['X-Requested-With'] = 'XMLHttpRequest'
- /*if (store.state.user.id_token) {
- config.headers['Authorization'] = 'Bearer ' + store.state.user.id_token
- }*/
- this.queue[url] = true
- return config
- }, error => {
- return Promise.reject(error)
- })
- // 响应拦截
- instance.interceptors.response.use(res => {
- this.distroy(url)
- // console.log(res)
- if (res.status === 200 || res.data.success) {
- return Promise.resolve(res.data)
- } else {
- return Promise.reject(res)
- }
- }, error => {
- this.distroy(url)
- console.log(error)
- if (error && error.response) {
- switch (error.response.status) {
- case 400:
- error.message = '请求错误'
- break
- case 401:
- error.message = '未授权,请重新登录'
- location.href = ''
- break
- default:
- Vue.prototype.$toast.center(error.response.data.message || '未知错误')
- }
- } else {
- Vue.prototype.$toast.center('未知错误')
- }
- return Promise.reject(error.response)
- })
- }
- request (options) {
- const instance = axios.create()
- options = Object.assign(this.getInsideConfig(), options)
- this.interceptors(instance, options.url)
- return instance(options)
- }
- }
- export default HttpRequest
|