fiee-official-website/src/store/stock-quote/index.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

import { ref } from 'vue'
import { createGlobalState, useLocalStorage } from '@vueuse/core'
import axios from 'axios'
2025-05-26 07:13:40 +00:00
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
export const useStockQuote = createGlobalState(() => {
const stockQuote = useLocalStorage('stockQuote', {
"Open": "",
"Volume": "",
"DayRange": "",
"WeekRange": "",
"MarketCap": "",
"change": [
"",
""
]
})
2025-05-26 07:13:40 +00:00
const date = new Date();
const options = {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true,
timeZone: 'America/New_York',
timeZoneName: 'short'
};
2025-05-26 07:13:40 +00:00
dayjs.extend(utc)
dayjs.extend(timezone)
/*
美股的常规发行日交易日为周一至周五遇到法定假日则顺延
如果你只需要上一个交易日不考虑法定假日的情况下
获取当前美东时间
如果今天是周一则上一个交易日为上周五
如果今天是周日则上一个交易日为上周五
如果今天是周六则上一个交易日为周五
其他情况上一个交易日为昨天
*/
const getLastTradingDay = () => {
2025-05-26 07:13:40 +00:00
const now = dayjs().tz('America/New_York')
let lastTradingDay
const dayOfWeek = now.day() // 0:周日, 1:周一, ..., 5:周五, 6:周六
const isBeforeClose = now.hour() < 16 || (now.hour() === 16 && now.minute() === 0 && now.second() === 0)
if (dayOfWeek === 0) { // 周日
// 返回本周五16:00
lastTradingDay = now.day(-2).hour(16).minute(0).second(0).millisecond(0)
} else if (dayOfWeek === 6) { // 周六
// 返回本周五16:00
lastTradingDay = now.day(-1).hour(16).minute(0).second(0).millisecond(0)
} else if (dayOfWeek === 1 && isBeforeClose) { // 周一16:00前
// 返回上周五16:00
lastTradingDay = now.day(-2).hour(16).minute(0).second(0).millisecond(0)
} else if (isBeforeClose) { // 工作日16:00前
// 返回前一天16:00
lastTradingDay = now.subtract(1, 'day').hour(16).minute(0).second(0).millisecond(0)
2025-05-26 07:13:40 +00:00
} else {
// 工作日16:00后返回今天16:00
lastTradingDay = now.hour(16).minute(0).second(0).millisecond(0)
2025-05-26 06:12:54 +00:00
}
return lastTradingDay.format('MMM D, YYYY, h:mm A [EDT]')
2025-05-26 07:13:40 +00:00
}
2025-05-26 06:12:54 +00:00
const formatted = ref(getLastTradingDay())
2025-05-26 07:13:40 +00:00
const getStockQuate= async()=>{
const res = await axios.get('https://saas-test.szjixun.cn/api/fiee/chart/forward/test')
stockQuote.value=res.data
2025-05-26 07:13:40 +00:00
}
return {
formatted,
getStockQuate,
stockQuote
}
})