shortPool.js 8.2 KB

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