import Vue from 'vue'
import http from '@/plugins/http'

// mixin
Vue.mixin({
  async fetch({ store, req }) {
    let headers = req && req.headers
    let res = await http.get('/api/user/getInfo', {}, { config: { headers }, neverLogout: true })
    if(res) {
      store.commit('updateUserinfo', { userinfo: res.data })
    }
  },
  components: {
  },
  data() {
    return {
    }
  },
  mounted() {},
  computed: {
    userinfo() {
      return this.$store.state.userinfo
    },
    hasLogined() {
      return !!this.userinfo.uid
    },
  },
  methods: {
    goLogin(e, noAlert) {
      if(noAlert) {
        location.href = `/?loginbox=show`
      }else {
        this.$alert('未登录, 前往登录', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            location.href = `/?loginbox=show`
          }
        })
      }
    },
    noCompetence(title = "没有权限") {
      this.$alert(title, '提示', {
        confirmButtonText: '确定',
        callback: action => {
          location.go(-1)
        }
      })
    },
  }
})