deviceList_frm.html
7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!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:'../../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() {
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', []);
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);
}else {
var addDomHtml = addDeviceListHtml(deviceList[i].storename,deviceList[i].imei,deviceList[i].province + ' ' + deviceList[i].city,false);
}
$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();
})
}
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
});
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>