239 lines
7.2 KiB
JavaScript
239 lines
7.2 KiB
JavaScript
// Learn cc.Class:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
|
|
// Learn Attribute:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
|
|
// Learn life-cycle callbacks:
|
|
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
|
|
|
|
//var httpclient = require("httpclient");
|
|
//var urlbuilder = require("urlbuilder");
|
|
var httpclient = require('httpclient');
|
|
var urlbuilder = require('urlbuilder');
|
|
const BaseNet = require('../../BaseNet');
|
|
var storage = require('storage');
|
|
|
|
const JC_NOTIFY_MSG_KEY = 'jc_notify_msg';
|
|
|
|
module.exports = {
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {},
|
|
|
|
// start () {},
|
|
|
|
// update (dt) {},
|
|
__getMsglist(successcb, failcb){
|
|
this.urlbd.clear();
|
|
this.urlbd.addKV('c', 'MsgQueue')
|
|
.addKV('a', 'getMsgList')
|
|
.addKV('session_id', this.sessionid)
|
|
.addKV('account_id', this.accountid)
|
|
.addKV('sid', this.sid)
|
|
.addKV('wid', this.wid)
|
|
.addKV('ptid', this.ptid)
|
|
;
|
|
httpclient.httpGet(this.urlbd.baseurl, (restext) => {
|
|
console.log('[__getMsglist]result:');
|
|
//console.log(restext);
|
|
var obj = httpclient.JSON_parse(restext);
|
|
if(obj.errcode == 0){
|
|
console.log('[__getMsglist]success!'+JSON.stringify(obj));
|
|
//var msglst = httpclient.JSON_parse(obj.msglist);
|
|
var msglst = obj.msglist;
|
|
if(msglst){
|
|
successcb && successcb(msglst);
|
|
}else{
|
|
failcb && failcb(0, -1, "msglst is not obj");
|
|
}
|
|
}else{
|
|
console.log('[__getMsglist]failed!'+obj.errcode+":"+obj.errmsg);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
}, (errcode, errmsg) => {
|
|
console.log('[__getMsglist]failed!'+errcode+":"+errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
});
|
|
},
|
|
|
|
__markMsgs(msgidlst){
|
|
let smsgs = (msgidlst instanceof Array)? msgidlst.join(): msgidlst;
|
|
this.urlbd.clear();
|
|
this.urlbd.addKV('c', 'MsgQueue')
|
|
.addKV('a', 'markMsg')
|
|
.addKV('session_id', this.sessionid)
|
|
.addKV('account_id', this.accountid)
|
|
.addKV('sid', this.sid)
|
|
.addKV('wid', this.wid)
|
|
.addKV('ptid', this.ptid)
|
|
.addKV('msg_ids', smsgs);
|
|
httpclient.httpGet(this.urlbd.baseurl, (res) => {
|
|
if(!this.isoffical){
|
|
console.log('[__markMsgs]'+res);
|
|
}
|
|
}, (err, errmsg) => {
|
|
if(!this.isoffical){
|
|
console.log('[__markMsgs]Err:'+err+'|'+errmsg);
|
|
}
|
|
});
|
|
},
|
|
|
|
init(channelid, gameid, isoffical, owner){
|
|
this.owner = owner;
|
|
this.gameid = gameid;
|
|
this.channelid = channelid;
|
|
this.isoffical = isoffical;
|
|
this.urlbd = new urlbuilder(BaseNet.getNormalApiUrl('notify'));
|
|
this.initMsglist();
|
|
console.log("[jcnotify]init:"+gameid + "|" + channelid + "|" + isoffical);
|
|
},
|
|
|
|
setAccountID(accountid, sessionid, exobj){
|
|
this.accountid = accountid;
|
|
this.sessionid = sessionid;
|
|
if(exobj){
|
|
this.wid = exobj.wid;
|
|
this.sid = exobj.sid;
|
|
this.ptid = exobj.ptid;
|
|
}
|
|
|
|
this.getMsglist();
|
|
},
|
|
|
|
setNickName(nickname){
|
|
this.nickname = nickname;
|
|
},
|
|
|
|
setFromAppID(appid){
|
|
this.fromid = appid;
|
|
},
|
|
|
|
setLocalUUID(uuid){
|
|
this.localid = uuid;
|
|
},
|
|
|
|
setSystemInfo(info){
|
|
|
|
},
|
|
|
|
setServerTime(servertime, localtime){
|
|
this.loginSrvTime = servertime;
|
|
this.loginLocalTime = localtime;
|
|
},
|
|
|
|
getMsglist(){
|
|
this.__getMsglist((res) => {
|
|
let idlst = [];
|
|
res.forEach(element => {
|
|
let obj = this.msglist.find((item) => {
|
|
return item.id == element.msg_id;
|
|
});
|
|
if(!obj){
|
|
obj = {
|
|
id: element.msg_id,
|
|
type: element.msg_type,
|
|
content: element.content,
|
|
name: element.msg_name,
|
|
sendtime: element.sendtime,
|
|
expiretime: element.expiretime? element.expiretime: element.sendtime+24*3600000
|
|
};
|
|
this.msglist.push(obj);
|
|
}else{
|
|
obj.type = element.msg_type;
|
|
obj.content = element.content;
|
|
obj.name = element.msg_name;
|
|
obj.sendtime = element.sendtime;
|
|
obj.expiretime = element.expiretime? element.expiretime: element.sendtime+24*3600000;
|
|
}
|
|
idlst.push(element.msg_id);
|
|
});
|
|
if(idlst.length > 0){
|
|
//this.__markMsgs(idlst);
|
|
this._checkMsglist();
|
|
}
|
|
this._reqMsglist(false);
|
|
}, () => {
|
|
this._reqMsglist(true);
|
|
});
|
|
},
|
|
|
|
initMsglist(){
|
|
if(!this.msglist){
|
|
this.msglist = [];
|
|
}else{
|
|
this.msglist.length = 0;
|
|
}
|
|
let obj = storage.getjson(JC_NOTIFY_MSG_KEY);
|
|
if(obj){
|
|
this.msglist = this.msglist.concat(obj);
|
|
}
|
|
this._lastidx = this.msglist.length > 0? 0: -1;
|
|
},
|
|
|
|
resetMsglist(){
|
|
storage.remove(JC_NOTIFY_MSG_KEY);
|
|
this.msglist.length = 0;
|
|
this.getMsglist();
|
|
},
|
|
|
|
_reqMsglist(breload){
|
|
let dt = breload? 10000: 600000;
|
|
setTimeout(() => {
|
|
this.getMsglist();
|
|
}, dt);
|
|
},
|
|
|
|
_checkMsglist(){
|
|
let tm = this._getServerNowTime();
|
|
let i = this.msglist.length;
|
|
while(i--){
|
|
let obj = this.msglist[i];
|
|
if(this._isExpire(obj, tm)){
|
|
this.msglist.splice(i, 1);
|
|
}
|
|
}
|
|
|
|
this.msglist.sort((a, b) => {
|
|
return a.sendtime > b.sendtime;
|
|
});
|
|
|
|
this._handleMsglist(this.msglist, this);
|
|
|
|
storage.setjson(JC_NOTIFY_MSG_KEY, this.msglist);
|
|
},
|
|
|
|
_getServerNowTime(){
|
|
if(this.owner){
|
|
return this.owner.getServerNowTime();
|
|
}else{
|
|
let tm = new Date().getTime();
|
|
return this.loginSrvTime + (tm - this.loginLocalTime) / 1000;
|
|
}
|
|
},
|
|
|
|
_isExpire(msg, nowtime){
|
|
return msg.expiretime < nowtime;
|
|
},
|
|
|
|
getOneMsg(){
|
|
if(!this.msglist){
|
|
return '';
|
|
}
|
|
if(this._lastidx >= this.msglist.length){
|
|
this._lastidx = this.msglist.length > 0? 0: -1;
|
|
}
|
|
if(this._lastidx < this.msglist.length && this._lastidx >= 0){
|
|
let obj = this.msglist[this._lastidx];
|
|
this._lastidx++;
|
|
return obj.content;
|
|
}
|
|
return '';
|
|
},
|
|
|
|
_handleMsglist(lst, owner){
|
|
this.owner && this.owner._handleNotifyMsgs && this.owner._handleNotifyMsgs(lst, owner);
|
|
}
|
|
};
|