578 lines
13 KiB
JavaScript
578 lines
13 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');
|
|
|
|
module.exports = {
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {},
|
|
|
|
// start () {},
|
|
|
|
// update (dt) {},
|
|
|
|
__getConfig(channelid, gameid, openid, successcb, failcb) {
|
|
channelid = cc.channelid; //
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'Config')
|
|
.addKV('a', 'read')
|
|
.addKV('gameid', gameid)
|
|
.addKV('channel', channelid)
|
|
.addKV('openid', openid);
|
|
//.addKV('publish_env', 'dev');//
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
var obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log('[__getConfig]success!' + JSON.stringify(obj));
|
|
var kvobj = httpclient.JSON_parse(obj.KVList);
|
|
if (kvobj) {
|
|
successcb && successcb(kvobj);
|
|
} else {
|
|
failcb && failcb(0, -1, 'kvlist is not obj');
|
|
}
|
|
} else {
|
|
console.log(
|
|
'[__getConfig]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__getConfig]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__getStorage(accountid, sessionid, token, skey, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'getStorage')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token)
|
|
.addKV('keys', skey);
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
var obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log('[__getStorage]success!' + JSON.stringify(obj));
|
|
//var kvobj = httpclient.JSON_parse(obj.KVList);
|
|
var kvobj = obj.KVList;
|
|
if (kvobj) {
|
|
successcb && successcb(kvobj);
|
|
} else {
|
|
failcb && failcb(0, -1, 'kvlist is not obj');
|
|
}
|
|
} else {
|
|
console.log(
|
|
'[__getStorage]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__getStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__getExpireTime(accountid, sessionid, token, skey, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'ttl')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token)
|
|
.addKV('keys', skey);
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
var obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log(
|
|
'[__getExpireTime]success!' + JSON.stringify(obj)
|
|
);
|
|
var ktobj = httpclient.JSON_parse(obj.KTList);
|
|
if (ktobj) {
|
|
successcb && successcb(ktobj);
|
|
} else {
|
|
failcb && failcb(0, -1, 'kvlist is not obj');
|
|
}
|
|
} else {
|
|
console.log(
|
|
'[__getExpireTime]failed!' +
|
|
obj.errcode +
|
|
':' +
|
|
obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log(
|
|
'[__getExpireTime]failed!' + errcode + ':' + errmsg
|
|
);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__setStorage(accountid, sessionid, token, kvlist, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'setStorage')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token);
|
|
|
|
let value = JSON.stringify(kvlist);
|
|
|
|
httpclient.httpPost(
|
|
this.urlbd.baseurl,
|
|
value,
|
|
function (restext) {
|
|
let obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log('[__setStorage]success!' + JSON.stringify(obj));
|
|
successcb && successcb();
|
|
} else {
|
|
console.log(
|
|
'[__setStorage]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__setStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__setExpireTime(
|
|
accountid,
|
|
sessionid,
|
|
token,
|
|
skey,
|
|
origntype,
|
|
timevalue,
|
|
successcb,
|
|
failcb
|
|
) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'expire')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token)
|
|
.addKV('keys', skey)
|
|
.addKV('time_origin', origntype)
|
|
.addKV('time_val', timevalue);
|
|
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
let obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log('[__delStorage]success!' + JSON.stringify(obj));
|
|
successcb && successcb();
|
|
} else {
|
|
console.log(
|
|
'[__delStorage]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__delStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__clearStorage(accountid, sessionid, token, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'clearStorage')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token);
|
|
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
let obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log(
|
|
'[__clearStorage]success!' + JSON.stringify(obj)
|
|
);
|
|
successcb && successcb();
|
|
} else {
|
|
console.log(
|
|
'[__clearStorage]failed!' +
|
|
obj.errcode +
|
|
':' +
|
|
obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__clearStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
__delStorage(accountid, sessionid, token, skey, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'CloudStorage')
|
|
.addKV('a', 'delKeys')
|
|
.addKV('account_id', accountid)
|
|
.addKV('session_id', sessionid)
|
|
.addKV('access_token', token)
|
|
.addKV('keys', skey);
|
|
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
let obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
console.log('[__delStorage]success!' + JSON.stringify(obj));
|
|
successcb && successcb();
|
|
} else {
|
|
console.log(
|
|
'[__delStorage]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb(0, obj.errcode, obj.errmsg);
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__delStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb(errcode, 0, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
init(channelid, gameid, isoffical) {
|
|
this.gameid = gameid;
|
|
this.channelid = channelid;
|
|
this.token = '';
|
|
this.urlbd = new urlbuilder(
|
|
BaseNet.getNormalApiUrl('cloud')
|
|
);
|
|
this.cfg = {
|
|
_reborntype: -1,
|
|
_getrewardtype: -1,
|
|
_exchange_rewardtype: -1,
|
|
_navigateflag: -1,
|
|
_serverversion: -1,
|
|
_gglFlag: -1,
|
|
};
|
|
this.cfginited = false;
|
|
console.log(
|
|
'[jccloud]init:' + gameid + '|' + channelid + '|' + isoffical
|
|
);
|
|
},
|
|
|
|
setAccountID(accountid, sessionid) {
|
|
this.accountid = accountid;
|
|
this.sessionid = sessionid;
|
|
},
|
|
|
|
setNickName(nickname) {
|
|
this.nickname = nickname;
|
|
},
|
|
|
|
setFromAppID(appid) {
|
|
this.fromid = appid;
|
|
},
|
|
|
|
setLocalUUID(uuid) {
|
|
this.localid = uuid;
|
|
},
|
|
|
|
setSystemInfo(info) {},
|
|
|
|
getConfig(openid, successcb, failcb) {
|
|
if (this._configgetting) {
|
|
return 1;
|
|
}
|
|
this._configgetting = true;
|
|
this.__getConfig(
|
|
this.channelid,
|
|
this.gameid,
|
|
openid,
|
|
(kvlist) => {
|
|
this._configgetting = false;
|
|
successcb && successcb(kvlist);
|
|
},
|
|
(neterr, logicerr, errmsg) => {
|
|
this._configgetting = false;
|
|
failcb && failcb(neterr, logicerr, errmsg);
|
|
}
|
|
);
|
|
return 0;
|
|
},
|
|
|
|
initConfig(openid, successcb, failcb) {
|
|
if (this.cfginited) {
|
|
successcb && successcb(this.cfg);
|
|
return 0;
|
|
}
|
|
this.cfginited = false;
|
|
return this.getConfig(
|
|
openid,
|
|
(res) => {
|
|
console.log(JSON.stringify(res));
|
|
if (res instanceof Array) {
|
|
res.forEach((element) => {
|
|
this.cfg[element.key] = element.value;
|
|
});
|
|
this.cfginited = true;
|
|
successcb && successcb(this.cfg);
|
|
} else {
|
|
failcb && failcb(0, -1, '');
|
|
}
|
|
},
|
|
(neterr, logicerr, errmsg) => {
|
|
failcb && failcb(neterr, logicerr, errmsg);
|
|
}
|
|
);
|
|
},
|
|
|
|
//
|
|
getServerConfig(successcb, failcb, bForce) {
|
|
if (bForce) {
|
|
this.cfginited = false;
|
|
}
|
|
return this.initConfig('', successcb, failcb);
|
|
},
|
|
|
|
setStorage: function (
|
|
skey,
|
|
svalue,
|
|
successcb,
|
|
failcb,
|
|
timeorign_type,
|
|
time_value
|
|
) {
|
|
let obj = {
|
|
key: skey,
|
|
value: svalue,
|
|
};
|
|
if (timeorign_type && time_value) {
|
|
obj.time_origin = timeorign_type;
|
|
obj.time_val = time_value;
|
|
}
|
|
let lst = [obj];
|
|
this.__setStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
lst,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
setStorages: function (kvlist, successcb, failcb) {
|
|
this.__setStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
kvlist,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
getStorage: function (key, successcb, failcb) {
|
|
this.__getStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
key,
|
|
(kvlist) => {
|
|
if (kvlist.length > 0) {
|
|
let obj = kvlist[0];
|
|
successcb && successcb(obj.value, obj.ttl_seconds, obj.key);
|
|
} else {
|
|
successcb && successcb(null, -1, key);
|
|
}
|
|
},
|
|
failcb
|
|
);
|
|
},
|
|
|
|
getStorages: function (keylist, successcb, failcb) {
|
|
let skey = keylist.join();
|
|
this.__getStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
skey,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
delStorage: function (key, successcb, failcb) {
|
|
this.__delStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
key,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
delStorages: function (keylist, successcb, failcb) {
|
|
let skey = keylist.join();
|
|
this.__delStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
skey,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
clearStorage: function (successcb, failcb) {
|
|
this.__clearStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
setExpireTime: function (
|
|
key,
|
|
timeorign_type,
|
|
time_value,
|
|
successcb,
|
|
failcb
|
|
) {
|
|
this.__setExpireTime(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
key,
|
|
timeorign_type,
|
|
time_value,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
setExpireTimes: function (
|
|
keylist,
|
|
timeorign_type,
|
|
time_value,
|
|
successcb,
|
|
failcb
|
|
) {
|
|
let skey = keylist.join();
|
|
this.__setExpireTime(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
skey,
|
|
timeorign_type,
|
|
time_value,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
getExpireTime: function (key, successcb, failcb) {
|
|
this.__getStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
key,
|
|
(ktlist) => {
|
|
if (ktlist.length > 0) {
|
|
let obj = ktlist[0];
|
|
successcb && successcb(obj.ttl_seconds, obj.key);
|
|
} else {
|
|
successcb && successcb(-1, key);
|
|
}
|
|
},
|
|
failcb
|
|
);
|
|
},
|
|
|
|
getExpireTimes: function (key, successcb, failcb) {
|
|
this.__getStorage(
|
|
this.accountid,
|
|
this.sessionid,
|
|
this.token,
|
|
key,
|
|
successcb,
|
|
failcb
|
|
);
|
|
},
|
|
|
|
getShareData(psex, pprovince, pcity, successcb, failcb) {
|
|
this.urlbd.clear();
|
|
this.urlbd
|
|
.addKV('c', 'Config')
|
|
.addKV('a', 'share')
|
|
.addKV('account_id', this.accountid)
|
|
.addKV('session_id', this.sessionid)
|
|
.addKV('gameid', this.gameid)
|
|
.addKV('sex', psex)
|
|
.addKV('province', pprovince)
|
|
.addKV('city', pcity);
|
|
|
|
httpclient.httpGet(
|
|
this.urlbd.baseurl,
|
|
function (restext) {
|
|
let obj = httpclient.JSON_parse(restext);
|
|
if (obj.errcode == 0) {
|
|
successcb && successcb(obj.share_info);
|
|
} else {
|
|
console.log(
|
|
'[__delStorage]failed!' + obj.errcode + ':' + obj.errmsg
|
|
);
|
|
failcb && failcb();
|
|
}
|
|
},
|
|
function (errcode, errmsg) {
|
|
console.log('[__delStorage]failed!' + errcode + ':' + errmsg);
|
|
failcb && failcb();
|
|
}
|
|
);
|
|
},
|
|
};
|