mirror of
https://github.com/jiangrui1994/CloudSaver.git
synced 2026-01-13 16:48:46 +08:00
feat:版本迭代
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import axios, { AxiosInstance, AxiosRequestHeaders } from "axios";
|
||||
import tunnel from "tunnel";
|
||||
import { config } from "../config";
|
||||
import GlobalSetting from "../models/GlobalSetting";
|
||||
|
||||
interface ProxyConfig {
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
export function createAxiosInstance(
|
||||
baseURL: string,
|
||||
headers: AxiosRequestHeaders,
|
||||
useProxy: boolean = false
|
||||
useProxy: boolean = false,
|
||||
proxyConfig?: ProxyConfig
|
||||
): AxiosInstance {
|
||||
let agent;
|
||||
|
||||
if (useProxy) {
|
||||
console.log(proxyConfig);
|
||||
if (useProxy && proxyConfig) {
|
||||
agent = tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: config.httpProxy.host,
|
||||
port: Number(config.httpProxy.port),
|
||||
},
|
||||
proxy: proxyConfig,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
21
backend/src/utils/index.ts
Normal file
21
backend/src/utils/index.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
import { Request } from "express";
|
||||
import { config } from "../config";
|
||||
|
||||
interface JwtPayload {
|
||||
userId: string;
|
||||
}
|
||||
|
||||
export function getUserIdFromToken(req: Request): string | null {
|
||||
try {
|
||||
const token = req.headers.authorization?.split(" ")[1];
|
||||
if (!token) {
|
||||
throw new Error("Token not found");
|
||||
}
|
||||
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
||||
return decoded.userId;
|
||||
} catch (error) {
|
||||
console.error("Invalid token:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
17
backend/src/utils/response.ts
Normal file
17
backend/src/utils/response.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Response } from "express";
|
||||
|
||||
interface ResponseData {
|
||||
code?: number; // 业务状态码
|
||||
message?: string;
|
||||
data?: any;
|
||||
}
|
||||
|
||||
export const sendSuccess = (res: Response, response: ResponseData, businessCode: number = 0) => {
|
||||
response.code = businessCode;
|
||||
res.status(200).json(response);
|
||||
};
|
||||
|
||||
export const sendError = (res: Response, response: ResponseData, businessCode: number = 10000) => {
|
||||
response.code = businessCode;
|
||||
res.status(200).json(response);
|
||||
};
|
||||
Reference in New Issue
Block a user