index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <view class="page-container">
  3. <scroll-view class="scroll-view" scroll-y>
  4. <view class="content-wrapper">
  5. <!-- 顶部查询卡片 -->
  6. <view class="card search-card" style="position: relative; z-index: 100;">
  7. <text class="card-title">量化分数实时查询 & 历史数据</text>
  8. <view class="search-row">
  9. <input
  10. class="search-input"
  11. type="text"
  12. placeholder="请输入股票代码/名称 (如: 600030)"
  13. placeholder-class="search-placeholder"
  14. confirm-type="search"
  15. v-model="keyword"
  16. @input="onKeywordChange"
  17. @confirm="handleSearchClick"
  18. @blur="onInputBlur"
  19. />
  20. <!-- 搜索按钮(统一处理登录检查) -->
  21. <view class="search-button" @click="handleSearchClick">
  22. <image class="search-icon" src="/static/images/tab_search.png" mode="aspectFit"></image>
  23. </view>
  24. </view>
  25. <!-- 模糊搜索下拉列表 -->
  26. <view class="search-dropdown" v-if="showDropdown && suggestions && suggestions.length > 0">
  27. <view
  28. v-for="(item, index) in suggestions"
  29. :key="index"
  30. class="dropdown-item"
  31. hover-class="dropdown-item-hover"
  32. @tap.stop="onSelectSuggestion(item)"
  33. >
  34. <text class="item-name">{{item.name}}</text>
  35. <text class="item-code">{{item.code}}</text>
  36. </view>
  37. </view>
  38. <text class="search-tip">支持A股代码或名称模糊查询{{ isLoggedIn ? '' : '(需登录)' }}</text>
  39. </view>
  40. <!-- 查询结果 -->
  41. <view v-if="hasSearched" class="card result-card">
  42. <view v-if="loading">
  43. <text class="result-loading">正在查询,请稍候...</text>
  44. </view>
  45. <view v-else-if="errorMsg">
  46. <text class="result-error">{{errorMsg}}</text>
  47. </view>
  48. <view v-else-if="result">
  49. <!-- 头部:名称 / 代码 / 分数徽章 -->
  50. <view class="detail-header">
  51. <view>
  52. <view class="detail-name">{{result.stockName}}({{result.stockCode}})</view>
  53. <view class="detail-sub">最新量化系统评分</view>
  54. </view>
  55. <view class="score-badge">{{result.score}}</view>
  56. </view>
  57. <!-- 历史评分趋势 -->
  58. <view class="section">
  59. <view class="section-title">历史评分趋势</view>
  60. <view class="history-row" v-for="(item, index) in result.history" :key="index">
  61. <text class="history-date">{{item.date}} 的量化评分:</text>
  62. <text :class="['history-score', item.score >= 90 ? 'tag-danger' : (item.score >= 80 ? 'tag-success' : 'tag-info')]">
  63. {{item.score}}
  64. </text>
  65. </view>
  66. <text class="history-note">(注意:此为模拟历史数据,仅供展示。)</text>
  67. </view>
  68. <!-- 评分因子构成 -->
  69. <view class="section">
  70. <view class="section-title">评分因子构成</view>
  71. <view class="factor-row" v-for="(item, index) in result.factors" :key="index">
  72. <text class="factor-name">{{item.name}}</text>
  73. <text class="factor-score">{{item.value}}</text>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. <!-- 量化投资的优势 -->
  79. <view class="card advantage-card">
  80. <view class="card-header">
  81. <view class="icon-circle">
  82. <text class="icon-check">✓</text>
  83. </view>
  84. <text class="card-header-title">量化投资的优势</text>
  85. </view>
  86. <view class="advantage-item">
  87. <text class="advantage-label">客观纪律性:</text>
  88. <text class="advantage-desc">排除情绪干扰,严格执行预设的交易信号。</text>
  89. </view>
  90. <view class="advantage-item">
  91. <text class="advantage-label">高效覆盖广度:</text>
  92. <text class="advantage-desc">能同时分析数千只股票,人工无法企及。</text>
  93. </view>
  94. </view>
  95. <!-- 风险提示(免责声明) -->
  96. <view class="card risk-card">
  97. <view class="card-header">
  98. <view class="icon-warning">
  99. <text class="icon-warning-text">!</text>
  100. </view>
  101. <text class="card-header-title">风险提示(免责声明)</text>
  102. </view>
  103. <text class="risk-text">
  104. 本系统的量化分数和股票池信息均基于历史数据和特定模型计算,并非对未来市场的保证或预测。市场环境瞬息万变,
  105. 量化模型可能存在失效或回撤风险。请勿将本系统数据作为投资决策的唯一依据,请您充分理解股票投资风险,并独立做出投资判断。
  106. </text>
  107. </view>
  108. <!-- 预留底部空间,避免被 tabBar 遮挡 -->
  109. <view class="bottom-safe-area"></view>
  110. </view>
  111. </scroll-view>
  112. </view>
  113. </template>
  114. <script setup>
  115. import { ref, onMounted } from 'vue'
  116. import { getSuggestions, searchStocks } from '../../utils/api.js'
  117. import { isLoggedIn as checkLoginStatus } from '../../utils/auth.js'
  118. // 引入 onShow 生命周期
  119. import { onShow } from '@dcloudio/uni-app'
  120. const keyword = ref('')
  121. const loading = ref(false)
  122. const hasSearched = ref(false)
  123. const errorMsg = ref('')
  124. const result = ref(null)
  125. const suggestions = ref([])
  126. const showDropdown = ref(false)
  127. const isLoggedIn = ref(false)
  128. let timer = null
  129. /**
  130. * 页面加载时检查登录状态
  131. */
  132. onMounted(() => {
  133. isLoggedIn.value = checkLoginStatus()
  134. console.log('[首页] 登录状态:', isLoggedIn.value)
  135. })
  136. /**
  137. * 页面显示时检查登录状态(从登录页返回时会触发)
  138. */
  139. onShow(() => {
  140. isLoggedIn.value = checkLoginStatus()
  141. // 设置导航栏标题
  142. uni.setNavigationBarTitle({ title: '量化交易大师' })
  143. })
  144. /**
  145. * 处理搜索按钮点击(统一登录校验UI)
  146. */
  147. const handleSearchClick = async () => {
  148. console.log('=== 点击搜索按钮 ===')
  149. console.log('当前登录状态:', isLoggedIn.value)
  150. // 检查登录状态
  151. if (!isLoggedIn.value) {
  152. console.log('未登录,跳转到登录页')
  153. uni.showModal({
  154. title: '登录提示',
  155. content: '此功能需要登录后使用,是否前往登录?',
  156. confirmText: '去登录',
  157. cancelText: '取消',
  158. success: (res) => {
  159. if (res.confirm) {
  160. uni.navigateTo({
  161. url: '/pages/login/login'
  162. })
  163. }
  164. }
  165. })
  166. return
  167. }
  168. console.log('已登录,执行搜索')
  169. // 已登录,执行搜索
  170. onSearch()
  171. }
  172. const onKeywordChange = (e) => {
  173. const value = e.detail.value
  174. keyword.value = value
  175. console.log('输入关键词:', value)
  176. if (timer) {
  177. clearTimeout(timer)
  178. }
  179. timer = setTimeout(() => {
  180. doSearchSuggestions(value)
  181. }, 500)
  182. }
  183. const doSearchSuggestions = async (kw) => {
  184. if (!kw || !kw.trim()) {
  185. suggestions.value = []
  186. showDropdown.value = false
  187. return
  188. }
  189. try {
  190. // getSuggestions 返回完整响应对象 {code, message, data}
  191. const response = await getSuggestions(kw.trim())
  192. console.log('模糊查询返回数据:', response)
  193. // 从响应中提取 data 数组
  194. const list = response.data || []
  195. suggestions.value = Array.isArray(list) ? list : []
  196. showDropdown.value = suggestions.value.length > 0
  197. console.log('下拉框状态:', { showDropdown: showDropdown.value, suggestionsLength: suggestions.value.length })
  198. } catch (err) {
  199. console.error('模糊查询错误:', err)
  200. suggestions.value = []
  201. showDropdown.value = false
  202. }
  203. }
  204. const onSelectSuggestion = (item) => {
  205. const searchText = `${item.name} (${item.code})`
  206. keyword.value = searchText
  207. suggestions.value = []
  208. showDropdown.value = false
  209. doSearch(item.code)
  210. }
  211. const onSearch = () => {
  212. const kw = (keyword.value || '').trim()
  213. if (!kw) {
  214. uni.showToast({
  215. title: '请输入股票代码或名称',
  216. icon: 'none'
  217. })
  218. return
  219. }
  220. let searchCode = kw
  221. const codeMatch = kw.match(/\((\d{6})\)/)
  222. if (codeMatch) {
  223. searchCode = codeMatch[1]
  224. }
  225. doSearch(searchCode)
  226. }
  227. const doSearch = async (queryCode) => {
  228. loading.value = true
  229. hasSearched.value = true
  230. errorMsg.value = ''
  231. result.value = null
  232. suggestions.value = []
  233. showDropdown.value = false
  234. try {
  235. const res = await searchStocks(queryCode)
  236. if (res.code === 0 && res.data) {
  237. result.value = res.data
  238. } else {
  239. errorMsg.value = res.message || '未查询到相关股票数据'
  240. }
  241. } catch (err) {
  242. errorMsg.value = '网络请求失败,请检查网络连接'
  243. } finally {
  244. loading.value = false
  245. }
  246. }
  247. const onInputBlur = () => {
  248. // 延迟关闭,给点击下拉项留出时间
  249. setTimeout(() => {
  250. showDropdown.value = false
  251. }, 300)
  252. }
  253. </script>
  254. <style>
  255. /** 首页量化查询界面样式 **/
  256. .page-container {
  257. height: 100vh;
  258. display: flex;
  259. flex-direction: column;
  260. background: #f5f6fb;
  261. }
  262. .scroll-view {
  263. flex: 1;
  264. height: 0;
  265. }
  266. .content-wrapper {
  267. padding: 32rpx 32rpx 48rpx;
  268. }
  269. .card {
  270. background: #ffffff;
  271. border-radius: 24rpx;
  272. padding: 32rpx 32rpx 36rpx;
  273. box-shadow: 0 16rpx 40rpx rgba(37, 52, 94, 0.08);
  274. margin-bottom: 32rpx;
  275. position: relative;
  276. }
  277. .search-card {
  278. margin-top: 16rpx;
  279. z-index: 10;
  280. }
  281. .card-title {
  282. display: block;
  283. font-size: 32rpx;
  284. font-weight: 600;
  285. color: #222222;
  286. margin-bottom: 32rpx;
  287. }
  288. .search-row {
  289. display: flex;
  290. align-items: center;
  291. background: #f7f8fc;
  292. border-radius: 999rpx;
  293. padding: 0 8rpx 0 32rpx;
  294. height: 96rpx;
  295. box-sizing: border-box;
  296. }
  297. .search-input {
  298. flex: 1;
  299. font-size: 28rpx;
  300. color: #222222;
  301. }
  302. .search-placeholder {
  303. color: #b4b8c6;
  304. }
  305. .search-button {
  306. width: 96rpx;
  307. height: 80rpx;
  308. margin-left: 12rpx;
  309. border-radius: 999rpx;
  310. background: #f5f6fb;
  311. display: flex;
  312. align-items: center;
  313. justify-content: center;
  314. }
  315. .search-icon {
  316. width: 48rpx;
  317. height: 48rpx;
  318. }
  319. .search-tip {
  320. display: block;
  321. margin-top: 20rpx;
  322. font-size: 24rpx;
  323. color: #9ca2b5;
  324. }
  325. /* 下拉列表样式 */
  326. .search-dropdown {
  327. position: absolute;
  328. top: 100%; /* 相对于父元素定位,在搜索框下方 */
  329. left: 0;
  330. right: 0;
  331. margin-top: 8rpx;
  332. background: #ffffff;
  333. border-radius: 16rpx;
  334. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.15);
  335. max-height: 400rpx;
  336. overflow-y: auto;
  337. z-index: 1000; /* 提高层级,确保在最上层 */
  338. border: 1rpx solid #f0f0f0;
  339. }
  340. .dropdown-item {
  341. padding: 24rpx 32rpx;
  342. border-bottom: 1rpx solid #f5f6fb;
  343. display: flex;
  344. justify-content: space-between;
  345. align-items: center;
  346. }
  347. .dropdown-item:last-child {
  348. border-bottom: none;
  349. }
  350. .dropdown-item-hover {
  351. background-color: #f7f8fc;
  352. }
  353. .item-name {
  354. font-size: 28rpx;
  355. color: #333;
  356. }
  357. .item-code {
  358. font-size: 24rpx;
  359. color: #999;
  360. }
  361. .result-card {
  362. margin-top: 8rpx;
  363. }
  364. .result-loading {
  365. font-size: 26rpx;
  366. color: #666a7f;
  367. }
  368. .result-error {
  369. font-size: 26rpx;
  370. color: #ff5b5b;
  371. }
  372. .detail-header {
  373. display: flex;
  374. justify-content: space-between;
  375. align-items: center;
  376. margin-bottom: 24rpx;
  377. }
  378. .detail-name {
  379. font-size: 30rpx;
  380. font-weight: 700;
  381. color: #1f1f2e;
  382. }
  383. .detail-sub {
  384. margin-top: 8rpx;
  385. font-size: 24rpx;
  386. color: #9da3b5;
  387. }
  388. .score-badge {
  389. min-width: 140rpx;
  390. padding: 12rpx 22rpx;
  391. border-radius: 20rpx;
  392. background: #f16565;
  393. color: #ffffff;
  394. font-size: 40rpx;
  395. font-weight: 700;
  396. text-align: right;
  397. box-shadow: 0 10rpx 24rpx rgba(241, 101, 101, 0.35);
  398. }
  399. .section {
  400. margin-top: 20rpx;
  401. }
  402. .section-title {
  403. font-size: 28rpx;
  404. font-weight: 700;
  405. color: #1f1f2e;
  406. margin-bottom: 16rpx;
  407. }
  408. .history-row {
  409. display: flex;
  410. align-items: center;
  411. justify-content: space-between;
  412. padding: 18rpx 0;
  413. border-bottom: 1rpx solid #f1f2f6;
  414. }
  415. .history-row:last-child {
  416. border-bottom: none;
  417. }
  418. .history-date {
  419. font-size: 26rpx;
  420. color: #3c4050;
  421. }
  422. .history-score {
  423. min-width: 96rpx;
  424. text-align: center;
  425. padding: 6rpx 18rpx;
  426. border-radius: 30rpx;
  427. font-size: 28rpx;
  428. font-weight: 700;
  429. color: #ffffff;
  430. }
  431. .tag-danger {
  432. background: #f16565;
  433. }
  434. .tag-success {
  435. background: #3abf81;
  436. }
  437. .tag-info {
  438. background: #4c86ff;
  439. }
  440. .history-note {
  441. display: block;
  442. margin-top: 12rpx;
  443. font-size: 22rpx;
  444. color: #9da3b5;
  445. }
  446. .factor-row {
  447. display: flex;
  448. align-items: center;
  449. justify-content: space-between;
  450. padding: 14rpx 0;
  451. }
  452. .factor-name {
  453. font-size: 26rpx;
  454. color: #3c4050;
  455. }
  456. .factor-score {
  457. font-size: 28rpx;
  458. font-weight: 600;
  459. color: #4c86ff;
  460. }
  461. .card-header {
  462. display: flex;
  463. align-items: center;
  464. margin-bottom: 24rpx;
  465. }
  466. .card-header-title {
  467. font-size: 30rpx;
  468. font-weight: 600;
  469. color: #222222;
  470. }
  471. .icon-circle {
  472. width: 40rpx;
  473. height: 40rpx;
  474. border-radius: 50%;
  475. background: #e7f7ef;
  476. display: flex;
  477. align-items: center;
  478. justify-content: center;
  479. margin-right: 16rpx;
  480. }
  481. .icon-check {
  482. font-size: 24rpx;
  483. color: #28a745;
  484. }
  485. .advantage-item {
  486. margin-bottom: 12rpx;
  487. font-size: 26rpx;
  488. line-height: 1.6;
  489. color: #4a4f63;
  490. }
  491. .advantage-label {
  492. font-weight: 600;
  493. }
  494. .advantage-desc {
  495. font-weight: 400;
  496. }
  497. .risk-card {
  498. margin-bottom: 24rpx;
  499. }
  500. .icon-warning {
  501. width: 40rpx;
  502. height: 40rpx;
  503. border-radius: 50%;
  504. background: #ffecef;
  505. display: flex;
  506. align-items: center;
  507. justify-content: center;
  508. margin-right: 16rpx;
  509. }
  510. .icon-warning-text {
  511. font-size: 26rpx;
  512. color: #ff5b5b;
  513. }
  514. .risk-text {
  515. display: block;
  516. font-size: 24rpx;
  517. line-height: 1.8;
  518. color: #666a7f;
  519. }
  520. .bottom-safe-area {
  521. height: 80rpx;
  522. }
  523. </style>