import request from "./request";

// 全局变量
let loginData = {};

// 进行登录
function getUserInfo(data) {
  return new Promise((resolve, reject) => {
    const globalData = getApp().globalData;
    if (globalData.fuid) data.from = globalData.fuid;

    request
      .post("/weapp/login/v1", data)
      .then((res) => {
        if (res.errno == 10000 && res.data) {
          uni.setStorageSync("userinfo", JSON.stringify(res.data));
          resolve({ ...res.data, coupons: res.coupons || null });
        } else {
          reject({ errMsg: res.errmsg });
        }
      })
      .catch((e) => {
        reject(e);
      });
  });
}

// 手机号绑定
function setPhoneNumber(data) {
  return new Promise((resolve, reject) => {
    request
      .post("/weapp/login/setPhoneNumber", data)
      .then((res) => {
        if (res.errno == 10000 && res.data) {
          uni.setStorageSync("userinfo", JSON.stringify(res.data));
          resolve(res.data);
        } else {
          reject({ errMsg: res.errmsg });
        }
      })
      .catch((e) => {
        reject(e);
      });
  });
}
function wxLogout() {
  return new Promise((resolve) => {
    uni.removeStorage({
      key: "userinfo",
    });
    resolve();
  });
}

//
function tryLogin() {
  if (loginData.userInfo && loginData.code) {
    const xdata = loginData;
    loginData = {};
    getUserInfo(xdata)
      .then((data) => {
        xdata.callback && xdata.callback(data);
      })
      .catch((e) => {
        uni.showToast({
          icon: "none",
          title: "登录失败:" + e.errmsg,
        });
      });
  }
  if (loginData.phone_code) {
    const xdata = loginData;
    loginData = {};
    setPhoneNumber(xdata)
      .then((data) => {
        xdata.callback && xdata.callback(data);
      })
      .catch((e) => {
        xdata.fail && xdata.fail(e);
        uni.showToast({
          icon: "none",
          title: "登录失败:" + e.errmsg,
        });
      });
  }
}

// 微信授权获取用户手机号
function setPhoneData(params) {
  if (params.phone_code) {
    loginData = params;
    tryLogin("phone");
  } else {
    uni.showToast({
      icon: "none",
      title: "手机号授权失败",
    });
  }
}

// 微信授权获取用户信息
function wxLogin(callback) {
  loginData.userInfo = null;
  loginData.code = null;

  loginData.callback = callback;
  uni.getUserProfile({
    desc: "获取你的昵称、头像、地区及性别",
    success(data) {
      loginData.userInfo = {
        signature: data.signature,
        encryptedData: data.encryptedData,
        iv: data.iv,
        rawData: data.rawData,
      };
      tryLogin("userProfile");
    },
    fail() {
      uni.showToast({
        icon: "none",
        title: "授权失败",
      });
    },
  });

  uni.login({
    success(res) {
      if (res.code) {
        loginData.code = res.code;
        tryLogin("code");
      } else {
        uni.showToast({
          icon: "none",
          title: "授权失败",
        });
      }
    },
    fail() {
      uni.showToast({
        icon: "none",
        title: "授权失败",
      });
    },
  });
}

export default {
  logout: wxLogout,
  setPhoneData: setPhoneData,
  wxLogin: wxLogin,
};
