my_devices_frm.html 10.9 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:'../../image/alpha0.png',
            bgColor: '#5B7FF8',
            textColor: 'rgba(255,255,255,0.5)',
            textDown: '下拉刷新设备列表...',
            textUp: '松开刷新...'
        }, function(ret, err) {
          getDeviceList();
          api.refreshHeaderLoadDone();
        });

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

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

        })
      }

      function getCurDevicePwd(imei) {
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '获取中...',
            text: '',
            modal: true
        });
        api.ajax({
          url: HOST + '/iot_api/v1/app/device?imei='+imei + '&user_id=' + userId,
          method: 'GET',
          headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'token': token
          },
        }, function(ret, err) {
          api.hideProgress();
          if (ret) {
            var pwd = ret.data.device.password;
            modifyPwd(imei,pwd);
          }
        })

      }

      function modifyPwd(imei,pwd) {
        var dialogBox = api.require('dialogBox');
        dialogBox.list({
            tapClose: true,
            texts: {
                title: '设备当前密码:' + pwd,
                enter: '确认'
            },
            listTitles: ['修改密码', '确认密码'],
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 230,
                title: {
                    h: 50,
                    size: 18,
                    color: '#000'
                },
                list: {
                    h: 128,
                    row: 2,
                    title: {
                        marginL: 20,
                        size: 14,
                        color: '#696969'
                    },
                    content: {
                        marginL: 20,
                        size: 14,
                        color: '#000'
                    }
                },
                dividingLine: {
                    width: 1,
                    color:'rgba(240,245,255,1)',
                },
                enter: {
                    w: 50,
                    h: 50,
                    bg: '#FFFFFF',
                    color: '#5B7FF8',
                    size: 14
                }
            }
        }, function(ret) {
            if (ret.eventType == 'enter') {
              var dialogBox = api.require('dialogBox');
              dialogBox.close({
                  dialogName: 'list'
              });
              if (ret.amounts[0] != ret.amounts[1]) {
                api.toast({msg: '两次密码不一致'});
                return;
              }
              if (ret.amounts[0].length <= 6 && ret.amounts[0]!= '') {
                  var result = /^\d+$/.test(ret.amounts[0]);
                  if (result) {
                    api.ajax({
                      url: HOST+'/iot_api/v1/app/update_store_password',
                      method: 'post',
                      headers: {
                        'Content-Type': 'application/json;charset=utf-8',
                        'token': token
                      },
                      data: {
                        body: {
                          old_password: pwd,
                          new_password: ret.amounts[0],
                          imei: imei
                        }
                      }
                    }, function(ret, err) {
                      if (ret.code == 0) {
                        getDeviceList();
                      }
                    })
                  }else {
                    api.toast({msg: '请输入6位以内数字'});
                    return;
                  }
              }else {
                api.toast({msg: '设备密码不可为空'});
                return;
              }
            }
        });
      }

      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: 30,
                    icon: '',
                    iconSize: 40,
                    titleSize: 16,
                    titleColor: '#37394e'
                },
                content: {
                    color: '#818295',
                    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'
              });
              handleRemove(imei);
            }
        });
      }

      function handleRemove(imei) {
        if (defaultImei == imei) {
          console.log(111);
          var jsonLen = Object.keys(idList).length;
          if (jsonLen == 1) {
            $api.setStorage('defaultDevice', '');
          }else {
            for(var key in idList){
              if (key != defaultImei) {
                bindNewImei(key);
                break;
              }
              // console.log(key+":"+idList[key]);//json对象中属性的名字:对象中属性的值
            }
          }
        }
        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) {
          if (ret) {
            if (ret.code == 0) {
              //删除设备同步推送
              var ajpush = api.require('ajpush');
              initAndListenPush(ajpush, false);
              getDeviceList();
              api.toast({
                  msg: '移除设备成功!',
                  duration: 2000,
                  location: 'bottom'
              });

              api.sendEvent({
                  name: 'change_device',
              });
            }
          }
        });
      }

      function bindNewImei(imei) {
        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.sendEvent({
              name: 'change_device',
          });

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