You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.9 KiB

  1. var util = {}
  2. util.data = {}
  3. util.data.base_url = 'https://dbqb.nfdxy.net/devApi'
  4. // util.data.base_url = 'https://dbqb.nfdxy.net/prodApi'
  5. // AJAX 请求方法
  6. util.request = (url, callback, data = {}, failCallback) => {
  7. url = util.data.base_url + url
  8. console.log('请求该接口->', url, '请求参数为->', data);
  9. uni.request({
  10. url: url, //仅为示例,并非真实接口地址。
  11. data,
  12. method: 'post',
  13. header: {
  14. 'content-type': 'application/json',
  15. 'version': uni.getSystemInfoSync().appVersion,
  16. 'client': uni.getSystemInfoSync().platform == 'ios' ? 'ios' : 'android',
  17. 'token': uni.getStorageSync('token')
  18. },
  19. sslVerify: false,
  20. success: callback,
  21. fail: failCallback
  22. });
  23. }
  24. export default util
  25. // 画图需要用到的方法
  26. export const utils = {
  27. // 格式化价格
  28. formatPrice(price) {
  29. return price.toFixed(2);
  30. },
  31. // 计算数据范围
  32. calculateDataRange(data, key) {
  33. if (!data || data.length === 0) {
  34. return {
  35. min: 0,
  36. max: 0,
  37. };
  38. }
  39. const values = data.map((item) => item[key]);
  40. return {
  41. min: Math.min(...values),
  42. max: Math.max(...values),
  43. };
  44. },
  45. // 计算标签
  46. calculateLabel(data, type = 2, preClosePrice = 0, key, num) {
  47. let label = [];
  48. if (key === "price") {
  49. // 分时价格区间
  50. if (type == 1) {
  51. const priceRange = utils.calculateDataRange(data, "price");
  52. const theMost = Math.max(priceRange.max - preClosePrice, preClosePrice - priceRange.min);
  53. const mid = (num - 1) / 2;
  54. // 计算分时价格标签
  55. label[mid] = {
  56. value: utils.formatPrice(preClosePrice),
  57. ratio: utils.formatPrice(0) + "%",
  58. };
  59. for (let i = 0; i < mid; i++) {
  60. label[i] = {
  61. value: utils.formatPrice(preClosePrice + (theMost * (mid - i)) / mid),
  62. ratio: utils.formatPrice((100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  63. };
  64. label[num - 1 - i] = {
  65. value: utils.formatPrice(preClosePrice - (theMost * (mid - i)) / mid),
  66. ratio: utils.formatPrice((-1 * 100 * (theMost * (mid - i))) / mid / preClosePrice) +
  67. "%",
  68. };
  69. }
  70. timeChartObject.value.max = preClosePrice + theMost;
  71. timeChartObject.value.min = preClosePrice - theMost;
  72. return label;
  73. } else {
  74. const highPriceRange = utils.calculateDataRange(data, "high");
  75. const lowPriceRange = utils.calculateDataRange(data, "low");
  76. const priceDiff = highPriceRange.max * 1.01 - lowPriceRange.min * 0.99;
  77. for (let i = 0; i < num; ++i) {
  78. label[i] = {
  79. value: utils.formatPrice(highPriceRange.max - (i * priceDiff) / num),
  80. };
  81. }
  82. return label;
  83. }
  84. } else if (key === "volume") {
  85. const volumeRange = utils.calculateDataRange(data, "volume");
  86. label[0] = {
  87. value: utils.formatPrice(volumeRange.max),
  88. };
  89. label[1] = {
  90. value: utils.formatPrice(0),
  91. };
  92. return label;
  93. }
  94. return null;
  95. },
  96. // 线性插值
  97. lerp(start, end, factor) {
  98. return start + (end - start) * factor;
  99. },
  100. // 股市数值格式化方法
  101. formatStockNumber(value, decimalPlaces = 2) {
  102. const num = Number(value);
  103. if (isNaN(num)) return "0";
  104. const absNum = Math.abs(num);
  105. const sign = num < 0 ? "-" : "";
  106. if (absNum >= 1000000000000) {
  107. // 万亿级别
  108. return sign + (absNum / 1000000000000).toFixed(decimalPlaces) + "万亿";
  109. } else if (absNum >= 100000000) {
  110. // 亿级别
  111. return sign + (absNum / 100000000).toFixed(decimalPlaces) + "亿";
  112. } else if (absNum >= 10000) {
  113. // 万级别
  114. return sign + (absNum / 10000).toFixed(decimalPlaces) + "万";
  115. } else {
  116. // 小于万的直接显示
  117. return sign + absNum.toFixed(decimalPlaces);
  118. }
  119. },
  120. };
  121. // 防抖函数
  122. export const throttle = (fn, delay = 1000) => {
  123. //距离上一次的执行时间
  124. let lastTime = 0;
  125. return function() {
  126. let _this = this;
  127. let _arguments = arguments;
  128. let now = new Date().getTime();
  129. //如果距离上一次执行超过了delay才能再次执行
  130. if (now - lastTime > delay) {
  131. fn.apply(_this, _arguments);
  132. lastTime = now;
  133. }
  134. };
  135. };