83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
import { ref } from 'vue'
|
|
import { createGlobalState, useLocalStorage } from '@vueuse/core'
|
|
import axios from 'axios'
|
|
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": [
|
|
"",
|
|
""
|
|
]
|
|
})
|
|
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'
|
|
};
|
|
let lastTradingDay
|
|
dayjs.extend(utc)
|
|
dayjs.extend(timezone)
|
|
/*
|
|
美股的常规发行日(交易日)为周一至周五,遇到法定假日则顺延。
|
|
如果你只需要“上一个交易日”(不考虑法定假日)的情况下
|
|
获取当前美东时间。
|
|
如果今天是周一,则上一个交易日为上周五。
|
|
如果今天是周日,则上一个交易日为上周五。
|
|
如果今天是周六,则上一个交易日为周五。
|
|
其他情况,上一个交易日为昨天。
|
|
*/
|
|
|
|
const getLastTradingDay = async () => {
|
|
const toDate = dayjs().format('YYYY-MM-DD');
|
|
const finalFromDate = dayjs().subtract(7, 'day').format('YYYY-MM-DD');
|
|
let url =
|
|
'https://common.szjixun.cn/api/stock/history/list?from=' +
|
|
finalFromDate +
|
|
'&to=' +
|
|
toDate;
|
|
const res = await axios.get(url)
|
|
if (res.status === 200) {
|
|
if (res.data.status === 0) {
|
|
lastTradingDay = dayjs(res.data.data[0].date)
|
|
}
|
|
return lastTradingDay.format('MMM D, YYYY') + ' 4:00 PM [EDT]'
|
|
}
|
|
}
|
|
|
|
const formatted = ref(null)
|
|
const init = async () => {
|
|
formatted.value = await getLastTradingDay()
|
|
}
|
|
init()
|
|
const getStockQuate = async () => {
|
|
// const res = await axios.get('https://saas-test.szjixun.cn/api/fiee/chart/forward/test')
|
|
const res = await axios.get('https://common.szjixun.cn/api/stock/company/data')
|
|
// console.error(res)
|
|
if (res.status === 200) {
|
|
if (res.data.status === 0) {
|
|
stockQuote.value = res.data.data
|
|
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
formatted,
|
|
getStockQuate,
|
|
stockQuote
|
|
}
|
|
})
|