import axios                   from 'axios';
import { MessageBox, Message } from 'element-ui';
import store                   from '@/store';
// import { getToken }            from '@/utils/auth';
import { Md5 }                 from 'ts-md5/dist/md5';
import router                  from '@/router';

let _ = require('lodash');

// create an axios instance
const service = axios.create({
                                 baseURL:         process.env.VUE_APP_BASE_API, // url = base url + request url
                                 withCredentials: false, // send cookies when cross-domain requests
                                 timeout:         10000, // request timeout
                             });

// request interceptor
service.interceptors.request.use(
    config => {
        // do something before request is sent

        return config;
    },
    error => {
        return Promise.reject(error);
    },
);

let stMsg =
    _.debounce((res) => {
        Message({
                    message:  res.code + '|' + res.msg,
                    type:     'error',
                    duration: 5 * 1000,
                });
    }, 500);

// 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 => {
        if (response.headers['content-disposition'] !== undefined) {
            //   describe: 修改 author: Wgy date:2019-07-23
            const str = response.headers['content-disposition'];
            response.fileName = str.substring(str.indexOf('=') + 1);
            return response;
        }
        const res = response.data;

        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 0) {
            stMsg(res);
            /*Message({
                        message:  res.msg || 'error',
                        type:     'error',
                        duration: 3 * 1000,
                    });*/


            // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
            /*if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
                // to re-login
                MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
                    confirmButtonText: 'Re-Login',
                    cancelButtonText:  'Cancel',
                    type:              'warning',
                }).then(() => {
                    store.dispatch('user/resetToken').then(() => {
                        location.reload();
                    });
                });
            }*/

            if (res.code === 1001) {

                setTimeout(function () {
                    if (window.loading1) {
                        window.loading1.close();
                    }
                }, 2000);
            } else if (res.code === 401) {
                // 401:登录超时
                setTimeout(function () {
                    if (window.loading) {
                        window.loading.close();
                    }
                    if (router.app.$route.path.indexOf('/c/') === 0) {
                        router.push('/c/login');
                    } else {
                        router.push('/a/login');
                    }
                }, 2000);
            } else {
                setTimeout(function () {
                    if (window.loading1) {
                        window.loading1.close();
                    }

                }, 2000);
            }

            if (res.code === 500 || res.code === 1001) {
                return Promise.reject('error' + res.code + '-' + (res.message === undefined ? 'nomessage': res.message));
            }

            return Promise.reject(res.msg || 'error');
        } else {
            return res;
        }
    },
    error => {
        Message({
                    message:  error.code + '|' + error.msg,
                    type:     'error',
                    duration: 3 * 1000,
                });
        return Promise.reject(error);
    },
);

export default service;
