//app.js
const {
  request,serverName
} = require('./utils/services');

App({

  onLaunch: function () {
    console.log('wx',wx)
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
      }
    })

    let loginSessionKey = wx.getStorageSync("token") || "";

    if (loginSessionKey) {
      wx.request({
        url: serverName + '/wx/api/user/getBrowsedExhibitions',
        data: {
          loginSessionKey
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "post",
        success: (res) => {
          if (res.data.code == 0) {
            let cookieIds = res.data.data.ids||undefined
            if (cookieIds){
              this.globalData.cookieIDs = cookieIds.split(',');

            }

          } else {
            return

          }
        }
      })
    }
    else
    { }
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })

    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate((res) => {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(() => {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })

    })

    updateManager.onUpdateFailed(() => {
      // 新的版本下载失败
    })
  },

  //应用关闭的函数
  onHide: function () {
    let {
      cookieIDs
    } = this.globalData;
    let ids = undefined;
    console.log(cookieIDs)
    if(cookieIDs){
      if (cookieIDs.length > 9) {
        cookieIDs.length = 10
      }
     ids = cookieIDs.join(",") || undefined;
    }
    let loginSessionKey = wx.getStorageSync("token") || "";
    //关闭应用的时候发送你浏览过的场景
    if (loginSessionKey) {
      wx.request({
        url: serverName + '/wx/api/user/saveBrowsedExhibitions',
        data: {
          ids:ids,
          loginSessionKey
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "post",
        success: (res) => {
        }
      })
    } else {
      return
    }
  },

  globalData: {
    userInfo: null,
    city: "",
    collectedArr: [],
    collectedChange: false,
    clickToSelect: false,
    isLogin: false,
    cookieIDs:[],
    currentUrl:'',
    currentShareImg:''
  }
})

let app = getApp()
export default app

