Commit 11f3e717 by 朱建香

2017/12/18

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