const util = require('../utils/util.js');
const openidStatus = () => {
  return new Promise((resolve, reject) => {
    // if (wx.getStorageSync('openId')) {
    //   resolve([wx.getStorageSync('openId'), wx.getStorageSync('sessionKey')]);
    // } else {
      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.data.openid,'我是wx.login的openid')
                console.log(res.data.sessionKey,'我是wx.login的sessionKey')
                if (res.statusCode == 200) {
                  wx.setStorageSync('openId', res.data.openid)
                  wx.setStorageSync('sessionKey', res.data.sessionKey)
                  resolve([res.data.openid, res.data.sessionKey]);
                }
              }
            });
          } else {
            reject(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) {
          if (res.data.options) {
            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)
        }
      }
    })
  })
};
// 判断是否登录
const isLogin = () => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success: res => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              resolve(res)
            }
          })
        } else {
         reject(false)
        }
      }
    })
    // wx.request({
    //   url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
    //   method: "get",
    //   success: (res) => {
    //     console.log(res)
    //     if (res.data.code == 0 && res.data.info.avatar) {
    //       resolve(res.data.info.avatar);
    //     } else {
    //       reject(res)
    //     }
    //   }
    // })
  })
};
// 判断是否完善个人信息
const isFinishInfo = () => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/info',
      method: "get",
      success: (res) => {
        console.log(res)
        if (res.data.code == 0&&res.data.info.name&&res.data.info.tel) {
          resolve(res.data.info)
        } else {
          reject(res.data.code)
        }
      }
    })
  })
};
const isFinishTargetInfo = () => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: util.globalData.publicUrl + '/wx/member/' + wx.getStorageSync('openId') + '/standard',
      method: "get",
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          resolve(res.data.standard);
        } else {
          reject(res.data.code)
        }
      }
    })
  })
};
module.exports = {
  openidStatus,
  formDetails,
  formatTime,
  isLogin,
  isFinishInfo,
  isFinishTargetInfo
}