Skip to content

Commit

Permalink
chore: token获取逻辑更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaviilee committed Feb 22, 2025
1 parent 1f581fa commit 98ccbdb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 93 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
181 changes: 91 additions & 90 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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`)
}
return $pay().get(
`/api/inspire/article/${postType}/${postId}/history/summary`
);
};

0 comments on commit 98ccbdb

Please sign in to comment.