const IssuesAPI = { requestIssuesAPI(url, callback, timeout) { let retryTimes = 10; function request() { return new Promise((resolve, reject) => { let status = 0; // 0 等待 1 完成 2 超时 let timer = setTimeout(() => { if (status === 0) { status = 2; timer = null; reject('请求超时'); if (retryTimes == 0) { timeout(); } } }, 5000); fetch(url).then(function(response) { if (status !== 2) { clearTimeout(timer); resolve(response); timer = null; status = 1; } if (response.ok) { return response.json(); } throw new Error('Network response was not ok.'); }).then(function(data) { retryTimes = 0; callback(data); }).catch(function(error) { if (retryTimes > 0) { retryTimes -= 1; setTimeout(() => { request(); }, 5000); } else { timeout(); } }); }); } request(); }, parseIssueStrToJson(str) { let jsonStr = str.match(/```json[\s|\S]*```/); if (jsonStr && jsonStr.length > 0) { jsonStr = jsonStr[0]; } if (jsonStr) { jsonStr = jsonStr.split('```json')[1].split('```')[0]; if (jsonStr) { return JSON.parse(jsonStr); } } return undefined; }, groupIssuesData(cfg, data) { var groups = new Object(); if (data.length > 0) { if (cfg.group != undefined) { const arr = cfg.group.split('='); if (arr.length > 1) { const groupKey = arr[0]; let groupList = arr[1]; if (groupKey && groupList) { groupList = groupList.split(','); } cfg.group = groupList; for (i = 0; i < data.length; i++) { const obj = this.parseIssueStrToJson(data[i].body); if (obj && (groupKey in obj)) { let tmp = obj[groupKey]; tmp = tmp.replace(', ', ',').split(','); for (var j = 0; j < tmp.length; j++) { if (groupList.includes(tmp[j])) { let arr = groups[tmp[j]]; if (arr == undefined) { arr = new Array(); } arr.push(obj); groups[tmp[j]] = arr; } } } } } } else { cfg.group = ['']; for (i = 0; i < data.length; i++) { const obj = this.parseIssueStrToJson(data[i].body); if (obj) { let arr = groups['']; if (arr == undefined) { arr = new Array(); } arr.push(obj); groups[''] = arr; } } } } return groups; }, getIssuesAPIForSites(cfg) { const el = $(cfg.el)[0]; $(el).append('
正在加载
正在加载
' + data[i].title + a + '
' + data[i].body + '