"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_api = require("../../utils/api.js"); const utils_auth = require("../../utils/auth.js"); const _sfc_main = { __name: "index", setup(__props) { const keyword = common_vendor.ref(""); const loading = common_vendor.ref(false); const hasSearched = common_vendor.ref(false); const errorMsg = common_vendor.ref(""); const result = common_vendor.ref(null); const suggestions = common_vendor.ref([]); const showDropdown = common_vendor.ref(false); const isLoggedIn = common_vendor.ref(false); const showPhoneAuth = common_vendor.ref(false); let timer = null; common_vendor.onMounted(() => { isLoggedIn.value = utils_auth.isLoggedIn(); console.log("[首页] 登录状态:", isLoggedIn.value); console.log("[首页] Token:", common_vendor.index.getStorageSync("user_token")); }); common_vendor.onShow(() => { isLoggedIn.value = utils_auth.isLoggedIn(); console.log("[首页] 登录状态:", isLoggedIn.value); }); const handleSearchClick = async () => { console.log("=== 点击搜索按钮 ==="); console.log("当前登录状态:", isLoggedIn.value); if (!isLoggedIn.value) { console.log("未登录,直接调用微信授权"); handleLogin(); return; } console.log("已登录,执行搜索"); onSearch(); }; const handleLogin = () => { console.log("[首页] 显示手机号授权弹窗"); showPhoneAuth.value = true; }; const onGetPhoneNumber = async (e) => { console.log("[首页] 获取手机号回调:", e.detail); if (e.detail.errMsg === "getPhoneNumber:ok") { const phoneCode = e.detail.code; console.log("[首页] phoneCode:", phoneCode); common_vendor.index.showLoading({ title: "登录中...", mask: true }); try { const loginRes = await common_vendor.index.login(); console.log("[首页] uni.login完整响应:", loginRes); console.log("[首页] 微信登录code:", loginRes.code); if (!loginRes.code) { throw new Error("获取微信登录code失败"); } const result2 = await utils_auth.wxAuthLogin(loginRes.code, phoneCode); common_vendor.index.hideLoading(); if (result2) { showPhoneAuth.value = false; isLoggedIn.value = utils_auth.isLoggedIn(); onSearch(); } } catch (error) { common_vendor.index.hideLoading(); console.error("[首页] 登录失败:", error); } } else { showPhoneAuth.value = false; common_vendor.index.showToast({ title: "需要授权手机号才能完成登录", icon: "none", duration: 2e3 }); } }; const closePhoneAuth = () => { showPhoneAuth.value = false; tempUserInfo.value = null; }; const onKeywordChange = (e) => { const value = e.detail.value; keyword.value = value; console.log("输入关键词:", value); if (timer) { clearTimeout(timer); } timer = setTimeout(() => { doSearchSuggestions(value); }, 500); }; const doSearchSuggestions = async (kw) => { if (!kw || !kw.trim()) { suggestions.value = []; showDropdown.value = false; return; } try { const response = await utils_api.getSuggestions(kw.trim()); console.log("模糊查询返回数据:", response); const list = response.data || []; suggestions.value = Array.isArray(list) ? list : []; showDropdown.value = suggestions.value.length > 0; console.log("下拉框状态:", { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length }); } catch (err) { console.error("模糊查询错误:", err); suggestions.value = []; showDropdown.value = false; } }; const onSelectSuggestion = (item) => { const searchText = `${item.name} (${item.code})`; keyword.value = searchText; suggestions.value = []; showDropdown.value = false; doSearch(item.code); }; const onSearch = () => { const kw = (keyword.value || "").trim(); if (!kw) { common_vendor.index.showToast({ title: "请输入股票代码或名称", icon: "none" }); return; } let searchCode = kw; const codeMatch = kw.match(/\((\d{6})\)/); if (codeMatch) { searchCode = codeMatch[1]; } doSearch(searchCode); }; const doSearch = async (queryCode) => { loading.value = true; hasSearched.value = true; errorMsg.value = ""; result.value = null; suggestions.value = []; showDropdown.value = false; try { const res = await utils_api.searchStocks(queryCode); if (res.code === 0 && res.data) { result.value = res.data; } else { errorMsg.value = res.message || "未查询到相关股票数据"; } } catch (err) { errorMsg.value = "网络请求失败,请检查网络连接"; } finally { loading.value = false; } }; const onInputBlur = () => { setTimeout(() => { showDropdown.value = false; }, 300); }; return (_ctx, _cache) => { return common_vendor.e({ a: common_vendor.o([($event) => keyword.value = $event.detail.value, onKeywordChange]), b: common_vendor.o(handleSearchClick), c: common_vendor.o(onInputBlur), d: keyword.value, e: common_vendor.o(handleSearchClick), f: showDropdown.value && suggestions.value && suggestions.value.length > 0 }, showDropdown.value && suggestions.value && suggestions.value.length > 0 ? { g: common_vendor.f(suggestions.value, (item, index, i0) => { return { a: common_vendor.t(item.name), b: common_vendor.t(item.code), c: index, d: common_vendor.o(($event) => onSelectSuggestion(item), index) }; }) } : {}, { h: common_vendor.t(isLoggedIn.value ? "" : "(需登录)"), i: hasSearched.value }, hasSearched.value ? common_vendor.e({ j: loading.value }, loading.value ? {} : errorMsg.value ? { l: common_vendor.t(errorMsg.value) } : result.value ? { n: common_vendor.t(result.value.stockName), o: common_vendor.t(result.value.stockCode), p: common_vendor.t(result.value.score), q: common_vendor.f(result.value.history, (item, index, i0) => { return { a: common_vendor.t(item.date), b: common_vendor.t(item.score), c: common_vendor.n(item.score >= 90 ? "tag-danger" : item.score >= 80 ? "tag-success" : "tag-info"), d: index }; }), r: common_vendor.f(result.value.factors, (item, index, i0) => { return { a: common_vendor.t(item.name), b: common_vendor.t(item.value), c: index }; }) } : {}, { k: errorMsg.value, m: result.value }) : {}, { s: showPhoneAuth.value }, showPhoneAuth.value ? { t: common_vendor.o(onGetPhoneNumber), v: common_vendor.o(() => { }), w: common_vendor.o(closePhoneAuth) } : {}); }; } }; const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/index/index.vue"]]); wx.createPage(MiniProgramPage);