const util = require('../utils/util.js');
const openidStatus = () => {
  if (wx.getStorageSync('openId')) {
    return [wx.getStorageSync('openId'), wx.getStorageSync('sessionKey')]
  } else {
    return new Promise((resolve, reject) => {
      wx.login({
        success: (res) => {
          if (res.code) {
            wx.request({
              url: util.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/login',
              method: "GET",
              data: {
                code: res.code
              },
              success: res => {
                console.log(res)
                if (res.statusCode == 200) {
                  wx.setStorageSync('openId', res.data.openid)
                  wx.setStorageSync('sessionKey', res.data.sessionKey)
                  return [res.data.openid, res.data.sessionKey];
                }
              }
            });
          } else {
            console.log('获取用户登录态失败！' + res.errMsg);
          }
        }
      })
    })
  }
};

function formatTime(date) {
  var year = date.getFullYear()
  return year
}
const formDetails = (type) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: util.globalData.publicUrl + '/wx/member/'+ wx.getStorageSync('openId') +'/options',
      method: "GET",
      data: {
        type
      },
      success: res => {
        console.log(res)
        if (res.data.code == 0) {
          let labelArr = res.data.options.map((obj, index) => {
            return obj.dictLabel;
          })
          let valueArr = res.data.options.map((obj, index) => {
            return obj.dictValue;
          })
          resolve([labelArr,valueArr])
        }else{
          reject(res)
        }
      }
    })
  })
};
module.exports = {
  openidStatus,
  formDetails,
  formatTime
}