/*
 * @Author: jixiang
 * @Date: 2021-04-07 11:06:09
 * @Description:
 * @FilePath: /suqian_pc_new/src/store/modules/user/actions.ts
 * 56580223@qq.com
 */
import UserService from '@/api/user';
import { message as meg } from 'ant-design-vue';
import cookie from 'js-cookie';
import { setToken, getToken } from '@/utils/cookie';
import { SessionStorageService, LocalStorageService } from '@/utils/storage';
import router from '@/router';

export default {
  //
  loginInfo({ commit, dispatch }: any, payload: any) {
    UserService.login(payload).then(async (res) => {
      const { data, code, message } = res.data;
      if (code === 200) {
        //登录信息存起来
        await commit('__set', { key: 'authorization', val: data.token });
        // await commit('__set', { key: 'userInfo', val: data.loginInfo });
        // await cookie.set('authorization', data.token);
        await LocalStorageService.set('userId', data?.userId);
        await setToken(data.token);

        dispatch('getUserInfo', { id: data?.userId });
        // router.push({ path: '/Package', replace: true });
        // meg.success(message);
      } else {
        meg.error(message);
      }
    });
  },

  // 获取登录信息
  getToken({ commit, dispatch }: any, payload: any) {
    commit('__set', { key: 'authorization', val: payload.token });
    LocalStorageService.set('userId', payload.userId);
    setToken(payload.token);
    dispatch('getUserInfo', { id: payload.userId });
  },

  getUserInfo({ commit }: any, userinfo: any) {
    commit('__set', { key: 'userInfo', val: userinfo });
    LocalStorageService.set('userInfo', userinfo);
  },
  LogOut({ commit }: any, refresh: any) {
    return new Promise<void>((resolve, reject) => {
      commit('DEL_STORAGE');
      resolve();
    });
  },
};
