import axios from 'axios'
// import qs from 'qs'
// import message from '@/components/public/message/src'
import router from '@/router/index'
import {getCookie} from '@/tool/cookie'
// import cookie from '@/tools/cookie.js'

axios.defaults.timeout = 60000;                        //响应时间
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';        //配置请求头
axios.defaults.headers.post["Content-Type"] = "application/json";


axios.defaults.headers.post['lang'] = 'en';        //配置语言请求头
axios.defaults.withCredentials = true;  //跨域携带cookie
import { message } from 'ant-design-vue';

// if(process.env.NODE_ENV == "development"){
// 	axios.defaults.baseURL = "";   //配置接口地址
// }else{
// 	axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;   //配置接口地址
// }
// let httpIp 
// if(process.env.NODE_ENV == 'development'){
// 	httpIp = 'http://192.168.1.12:10086'
// }else{
// 	httpIp = ''
// }
let httpIp = process.env.NODE_ENV == 'development' ? "" : "";
// let httpIp = process.env.NODE_ENV == 'development' ? "http://192.168.1.10:10086" : "";
console.log(httpIp);
axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;   //配置接口地址
// console.log(process.env.VUE_APP_BASE_URL);
//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
    //在发送请求之前做某件事
    if(config.method  === 'post' || config.method  === 'put' || config.method  === 'delete'){
        // config.data = qs.stringify(config.data);
        // config.data = JSON.stringify(config.data);
    }

    config.headers.Authorization = getCookie('token'); 
    return config;
},(error) =>{
    return Promise.reject(error);
});

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) =>{
	// if(res.data.data == null){
	// 	message.warning(res.data.errMsg)
    //     return Promise.reject(res.data);
	// }else 
	if (res.data) {
        if (res.data.errCode === 0) {
            // message.error(res.data.errMsg)
            return Promise.resolve(res.data.data);
        } else {
            message.warning(res.data.errMsg)
            return Promise.reject(res.data);
        }

    } else {
        message.warning(res.data.errMsg)
        // message.error(res.data.errMsg)
        return Promise.reject(res.data);
    }
    
}, function(error) {
    if(error?.response?.status === 401){
        router.replace('/login')
        return Promise.reject()
    }
    let data_new = error?.response?.data
    message.error(data_new?.errMsg || 'Error: server exception')
    return Promise.reject(data_new);
    
});

export const Https = {
    httpUrls: {
        interfaceUrl: '',
        accountIsLogin:'/api/account/isLogin', //判断用户是否登录
        accountLogin:`/api/account/login`, //账号密码登录接口
        preLogin:'/api/account/preLogin',//预先登入
        accountSendEmail:`/api/account/sendEmail`, //发送邮件
        accountResetPwd:'/api/account/resetPwd', //忘记密码修改
        accountLogout:'/api/account/logout',//登出
        accountBindEmail:'/api/account/bindEmail', //绑定邮箱
        elementGeneratePrint:'/api/element/generatePrint', //生成印花
        elementSavePrint:'/api/element/savePrint',//保存印花
        getRgbByTcx:'/api/element/getRgbByTcx', // 通过hsv值获取潘通信息
        getRgbByHsv:'/api/element/getRgbByHsv', //通过hsv值获取潘通信息
        designCollection:`/api/design/designCollection`, //设计 Conllection
        reDesignCollection:`/api/design/reDesignCollection`,//重新设计 Conllection
        countDesignProcess:'/api/design/countDesignProcess', //统计design进度
        designProcess:`/api/design/designProcess`, //统计design进度
		
        getRgbByHsvBatch:`/api/element/getRgbByHsvBatch`, //通过hsv值数组批量获取潘通信息
        designLike:`/api/design/like`, //Design Like
        designDislike: `/api/design/dislike`, //Design Dislike
        queryUserGroup:`/api/history/queryUserGroup`, //History用户分页分组列表
        deleteUserGroup:`/api/history/deleteUserGroup`, //History删除用户分组
        updateUserGroupName:`/api/history/updateUserGroupName`, //History修改用户分组名
        historyChoose:`/api/history/choose`, //History choose
        getDesignDetail:`/api/design/detail/getDetail`,//查询design详情
        getNextSysElement:'/api/design/detail/getNextSysElement',//切换系统的element
        detailPrintDot:'/api/design/detail/printDot',//print打点预览
        designSingle:`/api/design/detail/designSingle`,//单个design
        queryLibraryPage:`/api/library/queryLibraryPage`,//Library分页列表
        libraryUpload:`/api/library/upload`, // Library文件上传
        setSketchLibrary:`/api/library/updateLibraryLevel2Type`, // Library文件上传
		
        batchUpdateLibraryName:'/api/library/batchUpdateLibraryName',//Library修改用户文件名
        batchDeleteLibrary:'/api/library/batchDeleteLibrary',//删除library
        queryLibraryTopAndBottomPage:'/api/library/queryLibraryTopAndBottomPage',//Library分页列表(查询top和bottom)
        saveOrEditTemplatePoint:'/api/library/saveOrEditTemplatePoint',//保存或者编辑template打点
        libraryModelsDot:'/api/library/modelsDot',//Models打点预览
        chatStreamTest:`/api/python/chatStream`,//机器人助力
        pictureLikeOrUnLike:`/api/python/pictureLikeOrUnLike`,//机器人生成图喜欢
        getBloodBars:`/api/python/getBloodBars`,//机器人血条
	
        workspaceDetail:`${httpIp}/api/workspace/detail`,//用户习惯详情
        workspaceenumValues:`/api/workspace/enumValues`,//getSex
		
        workspaceRemove:`${httpIp}/api/workspace/remove`,//删除用户习惯详情
        workspacesaveOrUpdate:`${httpIp}/api/workspace/saveOrUpdate`,//修改用户习惯详情
        getMannequins:`${httpIp}/api/workspace/getMannequins`,//模特
		
		workspaceList:`${httpIp}/api/workspace/list`,
        sketchAndPrintGenerate:'/api/generate/sketchAndPrint',//sketchGenerate生成图片
		generateLike:'/api/generate/like',//喜欢ganerate图片
		generateDislike:'/api/generate/dislike',//喜欢ganerate图片
        elementUpload:`/api/element/upload`,//上传图片
		
    },

    axiosGet(url,config) {
        return new Promise((resolve, reject) => {
            axios.get(url,config).then(response => {
                    resolve(response)
                }).catch((error) => {
                    reject(error)
            })
        });    
    },

    axiosPut(url, data) {
        return new Promise((resolve, reject) => {
            axios.put(url, data).then(response => {
                    resolve(response)
                }).catch((error) => {
                    reject(error)
            })
        });    
    },

    axiosPost(url, data,config) {
        return new Promise((resolve, reject) => {
            axios.post(url, data,config).then(response => {
                    resolve(response)
                }).catch((error) => {
                    reject(error)
            })
        });    
    },

    axiosDelete(url, newData) {
        return new Promise((resolve, reject) => {
            axios.delete(url,{data:newData}).then(response => {
                    resolve(response)
                }).catch((error) => {
                    reject(error)
            })
        });    
    },

 



}
