let systenInfo = localStorage.getItem('systemInfo') ? JSON.parse(localStorage.getItem('systemInfo')) : {
    'device_id': 0,
    'version': 0,
    'channel': 0,
    'cs_product_uuid': 0,
    'os_version': '',
    'brand': '',
    'model': '',
    'screen_size': '',
    'network_type': ''
}
let headers = {
        'appType': 1,
        'userId': localStorage.getItem('userId') || 0,
        'appVersion': systenInfo && systenInfo.version ? systenInfo.version : 0,
        'deviceId': systenInfo && systenInfo.device_id ? systenInfo.device_id : 0,
        'appChannel': systenInfo && systenInfo.channel ? systenInfo.channel : 0,
        'system': checkProcessor()
    }
    // 启动客户端默认先读取index中存入的信息，若获取不到，再次获取信息
if (!headers.deviceId || !headers.appVersion || !headers.appChannel) {
    Client.getSystemInfo().then((res) => {
        localStorage.setItem('systemInfo', res)
        systenInfo = JSON.parse(res)
        if (systenInfo) {
            headers.appChannel = systenInfo.channel || 0
            headers.appVersion = systenInfo.version || 0
            headers.deviceId = systenInfo.device_id || 0
        }
    })
}
if (!headers.userId) {
    Client.getData('userInfo').then(returnValue => {
        let userInfo = returnValue ? JSON.parse(returnValue) : {
            userId: 0,
            phone: '',
            nickname: '未登录',
            headUrl: '',
            email: ''
        }
        headers.userId = userInfo.userId || 0
    })
}

let baseURL
if (is_test) {
    baseURL = 'http://pctest.migufun.com:8089'
} else {
    baseURL = 'https://pc-api.migufun.com:8089'
}

function getHeaderInfo() {
    return new Promise(function(resolve, reject) {
        if (!headers.userId) {
            Client.getData('userInfo').then(returnValue => {
                let userInfo = returnValue ? JSON.parse(returnValue) : {
                    userId: 0,
                    phone: '',
                    nickname: '未登录',
                    headUrl: '',
                    email: ''
                }
                headers.userId = userInfo.userId || 0
                if (!headers.deviceId || !headers.appVersion || !headers.appChannel) {
                    Client.getSystemInfo().then((res) => {
                        localStorage.setItem('systemInfo', res)
                        systenInfo = JSON.parse(res)
                        if (systenInfo) {
                            headers.appChannel = systenInfo.channel || 0
                            headers.appVersion = systenInfo.version || 0
                            headers.deviceId = systenInfo.device_id || 0
                        }
                        console.log('headers2', headers)
                        Client.log(2, '头部信息2:' + JSON.stringify(headers))
                        resolve(true)
                    })
                } else {
                    // 获取用户信息成功
                    console.log('headers1', headers)
                    Client.log(2, '头部信息1:' + JSON.stringify(headers))
                    resolve(true)
                }

            })
        } else {
            // 获取用户信息成功
            console.log('headers3', headers)
            Client.log(2, '头部信息3:' + JSON.stringify(headers))
            resolve(true)
        }
    })
}


function getSystemInfo() {
    if (systenInfo.device_id) {
        return systenInfo
    } else {
        return localStorage.getItem('systemInfo') ? JSON.parse(localStorage.getItem('systemInfo')) : {
            'device_id': 0,
            'version': 0,
            'channel': 0,
            'cs_product_uuid': 0,
            'os_version': '',
            'brand': '',
            'model': '',
            'screen_size': '',
            'network_type': ''
        }
    }
}

function bootAdsInfo() {
    console.log("bootAdsInfoheader", headers)
    let bootAdsInfoData = {
        adsType: 1,
        useControlShow: 1
    }
    let url = baseURL + '/extend/client/bootAdsInfo'
    return new Promise(function(resolve, reject) {
        $.ajax({
            type: "post",
            url: url,
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify(bootAdsInfoData),
            headers: headers,
            success: (res) => {
                Client.log(2, url + ' || 头部信息:' + JSON.stringify(headers) + ' || 入参:' + JSON.stringify(bootAdsInfoData) + ' || 返回码:' + JSON.stringify(res))
                resolve(res)
            },
            error: (err) => {
                reject(err)
            }
        });
    })
}

function postJinFenLogApi(param) {
    let url = baseURL + '/dataReport/logs/biReport'
    $.ajax({
        type: "post",
        url: url,
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify(param.data),
        headers: param.headers
    });
}

function checkProcessor() {
    var b, fi, w
    w = window.navigator.platform
    fi = function(s) {
        return window.navigator.userAgent.indexOf(s) > -1
    }
    if (fi('x86_64') || fi('x86-64') || fi('Win64') || fi('x64;') || fi('amd64') || fi('AMD64') || fi('WOW64') || fi('x64_64') || w === 'MacIntel' || w === 'Linux x86_64') {
        b = 64
    } else if (w === 'Linux armv7l' || w === 'iPad' || w === 'iPhone' || w === 'Android' || w === 'iPod' || w === 'BlackBerry') {
        b = 0
    } else if (w === 'Linux i686') {
        b = -1
    }

    if (b === -1) {
        return 0
    } else if (b === 0) {
        return 0
    } else if (b === 64) {
        // 64位
        return 2
    } else {
        // 32位
        return 1
    }
}