Skip to content

Commit

Permalink
feat: 请求token更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaviilee committed Jan 4, 2025
1 parent 9a96eee commit de9ecbf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
24 changes: 20 additions & 4 deletions js/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const server_map = {
// cms通用请求接口
function $cms(options) {
let domain = (options && options.domain) || __cms;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "cms common request",
},
baseURL: process.env.NODE_ENV === "production" ? domain : "/",
Expand Down Expand Up @@ -76,11 +78,13 @@ function $helper(options) {
// next通用请求接口
function $next(options) {
let domain = (options && options.domain) || __next;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "next common request",
},
baseURL: process.env.NODE_ENV === "production" ? domain : "/",
Expand Down Expand Up @@ -114,11 +118,13 @@ function $lua(options) {
// node
function $node(options) {
let domain = (options && options.domain) || __node;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "node common request",
},
baseURL: domain,
Expand Down Expand Up @@ -183,11 +189,14 @@ function $_https(server, options) {
return Promise.reject("请先登录");
}

let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));

let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "$_https common request",
},
baseURL: process.env.NODE_ENV === "production" ? server_map[server] : "/",
Expand Down Expand Up @@ -220,5 +229,12 @@ function $_https(server, options) {
}


// 从url中获取参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
return r ? decodeURIComponent(r[2]) : null;
}


export { $https, $_https, $cms, $helper, $next, $team, $pay, $node, $lua, axios, SSE };
24 changes: 19 additions & 5 deletions js/https_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { SSE } from "./sse";
// cms通用请求接口
function $cms(options) {
let domain = (options && options.domain) || domains.__cms;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "cms common request",
},
baseURL: process.env.NODE_ENV === "production" ? domain : "/",
Expand Down Expand Up @@ -73,12 +75,13 @@ function $helper(options) {
// next 通用请求接口
function $next(options) {
let domain = (options && options.domain) || domains.__next;

let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "next common request",
},
baseURL: process.env.NODE_ENV === "production" ? domain : "/",
Expand Down Expand Up @@ -121,11 +124,13 @@ function $lua(options) {
// node 通用请求接口
function $node(options) {
let domain = (options && options.domain) || domains.__node;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "node common request",
},
baseURL: domain,
Expand All @@ -151,11 +156,13 @@ function $node(options) {
// 默认请求
function $http(options) {
let domain = typeof options == "string" ? options : options.domain;
let token = getUrlParam("__token");
token = token ? token : (localStorage && localStorage.getItem("token"));
let config = {
// 同时发送cookie和basic auth
withCredentials: true,
auth: {
username: (localStorage && localStorage.getItem("token")) || "",
username: token || "",
password: "common request",
},
baseURL: domain,
Expand Down Expand Up @@ -300,4 +307,11 @@ function installHelperInterceptors(target, options) {
);
}

// 从url中获取参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
return r ? decodeURIComponent(r[2]) : null;
}

export { $cms, $next, $helper, $node, $team, $pay, $lua, $http, axios, SSE };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jx3box/jx3box-common",
"version": "8.5.3",
"version": "8.5.4",
"description": "JX3BOX公共基础模块",
"main": "main.js",
"repository": {
Expand Down

0 comments on commit de9ecbf

Please sign in to comment.