From 98ccbdb427757f576cd6e4f92081730d295cb5c0 Mon Sep 17 00:00:00 2001 From: Kaviilee Date: Sat, 22 Feb 2025 22:52:29 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20token=E8=8E=B7=E5=8F=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 6 +- src/service.js | 181 +++++++++++++++++++++++++------------------------ 2 files changed, 94 insertions(+), 93 deletions(-) diff --git a/package.json b/package.json index a1e12ca..6ed8019 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jx3box/jx3box-comment-ui", - "version": "2.0.3", + "version": "2.0.4", "scripts": { "dev": "env DEV_SERVER=true vue-cli-service serve", "serve": "vue-cli-service serve", @@ -10,8 +10,8 @@ }, "main": "index.js", "dependencies": { - "@jx3box/jx3box-common": "^8.5.4", - "@jx3box/jx3box-common-ui": "^9.0.27", + "@jx3box/jx3box-common": "^8.6.5", + "@jx3box/jx3box-common-ui": "^9.2.1", "@jx3box/jx3box-emotion": "^1.2.8", "core-js": "^3.6.4", "csslab": "^4.0.4", diff --git a/src/service.js b/src/service.js index 9ae6359..2c7a5a5 100644 --- a/src/service.js +++ b/src/service.js @@ -1,157 +1,156 @@ //import { Notification } from 'element-ui'; -import { __Links, __next } from "@jx3box/jx3box-common/data/jx3box.json" -import { Notification } from "element-ui" -import { $pay } from "@jx3box/jx3box-common/js/https" - -// 从url中获取参数 -function getUrlParam(name) { - var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); - var r = window.location.search.substr(1).match(reg); - return r ? decodeURIComponent(r[2]) : null; -} - +import { __Links, __next } from "@jx3box/jx3box-common/data/jx3box.json"; +import { Notification } from "element-ui"; +import { $pay } from "@jx3box/jx3box-common/js/https"; +import { getTokenFromUrl } from "@jx3box/jx3box-common/js/utils"; export const GET = function (url, queryParams) { let options = { - "method": "GET", + method: "GET", headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + Accept: "application/json", + "Content-Type": "application/json", }, - } - return __fetch(url, queryParams, options) -} + }; + return __fetch(url, queryParams, options); +}; -var postRecord = {} +var postRecord = {}; export const POST = function (url, queryParams, body) { - if (!postRecord[url]) { postRecord[url] = { lastest: Date.now(), - count: 0 - } + count: 0, + }; // 60 秒内发送评论超过10条 } else if (Date.now() - postRecord[url].lastest < 60 * 1000) { - if (postRecord[url].count >= 6) { Notification.warning({ title: "系统", message: "你单身多久了? 动作这么快, 系统处理不过来 ( T_T )", duration: 3000, - position: "bottom-right" - }) + position: "bottom-right", + }); return new Promise((reslove, reject) => { - reject() - }) + reject(); + }); } else { - postRecord[url].count = postRecord[url].count + 1 + postRecord[url].count = postRecord[url].count + 1; } - } else if (Date.now() - postRecord[url].lastest > 60 * 1000) { postRecord[url] = { lastest: Date.now(), - count: 0 - } + count: 0, + }; } let options = { - "method": "POST", + method: "POST", headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + Accept: "application/json", + "Content-Type": "application/json", }, - body: JSON.stringify(body) - } - return __fetch(url, queryParams, options) -} + body: JSON.stringify(body), + }; + return __fetch(url, queryParams, options); +}; export const PUT = function (url, queryParams, body) { let options = { - "method": "PUT", + method: "PUT", headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + Accept: "application/json", + "Content-Type": "application/json", }, - body: JSON.stringify(body) - } - return __fetch(url, queryParams, options) -} + body: JSON.stringify(body), + }; + return __fetch(url, queryParams, options); +}; export const DELETE = function (url, queryParams) { let options = { - "method": "DELETE" - } - return __fetch(url, queryParams, options) -} + method: "DELETE", + }; + return __fetch(url, queryParams, options); +}; function __fetch(url, queryParams, options) { - let domain = process.env.NODE_ENV == "production" ? __next : "/" + let domain = process.env.NODE_ENV == "production" ? __next : "/"; if (domain[domain.length - 1] == "/") { - domain = domain.substring(0, domain.length - 1) + domain = domain.substring(0, domain.length - 1); } - url = domain + url - url = url.replace("/api/", "/api/next2/") - options.credentials = 'include' + url = domain + url; + url = url.replace("/api/", "/api/next2/"); + options.credentials = "include"; if (queryParams) { - let queryQueue = [] + let queryQueue = []; Object.keys(queryParams).forEach((key) => { - queryQueue.push(key + "=" + queryParams[key]) - }) - let domain = __next + queryQueue.push(key + "=" + queryParams[key]); + }); + let domain = __next; if (domain[domain.length - 1] == "/") { - domain = domain.substring(0, domain.length - 1) + domain = domain.substring(0, domain.length - 1); } - url = url + "?" + queryQueue.join("&") + url = url + "?" + queryQueue.join("&"); } // 设置认证头 - let token = getUrlParam("__token"); - token = token ? token : (localStorage && localStorage.getItem("token")); + let token = getTokenFromUrl(); + token = token ? token : localStorage && localStorage.getItem("token"); if (token) { - const credentials = btoa(token + ':' + 'next common request'); + const credentials = btoa(token + ":" + "next common request"); if (!options.headers) { - options.headers = {} + options.headers = {}; } - options.headers["Authorization"] = "Basic " + credentials + options.headers["Authorization"] = "Basic " + credentials; } return fetch(url, options).then((resp) => { switch (resp.status) { case 200: - break + break; case 401: - window.location.href = __Links.account.login + "?redirect=" + encodeURIComponent(window.location.href) - throw new Error("错误:" + resp.statusText) + window.location.href = + __Links.account.login + + "?redirect=" + + encodeURIComponent(window.location.href); + throw new Error("错误:" + resp.statusText); case 403: - window.location.href = __Links.account.login + "?redirect=" + encodeURIComponent(window.location.href) - throw new Error("错误:" + resp.statusText) + window.location.href = + __Links.account.login + + "?redirect=" + + encodeURIComponent(window.location.href); + throw new Error("错误:" + resp.statusText); case 423: - window.location.href = __Links.account.email_verify + "?redirect=" + encodeURIComponent(window.location.href) - throw new Error("错误:" + resp.statusText) + window.location.href = + __Links.account.email_verify + + "?redirect=" + + encodeURIComponent(window.location.href); + throw new Error("错误:" + resp.statusText); case 406: resp.text().then((body) => { Notification.warning({ title: "系统", message: body || "提交内容不合法,请重新提交", duration: 3000, - position: "bottom-right" - }) - }) + position: "bottom-right", + }); + }); - throw new Error("错误:" + resp.statusText) + throw new Error("错误:" + resp.statusText); default: resp.text().then((body) => { Notification.error({ title: "系统:" + resp.statusText, message: body || "系统错误,请稍后重试!", duration: 3000, - position: "bottom-right" - }) - }) + position: "bottom-right", + }); + }); - throw new Error("错误:" + resp.statusText) + throw new Error("错误:" + resp.statusText); } - let contentType = resp.headers.get("Content-Type") - contentType = contentType && contentType.split(";").shift() + let contentType = resp.headers.get("Content-Type"); + contentType = contentType && contentType.split(";").shift(); switch (contentType) { case "application/json": return resp.json().then((data) => { @@ -160,18 +159,20 @@ function __fetch(url, queryParams, options) { title: "提示", message: data.code + ":" + data.msg, duration: 3000, - position: "bottom-right" - }) - return null + position: "bottom-right", + }); + return null; } - return data - }) + return data; + }); default: - return resp.text() + return resp.text(); } - }) + }); } export const getHistorySummary = (postType, postId) => { - return $pay().get(`/api/inspire/article/${postType}/${postId}/history/summary`) -} \ No newline at end of file + return $pay().get( + `/api/inspire/article/${postType}/${postId}/history/summary` + ); +};