File size: 702 Bytes
186b709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { onMounted, ref } from 'vue'
import { TIP_THRESHOLD, strNum2Num } from './common.js'

const countCacheKey = '__chat_tip_count__'

export function useChatTip() {
  const tipCount = ref(0)

  onMounted(() => {
    forceSyncCount()
  })

  /** 从localStorage中同步count */
  function forceSyncCount() {
    const cacheCount = strNum2Num(localStorage.getItem(countCacheKey) || '0')

    if (cacheCount !== tipCount.value)
      tipCount.value = cacheCount
  }

  function increase() {
    tipCount.value++

    if (tipCount.value > TIP_THRESHOLD)
      tipCount.value = 0

    localStorage.setItem(countCacheKey, String(tipCount.value))
  }

  return {
    increase,
    count: tipCount,
  }
}