devices.js 5.11 KB
const restapi = require('../restapi.js')
const filter = require('../../../../utils/filter.js');
const app = getApp()
Page(filter.identityFilter({

  /**
   * 页面的初始数据
   */
  data: {
    authToast: true,
    img:{
      active:'../../../resources/images/open.png',
      nothing:'../../../resources/images/off.png'
    },
    list:[],
    selected:'',
    realnameurl: '',
    divicedetail:'',
    openId:'',
    bindUrl:'',
    extra:{},
    loadingFlag: true,
    deviceNum:0
  },
  /**
   * 跳转小程序
   */
  jump(e) {
    wx.navigateToMiniProgram({
      appId: 'wx3e135818f9a3c815',
      path: 'packageSmartcar/pages/devices/devices',
      extra: this.data.extra
    })
  },
  /**
   * 跳转设备绑定页面
  */
  toBindPage(){
    wx.navigateTo({
      url: '/packageSmartPlus/pages/device/bindDevice/index'
    })
  },
  /**
   * 选中设备
   */
  choose(e){
    let index = e.currentTarget.dataset.index;
    let length = this.data.list.length;
    let device = e.currentTarget.dataset.device;
    let list = this.data.list;
    list.forEach(el =>{
      el.active = false
    })
    list[index].active = true
    this.setData({
      list: list
    })
    this.checkDevice(device);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.accountId) {
      app.userInfoReadyCallback = userInfo => {
        if (userInfo != null) {
          console.log(app.globalData.accountId)
        }
      }
    }
    let selected = app.globalData.selectedDevice;
    if (selected.userId != ''){
      this.setData({
        selected: selected.userId
      })
    }
  },

  /**
   * 切换设备
   */
  checkDevice(device){
    let userInfo = app.globalData.userInfo;
    console.log(userInfo);
    let accountId = app.globalData.accountId;
    console.log(device);
    let unionId = app.globalData.unionId;
    let that = this;
    let presentDevice = {
      accountId: accountId,
      protocolId: device.userId,
      deviceType: device.typeId,
      unionId: unionId,
      createdBy: accountId,
      updatedBy: accountId
    }
    console.log("presentDevice", presentDevice);
    wx.showLoading({
      title: '切换中...',
    })

    let appUserId = app.globalData.selectedDevice.userId;
    let chosenUserId = device.userId;
    if (appUserId && appUserId == chosenUserId){
      console.log("没切换设备");
      wx.hideLoading();
      wx.switchTab({
        url: '/pages/tab/smartplus/smartplus',
      })
    }else{
      console.log("设备切换了");
      restapi.checkDevice(presentDevice, function (data) {
        console.log("checkDevice", data);
        if (data.returnCode == '0') {
          app.globalData.selectedDevice = {
            userId: device.userId,
            typeNmae: device.name,
            typeId: device.typeId
          }
          console.log(app.globalData.selectedDevice);
          let promise = new Promise((resolve, reject) => {
            console.log("渲染完了回首页");
            setTimeout(() => {
              wx.hideLoading();
              wx.switchTab({
                url: '/pages/tab/smartplus/smartplus',
              })
            }, 4000)
            resolve();
          })
        } else {
          app.showMessage('切换失败,请稍后重试');
          setTimeout(() => {
            wx.hideLoading();
          }, 5000)
        }
      }, function () {
        app.showMessage('切换失败,请稍后重试');
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData()
  },

  /**
   * 请求列表数据
   */
  requestData:function(){
    restapi.getDeviceList(data => {
      console.log("getDeviceList",data);
      if (data.returnCode == '0') {
        this.setData({
          realnameurl: data.data.realnameurl,
          divicedetail: data.data.todivicedetail,
          openId: data.data.openId,
          bindUrl: data.data.tobind
        })
        if(data.data.list.length == 0){
          this.setData({
            loadingStatus: 3
          })
        }else{
          this.setData({
            loadingFlag: false,
          })
          this.sealData(data.data.list);
        } 
      } else {
        this.setData({
          loadingStatus: 2
        })
      }
    })
  },

  /**
   * 处理页面展示数据
   */
  sealData(list){
    let deviceList = [];
    let selected = this.data.selected;
    for(let i = 0; i < list.length; i++){
      let device = {
        userId: list[i].userId,
        imgsrc: '../../../resources/images/device_' + list[i].typeId + '.png',
        state: list[i].pass,
        imei: list[i].imei,
        iccid: list[i].iccid,
        name: list[i].type,
        typeId: list[i].typeId
      }
      if (selected == list[i].userId){
        device.active = true;
      }
      deviceList.push(device);
    }
    this.setData({
      list: deviceList
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
     * 监听登录成功--页面onShow时触发
     */
  onLogin: function () {

  }
}))