var Parse = getApp().Parse;

function loginNow(authPage = 'plugin://fm-plugin/fm-auth') {
  console.log(Parse.User.current()?.get('mobile'));
  if (Parse.User.current()?.get('mobile')) {
    wx.setStorageSync("userLogin", Parse.User.current().id);
  }
  let userInfo = wx.getStorageSync('userLogin');
  if (userInfo == '') {
    wx.navigateTo({
      url: authPage,
    });
    return false;
  } else {
    return true;
  }
}
/* 通过小程序wx.login获取openid和session_key */
function wxLogin() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: function (res) {
        if (res.code) {
          let url = 'https://server.fmode.cn/api/wxapp/auth_wxapp'
          wx.request({
            url: url,
            data: {
              c: getApp().globalData.company,
              code: res.code,
              appType: getApp().globalData.appType ? getApp().globalData.appType : ''
            },
            async success(res) {
              wx.setStorageSync("userInfo", res.data);
              resolve(res)
            },
          });
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '服务器繁忙',
          icon: 'error'
        })
        resolve()
      }
    });
  })
}
// async function loginParse() {
//     let userInfo = wx.getStorageSync("userInfo");
//     let appId = getApp().globalData.appid
//     let username, password;
//     username = userInfo.openid; // openid作为用户名
//     password = username.substr(username.length - 6, 6); // 后六位作为登录密码
//     let query = new Parse.Query("_User")
//     query.equalTo(`wxapp.${appId}.openid`, username);
//     let current = await query.first()
//     if (current) {
//         await Parse.User.logIn(username, password);
//     }
// }
module.exports = {
  loginNow,
  // loginParse,
  wxLogin
}