import cloudApi from "../apis/cloudApi"

function wxSetSessionKey() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: async res => {
        let res1 = await cloudApi('code2Session', {
          JSCODE: res.code
        })
        wx.setStorageSync('session_key', res1.result.session_key)
        wx.setStorageSync('openId', res1.result._openid)
        resolve({
          session_key: res1.result.session_key
        })
      }
    })
  })

}

function getUserProfile() {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: "'用于完善用户信息",
      success: e => {
        let {
          userInfo
        } = e
        resolve({
          status: 0,
          userInfo
        })
      },
      fail: e => {
        resolve({
          errMsg: e.errMsg,
          status: 1
        })
      }
    })
  })
}


module.exports = {
  wxSetSessionKey,
  getUserProfile,
}