2023-01-29 13:44:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 安全地解析 json 字符串
|
|
|
|
|
|
* @param {String} jsonString 需要解析的 json 字符串
|
|
|
|
|
|
* @param {String} defaultValue 默认值
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { uiContext } from "@fast-crud/fast-crud";
|
2025-04-17 22:34:21 +08:00
|
|
|
|
import { CodeError } from "/@/api/service";
|
2023-01-29 13:44:19 +08:00
|
|
|
|
|
|
|
|
|
|
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 错误对象
|
|
|
|
|
|
*/
|
2024-10-22 18:46:29 +08:00
|
|
|
|
export function errorLog(error: any, notify = true) {
|
2023-01-29 13:44:19 +08:00
|
|
|
|
// 打印到控制台
|
2023-06-27 22:45:27 +08:00
|
|
|
|
console.error("errorLog", error);
|
2023-08-19 19:23:55 +00:00
|
|
|
|
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请求";
|
|
|
|
|
|
}
|
2024-10-22 18:46:29 +08:00
|
|
|
|
if (notify) {
|
|
|
|
|
|
// 显示提示
|
|
|
|
|
|
uiContext.get().notification.error({ message });
|
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 创建一个错误
|
|
|
|
|
|
*/
|
2025-04-17 22:34:21 +08:00
|
|
|
|
export function errorCreate(msg: string, notify = true, data?: any) {
|
|
|
|
|
|
const err = new CodeError(msg, data.code, data.data);
|
2023-06-27 22:45:27 +08:00
|
|
|
|
console.error("errorCreate", err);
|
2024-10-22 18:46:29 +08:00
|
|
|
|
if (notify) {
|
|
|
|
|
|
uiContext.get().notification.error({ message: err.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
|
throw err;
|
2023-01-29 13:44:19 +08:00
|
|
|
|
}
|