function checkPlatformUserLoginStatus(platformIds, showMessageFlag, loginUrlRedirectFlag) {
    var platformNamesInput = [];
    for (let i in platformIds){
        if (window.globalConfig.platformIdNameMap.hasOwnProperty(platformIds[i])){
            platformNamesInput.push(window.globalConfig.platformIdNameMap[platformIds[i]]);
        }
    }
    $.each(window.globalConfig.loginStatusCheckUrlMaps, function (platformName, matchInfo) {
        if (platformName == 'tmall' && document.location.href.indexOf('taobao') > -1) {
            return;
        }
        chrome.runtime.sendMessage({type: 'getCookies', url: matchInfo.url}, function (cookies) {
            chrome.runtime.sendMessage({
                type: 'getBackgroundCacheValue',
                key: 'platformLoginInfo'
            }, function (platformLoginInfo) {
                platformLoginInfo = platformLoginInfo || {};
                if (matchInfo.regExpUrl.exec(document.location.href) || platformNamesInput.indexOf(platformName) > -1) {
                    console.log(platformLoginInfo);
                    if (!platformLoginInfo.hasOwnProperty(platformName) || platformLoginInfo[platformName].hasOwnProperty('loginState') || platformNamesInput.indexOf(platformName) > -1) {
                        if (platformLoginInfo.hasOwnProperty(platformName) && platformLoginInfo[platformName]['loginState'] && (new Date().getTime() / 1000 - platformLoginInfo[platformName]['time'] < 10*60)) {
                            console.log('登录成功后10分钟内不再检测', platformName);
                            return;
                        }

                        console.log('checkPlatformUserLoginStatus', platformName);
                        chrome.runtime.sendMessage({
                            type: 'ajax',
                            url: matchInfo.url,
                        }, function (response) {
                            var loginInfo = {
                                loginState: false
                            };

                            if (response.indexOf(matchInfo.loginOutWords) > 0) {
                                showMessageFlag && showMessage('{0} 采购账号未登录，<a href="{1}" style="color: red" target="_blank">去登录</a><br>'.format(window.globalConfig.loginUrlMaps[platformName].platformName, window.globalConfig.loginUrlMaps[platformName].url));
                                if (loginUrlRedirectFlag) {
                                    var redirectParamName = 'redirectURL';
                                    if (platformName == '1688') {
                                        redirectParamName = 'Done';
                                    } else if (platformName == 'jd') {
                                        redirectParamName = 'ReturnUrl';
                                    } else if (platformName == 'pdd') {
                                        redirectParamName = 'from';
                                    } else if (platformName == '17zwd') {
                                        redirectParamName = 'redirectUrl';
                                    } else if (platformName == 'vvic') {
                                        redirectParamName = 'reurl';
                                    }
                                    window.location.href = window.globalConfig.loginUrlMaps[platformName].url + '?' + redirectParamName + '=' + encodeURIComponent(window.location.href);
                                }
                            } else {
                                var ret = null;
                                for (let i = 0; i < 3; i++) {
                                    if (platformName == 'pdd' || platformName == 'hznzcn') {
                                        ret = matchInfo.regExpUserName.exec(response);
                                    } else {
                                        ret = matchInfo.regExpUserName.exec(cookies);
                                    }
                                    if (ret) {
                                        break;
                                    }
                                }
                                if (ret && ret[1] || ret && ret[2]) {
                                    loginInfo['loginState'] = true;
                                    loginInfo['username'] = ret[1] ? ret[1] : ret[2];
                                    loginInfo['username'] = unescape(decodeURIComponent(loginInfo['username']).replace(new RegExp('\\\\'),'%'));
                                    loginInfo['time'] = new Date().getTime() / 1000;
                                    loginInfo['dateTime'] = timeStampToDate(loginInfo['time']);
                                }
                            }
                            platformLoginInfo[platformName] = loginInfo;
                            if (platformName == 'taobao') {
                                platformLoginInfo['tmall'] = loginInfo;
                            } else if (platformName == 'tmall') {
                                platformLoginInfo['taobao'] = loginInfo;
                            }
                            chrome.runtime.sendMessage({
                                type: 'setBackgroundCacheValue',
                                key: 'platformLoginInfo',
                                value: platformLoginInfo
                            });
                        });
                    }
                }
            });
        });
    });
}

function checkShopUserLoginStatusPop(){
    var patterns = [
        {
            'domain': 'shop.jd.com',
            'regExpVenderId': /"venderId":"(\d+)"/g,
            'regExpShopName': /"shopName":"(.*?)"/g
        }
    ];

    for(let i = 0; i < patterns.length; i++) {
        if (document.location.href.indexOf(patterns[i].domain) > -1) {
            chrome.runtime.sendMessage({type: 'getBackgroundCacheValue', key: 'serverUrls'}, function (serverUrls) {
                chrome.runtime.sendMessage({
                    type: 'ajax',
                    url: serverUrls['venderInfoUrl'].format(Date.now(), Date.now()),
                }, function (response) {
                    var venderIdResult = patterns[i].regExpVenderId.exec(response);
                    var shopNameResult = patterns[i].regExpShopName.exec(response);
                    if (venderIdResult && shopNameResult) {
                        console.log(venderIdResult[1]);
                        console.log(shopNameResult[1]);
                        chrome.runtime.sendMessage({
                            type: 'setBackgroundCacheValue',
                            key: 'venderInfos',
                            value: {venderId: venderIdResult[1], shopName: shopNameResult[1]}
                        });
                        setTimeout(function () {
                            chrome.runtime.sendMessage({type: 'reloadTabPage', url: chrome.runtime.getURL('collect.html')});
                        }, 500);
                    }
                });
            });
        }
    }
}

function checkShopUserLoginStatusXd(){
    var shopName = '';
    var venderId = '';

    var shopNameElemXd = $('.shop-menu-account__right-account-shop-name');
    var venderIdElemXd = $('.shop-menu-account__account-dropdown-top-txt:eq(0)');
    if (shopNameElemXd && venderIdElemXd) {
        shopName = shopNameElemXd.text();
        venderId = venderIdElemXd.text().replace(' 商家ID：', '');
    }
    console.log(shopName);
    console.log(venderId);
    if (shopName && venderId) {
        chrome.runtime.sendMessage({
            type: 'setBackgroundCacheValue',
            key: 'venderInfos',
            value: {venderId: venderId, shopName: shopName}
        });
        setTimeout(function () {
            chrome.runtime.sendMessage({type: 'reloadTabPage', url: chrome.runtime.getURL('collect.html')});
        }, 500);
    }
}

function checkShopUserLoginStatus() {
    var patterns = [
        {
            'domain': 'jp.shujymei.com',
            'regExp': /(jpredis_session=.*?);|(jpredis_session=.*?)$/g
        },
        {
            'domain': 'ym.shujymei.com',
            'regExp': /(ymredis_session=.*?);|(ymredis_session=.*?)$/g
        },
        {
            'domain': 'ym.xhs.shujymei.com/',
            'regExp': /(ymredisxhs_session=.*?);|(ymredisxhs_session=.*?)$/g
        },
        {
            'domain': 'ymdy.shujymei.com/',
            'regExp': /(redisdy_session=.*?);|(redisdy_session=.*?)$/g
        }
    ]


    // 检测京麦商家登录状态
    if (window.location.href.indexOf('shop.jd') > -1) {
        // if ($('.shop-menu-account__right-account-shop-name').length) {
        //     checkShopUserLoginStatusXd();
        // } else {
        //     checkShopUserLoginStatusPop();
        // }
    } else {
        for(let i = 0; i < patterns.length; i++) {
            if (document.location.href.indexOf(patterns[i].domain) > -1) {
                chrome.runtime.sendMessage({type: 'getBackgroundCacheValue', key: 'serverUrls'}, function (serverUrls) {
                    chrome.runtime.sendMessage({type: 'getCookies', url: serverUrls['indexPageUrl']}, function(cookies) {
                        var results = cookies.match(patterns[i].regExp);
                        for(let j = 0; j < results.length; j++) {
                            var sessionKey = results[j].replace(';', '').replace(' ', '');
                            console.log(sessionKey);
                            chrome.runtime.sendMessage({
                                type: 'ajax',
                                url: window.globalConfig.parseVenderInfoUrl.format(encodeURIComponent(sessionKey)),
                            }, function (response) {
                                var responseData = JSON.parse(response);
                                responseData['venderId'] && chrome.runtime.sendMessage({
                                    type: 'setBackgroundCacheValue',
                                    key: 'venderInfos',
                                    value: responseData
                                });
                                setTimeout(function () {
                                    chrome.runtime.sendMessage({type: 'reloadTabPage', url: chrome.runtime.getURL('collect.html')});
                                    // chrome.runtime.sendMessage({type: 'reloadTabPage', url: 'https://www.baidu.com'});
                                }, 500);
                            });
                        }
                    });
                });
                break;
            }
        }
    }
}


setTimeout(function () {
    checkPlatformUserLoginStatus([], false, true);
}, 1000);
setTimeout(checkShopUserLoginStatus, 3000);