import service from '@/utils/request'
//获取ip列表
export const ipLogList = (data) => {
    return service({
        url: '/ipLog/getIpLogList',
        method: 'post',
        data
    })
}

//获取ip
export const getIp = (data) => {
    return service({
        url: '/ipLog/getIp',
        method: 'post',
        data
    })
}

//gameId分组获取ip列表
export const gameIpList = (data) => {
    return service({
        url: '/ipLog/getGameIpList',
        method: 'post',
        data
    })
}

//gameId分组获取ip
export const getGameIp = (data) => {
    return service({
        url: '/ipLog/getGameIp',
        method: 'post',
        data
    })
}


const handleFileError = (res, fileName) => {
    if (typeof (res.data) !== 'undefined') {
        if (res.data.type === 'application/json') {
            const reader = new FileReader()
            reader.onload = function () {
                const message = JSON.parse(reader.result).msg
                ElMessage({
                    showClose: true,
                    message: message,
                    type: 'error'
                })
            }
            reader.readAsText(new Blob([res.data]))
        }
    } else {
        var downloadUrl = window.URL.createObjectURL(new Blob([res]))
        var a = document.createElement('a')
        a.style.display = 'none'
        a.href = downloadUrl
        a.download = fileName
        var event = new MouseEvent('click')
        a.dispatchEvent(event)
    }
}



//导出gameIp列表
export const gameIpExport = (tableData, fileName) => {
    service({
        url: '/ipLog/gameIpExport',
        method: 'post',
        data: {
            fileName: fileName,
            infoList: tableData
        },
        responseType: 'blob'
    }).then((res) => {
        handleFileError(res, fileName)
    })
}

//获取异常ip列表
export const getAbnormalIpLogList = (data) => {
    return service({
        url: '/ipLog/getAbnormalIpLogList',
        method: 'post',
        data
    })
}