require("./utils/string.js")
const ald = require('./libs/ald-stat.js')
import {
  updataStorageData
} from './utils/storage.js'
var startTime = Date.now(); //启动时间
App({
  onLaunch: function(options) {
    this.aldstat.sendEvent('小程序的启动时长', {
      time: Date.now() - startTime
    })
    // 小程序版本更新
    if (wx.getUpdateManager) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate((res) => {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)
      })

      updateManager.onUpdateReady(() => {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: (res) => {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              wx.removeStorageSync("user_id");
              wx.removeStorageSync("member_id");
              wx.removeStorageSync("user_token");
              wx.removeStorageSync("userInfo");
              this.globalData.userId = '';
              this.globalData.userToken = '';
              this.globalData.memberId = '';
              this.globalData.userInfo = null;
              updateManager.applyUpdate()
            }
          }
        })
      })

      updateManager.onUpdateFailed(function() {
        // 新版本下载失败
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，部分功能可能无法使用，请升级到最新微信版本后重试。'
      })
    }
  },
  onShow(options) {
    console.log("rr", options)
    //群聊信息
    if (options.shareTicket) {
      this.getShareInfo(options.shareTicket)
    }
    wx.onAppRoute((data) => {
      const route = getCurrentPages();
      const page = route[route.length - 1];
      console.log(data)
      console.log(page)
      if (!page.data.hasShare) {
        let _str = '';
        for (let key in data.query) {
          _str += `${key}=${data.query[key]}&&`
        }
        const shareTitle = this.shareTitle[page.route]
        page.onShareAppMessage = function(e) {
          return {
            path: `/${page.route}?${_str}`,
            // imageUrl: page.data.shareImageUrl || '/images/happyjob/detail_share.jpg',
            title: shareTitle || '开心工作'
          }
        }.bind(page)
      }
    })
  },
  shareTitle: {

  },
  globalData: {
    openId: '',
    userInfo: null,
    userId: '',
    userToken: '',
    memberId: "",
    sessionKey: '',
    city: updataStorageData('city') || '无锡'
  },
  //页面分享
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: true,
      success: (res) => { // 成功后要做的事情
        console.log(res)
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },
  //获取群聊分享信息
  getShareInfo(shareTicket) {
    wx.getShareInfo({
      shareTicket: shareTicket,
      success: (res) => {
        //需后台解析数据 encryptedData iv
        console.log(res)
      },
      fail: function(res) {
        console.log(res)
      },
      complete: function(res) {}
    })
  },
  // 跳转登录页面
  goLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  watermark(height) {
    const system = wx.getSystemInfoSync();
    const _height = system.windowHeight > height ? system.windowHeight : height;
    const row = Math.floor(_height * (750 / system.windowWidth) / 200);
    const json = [];
    for (let i = 0; i < row; i++) {
      json.push([])
      let _tmp = i % 2;
      if (_tmp) {
        for (let j = 0; j < 2; j++) {
          json[i].push({
            top: 30 + 200 * i,
            left: 160 + 235 * j
          })
        }
      } else {
        for (let j = 0; j < 3; j++) {
          json[i].push({
            top: 30 + 200 * i,
            left: 30 + 250 * j
          })
        }
      }
    }
    return json;
  }
})