const getReq = require('../config.js').getReq
const api    = require('../config.js').host

export default class WxAuthor {
  fGetSetting = true

  constructor(app, getsetting) {
    this.target = app
    this.init(getsetting)
  }
  // 首先wx.login
  init(getsetting) {
    this.target.globalData.fMinistart = false

    this.fGetSetting = getsetting
    wx.login({
      success: res => {
        this.ministart(res.code)
      }
    })
  }
  // 拿到code 请求act=login op=ministart
  ministart(code) {
    let params = {
      act: "login",
      op: "ministart",
      code
    }
    getReq(this.target,params, res => {
      this.target.globalData.fMinistart = true
      if (res.code == 200) {
        let { openid, unionid } = res.datas
        if (openid && unionid) {
          this.setId(openid, unionid)
          if(this.fGetSetting) {
            this.getSetting(false)
          }
        } else {
          if(this.fGetSetting) {
            this.getSetting(true)
          }          
        }
      }
    })
  }

  getSetting(flag) {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log('wx getSetting:',res)
          this.getUserInfo(flag)
        }
      }
    })
  }

  getUserInfo(flag) {
    let withCredentials = flag
    wx.getUserInfo({
      withCredentials,
      success: res => {
        this.setUserInfo(res.userInfo, res.userInfo.nickName)

        let userInfo = this.target.globalData.userInfo
        if (flag) {
          let { encryptedData, signature, iv } = res
          userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
        }
        else {
          userInfo = Object.assign({}, this.target.globalData.userId, userInfo)
        }
        this.wxauthen(userInfo)
      }
    })
  }
  wxauthen(userInfo) {
    let relay_id = this.target.globalData.relay_id
    let params = {
      user_info: userInfo,
      act: "login",
      op: "wxauthen",
      relay_id: relay_id
    }
    getReq(this.target,params, res => {
      if (res.code == 200) {
        this.target.globalData.member_id = res.datas.member_id
        this.target.globalData.hasmobile = res.datas.hasmobile
                
        let url = this.getUrl()
        wx.reLaunch({
          url
        })
      }
      else {
        wx.showToast({
          icon: 'none',
          title: '登陆失败',
          duration: 2000
        })
      }
    })
  }

  getUrl() {
    let len = getCurrentPages().length
    if(len < 1) return ''
     
    let currentPage = getCurrentPages()[len - 1]
    let params = ''
    if (currentPage.options) {
      for (let k in currentPage.options) {
        params = params + k + '=' + currentPage.options[k] + '&'
      }
      params = params.slice(0, params.length - 1)
    }
    let current_url = "/" + currentPage['route']
    if (params) {
      current_url = current_url + '?' + params
    }
    return current_url
  }

  // 设置用户信息
  setUserInfo(userInfo, nickName) {
    this.target.globalData.userInfo = userInfo
    this.target.globalData.userInfo.nickname = nickName
  }

  //设置openid, unionid
  setId(openid, unionid) {
    this.target.globalData.userId.openid = openid
    this.target.globalData.userId.unionid = unionid
    var timeStamp = new Date().getTime();
    timeStamp = Math.floor(timeStamp);
    this.target.globalData.userId.timeStamp = timeStamp
  }
}