StockListItem.js 8.2 KB

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