2021-03-03 22:05:29 +08:00
|
|
|
const friendsjs = {
|
|
|
|
requestAPI: (url, callback, timeout) => {
|
2021-03-02 23:53:51 +08:00
|
|
|
let retryTimes = 5;
|
2021-02-19 23:33:19 +08:00
|
|
|
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();
|
|
|
|
},
|
2021-03-03 22:05:29 +08:00
|
|
|
layout: (cfg) => {
|
2021-02-19 23:33:19 +08:00
|
|
|
const el = $(cfg.el)[0];
|
2021-03-03 22:05:29 +08:00
|
|
|
$(el).append('<div class="loading-wrap"><svg class="loading" style="vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2709"><path d="M832 512c0-176-144-320-320-320V128c211.2 0 384 172.8 384 384h-64zM192 512c0 176 144 320 320 320v64C300.8 896 128 723.2 128 512h64z" p-id="2710"></path></svg><p></p></div>');
|
|
|
|
friendsjs.requestAPI(cfg.api, function(data) {
|
|
|
|
$(el).find('.loading-wrap').remove();
|
2021-03-02 23:53:51 +08:00
|
|
|
const arr = data.content;
|
|
|
|
arr.forEach((item, i) => {
|
2021-03-04 22:30:56 +08:00
|
|
|
var user = '<div class="user-card">';
|
|
|
|
user += '<a class="card-link" target="_blank" rel="external nofollow noopener noreferrer"';
|
2021-03-02 23:53:51 +08:00
|
|
|
user += ' href="' + item.url + '">';
|
2021-03-04 22:30:56 +08:00
|
|
|
user += '<img src="' + (item.avatar || cfg.avatar) + '" onerror="javascript:this.src=\'' + cfg.avatar + '\';">';
|
2021-03-02 23:53:51 +08:00
|
|
|
user += '<div class="name"><span>' + item.title + '</span></div>';
|
|
|
|
user += '</a>';
|
|
|
|
user += '</div>';
|
|
|
|
$(el).find('.group-body').append(user);
|
2021-02-19 23:33:19 +08:00
|
|
|
});
|
|
|
|
}, function() {
|
2021-03-03 22:05:29 +08:00
|
|
|
$(el).find('.loading-wrap svg').remove();
|
|
|
|
$(el).find('.loading-wrap p').text('加载失败,请稍后重试。');
|
2021-02-19 23:33:19 +08:00
|
|
|
});
|
|
|
|
},
|
2021-03-03 22:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
const els = document.getElementsByClassName('friendsjs-wrap');
|
|
|
|
for (var i = 0; i < els.length; i++) {
|
|
|
|
const el = els[i];
|
|
|
|
const api = el.getAttribute('api');
|
|
|
|
if (api == null) {
|
|
|
|
continue;
|
2021-02-19 23:33:19 +08:00
|
|
|
}
|
2021-03-03 22:05:29 +08:00
|
|
|
var cfg = new Object();
|
|
|
|
cfg.el = el;
|
|
|
|
cfg.api = api;
|
2021-03-04 22:30:56 +08:00
|
|
|
cfg.class = el.getAttribute('class');
|
2021-03-08 01:41:59 +08:00
|
|
|
cfg.avatar = 'https://cdn.jsdelivr.net/gh/cdn-x/placeholder@1.0.1/avatar/round/3442075.svg';
|
2021-03-03 22:05:29 +08:00
|
|
|
friendsjs.layout(cfg);
|
2021-02-19 23:33:19 +08:00
|
|
|
}
|
2021-03-02 23:53:51 +08:00
|
|
|
});
|