/*
* chinanet 接口总文件
* by wzdxx1314@163.com
* 2021-6-1
* */
// import {Message } from 'view-design';

// import path from "../../config/path";
import qs from 'qs';

const axios = require('axios');
import {Message} from "element-ui";
import router from "@/router";


const instance = axios.create({

    baseURL: '/api',

    timeout: 30000,
});


/*
*
* 请求的预处理 by wzd
* */
instance.interceptors.request.use((config) => {
    config.headers['Accept'] = '';
    config.headers['X-Requested-With'] = 'XMLHttpRequest';
    config.headers['Authorization'] =  localStorage.getItem('token');
    return config;
}, (error) => {
    return Promise.reject(error);
});

/*
*
* 返回的预处理 by wzd
* */
instance.interceptors.response.use(function (response) {
    // Do something with response data
    return response.data;
}, function (error) {
    // console.log(error);
    return Promise.reject(error);
});


const instanceMusic = axios.create(
    {
        baseURL:'musicApi',
        timeout:5000,
    }
)

instanceMusic.interceptors.request.use((config) => {
    // config.headers['Accept-Encoding'] = 'gzip';
    // config.headers['User-Agent'] = 'Dalvik/2.1.0 (Linux; U; Android 12; M2006J10C Build/SP1A.210812.016)';

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

instanceMusic.interceptors.response.use(function (response) {
    // Do something with response data
    return response.data;
}, function (error) {
    // console.log(error);
    return Promise.reject(error);
});

function fetch(ourl, params = {}, methods = 'post') {
    return new Promise((resolve, reject) => {
        let url = ourl;
        if (methods == 'get') {
            url = ourl + '?' + qs.stringify(params);
        }
        instance[methods](url, params).then(res => {
            if (res.code == 1) {
                resolve(res)
            } else if (res.code == 401 || res.code == 201) {
                router.push({name:'login'})
            } else {
                Message.error(res.msg)
                reject(res);
            }


        }).catch((err) => {
            Message.error(err.message);
            reject(err);
        })
    })
}

function fetchThrid(ourl, params = {}, methods = 'post') {
    return new Promise((resolve, reject) => {
        let url = ourl;
        if (methods == 'get') {
            url = ourl + '?' + qs.stringify(params);
        }
        instanceMusic[methods](url, params).then(res => {

            if (res.status == 1 || res.code == 0) {
                resolve(res)
            } else if (res.code == 401 || res.code == 201) {
                router.push({name:'login'})
            } else {
                Message.error(res.msg)
                reject(res);
            }
        }).catch((err) => {
            Message.error(err.message);
            reject(err);
        })
    })
}
export default {
    //登录
    login(params) {
        return fetch('/passport/', params,"post");
    },

    //活动List
    voteList(params){
        return fetch('/vote_activities/section/',params,'get');
    },

    //获取验证码

    verificationCode(params){
        return fetch('/verificationCode/',params,"get")
    },

    votes(params){
        return fetch('/votes/',params,"get")
    },
    addVoteActivity(params) {
        return fetch("/vote_activity/",params,"put");
    },
    editVoteActivity(params) {
        return fetch("/vote_activity/",params,"post");
    },
    addVote(params){
        return fetch('/vote/',params,"put")
    },
    editVote(params){
        return fetch('/vote/',params,"post")
    },
    noticeList(params){
        return fetch('/notices',params,"get")
    },
    addNotice(params){
        return fetch('/notice',params,"put");
    },
    editNotice(params){
        return fetch('/notice',params,"post");
    },
    getNoticeInfoById(params){
        return fetch('/notice',params,"get");
    },

    getHouseRelations(params){
        return fetch('/houseRelations',params,'get')
    },
    verifyHouseRelation(params){
        return fetch('/houseRelation/verify',params,'put')
    },
    voteStatis(params){
        return fetch('/vote/result',params,'get')
    },
    suggestion(params){
        return fetch('/suggestion/s',params,'get')
    },
    choices(params){
        return fetch('/vote/option/s/choice/s',params,'get')
    }




}

