/*
* chinanet 接口总文件
* by wzdxx1314@163.com
* 2021-6-1
* */

const axios = require('axios');
const qs = require('qs')
import router from "../router";
import { Dialog ,Notify } from 'vant';
const instance = axios.create({
    // baseURL: 'api',
    baseURL: '/api/',
    timeout: 30000,
});
const instanceFormData = axios.create({
    // baseURL: 'api',
    baseURL: '/api/',
    timeout: 30000,
});

/*
*
* 请求的预处理 by wzd
* */
instance.interceptors.request.use((config) => {
    config.headers['Accept'] = '';
    config.headers['X-Requested-With'] = 'XMLHttpRequest';
    return config;
}, (error) => {
    return Promise.reject(error);
});
instanceFormData.interceptors.request.use((config) => {
    config.headers['Accept'] = '';
    config.headers['X-Requested-With'] = 'XMLHttpRequest';
    config.headers['Content-Type'] = 'multipart/form-data';
    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);
});
instanceFormData.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' && qs.stringify(params)) {
            url = ourl + '?' + qs.stringify(params);
        }
        instance[methods](url, params).then(res => {
            if(res.code == 1){
                resolve(res)
            }else if(res.code == 104){

            }else if(res.code == 201){
                Dialog.confirm({
                    title: '未登录',
                    message: '是否回到登录页',
                })
                    .then(() => {
                        router.push({name:'login'})
                    })
                    .catch(() => {
                        // on cancel
                    });
            }else{
                Notify(res.message);
                reject(err)
            }

        }).catch((err) => {
            reject(err)
        })
    })
}
function fetchFormData(ourl, params = {}, methods = 'post') {
    return new Promise((resolve, reject) => {
        let url = ourl;
        if (methods == 'get' && qs.stringify(params)) {
            url = ourl + '?' + qs.stringify(params);
        }
        instanceFormData[methods](url, params).then(res => {
            if(res.code==1){
                resolve(res)
            }else{
                Notify(res.message);
                reject(res)
            }

        }).catch((err) => {
            reject(err)
        })
    })
}

export default {
    login(params){
        return fetch('passport/',params)
    },
    getUserInfo(params){
        return fetch('passport/',params,'get')
    },
    verificationCode(params){
        return fetch('verificationCode/',params,'get')
    },
    resiger(params){
        return fetch('verificationCode/',params,'post')
    },
    getVote_activities(params){
        return fetch('vote_activities/section',params,'get')
    },
    getVotes(params){
        return fetch('votes',params,'get')
    },
    getNotices(params){
        return fetch('/notices',params,'get')
    },
    getNotice(params){
        return fetch('/notice',params,'get')
    },
    upload(params){
        return fetchFormData('/house/cert',params,)
    },
    houseBlind(params){
        return fetch('house/passport/',params,'put')
    },
    //投票 choice
    voteItem(params){
        return fetch('choice',params,'put')
    },
    getVoteResult(params){
        return fetch('vote/result',params,'get')
    },
    getSuggestion(params){
        return fetch('suggestion/s',params,'get')
    },
    getVoteOptions(params){
        return fetch('option/s',params,'get')
    },
    getEstates(params){
        return fetch('estates/',params,'get')
    },

}
