import axios from "axios";
import Message from 'ant-design-vue/lib/message';
import store from '../store'
export const myRequest = {
    BaseTIP: process.env.VUE_APP_BaseTIP,
    base(option = { url, data: {}, method: 'post', type: '', headers: {}, params: {}, token: true }) {
        let { url, data, method, type, headers, params, token } = { ...option };
        let resType = type == 'blob' ? { responseType: 'blob' } : {};
        return new Promise((resolve, reject) => {
            axios({
                headers: {
                    "Content-Type": type == 'file' ? 'multipart/form-data' : "application/json",
                    '.aspnetcore.culture': 'zh-Hans',
                    authorization: localStorage.getItem("Token") ? 'Bearer ' + localStorage.getItem("Token") : '',
                    ...headers,
                },
                params: method == 'get' || method == 'delete' ? data : { ...params },
                method: method,
                url: myRequest.BaseTIP + url,
                ...resType,
                data: type ? data : method != 'get' || method != 'delete' ? JSON.stringify(data) : ''
            }).then(response => {
                let resHeader = response.headers || {}
                if (type == 'blob') {
                    response.data.headers = resHeader;
                    resolve(response.data)
                } else if (response.data.Code) {
                    if (response.data.Code == 200) {
                        resolve(response.data.result)
                    } else {
                        reject(response.data.Msg);
                    }
                } else {
                    resolve(response.data.result)
                }
            }).catch(err => {
                console.log(err.response,'err.response')
                if (err.response.status == 401) {
                    store.commit('ToLogin')
                }
                console.log(err.response, 'err')
                var status = (err?.response?.status) || '500';
                let errorObj = err.response.data.error;
                Message.error(errorObj.message)
                reject(errorObj);
            });
        })
    },
}