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.

142 lines
4.0 KiB

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