util.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { GetNotice } from '@/api/public'
  2. export function timeFix() {
  3. const time = new Date()
  4. const hour = time.getHours()
  5. return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
  6. }
  7. /**
  8. * 学期判断
  9. * 返回 [学期开始时间戳, 明日时间戳]
  10. */
  11. export function getSemesterTimestamps() {
  12. const now = new Date()
  13. const year = now.getFullYear()
  14. const feb1ThisYear = new Date(year, 1, 1, 0, 0, 0, 0) // 当年 2-01
  15. const aug31ThisYear = new Date(year, 7, 31, 0, 0, 0, 0) // 当年 8-31
  16. // 下学期:2 月 1 日 ~ 8 月 31 日
  17. // 上学期:8 月 31 日 ~ 次年 2 月 1 日
  18. // 1 月属于上一年的上学期
  19. return [(now >= feb1ThisYear && now < aug31ThisYear) ? feb1ThisYear.getTime() : new Date(now < feb1ThisYear ? year - 1 : year, 7, 31, 0, 0, 0, 0).getTime(), now.getTime() + 86400000]
  20. }
  21. export async function getNotice(key) {
  22. try {
  23. const res = await GetNotice({ key })
  24. if (!res || res.code !== 0) {
  25. console.log(`获取公告失败:${res?.msg ?? ''}`)
  26. return ''
  27. }
  28. return res.msg ?? ''
  29. } catch (error) {
  30. console.log('获取公告失败:', error)
  31. return ''
  32. }
  33. }