equipmentList.html 9.03 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>我的设备</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/style.css" />
</head>

<body class="equipment-list-view">
    <div class="aui-content aui-margin-b-15">
        <header class="aui-bar aui-bar-nav" id="header">
            <a class="aui-btn aui-pull-left" tapmode onclick="closeWin()">
                <span class="aui-iconfont aui-icon-left"></span>
            </a>
            <div class="aui-title">我的设备</div>
            <span class="aui-btn aui-pull-right" tapmode onclick="openCanner(false)">添加设备</span>
        </header>
        <ul class="aui-padded-10" id="devicesList">

        </ul>
    </div>
</body>

</html>
<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 header = $api.byId('header');
fixStatusBar_API(header);
apiready = function() {
    api.setStatusBarStyle({
        style: 'dark',
        color: 'rgba(0,0,0,0)'
    });
    api.parseTapmode();
    api.addEventListener({
        name: 'add_device'
    }, function(ret, err){
        if( ret ){
             showDeviceList();
        }else{
        }
    });

    showDeviceList();
}

function showDeviceList() {
    console.log('****');
    token = $api.getStorage('userToken');
    var userID = $api.getStorage('userID');
    $.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
      },
      success: function(ret) {
        if (ret) {
          if (ret.code == 0) {
            $api.setStorage('devices', JSON.stringify(ret.data.list));
            var devicesStr = $api.getStorage('devices');
            if (!devicesStr || devicesStr == "") {
                api.toast({
                    msg: '暂无设备',
                    duration: 4000,
                    location: 'bottom'
                });
                setTimeout(function() {
                    api.closeWin();
                },
                3000);
                return false;
            }

            var devices = JSON.parse(devicesStr);
            $("#devicesList").empty();
            var devicesHtmlStr = "";
            if (devices && devices.length > 0) {
                for (var i in devices) {
                    var dev = devices[i];
                    devicesHtmlStr += '<li tapmode onclick="openRepairDetail(\'' + dev.imei + '\');">'
                    + '<div><label class="tytx-color-b">设备编号:</label><b><a href="javascript:;" class="aui-pull-right" tapmode onclick="resetDeivcePwd(\''
                    + dev.imei
                    + '\')"><img src="../image/mima.png" /><span class="tytx-color-b">&nbsp&nbsp修改密码</span></a>'
                    + dev.imei
                    + '</b></div>'
                    + '<p class="aui-ellipsis-1"><img src="../image/dizhi-s.png" /><span>'
                    + dev.province + ' ' + dev.city + ' ' + dev.address
                    + '</span>'+ '<a href="javascript:;" class="aui-pull-right" tapmode onclick="removeDevice(\'' + dev.imei + '\')"><img src="../image/del.png" /><span style="color:#F00">移除设备</span></a>' +'</p>' + '</li>';
                }
            }
            $("#devicesList").html(devicesHtmlStr);
          }
        }
      },
      error: function() {
        api.toast({
            msg: '获取设备列表失败',
            duration: 2000,
            location: 'bottom'
        });
      }
    });
}

function resetDeivcePwd(imei) {
  var token = $api.getStorage('userToken');
  $.ajax({
      url: HOST + '/iot_api/v1/app/device?imei='+imei,
      type: 'GET',
      dataType: 'json',
      headers: {
          'Content-Type': 'application/json;charset=utf-8',
          'token': token
      },
      complete:function(){
        api.hideProgress();
      },
      success: function(ret) {
          if (ret) {
              if (ret.code == 0) {
                  var deviceInfo = ret.data.device;
                  $api.setStorage('curDevicePwd'+imei, deviceInfo.password);
                  if(deviceInfo){
                    showDeviceConfig(deviceInfo);
                  }

              }else{
                api.toast({
                    msg: '获取设备信息失败,请稍候再试',
                    duration: 4000,
                    location: 'bottom'
                });

              }
          }
      },
      error: function() {
        api.toast({
            msg: '获取设备信息失败,请退出重试',
            duration: 4000,
            location: 'bottom'
        });
      }
  });
  api.openWin({ slidBackEnabled:false,
      name: 'changeDevicePassword',
      url: 'changeDevicePassword.html',
      pageParam: {imei, imei}
  });
}

function openRepairDetail(imei){
    return false
}

function removeDevice(imei) {
    api.confirm({
        title: '解绑确认',
        msg: '是否确认删除设备绑定?',
        buttons: ['确定', '取消']
    },
    function(ret, err) {
        if (ret && ret.buttonIndex == 1) {
            var token = $api.getStorage('userToken');

            var devicesStr = $api.getStorage('devices');
            if (!devicesStr || devicesStr == "") {
                api.toast({
                    msg: '暂无设备',
                    duration: 4000,
                    location: 'bottom'
                });
                setTimeout(function() {
                    api.closeWin();
                },
                3000);
                return false;
            }

            var devices = JSON.parse(devicesStr);
            // if (devices.length == 1) {
            //     api.toast({
            //         msg: '最少保留一项设备!',
            //         duration: 2000,
            //         location: 'bottom'
            //     });
            //
            //     return false;
            // }
            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '操作请求中...',
                text: '',
                modal: true
            });
            var choseId = '';
            var firstDeviceImei = '';
            var choseIndex = -1;
            for (var i in devices) {
                if (devices[i].imei == imei) {
                    choseIndex = i;
                    choseId = devices[i].id;
                } else if (firstDeviceImei == '') {
                    firstDeviceImei = devices[i].imei;
                }
            }
            if (!choseId || choseId == '') {
                api.toast({
                    msg: '要删除的设备不存在!',
                    duration: 2000,
                    location: 'bottom'
                });

                return false;
            }
            $.ajax({
                url: HOST + '/iot_api/v1/app/unbind_device',
                type: 'POST',
                dataType: 'json',
                headers: {
                    'Content-Type': 'application/json;charset=utf-8',
                    'token': token
                },
                data: JSON.stringify({
                    id: choseId
                }),
                complete: function() {
                    api.hideProgress();
                },
                success: function(ret) {

                    if (ret) {
                        if (ret.code == 0) {
                            devices.splice(choseIndex, 1);
                            $api.setStorage('devices', JSON.stringify(devices));
                            showDeviceList();

                            //删除设备同步推送
                            var ajpush = api.require('ajpush');
                            initAndListenPush(ajpush, false);

                            var currentDevice = $api.getStorage('defaultDevice');
                            if (currentDevice == imei) {
                                api.sendEvent({
                                    name: 'change_device',
                                    extra: {
                                        imei: firstDeviceImei
                                    }
                                });
                            }
                        }
                    }
                },
                error: function() {
                    api.toast({
                        msg: '删除设备失败,请稍候再试',
                        duration: 2000,
                        location: 'bottom'
                    });

                }
            });
        }
    })
    return false;
}
</script>