mirror of
https://gitee.com/mirrors/AllinSSL.git
synced 2026-03-27 16:03:24 +08:00
【新增】私有证书
This commit is contained in:
107
frontend/apps/allin-ssl/src/api/ca.ts
Normal file
107
frontend/apps/allin-ssl/src/api/ca.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
// External library dependencies
|
||||
import axios, { AxiosResponse } from 'axios'
|
||||
|
||||
// Type imports
|
||||
import type { useAxiosReturn } from '@baota/hooks/axios'
|
||||
import type {
|
||||
CreateRootCaParams,
|
||||
CreateRootCaResponse,
|
||||
CreateIntermediateCaParams,
|
||||
CreateIntermediateCaResponse,
|
||||
GetCaListParams,
|
||||
GetCaListResponse,
|
||||
DeleteCaParams,
|
||||
DeleteCaResponse,
|
||||
DownloadCaCertParams,
|
||||
DownloadCaCertResponse,
|
||||
CreateLeafCertParams,
|
||||
CreateLeafCertResponse,
|
||||
GetLeafCertListParams,
|
||||
GetLeafCertListResponse,
|
||||
DeleteLeafCertParams,
|
||||
DeleteLeafCertResponse,
|
||||
} from '@/types/ca'
|
||||
|
||||
import { useApi, createApiToken } from "@api/index";
|
||||
import { isDev } from '@baota/utils/browser';
|
||||
|
||||
/**
|
||||
* @description 创建根证书
|
||||
* @param {CreateRootCaParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<CreateRootCaResponse, CreateRootCaParams>}
|
||||
*/
|
||||
export const createRootCa = (params?: CreateRootCaParams): useAxiosReturn<CreateRootCaResponse, CreateRootCaParams> =>
|
||||
useApi<CreateRootCaResponse, CreateRootCaParams>('/v1/private_ca/create_root_ca', params)
|
||||
|
||||
/**
|
||||
* @description 创建中间证书
|
||||
* @param {CreateIntermediateCaParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<CreateIntermediateCaResponse, CreateIntermediateCaParams>}
|
||||
*/
|
||||
export const createIntermediateCa = (params?: CreateIntermediateCaParams): useAxiosReturn<CreateIntermediateCaResponse, CreateIntermediateCaParams> =>
|
||||
useApi<CreateIntermediateCaResponse, CreateIntermediateCaParams>('/v1/private_ca/create_intermediate_ca', params)
|
||||
|
||||
/**
|
||||
* @description 获取CA列表
|
||||
* @param {GetCaListParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<GetCaListResponse, GetCaListParams>} 获取CA列表的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const getCaList = (params?: GetCaListParams): useAxiosReturn<GetCaListResponse, GetCaListParams> =>
|
||||
useApi<GetCaListResponse, GetCaListParams>('/v1/private_ca/get_ca_list', params)
|
||||
|
||||
/**
|
||||
* @description 删除CA
|
||||
* @param {DeleteCaParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<DeleteCaResponse, DeleteCaParams>} 删除CA的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const deleteCa = (params?: DeleteCaParams): useAxiosReturn<DeleteCaResponse, DeleteCaParams> =>
|
||||
useApi<DeleteCaResponse, DeleteCaParams>('/v1/private_ca/del_ca', params)
|
||||
|
||||
/**
|
||||
* @description 获取下载CA证书的URL
|
||||
* @param {DownloadCaCertParams} [params] 请求参数
|
||||
* @returns {string} 拼接好的下载URL
|
||||
*/
|
||||
export const downloadCaCert = (params?: DownloadCaCertParams): string => {
|
||||
const apiParams = createApiToken()
|
||||
const finalParams = isDev() ? { ...(params || {}), ...apiParams } : params || {}
|
||||
|
||||
// 构建查询字符串
|
||||
const searchParams = new URLSearchParams()
|
||||
if (finalParams) {
|
||||
Object.entries(finalParams).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
searchParams.append(key, String(value))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const queryString = searchParams.toString()
|
||||
const baseUrl = isDev() ? '/api' : '/'
|
||||
const fullUrl = queryString ? `${baseUrl}/v1/private_ca/download_cert?${queryString}` : `${baseUrl}/v1/private_ca/download_cert`
|
||||
return fullUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 创建叶子证书
|
||||
* @param {CreateLeafCertParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<CreateLeafCertResponse, CreateLeafCertParams>} 创建叶子证书的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const createLeafCert = (params?: CreateLeafCertParams): useAxiosReturn<CreateLeafCertResponse, CreateLeafCertParams> =>
|
||||
useApi<CreateLeafCertResponse, CreateLeafCertParams>('/v1/private_ca/create_leaf_cert', params)
|
||||
|
||||
/**
|
||||
* @description 获取叶子证书列表
|
||||
* @param {GetLeafCertListParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<GetLeafCertListResponse, GetLeafCertListParams>} 获取叶子证书列表的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const getLeafCertList = (params?: GetLeafCertListParams): useAxiosReturn<GetLeafCertListResponse, GetLeafCertListParams> =>
|
||||
useApi<GetLeafCertListResponse, GetLeafCertListParams>('/v1/private_ca/get_leaf_cert_list', params)
|
||||
|
||||
/**
|
||||
* @description 删除叶子证书
|
||||
* @param {DeleteLeafCertParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<DeleteLeafCertResponse, DeleteLeafCertParams>} 删除叶子证书的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const deleteLeafCert = (params?: DeleteLeafCertParams): useAxiosReturn<DeleteLeafCertResponse, DeleteLeafCertParams> =>
|
||||
useApi<DeleteLeafCertResponse, DeleteLeafCertParams>('/v1/private_ca/del_leaf_cert', params)
|
||||
@@ -1,16 +1,16 @@
|
||||
// External Libraries (sorted alphabetically by module path)
|
||||
import { HttpClient, useAxios, useAxiosReturn } from '@baota/hooks/axios'
|
||||
import { errorMiddleware } from '@baota/hooks/axios/model'
|
||||
import { isDev } from '@baota/utils/browser'
|
||||
import { AxiosError } from 'axios'
|
||||
import MD5 from 'crypto-js/md5'
|
||||
import { HttpClient, useAxios, useAxiosReturn } from "@baota/hooks/axios";
|
||||
import { errorMiddleware } from "@baota/hooks/axios/model";
|
||||
import { isDev } from "@baota/utils/browser";
|
||||
import { AxiosError } from "axios";
|
||||
import MD5 from "crypto-js/md5";
|
||||
|
||||
// Type Imports (sorted alphabetically by module path)
|
||||
import type { AxiosResponseData } from '@/types/public'
|
||||
import type { Ref } from 'vue'
|
||||
import type { AxiosResponseData } from "@/types/public";
|
||||
import type { Ref } from "vue";
|
||||
|
||||
// Relative Internal Imports (sorted alphabetically by module path)
|
||||
import { router } from '@router/index'
|
||||
import { router } from "@router/index";
|
||||
|
||||
/**
|
||||
* @description 处理返回数据,如果状态码为 401 或 404
|
||||
@@ -18,50 +18,53 @@ import { router } from '@router/index'
|
||||
* @returns {AxiosError} 错误对象
|
||||
*/
|
||||
export const responseHandleStatusCode = errorMiddleware((error: AxiosError) => {
|
||||
// 处理 401 状态码
|
||||
if (error.status === 401) {
|
||||
router.push(`/login`)
|
||||
}
|
||||
// 处理404状态码
|
||||
if (error.status === 404) {
|
||||
// router.go(0) // 刷新页面
|
||||
}
|
||||
return error
|
||||
})
|
||||
// 处理 401 状态码
|
||||
if (error.status === 401) {
|
||||
router.push(`/login`);
|
||||
}
|
||||
// 处理404状态码
|
||||
if (error.status === 404) {
|
||||
// router.go(0) // 刷新页面
|
||||
}
|
||||
return error;
|
||||
});
|
||||
|
||||
/**
|
||||
* @description 返回数据
|
||||
* @param {T} data 数据
|
||||
* @returns {AxiosResponseData<T>} 返回数据
|
||||
*/
|
||||
export const useApiReturn = <T>(data: T, message?: string): AxiosResponseData<T> => {
|
||||
return {
|
||||
code: 200,
|
||||
count: 0,
|
||||
data,
|
||||
message: message || '请求返回值错误,请检查',
|
||||
status: false,
|
||||
} as AxiosResponseData<T>
|
||||
}
|
||||
export const useApiReturn = <T>(
|
||||
data: T,
|
||||
message?: string
|
||||
): AxiosResponseData<T> => {
|
||||
return {
|
||||
code: 200,
|
||||
count: 0,
|
||||
data,
|
||||
message: message || "请求返回值错误,请检查",
|
||||
status: false,
|
||||
} as AxiosResponseData<T>;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 创建http客户端实例
|
||||
*/
|
||||
export const instance = new HttpClient({
|
||||
baseURL: isDev() ? '/api' : '/',
|
||||
timeout: 50000,
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
middlewares: [responseHandleStatusCode],
|
||||
})
|
||||
baseURL: isDev() ? "/api" : "/",
|
||||
timeout: 50000,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
middlewares: [responseHandleStatusCode],
|
||||
});
|
||||
|
||||
/**
|
||||
* @description API Token 结构
|
||||
*/
|
||||
interface ApiTokenResult {
|
||||
api_token: string
|
||||
timestamp: number
|
||||
api_token: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,11 +72,11 @@ interface ApiTokenResult {
|
||||
* @returns {ApiTokenResult} 包含API token和时间戳的对象
|
||||
*/
|
||||
export const createApiToken = (): ApiTokenResult => {
|
||||
const now = new Date().getTime()
|
||||
const apiKey = '123456' // 注意: 此处为硬编码密钥,建议后续优化
|
||||
const api_token = MD5(now + MD5(apiKey).toString()).toString()
|
||||
return { api_token, timestamp: now }
|
||||
}
|
||||
const now = new Date().getTime();
|
||||
const apiKey = "123456"; // 注意: 此处为硬编码密钥,建议后续优化
|
||||
const api_token = MD5(now + MD5(apiKey).toString()).toString();
|
||||
return { api_token, timestamp: now };
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 创建axios请求
|
||||
@@ -81,18 +84,42 @@ export const createApiToken = (): ApiTokenResult => {
|
||||
* @param {Z} [params] 请求参数
|
||||
* @returns {useAxiosReturn<T, Z>} 返回结果
|
||||
*/
|
||||
export const useApi = <T, Z = Record<string, unknown>>(url: string, params?: Z) => {
|
||||
const { urlRef, paramsRef, ...other } = useAxios<T>(instance)
|
||||
const apiParams = createApiToken()
|
||||
urlRef.value = url
|
||||
paramsRef.value = isDev() ? { ...(params || {}), ...apiParams } : params || {}
|
||||
return { urlRef, paramsRef: paramsRef as Ref<Z>, ...other } as useAxiosReturn<T, Z>
|
||||
}
|
||||
export const useApi = <T, Z = Record<string, unknown>>(
|
||||
url: string,
|
||||
params?: Z
|
||||
) => {
|
||||
const { urlRef, paramsRef, ...other } = useAxios<T>(instance);
|
||||
const apiParams = createApiToken();
|
||||
urlRef.value = url;
|
||||
paramsRef.value = isDev()
|
||||
? { ...(params || {}), ...apiParams }
|
||||
: params || {};
|
||||
return { urlRef, paramsRef: paramsRef as Ref<Z>, ...other } as useAxiosReturn<
|
||||
T,
|
||||
Z
|
||||
>;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description get请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {Z} [params] 请求参数
|
||||
* @returns {useAxiosReturn<T, Z>} 返回结果
|
||||
*/
|
||||
export const useGet = <T, Z = Record<string, unknown>>(
|
||||
url: string,
|
||||
params?: Z
|
||||
) => {
|
||||
return instance.get(url, {
|
||||
data: { ...createApiToken(), ...params },
|
||||
});
|
||||
};
|
||||
|
||||
// 导出所有模块
|
||||
export * from './public'
|
||||
export * from './workflow'
|
||||
export * from './cert'
|
||||
export * from "./ca";
|
||||
export * from './access'
|
||||
export * from './monitor'
|
||||
export * from './setting'
|
||||
|
||||
@@ -16,55 +16,80 @@ import type {
|
||||
} from '@/types/setting' // Sorted types
|
||||
|
||||
// Relative internal imports
|
||||
import { useApi } from '@api/index'
|
||||
import { useApi, createApiToken } from "@api/index";
|
||||
import { isDev } from "@baota/utils/browser";
|
||||
|
||||
/**
|
||||
* @description 获取系统设置
|
||||
* @param {GetSettingParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<GetSettingResponse, GetSettingParams>} 获取系统设置的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const getSystemSetting = (params?: GetSettingParams): useAxiosReturn<GetSettingResponse, GetSettingParams> =>
|
||||
useApi<GetSettingResponse, GetSettingParams>('/v1/setting/get_setting', params)
|
||||
export const getSystemSetting = (
|
||||
params?: GetSettingParams
|
||||
): useAxiosReturn<GetSettingResponse, GetSettingParams> =>
|
||||
useApi<GetSettingResponse, GetSettingParams>(
|
||||
"/v1/setting/get_setting",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 保存系统设置
|
||||
* @param {SaveSettingParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<AxiosResponseData, SaveSettingParams>} 保存系统设置的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const saveSystemSetting = (params?: SaveSettingParams): useAxiosReturn<AxiosResponseData, SaveSettingParams> =>
|
||||
useApi<AxiosResponseData, SaveSettingParams>('/v1/setting/save_setting', params)
|
||||
export const saveSystemSetting = (
|
||||
params?: SaveSettingParams
|
||||
): useAxiosReturn<AxiosResponseData, SaveSettingParams> =>
|
||||
useApi<AxiosResponseData, SaveSettingParams>(
|
||||
"/v1/setting/save_setting",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 添加告警
|
||||
* @param {AddReportParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<AxiosResponseData, AddReportParams>} 添加告警的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const addReport = (params?: AddReportParams): useAxiosReturn<AxiosResponseData, AddReportParams> =>
|
||||
useApi<AxiosResponseData, AddReportParams>('/v1/report/add_report', params)
|
||||
export const addReport = (
|
||||
params?: AddReportParams
|
||||
): useAxiosReturn<AxiosResponseData, AddReportParams> =>
|
||||
useApi<AxiosResponseData, AddReportParams>("/v1/report/add_report", params);
|
||||
|
||||
/**
|
||||
* @description 更新告警
|
||||
* @param {UpdateReportParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<AxiosResponseData, UpdateReportParams>} 更新告警的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const updateReport = (params?: UpdateReportParams): useAxiosReturn<AxiosResponseData, UpdateReportParams> =>
|
||||
useApi<AxiosResponseData, UpdateReportParams>('/v1/report/upd_report', params)
|
||||
export const updateReport = (
|
||||
params?: UpdateReportParams
|
||||
): useAxiosReturn<AxiosResponseData, UpdateReportParams> =>
|
||||
useApi<AxiosResponseData, UpdateReportParams>(
|
||||
"/v1/report/upd_report",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 删除告警
|
||||
* @param {DeleteReportParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<AxiosResponseData, DeleteReportParams>} 删除告警的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const deleteReport = (params?: DeleteReportParams): useAxiosReturn<AxiosResponseData, DeleteReportParams> =>
|
||||
useApi<AxiosResponseData, DeleteReportParams>('/v1/report/del_report', params)
|
||||
export const deleteReport = (
|
||||
params?: DeleteReportParams
|
||||
): useAxiosReturn<AxiosResponseData, DeleteReportParams> =>
|
||||
useApi<AxiosResponseData, DeleteReportParams>(
|
||||
"/v1/report/del_report",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 测试告警
|
||||
* @param {TestReportParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<AxiosResponseData, TestReportParams>} 测试告警的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const testReport = (params?: TestReportParams): useAxiosReturn<AxiosResponseData, TestReportParams> =>
|
||||
useApi<AxiosResponseData, TestReportParams>('/v1/report/notify_test', params)
|
||||
export const testReport = (
|
||||
params?: TestReportParams
|
||||
): useAxiosReturn<AxiosResponseData, TestReportParams> =>
|
||||
useApi<AxiosResponseData, TestReportParams>("/v1/report/notify_test", params);
|
||||
|
||||
/**
|
||||
* @description 获取告警类型列表
|
||||
@@ -72,14 +97,46 @@ export const testReport = (params?: TestReportParams): useAxiosReturn<AxiosRespo
|
||||
* @returns {useAxiosReturn<GetReportListResponse, GetReportListParams>} 获取告警类型列表的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const getReportList = (
|
||||
params?: GetReportListParams,
|
||||
params?: GetReportListParams
|
||||
): useAxiosReturn<GetReportListResponse, GetReportListParams> =>
|
||||
useApi<GetReportListResponse, GetReportListParams>('/v1/report/get_list', params)
|
||||
useApi<GetReportListResponse, GetReportListParams>(
|
||||
"/v1/report/get_list",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 获取版本信息
|
||||
* @param {GetVersionParams} [params] 请求参数
|
||||
* @returns {useAxiosReturn<GetVersionResponse, GetVersionParams>} 获取版本信息的组合式 API 调用封装。包含响应数据、加载状态及执行函数。
|
||||
*/
|
||||
export const getVersion = (params?: GetVersionParams): useAxiosReturn<GetVersionResponse, GetVersionParams> =>
|
||||
useApi<GetVersionResponse, GetVersionParams>('/v1/setting/get_version', params)
|
||||
export const getVersion = (
|
||||
params?: GetVersionParams
|
||||
): useAxiosReturn<GetVersionResponse, GetVersionParams> =>
|
||||
useApi<GetVersionResponse, GetVersionParams>(
|
||||
"/v1/setting/get_version",
|
||||
params
|
||||
);
|
||||
|
||||
/**
|
||||
* @description 获取下载数据的URL
|
||||
* @returns {string} 拼接好的下载URL
|
||||
*/
|
||||
export const downloadData = (): string => {
|
||||
const apiParams = createApiToken();
|
||||
const finalParams = isDev() ? { ...apiParams } : {};
|
||||
const searchParams = new URLSearchParams();
|
||||
if (finalParams) {
|
||||
Object.entries(finalParams).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const queryString = searchParams.toString();
|
||||
const baseUrl = isDev() ? "/api" : "/";
|
||||
const fullUrl = queryString
|
||||
? `${baseUrl}/v1/setting/download_data?${queryString}`
|
||||
: `${baseUrl}/v1/setting/download_data`;
|
||||
return fullUrl;
|
||||
};
|
||||
Reference in New Issue
Block a user