StockListItem.js 7.5 KB

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