tools.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @description 安全地解析 json 字符串
  3. * @param {String} jsonString 需要解析的 json 字符串
  4. * @param {String} defaultValue 默认值
  5. */
  6. import { uiContext } from '@fast-crud/fast-crud';
  7. export function parse(jsonString = '{}', defaultValue = {}) {
  8. let result = defaultValue;
  9. try {
  10. result = JSON.parse(jsonString);
  11. } catch (error) {
  12. console.log(error);
  13. }
  14. return result;
  15. }
  16. /**
  17. * @description 接口请求返回
  18. * @param {Any} data 返回值
  19. * @param {String} msg 状态信息
  20. * @param {Number} code 状态码
  21. */
  22. export function response(data = {}, msg = '', code = 0) {
  23. return [200, { code, msg, data }];
  24. }
  25. /**
  26. * @description 接口请求返回 正确返回
  27. * @param {Any} data 返回值
  28. * @param {String} msg 状态信息
  29. */
  30. export function responseSuccess(data = {}, msg = '成功') {
  31. return response(data, msg);
  32. }
  33. /**
  34. * @description 接口请求返回 错误返回
  35. * @param {Any} data 返回值
  36. * @param {String} msg 状态信息
  37. * @param {Number} code 状态码
  38. */
  39. export function responseError(data = {}, msg = '请求失败', code = 500) {
  40. return response(data, msg, code);
  41. }
  42. /**
  43. * @description 记录和显示错误
  44. * @param {Error} error 错误对象
  45. */
  46. export function errorLog(error: any, notification = true) {
  47. // 打印到控制台
  48. console.error(error);
  49. // 显示提示
  50. if (notification) {
  51. uiContext.get().notification.error({ message: error.message });
  52. }
  53. }
  54. /**
  55. * @description 创建一个错误
  56. * @param {String} msg 错误信息
  57. */
  58. export function errorCreate(msg: any, notification = true) {
  59. const error = new Error(msg);
  60. errorLog(error, notification);
  61. // throw error;
  62. }
  63. /**
  64. * @description base64转file
  65. * @param {String} base64 base64字符串
  66. * @param {String} fileName 文件名
  67. */
  68. export function base64ToFile(base64: any, fileName: string) {
  69. // 将base64按照 , 进行分割 将前缀 与后续内容分隔开
  70. let data = base64.split(',');
  71. // 利用正则表达式 从前缀中获取图片的类型信息(image/png、image/jpeg、image/webp等)
  72. let type = data[0].match(/:(.*?);/)[1];
  73. // 从图片的类型信息中 获取具体的文件格式后缀(png、jpeg、webp)
  74. let suffix = type.split('/')[1];
  75. // 使用atob()对base64数据进行解码 结果是一个文件数据流 以字符串的格式输出
  76. const bstr = window.atob(data[1]);
  77. // 获取解码结果字符串的长度
  78. let n = bstr.length;
  79. // 根据解码结果字符串的长度创建一个等长的整形数字数组
  80. // 但在创建时 所有元素初始值都为 0
  81. const u8arr = new Uint8Array(n);
  82. // 将整形数组的每个元素填充为解码结果字符串对应位置字符的UTF-16 编码单元
  83. while (n--) {
  84. // charCodeAt():获取给定索引处字符对应的 UTF-16 代码单元
  85. u8arr[n] = bstr.charCodeAt(n);
  86. }
  87. // 利用构造函数创建File文件对象
  88. // new File(bits, name, options)
  89. const file = new File([u8arr], `${fileName}.${suffix}`, {
  90. type: type,
  91. });
  92. // 将File文件对象返回给方法的调用者
  93. return file;
  94. }