Commit 11f3e717 by 朱建香

2017/12/18

1.增加获取和更新cid的方法
parent 7016142a
......@@ -229,7 +229,8 @@ iot.navigator.getCurrentWebView = function (){
}
function requestsend(url, opts, i){
opts.success(i);
let data = [0,0,0,0,0,0,0,1,0,0,0];
opts.success(data[i]);
}
function clearTime(interval, timeout){
......@@ -237,76 +238,103 @@ function clearTime(interval, timeout){
window.clearTimeout(timeout);
}
//uComponents.loop = function(request, timeout, interval, fincb, stopcondition){
// var handle = null;
// var time = null;
// var url = request.url;
// var opts = request.opts;
// var intervalTime = interval.delay;
// var intervalcb = interval.opts;
// var flag = true;
// var i = 0;
//
// handle = setInterval(() => {
// i++;
// console.log(i);
// if(flag){
// flag = false;
// requestsend(url, {
// data: {},
// success: (response) => {
// intervalcb.success(response);
// if(stopcondition(response)){
// fincb.success(response);
// clearTime(handle, time);
// }
// flag = true;
// },
// error: (error) => {
// intervalcb.success(response);
// fincb.success(error);
// clearTime(handle, time);
// },
// complete: {}
// }, i);
// }
// },intervalTime);
//
// time = setTimeout(function(){
// clearTime(handle, time);
// },timeout);
//}
//
//uComponents.loop({url:'getLockInfo',opts: {
// success: (response) => {
// console.log(response);
// },
// error: (error) => {
// console.log(error);
// }
//}}, 10000, {
// delay: 1000,
// opts: {
// success: (response) => {
// console.log('intervalcbSuccess:'+response);
// },
// error: (error) => {
// console.log(error);
// }
// }
//}, {
// success: (response) => {
// console.log('fincbSuccess:'+response);
// },
// error: (error) => {
// console.log(error);
// }
//}, (response) => {
// if(response == 5){
// return true;
// }else{
// return false;
// }
//});
uComponents.loop = function(request, timeout, interval, fincb, stopcondition){
var handle = null;
var time = null;
var url = request.url;
var opts = request.opts;
var intervalTime = interval.delay;
var intervalcb = interval.opts;
var flag = true;
var i = 0;
//一开始执行一次,发送一次请求
requestsend(url, {
data: {},
success: (response) => {
intervalcb.success(response);
if(stopcondition(response)){
//首次请求结果符合条件的话,结束整个方法
fincb.success(response);
clearTime(handle, time);
}else{
//首次请求结果不符合条件的话,每隔intervalTime时间发送请求
handle = setInterval(() => {
i++;
console.log(i);
if(flag){
flag = false;
requestsend(url, {
data: {},
success: (response) => {
intervalcb.success(response);
if(stopcondition(response)){
fincb.success(response);
clearTime(handle, time);
}
flag = true;
},
error: (error) => {
intervalcb.error(response);
fincb.error(error);
clearTime(handle, time);
},
complete: {}
}, i);
}
},intervalTime);
}
},
error: (error) => {
intervalcb.error(response);
fincb.error(error);
clearTime(handle, time);
},
complete: {}
}, 0);
time = setTimeout(function(){
clearTime(handle, time);
intervalcb.error('timeout');
fincb.error('timeout');
},timeout);
}
//传递的参数依次是:
//1.request: 请求参数(数据类型: map,包括:url、header等);
//2.timeout: 整个方法的超时时间;
//3.interval: 发送请求的间隔时间、callback;
//4.fincb: 整个方法的callback;
//5.stopcondition: 请求停止所要符合的条件
uComponents.loop({url:'getLockInfo',opts: {
success: (response) => {
console.log(response);
},
error: (error) => {
console.log(error);
}
}}, 7000, {
delay: 1000,
opts: {
success: (response) => {
console.log('intervalcbSuccess:'+response);
},
error: (error) => {
console.log('intervalcbSuccess:'+error);
}
}
}, {
success: (response) => {
console.log('fincbSuccess:'+response);
},
error: (error) => {
console.log('fincbError'+error);
}
}, (response) => {
if(response == 1){
return true;
}else{
return false;
}
});
export {iot, uPublic, uComponents}
\ No newline at end of file
......@@ -3,12 +3,19 @@
* unotify
*/
class unotify{
//在一个周期内获取cid
static async getCid(successCallback, errorCallback){
var a = null;
var cid = null;
try{
a = await this.getCidPeriotic(3);
//利用promise,直到执行完代码再获取cid
cid = await this.getCidPeriotic(3);
//获取后,在缓存中存储cid
if(typeof successCallback == 'function'){
successCallback(a);
iot.storage.setMap('clientId', cid, (res) => {
console.log("已成功保存cid"+cid);
successCallback(cid);
}, () => {
});
}
}
catch (err){
......@@ -18,6 +25,7 @@ class unotify{
}
}
}
//周期性获取cid,周期可调整
static getCidPeriotic(times){
var self = this;
return new Promise(function(resolve, reject){
......@@ -26,6 +34,7 @@ class unotify{
var handle = null;
i++;
cid = self.getCidOnce(i);
console.log("第"+i+"次"+"获取的cid为"+cid);
if(cid != null || i >= times){
window.clearInterval(handle);
resolve(cid);
......@@ -33,6 +42,7 @@ class unotify{
handle = setInterval(function(){
i++;
cid = self.getCidOnce(i);
console.log("第"+i+"次"+"获取的cid为"+cid);
if(cid != null || i >= times){
window.clearInterval(handle);
resolve(cid);
......@@ -41,13 +51,48 @@ class unotify{
}
});
}
//单次获取cid
static getCidOnce(i){
var clientInfo = plus.push.getClientInfo();
if(clientInfo && clientInfo.hasOwnProperty('clientid') && clientInfo.clientid){
if(i == 6){
return(clientInfo.clientid);
}else{
return(null);
}
}else{
return(null);
}
}
//长期不断循环获取cid,并更新cid
static async updateCid(successCallback, errorCallback){
var cid = null;
var oldCid = null;
iot.storage.getMap('clientId',(res) => {
oldCid = res;
console.log("原来的cid为"+oldCid);
}, (err) => {
console.log(err);
});
try{
cid = await this.getCidPeriotic(9);
if(cid !== oldCid){
//发送请求更新clientId
if(typeof successCallback == 'function'){
iot.storage.setMap('clientId', cid, (res) => {
console.log("更新后的cid为"+cid);
successCallback('success');
}, () => {
});
}
}
}
catch (err){
console.log(err);
if(typeof errorCallback == 'function'){
errorCallback(a);
}
}
}
}
export default unotify;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment