my_devices_frm.html 5.93 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');
        defaultImei = $api.getStorage('defaultDevice');

        /** 下拉刷新 */
        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;
                var addDomHtml = addMyDeviceListHtml(deviceList[i].storename,deviceList[i].imei,deviceList[i].province);
                $api.append($api.byId("device_panel"), addDomHtml);
              }
            }else {
              api.toast({msg: '网络错误',duration: 2000,location: 'bottom'});
            }
          }

        })
      }

      function removeDevice(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'
              });
              console.log(imei);
              handleRemove(imei);
            }
        });
      }

      function handleRemove(imei) {
        if (defaultImei == imei) {
          api.toast({msg: '不可删除默认设备',duration: 2000,location: 'bottom'});
          return;
        }
        api.ajax({
            url: HOST + '/iot_api/v1/app/unbind_device',
            method: 'post',
            headers: {
              'Content-Type': 'application/json;charset=utf-8',
              'token': token
            },
            data: {
              body: {
                id: idList[imei]
              }
            }
        }, function(ret, err) {
          console.log(JSON.stringify(ret));
          if (ret) {
            if (ret.code == 0) {
              //删除设备同步推送
              var ajpush = api.require('ajpush');
              initAndListenPush(ajpush, false);
              getDeviceList();
            }
          }
        });
      }
  </script>
  </html>