utils2.0

import axios from 'axios';
import { Toast } from 'vant';
import store from '@/store';//仓库
import router from '@/router'

axios.defaults.timeout = 60000;
axios.defaults.baseURL = 'this is api location';

const app_Id = "this is appId"

//开始loading,export导出去
export const loadingBegin = () => {
    Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true, // 禁用背景点击
        loadingType: 'spinner',
        message: '努力加载中'
    });

}
//关闭loading
export const loadingFinish = () => {
    Toast.clear();
}

//http request 拦截器 请求拦截(配置发送请求的信息)
axios.interceptors.request.use(config => {
    // 处理请求之前的配置
    let Token = window.localStorage.getItem('Token');

    if (Token) {
        config.headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + Token,
            "appId": app_Id
        }
    } else {
        // 请求失败的处理
        config.headers = {
            'Content-Type': 'application/json',
            "appId": app_Id
        }
        // router.replace({
        //     path:'/login',
        //     query:{
        //         redirect:router.currentRoute.fullpath
        //     }
        // })
    }
    return config;
}, error => {
    return Promise.reject(error);
}
);


//http response 拦截器 响应拦截(配置请求回来的信息)
axios.interceptors.response.use(response => {
    // 处理响应数据
//if (response.data.code == 1000 || response.data.code == 1001) {
        //Toast.fail('未授权,请重新登录');
        //window.localStorage.setItem("Token", "");

        //router.push('my');

   // }
    return response;
}, error => {

    // console.log('util.js error', error.response.data.code);

    // 处理响应失败
   // if (error.response.code == 1000 || error.response.code == 1001) {
        //Toast.fail('未授权,请重新登录');
        //router.push('my');
    //}else if (error.response.status == 401) {
        //Toast.fail('未授权,请重新登录');
       // router.push('my');
   // }
    return Promise.reject(error)
}
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {

                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

/**
* 封装delete请求
* @param url
* @param data
* @returns {Promise}
*/
export function del(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            params: params
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}

export const appId = app_Id
export const baseURL = axios.defaults.baseURL

使用
在API里引入

import { post, get, patch, put, del } from './util'

API.js

export default {
    upImg(params, config){//
        return post(`/upload/images/0005`, params, config)
    },
    code(phone, params) {//
        return get(`/verificationcode/${phone}`, params)
    },
    escrowWallet(params) {
        return post(`/escrowWallet`, params)
    },
}

在main.js里引入

import api from '@/js/API'
Vue.prototype.$api = api;

页面使用

      let params = c;
      let config = {
       // headers: {
         // a:"b"
        //}
      };
      this.$api.upImg(params, config).then(res => {
        //成功
      }).catch(err => {
          //失败
        });
this.$api.code(this.phone).then(res => {
           
          }).catch(res => {

          });
let data = {
          //password: this.password,
          //confirmPassword: this.confirmPassword
        };
        this.$api.escrowWallet(data).then(res => {
           
          })

你可能感兴趣的:(utils2.0)