shortPool.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_auth = require("../../utils/auth.js");
  4. const utils_api = require("../../utils/api.js");
  5. const _sfc_main = {
  6. __name: "shortPool",
  7. setup(__props) {
  8. const searchKeyword = common_vendor.ref("");
  9. const suggestions = common_vendor.ref([]);
  10. const showSuggestions = common_vendor.ref(false);
  11. const searching = common_vendor.ref(false);
  12. const stockList = common_vendor.ref([]);
  13. const loading = common_vendor.ref(false);
  14. const poolType = 1;
  15. let searchTimer = null;
  16. let refreshTimer = null;
  17. const getToken = () => common_vendor.index.getStorageSync("user_token") || null;
  18. const request = (options) => {
  19. return new Promise((resolve, reject) => {
  20. const token = getToken();
  21. const header = options.header || {};
  22. if (token)
  23. header["Authorization"] = `Bearer ${token}`;
  24. common_vendor.index.request({
  25. url: `http://localhost:8081${options.url}`,
  26. method: options.method || "GET",
  27. data: options.data || {},
  28. header,
  29. success: (res) => {
  30. var _a;
  31. return res.statusCode === 200 ? resolve(res.data) : reject(new Error(((_a = res.data) == null ? void 0 : _a.message) || "请求失败"));
  32. },
  33. fail: () => reject(new Error("网络异常"))
  34. });
  35. });
  36. };
  37. const handleBack = () => {
  38. const pages = getCurrentPages();
  39. pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
  40. };
  41. common_vendor.onShow(() => {
  42. checkAdminPermission();
  43. loadStockList();
  44. startAutoRefresh();
  45. });
  46. common_vendor.onHide(() => stopAutoRefresh());
  47. common_vendor.onUnmounted(() => stopAutoRefresh());
  48. const startAutoRefresh = () => {
  49. stopAutoRefresh();
  50. refreshTimer = setInterval(() => loadStockList(true), 5e3);
  51. };
  52. const stopAutoRefresh = () => {
  53. if (refreshTimer) {
  54. clearInterval(refreshTimer);
  55. refreshTimer = null;
  56. }
  57. };
  58. const checkAdminPermission = async () => {
  59. const userInfo = await utils_auth.refreshUserInfo();
  60. if (!userInfo || userInfo.status !== 2) {
  61. common_vendor.index.showToast({ title: "无权限访问", icon: "none" });
  62. setTimeout(() => common_vendor.index.navigateBack(), 1500);
  63. }
  64. };
  65. const loadStockList = async (silent = false) => {
  66. if (!silent)
  67. loading.value = true;
  68. try {
  69. const res = await request({ url: "/v1/stock/pool/admin/list", data: { poolType } });
  70. if (res.code === 200)
  71. stockList.value = res.data || [];
  72. } catch (e) {
  73. console.error("加载失败", e);
  74. } finally {
  75. if (!silent)
  76. loading.value = false;
  77. }
  78. };
  79. const getChangeClass = (changePercent) => {
  80. if (!changePercent || changePercent === "-")
  81. return "";
  82. return changePercent.startsWith("+") ? "up" : changePercent.startsWith("-") ? "down" : "";
  83. };
  84. const handleSearchInput = () => {
  85. if (searchTimer)
  86. clearTimeout(searchTimer);
  87. if (!searchKeyword.value.trim()) {
  88. suggestions.value = [];
  89. showSuggestions.value = false;
  90. return;
  91. }
  92. searching.value = true;
  93. showSuggestions.value = true;
  94. searchTimer = setTimeout(async () => {
  95. try {
  96. const res = await utils_api.getSuggestions(searchKeyword.value.trim());
  97. suggestions.value = (res.code === 0 || res.code === 200) && res.data ? res.data : [];
  98. } catch (e) {
  99. suggestions.value = [];
  100. } finally {
  101. searching.value = false;
  102. }
  103. }, 300);
  104. };
  105. const clearSearch = () => {
  106. searchKeyword.value = "";
  107. suggestions.value = [];
  108. showSuggestions.value = false;
  109. };
  110. const closeSuggestions = () => showSuggestions.value = false;
  111. const handleAddFromSuggestion = async (item) => {
  112. if (stockList.value.some((s) => s.code === item.code)) {
  113. common_vendor.index.showToast({ title: "该股票已在超短池中", icon: "none" });
  114. return;
  115. }
  116. try {
  117. const res = await request({
  118. url: "/v1/stock/pool/admin/add",
  119. method: "POST",
  120. header: { "content-type": "application/json" },
  121. data: { stockCode: item.code, poolType }
  122. });
  123. if (res.code === 200) {
  124. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  125. clearSearch();
  126. loadStockList();
  127. } else {
  128. common_vendor.index.showToast({ title: res.message || "添加失败", icon: "none" });
  129. }
  130. } catch (e) {
  131. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  132. }
  133. };
  134. const handleDeleteStock = (item) => {
  135. common_vendor.index.showModal({
  136. title: "确认撤出",
  137. content: `确定要将 ${item.name} 从超短池撤出吗?`,
  138. success: async (res) => {
  139. if (res.confirm) {
  140. try {
  141. const result = await request({
  142. url: "/v1/stock/pool/admin/delete",
  143. method: "POST",
  144. header: { "content-type": "application/json" },
  145. data: { stockCode: item.code, poolType }
  146. });
  147. if (result.code === 200) {
  148. common_vendor.index.showToast({ title: "撤出成功", icon: "success" });
  149. loadStockList();
  150. } else {
  151. common_vendor.index.showToast({ title: result.message || "撤出失败", icon: "none" });
  152. }
  153. } catch (e) {
  154. common_vendor.index.showToast({ title: "撤出失败", icon: "none" });
  155. }
  156. }
  157. }
  158. });
  159. };
  160. return (_ctx, _cache) => {
  161. return common_vendor.e({
  162. a: common_vendor.o(handleBack),
  163. b: common_vendor.o([($event) => searchKeyword.value = $event.detail.value, handleSearchInput]),
  164. c: common_vendor.o(($event) => showSuggestions.value = true),
  165. d: searchKeyword.value,
  166. e: searchKeyword.value
  167. }, searchKeyword.value ? {
  168. f: common_vendor.o(clearSearch)
  169. } : {}, {
  170. g: showSuggestions.value && suggestions.value.length > 0
  171. }, showSuggestions.value && suggestions.value.length > 0 ? {
  172. h: common_vendor.f(suggestions.value, (item, index, i0) => {
  173. return {
  174. a: common_vendor.t(item.name),
  175. b: common_vendor.t(item.code),
  176. c: index,
  177. d: common_vendor.o(($event) => handleAddFromSuggestion(item), index)
  178. };
  179. })
  180. } : {}, {
  181. i: showSuggestions.value && searching.value
  182. }, showSuggestions.value && searching.value ? {} : {}, {
  183. j: showSuggestions.value && !searching.value && searchKeyword.value && suggestions.value.length === 0
  184. }, showSuggestions.value && !searching.value && searchKeyword.value && suggestions.value.length === 0 ? {} : {}, {
  185. k: showSuggestions.value && suggestions.value.length > 0
  186. }, showSuggestions.value && suggestions.value.length > 0 ? {
  187. l: common_vendor.o(closeSuggestions)
  188. } : {}, {
  189. m: common_vendor.t(stockList.value.length),
  190. n: loading.value && stockList.value.length === 0
  191. }, loading.value && stockList.value.length === 0 ? {} : stockList.value.length === 0 ? {} : {
  192. p: common_vendor.f(stockList.value, (item, index, i0) => {
  193. return {
  194. a: common_vendor.t(item.name),
  195. b: common_vendor.t(item.code),
  196. c: common_vendor.t(item.currentPrice || "-"),
  197. d: common_vendor.t(item.changePercent || "-"),
  198. e: common_vendor.n(getChangeClass(item.changePercent)),
  199. f: common_vendor.o(($event) => handleDeleteStock(item), item.code),
  200. g: item.code
  201. };
  202. })
  203. }, {
  204. o: stockList.value.length === 0
  205. });
  206. };
  207. }
  208. };
  209. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-98720f9d"], ["__file", "D:/program/gupiao-wx/src/pages/admin/shortPool.vue"]]);
  210. wx.createPage(MiniProgramPage);