StockListItem.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const _sfc_main = {
  4. __name: "StockListItem",
  5. props: {
  6. stock: {
  7. type: Object,
  8. required: true
  9. },
  10. showDelete: {
  11. type: Boolean,
  12. default: false
  13. }
  14. },
  15. emits: ["delete"],
  16. setup(__props, { emit }) {
  17. const props = __props;
  18. let componentInstance = null;
  19. const deleteWidth = 60;
  20. const moveX = common_vendor.ref(0);
  21. let currentX = 0;
  22. const canvasId = common_vendor.ref(`chart-${props.stock.code}-${Math.random().toString(36).slice(2, 11)}`);
  23. const getMarketTag = (code) => {
  24. if (code.startsWith("6"))
  25. return "沪";
  26. if (code.startsWith("0"))
  27. return "深";
  28. if (code.startsWith("3"))
  29. return "创";
  30. return "沪";
  31. };
  32. const getMarketClass = (code) => {
  33. if (code.startsWith("6"))
  34. return "market-sh";
  35. if (code.startsWith("0"))
  36. return "market-sz";
  37. if (code.startsWith("3"))
  38. return "market-cy";
  39. return "market-sh";
  40. };
  41. const getChangeClass = (changePercent) => {
  42. if (!changePercent)
  43. return "";
  44. const str = String(changePercent).replace("%", "").replace("+", "");
  45. const value = parseFloat(str);
  46. if (value > 0)
  47. return "change-up";
  48. if (value < 0)
  49. return "change-down";
  50. return "";
  51. };
  52. const formatChangePercent = (changePercent) => {
  53. if (!changePercent)
  54. return "--";
  55. return String(changePercent);
  56. };
  57. const formatPrice = (price) => {
  58. if (!price)
  59. return "--";
  60. return parseFloat(price).toFixed(2);
  61. };
  62. const hasValidChange = (changePercent) => {
  63. if (!changePercent)
  64. return false;
  65. const str = String(changePercent).replace("%", "").replace("+", "");
  66. const value = parseFloat(str);
  67. return value !== 0 && !isNaN(value);
  68. };
  69. const drawTrendChart = (instance) => {
  70. console.log("[趋势图] 开始绘制:", props.stock.code, canvasId.value);
  71. let trendData = props.stock.trendData;
  72. if (!trendData || !Array.isArray(trendData) || trendData.length === 0) {
  73. console.log("[趋势图] 使用模拟数据");
  74. trendData = generateMockTrendData();
  75. } else {
  76. console.log("[趋势图] 使用真实数据,数据点数:", trendData.length);
  77. }
  78. const ctx = common_vendor.index.createCanvasContext(canvasId.value, instance);
  79. const width = 100;
  80. const height = 30;
  81. const padding = 2;
  82. const maxValue = Math.max(...trendData);
  83. const minValue = Math.min(...trendData);
  84. const range = maxValue - minValue || 1;
  85. console.log("[趋势图] 数据范围:", { min: minValue, max: maxValue, range });
  86. const changePercent = parseFloat(String(props.stock.changePercent || "0").replace("%", "").replace("+", ""));
  87. const isUp = changePercent >= 0;
  88. const lineColor = isUp ? "#FF3B30" : "#34C759";
  89. const fillColor = isUp ? "rgba(255, 59, 48, 0.1)" : "rgba(52, 199, 89, 0.1)";
  90. console.log("[趋势图] 颜色:", { isUp, lineColor, changePercent });
  91. ctx.beginPath();
  92. trendData.forEach((value, index) => {
  93. const x = padding + index / (trendData.length - 1) * (width - padding * 2);
  94. const y = height - padding - (value - minValue) / range * (height - padding * 2);
  95. if (index === 0) {
  96. ctx.moveTo(x, y);
  97. } else {
  98. ctx.lineTo(x, y);
  99. }
  100. });
  101. ctx.setStrokeStyle(lineColor);
  102. ctx.setLineWidth(1);
  103. ctx.stroke();
  104. ctx.lineTo(width - padding, height - padding);
  105. ctx.lineTo(padding, height - padding);
  106. ctx.closePath();
  107. ctx.setFillStyle(fillColor);
  108. ctx.fill();
  109. ctx.draw();
  110. console.log("[趋势图] 绘制完成");
  111. };
  112. const generateMockTrendData = () => {
  113. const changePercent = parseFloat(String(props.stock.changePercent || "0").replace("%", "").replace("+", ""));
  114. const points = 30;
  115. const data = [];
  116. let baseValue = 100;
  117. const trend = changePercent / 100;
  118. for (let i = 0; i < points; i++) {
  119. const randomChange = (Math.random() - 0.5) * 2;
  120. const trendChange = i / points * trend * 100;
  121. baseValue = baseValue + randomChange + trendChange / points;
  122. data.push(baseValue);
  123. }
  124. return data;
  125. };
  126. const handleMoveChange = (e) => {
  127. currentX = e.detail.x;
  128. };
  129. const handleMoveEnd = () => {
  130. if (!props.showDelete)
  131. return;
  132. if (currentX < -deleteWidth / 3) {
  133. moveX.value = -deleteWidth;
  134. } else {
  135. moveX.value = 0;
  136. }
  137. };
  138. const handleDelete = () => {
  139. moveX.value = 0;
  140. emit("delete");
  141. };
  142. common_vendor.onMounted(() => {
  143. console.log("[趋势图] 组件挂载:", props.stock.code);
  144. componentInstance = common_vendor.getCurrentInstance();
  145. common_vendor.nextTick$1(() => {
  146. setTimeout(() => {
  147. drawTrendChart(componentInstance);
  148. }, 300);
  149. });
  150. });
  151. common_vendor.watch(() => props.stock.trendData, (newData) => {
  152. if (newData && componentInstance) {
  153. console.log("[趋势图] 数据更新,重新绘制:", props.stock.code);
  154. common_vendor.nextTick$1(() => {
  155. drawTrendChart(componentInstance);
  156. });
  157. }
  158. }, { deep: true });
  159. common_vendor.watch(() => props.stock.changePercent, () => {
  160. if (componentInstance) {
  161. common_vendor.nextTick$1(() => {
  162. drawTrendChart(componentInstance);
  163. });
  164. }
  165. });
  166. return (_ctx, _cache) => {
  167. return common_vendor.e({
  168. a: common_vendor.t(__props.stock.name),
  169. b: common_vendor.t(getMarketTag(__props.stock.code)),
  170. c: common_vendor.n(getMarketClass(__props.stock.code)),
  171. d: common_vendor.t(__props.stock.code),
  172. e: canvasId.value,
  173. f: canvasId.value,
  174. g: hasValidChange(__props.stock.changePercent)
  175. }, hasValidChange(__props.stock.changePercent) ? {
  176. h: common_vendor.t(formatChangePercent(__props.stock.changePercent)),
  177. i: common_vendor.n(getChangeClass(__props.stock.changePercent))
  178. } : {}, {
  179. j: common_vendor.t(formatPrice(__props.stock.currentPrice)),
  180. k: __props.showDelete
  181. }, __props.showDelete ? {
  182. l: common_vendor.o(handleDelete)
  183. } : {}, {
  184. m: moveX.value,
  185. n: common_vendor.o(handleMoveChange),
  186. o: common_vendor.o(handleMoveEnd)
  187. });
  188. };
  189. }
  190. };
  191. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-29af7fd7"], ["__file", "D:/program/gupiao-wx/src/components/StockListItem.vue"]]);
  192. wx.createComponent(Component);