shortPool.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: `${utils_api.BASE_URL}${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 getRandomInterval = () => 4500 + Math.random() * 1e3;
  49. const startAutoRefresh = () => {
  50. stopAutoRefresh();
  51. const scheduleNextRefresh = () => {
  52. refreshTimer = setTimeout(async () => {
  53. await loadStockList(true);
  54. scheduleNextRefresh();
  55. }, getRandomInterval());
  56. };
  57. scheduleNextRefresh();
  58. };
  59. const stopAutoRefresh = () => {
  60. if (refreshTimer) {
  61. clearTimeout(refreshTimer);
  62. refreshTimer = null;
  63. }
  64. };
  65. const checkAdminPermission = async () => {
  66. const userInfo = await utils_auth.refreshUserInfo();
  67. if (!userInfo || userInfo.status !== 2) {
  68. common_vendor.index.showToast({ title: "无权限访问", icon: "none" });
  69. setTimeout(() => {
  70. const pages = getCurrentPages();
  71. pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
  72. }, 1500);
  73. }
  74. };
  75. const loadStockList = async (silent = false) => {
  76. if (!silent)
  77. loading.value = true;
  78. try {
  79. const res = await request({ url: "/v1/stock/pool/admin/list", data: { poolType } });
  80. if (res.code === 200)
  81. stockList.value = res.data || [];
  82. } catch (e) {
  83. console.error("加载失败", e);
  84. } finally {
  85. if (!silent)
  86. loading.value = false;
  87. }
  88. };
  89. const getChangeClass = (changePercent) => {
  90. if (!changePercent || changePercent === "-")
  91. return "";
  92. return changePercent.startsWith("+") ? "up" : changePercent.startsWith("-") ? "down" : "";
  93. };
  94. const handleSearchInput = () => {
  95. if (searchTimer)
  96. clearTimeout(searchTimer);
  97. if (!searchKeyword.value.trim()) {
  98. suggestions.value = [];
  99. showSuggestions.value = false;
  100. return;
  101. }
  102. searching.value = true;
  103. showSuggestions.value = true;
  104. searchTimer = setTimeout(async () => {
  105. try {
  106. const res = await utils_api.getSuggestions(searchKeyword.value.trim());
  107. suggestions.value = res.code === 200 && res.data ? res.data : [];
  108. } catch (e) {
  109. suggestions.value = [];
  110. } finally {
  111. searching.value = false;
  112. }
  113. }, 300);
  114. };
  115. const clearSearch = () => {
  116. searchKeyword.value = "";
  117. suggestions.value = [];
  118. showSuggestions.value = false;
  119. };
  120. const closeSuggestions = () => showSuggestions.value = false;
  121. const handleAddFromSuggestion = async (item) => {
  122. if (stockList.value.some((s) => s.code === item.code)) {
  123. common_vendor.index.showToast({ title: "该股票已在超短池中", icon: "none" });
  124. return;
  125. }
  126. try {
  127. const res = await request({
  128. url: "/v1/stock/pool/admin/add",
  129. method: "POST",
  130. header: { "content-type": "application/json" },
  131. data: { stockCode: item.code, poolType }
  132. });
  133. if (res.code === 200) {
  134. common_vendor.index.showToast({ title: "添加成功", icon: "success" });
  135. clearSearch();
  136. loadStockList();
  137. } else {
  138. common_vendor.index.showToast({ title: res.message || "添加失败", icon: "none" });
  139. }
  140. } catch (e) {
  141. common_vendor.index.showToast({ title: "添加失败", icon: "none" });
  142. }
  143. };
  144. const handleDeleteStock = (item) => {
  145. common_vendor.index.showModal({
  146. title: "确认撤出",
  147. content: `确定要将 ${item.name} 从超短池撤出吗?`,
  148. success: async (res) => {
  149. if (res.confirm) {
  150. try {
  151. const result = await request({
  152. url: "/v1/stock/pool/admin/delete",
  153. method: "POST",
  154. header: { "content-type": "application/json" },
  155. data: { stockCode: item.code, poolType }
  156. });
  157. if (result.code === 200) {
  158. common_vendor.index.showToast({ title: "撤出成功", icon: "success" });
  159. loadStockList();
  160. } else {
  161. common_vendor.index.showToast({ title: result.message || "撤出失败", icon: "none" });
  162. }
  163. } catch (e) {
  164. common_vendor.index.showToast({ title: "撤出失败", icon: "none" });
  165. }
  166. }
  167. }
  168. });
  169. };
  170. return (_ctx, _cache) => {
  171. return common_vendor.e({
  172. a: common_vendor.o(handleBack),
  173. b: common_vendor.o([($event) => searchKeyword.value = $event.detail.value, handleSearchInput]),
  174. c: common_vendor.o(($event) => showSuggestions.value = true),
  175. d: searchKeyword.value,
  176. e: searchKeyword.value
  177. }, searchKeyword.value ? {
  178. f: common_vendor.o(clearSearch)
  179. } : {}, {
  180. g: showSuggestions.value && suggestions.value.length > 0
  181. }, showSuggestions.value && suggestions.value.length > 0 ? {
  182. h: common_vendor.f(suggestions.value, (item, index, i0) => {
  183. return {
  184. a: common_vendor.t(item.name),
  185. b: common_vendor.t(item.code),
  186. c: index,
  187. d: common_vendor.o(($event) => handleAddFromSuggestion(item), index)
  188. };
  189. })
  190. } : {}, {
  191. i: showSuggestions.value && searching.value
  192. }, showSuggestions.value && searching.value ? {} : {}, {
  193. j: showSuggestions.value && !searching.value && searchKeyword.value && suggestions.value.length === 0
  194. }, showSuggestions.value && !searching.value && searchKeyword.value && suggestions.value.length === 0 ? {} : {}, {
  195. k: showSuggestions.value && suggestions.value.length > 0
  196. }, showSuggestions.value && suggestions.value.length > 0 ? {
  197. l: common_vendor.o(closeSuggestions)
  198. } : {}, {
  199. m: common_vendor.t(stockList.value.length),
  200. n: loading.value && stockList.value.length === 0
  201. }, loading.value && stockList.value.length === 0 ? {} : stockList.value.length === 0 ? {} : {
  202. p: common_vendor.f(stockList.value, (item, index, i0) => {
  203. return {
  204. a: common_vendor.t(item.name),
  205. b: common_vendor.t(item.code),
  206. c: common_vendor.t(item.currentPrice || "-"),
  207. d: common_vendor.t(item.changePercent || "-"),
  208. e: common_vendor.n(getChangeClass(item.changePercent)),
  209. f: common_vendor.o(($event) => handleDeleteStock(item), item.code),
  210. g: item.code
  211. };
  212. })
  213. }, {
  214. o: stockList.value.length === 0
  215. });
  216. };
  217. }
  218. };
  219. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-98720f9d"], ["__file", "D:/program/gupiao/gupiao-wx/src/pages/admin/shortPool.vue"]]);
  220. wx.createPage(MiniProgramPage);