import { getUser, logout } from '@/api/my'
export const state = () => ({
  user: {},
  isMobile: false,
  searchVisible: false,
  menuVisible: false
})

export const mutations = {
  setUser(state, user) {
    state.user = user
  },
  removeUser(state) {
    state.user = {}
  },
  setIsMobile(state, isMobile) {
    state.isMobile = isMobile
  },
  toggleSearch(state, visible) {
    state.searchVisible = visible
  },
  toggleMenu(state, visible) {
    state.menuVisible = visible
  }
}

export const actions = {
  getUser({ commit }) {
    getUser().then(response => {
      commit('setUser', response.data)
    })
  },
  // 退出登录
  logout({ commit }) {
    logout().then(response => {
      commit('setUser', {})
      this.$router.replace('/')
    })
  },
  // 检测登录状态
  async checkLogin({ commit }) {
    const isLogin = await getUser()
      .then(response => {
        commit('setUser', response.data)
        return true
      })
      .catch(() => {
        commit('setUser', {})
        return false
      })
    return isLogin
  }
}
