index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import request from './request'
  2. export function withInstall(component) {
  3. component.install = function (app) {
  4. app.component(component.name, component);
  5. };
  6. return component;
  7. }
  8. export function WithInstallOfGlobalMethod(globalName, method) {
  9. method.install = function (app) {
  10. app.prototype[globalName] = method;
  11. };
  12. return method;
  13. }
  14. export {
  15. request
  16. }
  17. export function calcPercent(bn, tq) {
  18. if (bn - tq === 0) {
  19. return '-'
  20. }
  21. if (tq === 0) {
  22. return '-'
  23. }
  24. return `${(parseInt((Math.abs(bn - tq) / tq).toFixed(2) * 100 ))}%`
  25. }
  26. export function getState(bn, tq) {
  27. if(tq === 0) {
  28. return 0;
  29. }
  30. if (bn -tq > 0) {
  31. return 1;
  32. } else if (bn - tq < 0){
  33. return -1;
  34. } else {
  35. return 0;
  36. }
  37. }
  38. export const throttle = (func, wait = 1000, type = 1) => {
  39. let previous = 0;
  40. let timeout;
  41. return function () {
  42. let context = this;
  43. let args = arguments;
  44. if (type === 1) {
  45. let now = Date.now();
  46. if (now - previous > wait) {
  47. func.apply(context, args);
  48. previous = now;
  49. }
  50. } else if (type === 2) {
  51. if (!timeout) {
  52. timeout = setTimeout(() => {
  53. timeout = null;
  54. func.apply(context, args)
  55. }, wait)
  56. }
  57. }
  58. }
  59. }