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

67 lines
1.6 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
export function errorLog(error: any) {
// 打印到控制台
console.error(error);
// 显示提示
uiContext.get().notification.error({ message: error.message });
}
/**
* @description
* @param {String} msg
*/
export function errorCreate(msg: any) {
const error = new Error(msg);
errorLog(error);
throw error;
}