Files
certd/packages/ui/certd-client/src/api/tools.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-01-29 13:44:19 +08:00
/**
* @description json
* @param {String} jsonString json
* @param {String} defaultValue
*/
import { uiContext } from "@fast-crud/fast-crud";
export function parse(jsonString = "{}", defaultValue = {}) {
let result = defaultValue;
try {
result = JSON.parse(jsonString);
} catch (error) {
console.log(error);
}
return result;
}
/**
* @description
* @param {Any} data
* @param {String} msg
* @param {Number} code
*/
export function response(data = {}, msg = "", code = 0) {
return [200, { code, msg, data }];
}
/**
* @description
* @param {Any} data
* @param {String} msg
*/
export function responseSuccess(data = {}, msg = "成功") {
return response(data, msg);
}
/**
* @description
* @param {Any} data
* @param {String} msg
* @param {Number} code
*/
export function responseError(data = {}, msg = "请求失败", code = 500) {
return response(data, msg, code);
}
/**
* @description
* @param {Error} error
*/
2023-06-25 15:30:18 +08:00
export function errorLog(error: any) {
2023-01-29 13:44:19 +08:00
// 打印到控制台
2023-06-27 22:45:27 +08:00
console.error("errorLog", error);
let message = error.message;
if (error.response?.data?.message) {
message = error.response.data.message;
}
2024-10-03 22:03:49 +08:00
if (message.indexOf("ssl3_get_record:wrong version number") >= 0) {
2024-10-03 01:29:12 +08:00
message = "http协议错误服务端要求http协议请检查是否使用了https请求";
}
2023-01-29 13:44:19 +08:00
// 显示提示
uiContext.get().notification.error({ message });
2023-01-29 13:44:19 +08:00
}
/**
* @description
* @param {String} msg
*/
2023-06-25 15:30:18 +08:00
export function errorCreate(msg: string) {
2023-06-27 09:29:43 +08:00
const err = new Error(msg);
2023-06-27 22:45:27 +08:00
console.error("errorCreate", err);
2023-06-27 09:29:43 +08:00
uiContext.get().notification.error({ message: err.message });
throw err;
2023-01-29 13:44:19 +08:00
}