method.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { isString } from "@vue/shared";
  2. import { isNumber } from "@vueuse/core";
  3. //获取图片
  4. export function getImg(url: string): string {
  5. return new URL(`../assets/images/${url}`, import.meta.url).href;
  6. }
  7. //字符串转数组 总数
  8. export function countFormat(val: any, total = 0) {
  9. let str = String(val);
  10. let numArr = str.split("");
  11. if (total === 0) return numArr;
  12. let c = numArr.length <= total && total - numArr.length;
  13. let arr = [];
  14. if (c || c === 0) {
  15. for (let i = 0; i < c; i++) {
  16. numArr.unshift("0");
  17. // console.log(numArr);
  18. }
  19. return numArr;
  20. } else {
  21. for (let i = 0; i < total; i++) {
  22. arr.push("9");
  23. }
  24. return arr;
  25. }
  26. }
  27. //hex2转rgba
  28. export const hex2Rgba = (bgColor: string | any[], alpha = 1) => {
  29. let color = bgColor.slice(1); // 去掉'#'号
  30. let rgba = [
  31. parseInt("0x" + color.slice(0, 2)),
  32. parseInt("0x" + color.slice(2, 4)),
  33. parseInt("0x" + color.slice(4, 6)),
  34. alpha,
  35. ];
  36. return "rgba(" + rgba.toString() + ")";
  37. };
  38. export function addUnit(value: unknown, defaultUnit = "px") {
  39. if (!value) return "";
  40. if (isString(value)) {
  41. return value;
  42. } else if (isNumber(value)) {
  43. return `${value}${defaultUnit}`;
  44. }
  45. // debugWarn(SCOPE, "binding value must be a string or number");
  46. }
  47. //计算百分比
  48. export const getPercent = (value: number, total: number, fixed = 0) => {
  49. return total && Number(((value / total) * 100).toFixed(fixed));
  50. };
  51. export function numToStr(num: number, fixed = 2) {
  52. const million = 10000; // 一万
  53. const billion = 100000000; // 一亿
  54. let unit = "";
  55. let result = Number(num);
  56. if (num >= billion) {
  57. unit = "亿";
  58. result = num / billion;
  59. } else if (num >= million) {
  60. unit = "万";
  61. result = num / million;
  62. }
  63. return result.toFixed(fixed) + unit;
  64. }
  65. export const strToNum = (str: any) => {
  66. const million = 10000; // 一万
  67. const billion = 100000000; // 一亿
  68. const string = String(str);
  69. // console.log(string);
  70. let num = parseFloat(string.replace(/[^\d\.]/g, ""));
  71. if (string.indexOf("亿") !== -1) {
  72. num *= billion;
  73. } else if (string.indexOf("万") !== -1) {
  74. num *= million;
  75. }
  76. return num;
  77. };
  78. export const getNumber = (string: string) => {
  79. return parseFloat(string.replace(/[^\d\.]/g, ""));
  80. };
  81. // 抽离成公共方法
  82. export const awaitWrap = async (promise: Promise<any>) => {
  83. try {
  84. const data = await promise;
  85. return [null, data];
  86. } catch (err) {
  87. return [err, null];
  88. }
  89. };
  90. //随机数
  91. export const genRandom = (min: number, max: number) =>
  92. ((Math.random() * (max - min + 1)) | 0) + min;