/**
 * @Description: 用户模块
 * @Author: 空白格
 * @Date: 2022-08-10 13:40:53
 * @LastEditors: 空白格
 * @LastEditTime: 2022-08-10 15:19:32
 * @FilePath: \veterans_client_web\src\store\modules\user.js
 * @Copyright: Copyright (c) 2016~2022 by 空白格, All Rights Reserved.
 */
import { getToken } from "@/utils/auth"
import { loginAuthCode, getAuthCode } from "@/api/Login";
const user = {
  state: {
    token: getToken(),
    isLogin: false,
    userInfo: {
      userName: 'xxx'
    }
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_LOGIN_FLAG: (state, flag) => {
      state.isLogin = flag;
    }
  },
  actions: {
    // 登录验证验证码
    Login({ commit, state }, userInfo) {
      return new Promise((resolve, reject) => {
        loginAuthCode({
          accessToken: state.user.token,
					userId: userInfo.userId,
					code: userInfo.code
        })
          .then(res => {
            commit('SET_LOGIN_FLAG', true)
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    GetLoginCode({ commit }) {
      return new Promise((resolve, reject) => {
        getAuthCode()
          .then(res => {
            commit('SET_TOKEN', res.accessToken)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}
export default user
