import store from "@/store";

var doubleClickList = []; //防连击的数组对象
// 请求地址对象
const endpoints = {
    upload: 'elab-marketing-system/behavior/brandMiniWeb/upload', //埋点接口
    login: 'elab-marketing-analyse/cockpit/login', //用户登录
    uploadBase64: 'elab-marketing-file/upload/uploadBase64', //uploadBase64
	// // webGL接口
	getHouseTypeSpaceDetailsV2: 'elab-marketing-file/space_generation_background/getHouseTypeSpaceDetails/v2', //V1.1.0-城市首页
	getHouseTypeSpaceDetails: 'elab-marketing-file/space_generation_background/getHouseTypeSpaceDetails', //V1.1.0-城市首页
	
	//    // 柔性定制接口
	getHouseTypeSpaceWalls: '/elab-marketing-file/space_generation_background/getHouseTypeSpaceWalls', // 批量获取墙面信息
	
	houseDetail: 'elab-marketing-user/house/detail',
	findHouseTypeSpaceList: 'elab-marketing-file/space_generation_background/findHouseTypeSpaceList',
	layoutList: 'elab-marketing-content/aiDreamHouse/V3.0/layoutList',
	labelList: 'elab-marketing-content/aiDreamHouse/V3.0/labelList',
	findOverallArrangementList: 'elab-marketing-file/space_generation_background/findOverallArrangementList',
	getOverallArrangementDetailsList: 'elab-marketing-file/space_generation_background/getOverallArrangementDetailsList',
	choiceList: "elab-marketing-content/aiDreamHouse/V3.0/choiceList",
	getHardboundEffects: "elab-marketing-file/space_vary/getHardboundEffects",
	uploadImgControlNet: "elab-marketing-content/aiBeautyFamily/uploadImgControlNet",
	// generateProcess: "elab-marketing-content/aiBeautyFamily/generateProcess",
	getUploadToken: "elab-marketing-file/upload/getUploadToken",
	findSpatialInfoList: "elab-marketing-file/flexible_customization/findSpatialInfoList",
	
	AIuploadImgControlNet: 'elab-marketing-content/aiGenerateImg/uploadImgControlNet', //ControlNet图片上传
	saveCustomizedRecord: 'elab-marketing-file/space_vary/saveCustomizedRecord', //用户AI信息上传
	getCustomizedRecord: 'elab-marketing-file/space_vary/getCustomizedRecord', //获取用户定制记录
	deleteCustomizedRecord: 'elab-marketing-file/space_vary/deleteCustomizedRecord', //删除用户定制记录
	chineseToPinyin: 'elab-marketing-file/tools/chineseToPinyin', //汉字转拼音
};
window.requestConfig = async(endpoint, options, isHideLoading = false, preventDoubleClick = false, method = 'post') => {
	if (!endpoints.hasOwnProperty(endpoint)) {
        console.error(`no such endpoint: ${endpoint}`);
        return {
            message: "不存在该接口"
        };
    }
    const requestOptions = {
        path: endpoints[endpoint],
        data: options,
    };
    if (preventDoubleClick) {
        //说明该接口需要防止连击
        if (doubleClickList[requestOptions.path] && doubleClickList[requestOptions.path].isRunning) {
            console.log(`${requestOptions.path}触发连击，屏蔽该请求`)
            return
        } else {
            console.log(`${requestOptions.path}触发连击，通过`)
            doubleClickList[requestOptions.path] = {
                isRunning: true
            }
        }
    }
    console.log(`获取${requestOptions.path}请求的参数:`, requestOptions.data)
    requestOptions.url = requestOptions.path;
    // var t = Math.round(new Date());
    // requestOptions.time = t;

    if (!requestOptions.url.includes('/elab-marketing-user/aysn/task/byone') &&
        !requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/saleTarget/add') &&
        !requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/customer/coverPopulation') &&
        !requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/customer/intention') &&
        !requestOptions.url.includes('elab-marketing-analyse/urbanCockpit/sky_eye_marketing/getAdCreativeDraftCheckResult') &&
        !requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/getCompareMapTotalPopulation')
    ) {
        store.state.loading = true; //显示loading态
    }
    if (isHideLoading) {
        store.state.loading = false; //显示loading态
    }
    if (requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/importLocations') ||
        requestOptions.url.includes('elab-marketing-sms/aliyun/openapi/uploadOss')) {
        requestOptions.ContentType = 'multipart/form-data'
    }
    if (requestOptions) {
        let platform = (window.userInfo && window.userInfo['userAccount'] && window.userInfo['userAccount'].platform) ? window.userInfo['userAccount'].platform : ''; //TODO 要还原成‘’的
        let currentProject = store.state.currentProject ? store.state.currentProject : null;
        let landId = currentProject ? currentProject.id : '';
        requestOptions.data = requestOptions.data ? requestOptions.data : {};
        let userTypeId = window.userInfo && window.userInfo.userType ? window.userInfo.userType.id : ''; //1客户端 ，2投手端
        if (userTypeId == 2) {
            if (!requestOptions.data.hasOwnProperty('platform') || !requestOptions.data.platform) {
                requestOptions.data.platform = platform
            }
        }
        if (!requestOptions.data.hasOwnProperty('landId') || !requestOptions.data.landId) {
            requestOptions.data.landId = landId
        }
    }
    var response;
    if (requestOptions.url.includes('http') || method == 'get') {
        let params = requestOptions.data;
        response = await axios.get(requestOptions.url, { params });
    } else {
        response = await axios.post(requestOptions.url, requestOptions.data);
    }
    //说明该接口需要防止连击
    if (preventDoubleClick && doubleClickList[requestOptions.path] && doubleClickList[requestOptions.path]
        .isRunning) {
        console.log(`${requestOptions.path}连击接口已返回，清空连击数组`)
        doubleClickList[requestOptions.path].isRunning = false
    }
    // console.log('返回' + requestOptions.path + '请求的结果:', response)
    return response;
}
