var _favCleanKey = "MyGames_NeedClean"; var _favBallKind = BALL.SOCCER; var Config = {}; Config.getFavMatchIds = function () { let arr = new Array(); if (isUserLogin()) { arr = _favHttp.local.match.concat(); } else { arr = getFavCookie(FAVHTTP.Match); } return arr; } Config.setFavMatchIds = function (arr) { if (isUserLogin()) { setFavLocal(FAVHTTP.Match, arr); throttle(() => uploadUserFav(FAVHTTP.Match), 1 * 1000); } else { setFavCookie(FAVHTTP.Match, arr); } throttle(() => countFavMatch(), 2 * 1000); } Config.getFavLeagueIds = function () { let arr = new Array(); if (isUserLogin()) { arr = _favHttp.local.league.concat(); } else { arr = getFavCookie(FAVHTTP.League); } return arr; } Config.setFavLeagueIds = function (arr) { if (isUserLogin()) { setFavLocal(FAVHTTP.League, arr); throttle(() => uploadUserFav(FAVHTTP.League), 1 * 1000); } else { setFavCookie(FAVHTTP.League, arr); } throttle(() => countFavMatch(), 2 * 1000); } Config.getFavTeamIds = function () { let arr = new Array(); if (isUserLogin()) { arr = _favHttp.local.team.concat(); } else { arr = getFavCookie(FAVHTTP.Team); } return arr; } Config.setFavTeamIds = function (arr) { if (isUserLogin()) { setFavLocal(FAVHTTP.Team, arr); throttle(() => uploadUserFav(FAVHTTP.Team), 1 * 1000); } else { setFavCookie(FAVHTTP.Team, arr); } throttle(() => countFavMatch(), 2 * 1000); } Config.isFavNeedClean = function () { let cl = getCookie(_favCleanKey); if (CheckEmpty(cl) && !CheckEmpty(this.getFavMatchIds())) { return true; } return false; } Config.hasCleanMatchFav = function () { writeShareCookie(_favCleanKey, 1, new Date((new Date()).getTime() + 3 * 24 * 3600000)); } var _soccerInpageVersionCookie = "Soccer_SummaryInpage"; Config.isSummaryInpage = function () { let val = getCookie(_soccerInpageVersionCookie); if (val == 1) { return true; } else { return false; } }; var FAVHTTP = { League: 1, Team: 2, Match: 3 } let _favHttp = { /*帐户数据*/ account: { match: [], league: [], team: [], }, /*本地暂存,延迟存入帐户*/ local: { match: [], league: [], team: [] }, /*待新增/删除的Id*/ pending: [], userId: 0, isLogin: false, initEnd: false, readLocalKey: _favBallKind == BALL.SOCCER ? "FAVHTTP_READ_LOCAL_SOCCER" : "FAVHTTP_READ_LOCAL_LQ", enable: _appModule != "Kor" && _appModule != "Pt", }; !(async function () { if (_favHttp.enable) { fetch(`${_userWebDomain}/user/info`, { credentials: 'include' }) .then(async response => { if (response.ok) { const result = await response.json(); if (result.code == 200) { _favHttp.userId = result.data.id; _favHttp.isLogin = true; } if (isReadLocal()) { setFavStore(FAVHTTP.Match, getFavCookie(FAVHTTP.Match)); setFavStore(FAVHTTP.League, getFavCookie(FAVHTTP.League)); setFavStore(FAVHTTP.Team, getFavCookie(FAVHTTP.Team)); setFavLocal(FAVHTTP.Match, getFavCookie(FAVHTTP.Match)); setFavLocal(FAVHTTP.League, getFavCookie(FAVHTTP.League)); setFavLocal(FAVHTTP.Team, getFavCookie(FAVHTTP.Team)); } } }).then(() => { if (_favHttp.userId && !isReadLocal()) { Promise.all([getUserFav(FAVHTTP.League), getUserFav(FAVHTTP.Team), getUserFav(FAVHTTP.Match)]) .then(() => { _favHttp.initEnd = true; const account = _favHttp.account; if (account.league.length == 0 && account.team.length == 0 && account.match.length == 0) { //帐户无收藏,同步cookie数据到帐户 uploadFavCookie(FAVHTTP.League); uploadFavCookie(FAVHTTP.Match); uploadFavCookie(FAVHTTP.Team); } if (account.league.length > 0 || account.team.length > 0 || account.match.length > 0) { //帐户有收藏,同步帐户数据到cookie setFavCookie(FAVHTTP.League, account.league); setFavCookie(FAVHTTP.Match, account.match); setFavCookie(FAVHTTP.Team, account.team); //存本地读取标识,5分钟内读本地 writeShareCookie(_favHttp.readLocalKey, 1, new Date((new Date()).getTime() + 5 * 60000)); } }) } else { _favHttp.initEnd = true; } }) .catch(error => { _favHttp.initEnd = true; console.log("Error occurred while fetching user info:", error); }); //设置超时时间 setTimeout(() => { _favHttp.initEnd = true }, 5 * 1000); } else { _favHttp.initEnd = true; } })(); function getUserFav(t) { return new Promise((resolve,reject) => { const body = new URLSearchParams(); body.append("userId", _favHttp.userId); body.append("type", (_favBallKind == BALL.SOCCER ? 5 : 15)); body.append("t", t); fetch("/ajax/favajax", { method: "post", body }) .then(response => { if (!response.ok) { throw new Error(`status:${response.status}`) } return response.json(); }) .then(result => { if (result.ErrCode != -1) { let arr = result.Data; if (arr && arr.length > 0) { setFavStore(t, arr); setFavLocal(t, arr); } resolve(); } else { reject(); } }) .catch(err => { console.log(`get user fav failed. ${err}`); reject(err); }) }) } function uploadUserFav(t) { return new Promise((resolve,reject) => { const store = getFavStore(t); let pending = _favHttp.pending; const addArr = store.local.filter(id => !store.account.includes(id) && id != "" && !pending.includes(id)); const dropArr = store.account.filter(id => !store.local.includes(id) && id != "" && !pending.includes(id)); addArr.forEach(id => !pending.includes(id) && pending.push(id)) dropArr.forEach(id => !pending.includes(id) && pending.push(id)) //新增 const addPromise = () => new Promise((resolve, reject) => { if (addArr.length > 0) { const body = new URLSearchParams(); body.append("userId", _favHttp.userId); body.append("type", (_favBallKind == BALL.SOCCER ? 6 : 16)); body.append("t", t); body.append("ids", addArr.join(",")); fetch("/ajax/favajax", { method: "post", body }) .then(response => { if (!response.ok) { throw new Error(`status:${response.status}`) } return response.json(); }) .then(result => { console.log(result.Data); if (result.ErrCode != -1 && result.Data) { resolve(); } else { reject(); } }) .catch(err => { console.log(`add user fav failed. ${err}`) reject(); }) .finally(() => { _favHttp.pending = _favHttp.pending.filter(id => !addArr.includes(id)); }) } else { resolve(); } }) //删除 const dropPromise = () => new Promise((resolve, reject) => { if (dropArr.length > 0) { const body = new URLSearchParams(); body.append("userId", _favHttp.userId); body.append("type", (_favBallKind == BALL.SOCCER ? 7 : 17)); body.append("t", t); body.append("ids", dropArr.join(",")); fetch("/ajax/favajax", { method: "post", body }) .then(response => { if (!response.ok) { throw new Error(`status:${response.status}`) } return response.json(); }) .then(result => { console.log(result.Data); if (result.ErrCode != -1 && result.Data) { resolve(); } else { reject(); } }) .catch(err => { console.log(`drop user fav failed. ${err}`) reject(); }) .finally(() => { _favHttp.pending = _favHttp.pending.filter(id => !dropArr.includes(id)); }) } else { resolve(); } }) Promise.all([addPromise(), dropPromise()]) .then(() => { setFavStore(t, store.local); /*setFavCookie(t, store.local);*/ resolve(); }) .catch(err => reject(err)); }) } function getFavStore(t) { var store = new Object(); if (t == FAVHTTP.League) { store.key = _favBallKind == BALL.SOCCER ? "MyGames_LeagueID" : "MyGames_LQ_LeagueID"; store.local = _favHttp.local.league; store.account = _favHttp.account.league; } if (t == FAVHTTP.Team) { store.key = _favBallKind == BALL.SOCCER ? "MyGames_TeamID" : "MyGames_LQ_TeamID"; store.local = _favHttp.local.team; store.account = _favHttp.account.team; } if (t == FAVHTTP.Match) { store.key = _favBallKind == BALL.SOCCER ? "MyGames_MatchID" : "MyGames_LQ_MatchID"; store.local = _favHttp.local.match; store.account = _favHttp.account.match; } return store; } function setFavStore(t, arr) { if (Array.isArray(arr)) { arr = arr.map(String); if (t == FAVHTTP.Team) _favHttp.account.team = arr; if (t == FAVHTTP.Match) _favHttp.account.match = arr; if (t == FAVHTTP.League) _favHttp.account.league = arr; } /*setFavLocal(t, arr);*/ } function setFavLocal(t, arr) { if (Array.isArray(arr)) { arr = arr.map(String); if (t == FAVHTTP.Team) _favHttp.local.team = arr; if (t == FAVHTTP.Match) _favHttp.local.match = arr; if (t == FAVHTTP.League) _favHttp.local.league = arr; } setFavCookie(t, arr); } //定时查询是否可以开始渲染页面收藏相关 function startInitFavTimer(func) { var favTimer = null; var timerFunc = function () { favTimer = setInterval(() => { if (!_favHttp.enable) { func(); clearInterval(favTimer); } else if (_favHttp.initEnd) { clearInterval(favTimer); func(); } }, 1000); } timerFunc(); } function isUserLogin() { var login = false; if (_favHttp.enable && _favHttp.isLogin) { login = true; } return login; } function isReadLocal() { var read = false; if (getCookie(_favHttp.readLocalKey) == "1") { read = true; } return read; } function setFavCookie(t, arr) { var list = arr.concat(); var store = getFavStore(t); //超过cookie最大长度时删减 while (list.toString().length > 2600) { list.splice(0, 1); } let idsStr = list.join(","); writeShareCookie(store.key, idsStr, new Date((new Date()).getTime() + 15 * 24 * 3600000)); } function getFavCookie(t) { var store = getFavStore(t); var cookie = getCookie(store.key); return cookie ? cookie.split(",") : new Array(); } function uploadFavCookie(t) { var arr = getFavCookie(t); setFavLocal(t, arr); uploadUserFav(t); };