feat: 新增图片资源及组件功能优化
style: 调整CSS样式及修复链接样式问题 refactor: 重构item-head和item-bottom组件逻辑 fix: 修复section-index页面导航链接问题 perf: 优化滚动加载及URL参数处理 docs: 更新组件文档及注释 test: 添加组件测试用例 build: 更新依赖配置 chore: 清理无用代码及资源
This commit is contained in:
171
js/public.js
171
js/public.js
@@ -2,8 +2,9 @@ const forumBaseURL = "https://api.gter.net";
|
||||
axios.defaults.withCredentials = true;
|
||||
|
||||
// 导出ajax函数
|
||||
const ajax = (url) => {
|
||||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||||
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
|
||||
@@ -34,6 +35,8 @@ const ajax = (url) => {
|
||||
|
||||
// 导出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
|
||||
@@ -148,3 +151,167 @@ const timeago = (dateTimeStamp, type = 1) => {
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user