index.js 596 Bytes
Newer Older
牛文敏's avatar
init    
牛文敏 committed
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
import { defineStore } from 'pinia'
import fetch from '@/api'

export const useStore = defineStore('main', {
  actions: {
    logout() {
      window.location.href = '/'
    },
    get(path, init) {
      if (!init) {
        init = {}
      }
      return fetch(path, init).catch((err) => {
        if (err) {
          this.logout()
        }
      })
    },
    post(path, init) {
      if (!init) {
        init = {}
      }
      init.method = 'POST'
      return fetch(path, init).catch((err) => {
        if (err) {
          this.logout()
        }
      })
    }
  },
  persist: true
})