import {
  request
} from '../_utils/request';
import {
  setStorage,
  getStorage
} from '~/utils/storage'

/** 登录 */
export function login(data) {
  return new Promise((resolve, reject) => {
    request({
      url: `WeChatLogin/Login`,
      method: 'POST',
      data: data,
      success: async function (res) {
        setStorage('userInfo', res.data.user, 86400);
        setStorage('Authorization', res.data.jwt, 86400);
        const pages = getCurrentPages(); //获取小程序页面栈
        const beforePage = pages[pages.length - 1]; //获取上个页面的实例对象  -3是上上一个页面
        wx.navigateBack({
          delta: 1,
        });
        beforePage.onLoad();
        wx.showToast({
          icon: 'success',
          title: '登录成功',
        });
        resolve(res);
      },
    });

  })

}