style: 调整CSS样式及修复链接样式问题 refactor: 重构item-head和item-bottom组件逻辑 fix: 修复section-index页面导航链接问题 perf: 优化滚动加载及URL参数处理 docs: 更新组件文档及注释 test: 添加组件测试用例 build: 更新依赖配置 chore: 清理无用代码及资源
318 lines
12 KiB
JavaScript
318 lines
12 KiB
JavaScript
const forumBaseURL = "https://api.gter.net";
|
||
axios.defaults.withCredentials = true;
|
||
|
||
// 导出ajax函数
|
||
const ajax = (url, data) => {
|
||
if (location.hostname == "127.0.0.1") axios.defaults.headers.common["Authorization"] = "yuphemk2bv532bl5oqur5tsq9tk6dgkk";
|
||
|
||
url = url.indexOf("https://") > -1 ? url : forumBaseURL + url;
|
||
return new Promise(function (resolve, reject) {
|
||
axios
|
||
.post(url, data, {
|
||
emulateJSON: true,
|
||
withCredentials: true,
|
||
})
|
||
.then(function (res) {
|
||
var data = typeof res.data == "string" ? JSON.parse(res.data) : res.data;
|
||
|
||
if (data.code == 401) {
|
||
// 需要登录
|
||
showWindow("login", "https://passport.gter.net/login/ajax", "get", -1, {
|
||
cover: true,
|
||
});
|
||
reject();
|
||
}
|
||
resolve(data);
|
||
})
|
||
.catch((err) => {
|
||
if (err.response.status == 401)
|
||
showWindow("login", "https://passport.gter.net/login/ajax", "get", -1, {
|
||
cover: true,
|
||
});
|
||
});
|
||
});
|
||
};
|
||
|
||
// 导出ajaxget函数
|
||
const ajaxget = (url) => {
|
||
if (location.hostname == "127.0.0.1") axios.defaults.headers.common["Authorization"] = "yuphemk2bv532bl5oqur5tsq9tk6dgkk";
|
||
|
||
url = url.indexOf("https://") > -1 ? url : forumBaseURL + url;
|
||
return new Promise(function (resolve, reject) {
|
||
axios
|
||
.get(
|
||
url,
|
||
{},
|
||
{
|
||
emulateJSON: true,
|
||
withCredentials: true,
|
||
}
|
||
)
|
||
.then(function (res) {
|
||
var data = typeof res.data == "string" ? JSON.parse(res.data) : res.data;
|
||
if (data.code == 401) {
|
||
// 需要登录
|
||
showWindow("login", "https://passport.gter.net/login/ajax", "get", -1, {
|
||
cover: true,
|
||
});
|
||
reject();
|
||
}
|
||
resolve(data);
|
||
})
|
||
.catch((error) => {
|
||
reject(error);
|
||
});
|
||
});
|
||
};
|
||
|
||
const strtimeago = (dateStr, type = 1) => {
|
||
dateStr = dateStr + ""; // 反之传入的不是字符串
|
||
dateStr = dateStr.replaceAll("-", "/"); // 修改格式
|
||
var minute = 1000 * 60; // 把分,时,天,周,半个月,一个月用毫秒表示
|
||
var hour = minute * 60;
|
||
var day = hour * 24;
|
||
var now = new Date().getTime(); // 获取当前时间毫秒
|
||
let objectTime = new Date(dateStr).getTime();
|
||
var diffValue = now - objectTime; // 时间差
|
||
if (diffValue < 0) return "刚刚";
|
||
|
||
var minC = diffValue / minute; // 计算时间差的分,时,天,周,月
|
||
var hourC = diffValue / hour;
|
||
var dayC = diffValue / day;
|
||
|
||
const diffInMilliseconds = now - objectTime;
|
||
const diffInYears = diffInMilliseconds / (1000 * 60 * 60 * 24 * 365);
|
||
const diffInMonths = diffInYears * 12;
|
||
|
||
let result = "";
|
||
if (dayC >= 7) {
|
||
var datetime = new Date(dateStr);
|
||
var Nyear = datetime.getFullYear();
|
||
var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
|
||
var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
|
||
var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
|
||
var Nmin = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
|
||
if (type == 4) {
|
||
if (new Date().getFullYear() !== Nyear) result = `${Nyear}年${Nmonth}月${Ndate}日`;
|
||
else result = `${Nmonth}月${Ndate}日 ${Nhour}:${Nmin}`;
|
||
}
|
||
if (type == 1) result = Nyear + "-" + Nmonth + "-" + Ndate;
|
||
if (type == 2) {
|
||
result = `${Nmonth}月${Ndate}日 ${Nhour}:${Nmin}`;
|
||
if (new Date().getFullYear() !== Nyear) result = `${Nyear}年${result}`;
|
||
}
|
||
if (type == 3) {
|
||
if (diffInYears >= 1) result = Math.floor(diffInYears) + "年前";
|
||
else if (diffInMonths >= 1) result = Math.floor(diffInMonths) + "个月前";
|
||
else result = parseInt(dayC) + "天前";
|
||
}
|
||
} else if (dayC >= 1) result = parseInt(dayC) + "天前";
|
||
else if (hourC >= 1 && hourC <= 24) result = parseInt(hourC) + "小时前";
|
||
else if (minC >= 1 && minC <= 60) result = parseInt(minC) + "分钟前";
|
||
else result = "刚刚";
|
||
|
||
return result;
|
||
};
|
||
|
||
const timeago = (dateTimeStamp, type = 1) => {
|
||
if (!dateTimeStamp) return "刚刚";
|
||
|
||
// 判断位数
|
||
if (dateTimeStamp.toString().length !== 13) dateTimeStamp = dateTimeStamp * 1000;
|
||
var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示
|
||
var hour = minute * 60;
|
||
var day = hour * 24;
|
||
var now = new Date().getTime(); //获取当前时间毫秒
|
||
var diffValue = now - dateTimeStamp; //时间差
|
||
if (diffValue < 0) return "刚刚";
|
||
|
||
var minC = diffValue / minute; //计算时间差的分,时,天,周,月
|
||
var hourC = diffValue / hour;
|
||
var dayC = diffValue / day;
|
||
|
||
let result = "";
|
||
if (dayC >= 7) {
|
||
var datetime = new Date(dateTimeStamp);
|
||
var Nyear = datetime.getFullYear();
|
||
var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
|
||
var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
|
||
var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
|
||
var Nmin = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
|
||
|
||
if (type == 1) result = Nyear + "-" + Nmonth + "-" + Ndate;
|
||
if (type == 2) {
|
||
result = `${Nmonth}月${Ndate}日 ${Nhour}:${Nmin}`;
|
||
if (new Date().getFullYear() !== Nyear) result = `${Nyear}年${result}`;
|
||
}
|
||
} else if (dayC >= 1) result = parseInt(dayC) + "天前";
|
||
else if (hourC >= 1 && hourC <= 24) result = parseInt(hourC) + "小时前";
|
||
else if (minC >= 1 && minC <= 60) result = parseInt(minC) + "分钟前";
|
||
else result = "刚刚";
|
||
|
||
return result;
|
||
};
|
||
|
||
const creationAlertBox = (type = "success", text) => {
|
||
if (!text) return;
|
||
const pathObj = {
|
||
success: `<path fill="currentColor" d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm-55.808 536.384-99.52-99.584a38.4 38.4 0 1 0-54.336 54.336l126.72 126.72a38.272 38.272 0 0 0 54.336 0l262.4-262.464a38.4 38.4 0 1 0-54.272-54.336L456.192 600.384z"></path>`,
|
||
error: `<path fill="currentColor" d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm0 393.664L407.936 353.6a38.4 38.4 0 1 0-54.336 54.336L457.664 512 353.6 616.064a38.4 38.4 0 1 0 54.336 54.336L512 566.336 616.064 670.4a38.4 38.4 0 1 0 54.336-54.336L566.336 512 670.4 407.936a38.4 38.4 0 1 0-54.336-54.336L512 457.664z"></path>`,
|
||
};
|
||
|
||
const colorObj = {
|
||
success: {
|
||
border: "#e1f3d8",
|
||
color: "#67c23a",
|
||
background: "#f0f9eb",
|
||
},
|
||
error: {
|
||
border: "#fde2e2",
|
||
color: "#f56c6c",
|
||
background: "#fef0f0",
|
||
},
|
||
};
|
||
|
||
if (!pathObj[type]) type = "success"; // 判断存不存在 给个默认值
|
||
|
||
const box = document.createElement("div");
|
||
|
||
box.style.position = "fixed";
|
||
box.style.zIndex = "10003";
|
||
box.style.top = "-51px";
|
||
box.style.left = "50%";
|
||
box.style.border = `${colorObj[type].border} 1px solid`;
|
||
box.style.color = colorObj[type].color;
|
||
box.style.background = colorObj[type].background;
|
||
box.style.display = "flex";
|
||
box.style.alignItems = "center";
|
||
box.style.fontSize = "14px";
|
||
box.style.padding = "15px 19px";
|
||
box.style.borderRadius = "4px";
|
||
box.style.transition = "0.5s";
|
||
box.style.transform = "translateX(-50%)";
|
||
|
||
const svg = `<svg style="width: 16px;height: 16px;margin-right: 10px;" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">${pathObj[type]}</svg>`;
|
||
|
||
box.innerHTML = `${svg}${text || "默认提示文本"}`;
|
||
document.body.appendChild(box);
|
||
|
||
setTimeout(() => {
|
||
box.style.top = 20 + "px";
|
||
setTimeout(() => {
|
||
box.style.top = -51 + "px";
|
||
setTimeout(() => {
|
||
box.remove();
|
||
}, 2000);
|
||
}, 2000);
|
||
}, 1);
|
||
};
|
||
|
||
const managerHide = (token, state, type = "offer") => {
|
||
return new Promise((resolve, reject) => {
|
||
const obj = {
|
||
offer: "Offer",
|
||
offer_summary: "总结",
|
||
interviewexperience: "面经",
|
||
thread: "帖子",
|
||
question: "帖子",
|
||
vote: "投票",
|
||
};
|
||
|
||
const isConfirmed = confirm(`确定要${state == 0 ? "隐藏" : "显示"}该${obj[type]}吗?`);
|
||
if (isConfirmed) {
|
||
ajax(`https://api.gter.net/v2/api/forum/setTopicHide`, {
|
||
token,
|
||
hidden: Number(state !== 1),
|
||
}).then((res) => {
|
||
const data = res.data;
|
||
creationAlertBox("success", res.message || "");
|
||
resolve(data.hidden);
|
||
});
|
||
}
|
||
});
|
||
};
|
||
|
||
// 推荐
|
||
const managerRecommend = (token, state) => {
|
||
return new Promise((resolve, reject) => {
|
||
const post = () => {
|
||
ajax(`/v2/api/forum/setTopicRecommend`, {
|
||
token,
|
||
recommend: state == 1 ? 0 : 1,
|
||
}).then((res) => {
|
||
const data = res.data;
|
||
creationAlertBox("success", res.message || "");
|
||
resolve(data.recommend);
|
||
});
|
||
};
|
||
|
||
if (state == 1) {
|
||
const isConfirmed = confirm(`确定要取消推荐吗?`);
|
||
if (isConfirmed) post();
|
||
else resolve(state);
|
||
} else post();
|
||
});
|
||
};
|
||
|
||
// 精华
|
||
const managerEssence = (token, state) => {
|
||
return new Promise((resolve, reject) => {
|
||
const post = () => {
|
||
ajax(`/v2/api/forum/setTopicBest`, {
|
||
token,
|
||
best: state == 1 ? 0 : 1,
|
||
}).then((res) => {
|
||
const data = res.data;
|
||
creationAlertBox("success", res.message || "");
|
||
resolve(data.best);
|
||
});
|
||
};
|
||
|
||
if (state == 1) {
|
||
const isConfirmed = confirm(`确定要取消精华吗?`);
|
||
if (isConfirmed) post();
|
||
else resolve(state);
|
||
} else post();
|
||
});
|
||
};
|
||
|
||
const getUrlParams = () => {
|
||
const params = {};
|
||
const queryString = window.location.search.slice(1);
|
||
if (queryString) {
|
||
queryString.split("&").forEach((pair) => {
|
||
const [key, value] = pair.split("=");
|
||
params[decodeURIComponent(key)] = decodeURIComponent(value || "");
|
||
});
|
||
}
|
||
return params;
|
||
};
|
||
|
||
const updateUrlParams = (params, replace = false) => {
|
||
// 解析当前URL
|
||
const url = new URL(window.location.href);
|
||
const searchParams = url.searchParams;
|
||
|
||
// 遍历参数对象,更新URL参数
|
||
Object.entries(params).forEach(([key, value]) => {
|
||
if (value === null || value === undefined) {
|
||
// 删除参数
|
||
searchParams.delete(key);
|
||
} else {
|
||
// 添加或修改参数
|
||
searchParams.set(key, value);
|
||
}
|
||
});
|
||
|
||
// 生成新的URL(保留协议、域名、路径等,只修改参数)
|
||
const newUrl = url.origin + url.pathname + (searchParams.toString() ? `?${searchParams.toString()}` : "");
|
||
|
||
// 修改URL(不刷新页面)
|
||
if (replace) {
|
||
history.replaceState(null, "", newUrl);
|
||
} else {
|
||
history.pushState(null, "", newUrl);
|
||
}
|
||
};
|
||
|