const getReq = require('../config.js').getReq
const api = require('../config.js').host
const wxauthen = require('../config.js').wxauthen
var ald = require('./ald-stat.js')


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 => {
        console.log('wx.login code:', 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
      console.log('ministart', res)
      if (res.code == 200) {
        let { openid, unionid, share_title, share_image } = res.datas
        console.log('getReq 0', openid, unionid);

        if (share_title && share_image) {
          this.setShare(share_title, share_image)
        }

        if (openid && unionid) {
          this.setId(openid, unionid)
          wx.aldstat.sendOpenid(openid)
          wx.aldPushSendOpenid(openid)
          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)
        }
        wxauthen(this.target, userInfo,
          (res) => {
            let url = this.target.getUrl()
            if (url == '') {
              url = "pages/home/home";
            }

            console.log('url:', url)
            if (url != '/pages/index/index' && url != "pages/home/home") {
              wx.reLaunch({
                url
              })
            }
          },
          (err) => {
            wx.showToast({
              icon: 'none',
              title: '授权登陆失败',
              duration: 2000
            })
          }
        )
      }
    })
  }

  // 设置用户信息
  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
  }

  //设置title, image
  setShare(title, image) {
    this.target.globalData.share_title = title;
    this.target.globalData.share_image = image;
  }
}