deviceList_frm.html 6.69 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">
      var token;
      var userId;
      var defaultImei;
      var idList = {};
      apiready = function(){
        api.parseTapmode();
        token = $api.getStorage('userToken');
        userId = $api.getStorage('userID');
        /** 下拉刷新 */
        api.setRefreshHeaderInfo({
            loadingImg:'',
            bgColor: '#5B7FF8',
            textColor: '#000',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
          getDeviceList();
          api.refreshHeaderLoadDone();
        });

        getDeviceList();
      };

      function getDeviceList() {
        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池子置空
        //------------------
        api.ajax({
          url: HOST + '/iot_api/v1/app/bind_list?user_id='+userId,
          method: 'GET',
          headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'token': token
          },
        }, function(ret, err) {
          api.hideProgress();
          if (ret) {
            if (ret.code == 0) {
              var deviceList = ret.data.list;
              if(!deviceList || deviceList.length==0){
                $api.setStorage('devices', JSON.stringify([]));
                api.toast({
                    msg: '请扫描添加设备',duration: 2000,location: 'bottom'
                });
                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,true);
                }else {
                  var addDomHtml = addDeviceListHtml(deviceList[i].storename,deviceList[i].imei,deviceList[i].province,false);
                }
                $api.append($api.byId("device_panel"), addDomHtml);
              }

              api.sendEvent({
                name: 'change_device',
                extra: {
                  imei: defaultImei,
                }
              });
            }else {
              api.toast({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:6,
                title: {
                    marginT: 20,
                    icon: '',
                    iconSize: 40,
                    titleSize: 14,
                    titleColor: '#000'
                },
                content: {
                    color: '#000',
                    size: 14
                },
                left: {
                    marginB: 7,
                    marginL: 20,
                    w: 130,
                    h: 35,
                    corner: 10,
                    bg: '#5B7FF8',
                    color: '#FFFFFF',
                    size: 12
                },
                right: {
                    marginB: 7,
                    marginL: 10,
                    w: 130,
                    h: 35,
                    corner: 10,
                    bg: '#5B7FF8',
                    color: '#FFFFFF',
                    size: 12
                }
            }
        }, 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!');
              }
              //切换设备
              api.showProgress({
                 style: 'default',
                 animationType: 'fade',
                 title: '正在切换中...',
                 text: '',
                 modal: true
              });
              api.ajax({
                  url: HOST + '/iot_api/v1/app/set_device_default',
                  method: 'post',
                  headers: {
                    'Content-Type': 'application/json;charset=utf-8',
                    'token': token
                  },
                  data: {
                    body: {
                      id: idList[imei]
                    }
                  }
              }, function(ret, err) {
                api.hideProgress();
                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',
                });

              });
            }

        });
      }
  </script>
  </html>