vue - vue-admin-element请求流程

请求逻辑

src->utils->request.js

使用了axios,store

//配置base_url
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    timeout: 5000 // request timeout
});
//在header里面设置token
// request interceptor
service.interceptors.request.use(
    async config => {
        // do something before request is sent

        if (store.getters.token) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            // config.headers['X-Token'] = getToken()
            config.headers.Authorization = store.getters.token;
        }
        return config;
    },
    error => {
        // do something with request error
        console.log(error); // for debug
        return Promise.reject(error);
    }
);
//对返回值进行拦截操作
// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        const res = response.data;
        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 200) {
            switch (res.code) {
                case 4000: //情况1
                case 4001: //情况2
                case 4002: //情况3
                   //do someting
                    break;
                default:
                    Message({
                        message: res.msg || 'Error',
                        type: 'error',
                        duration: 5 * 1000
                    });
                    break;
            }
            return Promise.reject(new Error(res.message || 'Error'));
        } else {
            return res;
        }
    },
    error => {
        console.log('err' + error); // for debug
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        });
        return Promise.reject(error);
    }
);

src->api->home.js

import request from '@/utils/request';

export function getInfo(params) {
    return request({
        url: '/home/info',
        method: 'get',
        params
    });
}

src->store->modules->home.js

import { getInfo } from '@/api/home';

const actions = {
    //get info
    getInfo({ commit }, param) {
    //解构传值
     const { limit } = param;
        return new Promise((resolve, reject) => {
            getInfo({ limit: limit })
                .then(response => {
                    const { data } = response;
                    resolve(data);
                })
                .catch(error => {
                    reject(error);
                });
        });
    }
};

export default {
    namespaced: true, //***
    actions
};

页面使用
src->views->home->index.vue

 methods: {
        //获取数据
        getInfo() {
            this.$store.dispatch('home/getInfo', { limit: '10' }).then(res => {
                this.list = res;
                this.mapVisible = true;
            });
        }
    },
posted @ 2019-11-26 09:44  gggggggxin  阅读(3632)  评论(0编辑  收藏  举报