import { login } from '@/api/login'
import { getToken } from '@/utils/auth'
const user = {
    namespaced: true,
    state: {
        token: getToken(),
        name: '',
        avatar: '',
        role: []
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        }
    },
    actions: {
        //登陆
        login({ commit }, userInfo) {
            const { username, password } = userInfo;
            return new Promise((resolve, reject) => {
                login({ username: username.trim(), password: password }).then(res => {
                    const { data } = res;
                    commit('SET_TOKEN', data.data);
                    localStorage.setItem('Authorization',data.data);
                    resolve(data);
                }).catch(error => {
                    console.log('登陆失败');
                    reject(error);
                })
            })
        },
    },
}
export default user
