// index.js
// 获取应用实例
import http from '../../base/httputil'
const app = getApp()

Page({
  data: {
    current: 0, //当前所在页面的 index

    indicatorDots: true, //是否显示面板指示点

    autoplay: true, //是否自动切换

    interval: 3000, //自动切换时间间隔

    duration: 800, //滑动动画时长

    circular: true, //是否采用衔接滑动

    imgUrls: [

      '../images/banner.jpg',

      '../images/banner3.jpg',

      '../images/banner2.jpg'

    ],

    links: [

      // '/pages/second/register',

      // '/pages/second/register',

      // '/pages/second/register'

    ],

    //功能数据-孙一石
    //记录首页点击-登录后继续跳转
    dofun: null

  },

  onLoad() {

  },

  // 待办跳转
  bindViewDaiBan() {
    wx.navigateTo({
      url: '../daiban/daiban'
    })
  },
  // 企业跳转
  bindViewQiYe() {
    wx.navigateTo({
      url: '../qiye/qiye'
    })
  },
  // 巡查跳转
  bindViewXunCha() {
    wx.navigateTo({
      url: '../xuncha/xuncha'
    })
  },
  // 通知公告更多跳转
  bindViewGengDuo() {
    wx.navigateTo({
      url: '../more/more'
    })
  },
  // 新手指南跳转
  bindViewNewComer() {
    wx.navigateTo({
      url: '../newcomer/newcomer',
    })
  },
  //巡检跳转
  bindViewXunJian() {
    wx.navigateTo({
      url: '../xunjian/xunjian',
    })
  },
  // 使用攻略跳转
  bindViewIntroduction() {
    wx.navigateTo({
      url: '../Introduction/Introduction'
    })
  },
  bindViewXunjianimg() {
    wx.navigateTo({
      url: '../xunjianimg/xunjianimg'
    })
  },
  // 图片轮播
  //轮播图的切换事件

  swiperChange: function (e) {

    this.setData({

      swiperCurrent: e.detail.current

    })

  },

  //点击指示点切换

  chuangEvent: function (e) {

    this.setData({

      swiperCurrent: e.currentTarget.id

    })

  },
  //点击图片触发事件

  swipclick: function (e) {

    // console.log(this.data.swiperCurrent);

    wx.switchTab({

      // url: this.data.links[this.data.swiperCurrent]

    })

  },

  //首页功能js-孙一石
  init_userInfo(e) {
    this.data.dofun = e.currentTarget.dataset.fun
    let that = this
    if (app.globalToken == null) {
      http.showLoading()
      wx.getUserProfile({
        desc: 'desc',
        success: (res) => {
          this.getSysUserInfo(res.userInfo)
        },
        fail: res => {
          console.log(res)
        },

      })
    } else {
      this.callByName(this.data.dofun)
    }
  },


  callByName(name) {
    if (name == "bindViewDaiBan") {
      //待办
      this.bindViewDaiBan()
    } else if (name == "bindViewQiYe") {
      //企业
      this.bindViewQiYe()
    } else if (name == "bindViewXunCha") {
      this.bindViewXunCha()
    } else if (name == "bindViewXunjianimg") {
      this.bindViewXunjianimg()
    }
  },

  initIndexPage() {
    http.send_post("/system/AppIndexController/getIndexPageData", null, this.initIndexPageSuccess)
  },
  initIndexPageSuccess(res) {
    http.hideLoading()
  },

  loginSuccess(res) {
    if (res.data.code != 200) {
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
    } else {
      app.globalToken = res.data.data.access_token
      //登录成功，获取首页数据
      this.initIndexPage()
    }
    console.log("!!!!!", app.globalToken)

  },







  getSysUserInfo(info) {
    let that = this
    wx.login({
      success(res) {
        var code = res.code
        var data = {
          wxCode: code,
          wxNickName: info.nickName,
          wxAvatarUrl: info.avatarUrl
        }
        // http.send_post_login("/minapp/AppLoginController/appLogin",data)
        http.send_post_login("/applogin", data, that.loginSuccess)
      },
      fail(res) {
        console.log("ffff", res)
      }
    })
  }






})