const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    // 轮播
    backgrounds: [{
        "pic": "/images/banner1.png",
        // link:'../aboutme/index',
      },
      {
        "pic": "/images/banner2.png",
        // link:'../aboutme/index',
      },
      {
        "pic": "/images/banner3.png",
        // link:'../aboutme/index',
      },
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 500,
    number: {},
    msgList: [
      // {
      //   url: "url",
      //   title: "欢迎来到吉顺致养小程序"
      // },
      // {
      //   url: "url",
      //   title: "欢迎来到吉顺致养小程序"
      // },
    ]
  },
  goCollect() {
    wx.switchTab({
      url: '/pages/collectInfo/collectInfo',
    })
  },
  goXunfang() {
    wx.switchTab({
      url: '/pages/visitandinfo/visitandinfo',
    })
  },
  goTongzhi() {
    if (wx.getStorageSync('user') == '') {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    } else {
      wx.navigateTo({
        url: '/pages/notice/notice',
      })
    }
  },
  goPaiming() {
    if (wx.getStorageSync('user') == '') {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    } else {
      wx.navigateTo({
        url: '/pages/paiming/paiming',
      })
    }
  },
  goSaoMa() {
    console.log("扫阿");
    // 允许从相机和相册扫码
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res, "打印锕");
        let length = res.result.substring(res.result.lastIndexOf('/') + 1, res.result.length)
        wx.navigateTo({
          url: '/pages/love/love?id=' + length,
        })
      },
      fail: (res) => {
        console.log(res, "假的");
        wx.showToast({
          title: '扫码失败',
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  depts() {
    console.log("打印走了么的接口呢");
    console.log(wx.getStorageSync('openId'));
    wx.request({
      url: app.globalData.publicUrl + '/applet/homeStatistics',
      method: "GET",
      data: {
        appletsId: wx.getStorageSync('openId')
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            number: res.data.data
          })
        }
      }
    })
  },
  welcomeMessage() {
    console.log("welcomeMessage");
    console.log(wx.getStorageSync('openId'));
    this.setData({
      msgList: []
    })
    if (wx.getStorageSync('user') == '') {
      wx.request({
        url: app.globalData.publicUrl + '/welcomeMessage/list',
        method: "GET",
        success: (res) => {
          if (res.data.code == 0) {
            console.log(res);
            let result = res.data.data[0].title
            this.data.msgList.push({
              // url: '',
              title: result
            });
            this.data.msgList.push({
              // url: '',
              title: result
            });
            this.setData({
              msgList: this.data.msgList
            })
          }
        }
      })
    } else {
      wx.request({
        url: app.globalData.publicUrl + '/appletNotice/listForAppletsFirst',
        method: "GET",
        header: {
          appletsId: wx.getStorageSync('openId')
        },
        success: (res) => {
          if (res.data.code == 0) {
            console.log(res);
            if (res.data.data.length == 0) {
              this.data.msgList.push({
                // url: '',
                title: '暂无数据!!!'
              });
              this.data.msgList.push({
                // url: '',
                title: '暂无数据!!!'
              });
              this.setData({
                msgList: this.data.msgList
              })
            } else {
              let result = res.data.data.title;
              this.data.msgList.push({
                // url: '',
                title: result
              });
              this.data.msgList.push({
                // url: '',
                title: result
              });
              this.setData({
                msgList: this.data.msgList
              })
            }

          }
        }
      })
    }

  },
  // onLoad() {
  //   console.log("onloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonload");
  //   this.depts();
  // },

  
  onShow() {
    console.log("onshowssshshshhshshshss");
    this.depts();
    this.welcomeMessage();
    
  },
  onLoad: function () {
    console.log("onload11111111111111111111111");
    var that = this;
    const apps = getApp()
    apps.getAuthKey().then(function (res) {
      that.depts();
    })
    console.log("onload12222222222222222222222222222221");

  }

  
})