deviceList_frm.html 8.43 KB
<!DOCTYPE html>
  <html>
  <head>
      <meta charset="utf-8">
      <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
      <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
      <title>设备列表frm</title>
      <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
      <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
      <link rel="stylesheet" type="text/css" href="../../css/utils/deviceList.css"/>
      <style>
        html,
        body{
          background-color: #F4F6F9;
        }
      </style>
  </head>
  <body>
    <div class="device-panel" id="device_panel">
    </div>
  </body>

  <script type="text/javascript" src="../../script/api.js"></script>
  <script type="text/javascript" src="../../script/common.js"></script>
  <script type="text/javascript" src="../../script/jquery-3.3.1.min.js"></script>
  <script type="text/javascript">
      var token;
      var userId;
      var defaultImei;
      var idList = {};
      apiready = function(){
        api.parseTapmode();
        token = $api.getStorage('userToken');
        userId = $api.getStorage('userID');
        /** 下拉刷新 */
        api.setRefreshHeaderInfo({
            loadingImg:'../../image/alpha0.png',
            bgColor: '#5B7FF8',
            textColor: 'rgba(255,255,255,0.5)',
            textDown: '下拉刷新设备列表...',
            textUp: '松开刷新...'
        }, function(ret, err) {
          getDeviceList();
        });

        getDeviceList();

        api.addEventListener({
            name: 'change_device_device_list'
        }, function(ret, err){
            if( ret ){
              getDeviceList();
            }
        });

      };

      function getDeviceList() {
        console.log(JSON.stringify(api.pageParam.type));
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '加载中...',
            text: '',
            modal: true
        });
       //---移除dom中的所有元素
        var allDom = $api.domAll('.list-panel');
        for (var i = 0; i < allDom.length; i++) {
          $api.remove(allDom[i]);
        }
        idList = {}; //将id池子置空
        //------------------
        $.ajax({
            url: HOST + '/iot_api/v1/app/bind_list?user_id='+userId,
            type: 'GET',
            dataType: 'json',
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'token': token
            },
            complete: function() {
              api.hideProgress();
            },
            success: function(ret) {
              if (ret.code == 0) {
                var deviceList = ret.data.list;
                if(!deviceList || deviceList.length==0){
                  $api.setStorage('devices', []);
                  api.alert({
                    title: '添加设备',
                    msg: '暂无设备,请先添加设备',
                  }, function(ret, err){
                    if( ret ){
                     openCanner(true);
                    }
                  });
                  return;
                }else {
                  $api.setStorage('devices', JSON.stringify(deviceList));
                }
                //确认默认设备
                for(var i in deviceList){
                  deviceList[i].storename == null ? deviceList[i].storename = '未知设备' :  deviceList[i].storename = deviceList[i].storename;
                  idList[deviceList[i].imei] = deviceList[i].id;
                  if (deviceList[i].default == true) {
                    $api.setStorage('defaultDevice', deviceList[i].imei);
                    defaultImei = deviceList[i].imei;
                    var addDomHtml = addDeviceListHtml(deviceList[i].storename,deviceList[i].imei,deviceList[i].province + ' ' + deviceList[i].city,true,deviceList[i].online);
                  }else {
                    var addDomHtml = addDeviceListHtml(deviceList[i].storename,deviceList[i].imei,deviceList[i].province + ' ' + deviceList[i].city,false,deviceList[i].online);
                  }
                  if (api.pageParam.type == 'online' && !deviceList[i].online) {
                    var addDomHtml = '';
                  }else if (api.pageParam.type == 'offline' && deviceList[i].online) {
                    var addDomHtml = '';
                  }
                  $api.append($api.byId("device_panel"), addDomHtml);
                }

                if (deviceList.length == 1 && deviceList[0].default == false) {
                  handleChange(deviceList[0].imei);
                }

                api.sendEvent({
                  name: 'change_device',
                  extra: {
                    imei: defaultImei,
                  }
                });
              }else {
                api.toast({msg: '网络错误',duration: 2000,location: 'bottom'});
              }
              api.refreshHeaderLoadDone();
            },
            error: function() {
              api.toast({msg: ERR_MSG,duration: 2000,location: 'bottom'});
            }
        });
      }

      function changeDevice(imei) {
        var dialogBox = api.require('dialogBox');
        dialogBox.alert({
            texts: {
                title: '切换设备',
                content: '切换至设备编号:' + imei,
                leftBtnTitle: '取消',
                rightBtnTitle: '确认'
            },
            styles: {
                bg: '#fff',
                w: 300,
                corner:2,
                title: {
                    marginT: 28,
                    icon: '',
                    iconSize: 30,
                    titleSize: 18,
                    titleColor: '#000'
                },
                content: {
                    color: '#000',
                    size: 14
                },
                left: {
                    marginB: 0,
                    marginL: 0,
                    w: 150,
                    h: 47,
                    corner: 10,
                    bg: '#FFFFFF',
                    color: '#818295',
                    size: 16
                },
                right: {
                    marginB: 0,
                    marginL: 0,
                    w: 150,
                    h: 47,
                    corner: 10,
                    bg: '#FFFFFF',
                    color: '#5B7FF8',
                    size: 16
                },
                horizontalLine:{
                  color:'rgba(240,245,255,1)',
                  height:1
                },
                verticalLine:{
                    color:'rgba(240,245,255,1)',
                    width:1                 //(可选项)数字类型;左右边按钮竖线的高度;默认:0
                }
            }
        }, function(ret) {
            if (ret.eventType == 'left') {
              dialogBox.close({
                  dialogName: 'alert'
              });
            }else if (ret.eventType == 'right') {
              dialogBox.close({dialogName: 'alert'});
              if (!idList[imei]) {
                console.log('nothing!');
              }
              handleChange(imei);
            }

        });
      }

      function handleChange(imei) {
        //切换设备
        api.showProgress({
           style: 'default',
           animationType: 'fade',
           title: '正在切换中...',
           text: '',
           modal: true
        });

        $.ajax({
            url: HOST + '/iot_api/v1/app/set_device_default',
            type: 'POST',
            dataType: 'json',
            headers:{
                'Content-Type':'application/json;charset=utf-8',
                'token': token
            },
            data: JSON.stringify({
                id: idList[imei]
            }),
            complete: function() {
              api.hideProgress();
            },
            success: function(ret) {
              if (ret.code == 0) {
                api.toast({msg: '设备切换成功!',duration: 2000,location: 'bottom'});
              }else {
                api.toast({msg: '设备切换失败',duration: 2000,location: 'bottom'});
              }
              getDeviceList();
              api.sendEvent({
                  name: 'change_device',
              });
            },
            error: function() {
              api.toast({msg: ERR_MSG,duration: 2000,location: 'bottom'});
            }
        });
      }
  </script>
  </html>