(function(obj) { var ethUrl = app.ethBaseUrl; // ETH模块请求连接 var btcUrl = app.btcBaseUrl; // BTC模块请求连接 var eosUrl = app.eosBaseUrl; // EOS模块请求连接 var type = { GET: "GET", POST: "POST" } // 钱包类型 var walletType = { CCT: "CCT", C2C: "C2C" } // 钱包公用API var baseApi = { // 获取公钥请求信息 AJAX_PULICKEY: { getUrl: ethUrl + "wallet/getPublicKey", getType: type.GET }, // 查询是否绑定资金密码 AJAX_WALLETPASS: { getUrl: ethUrl + "wallet/existsPass", getType: type.GET } } // 钱包API var walletApi = { eth: { //请求所有钱包信息 WALLET_INFOS: { getUrl: ethUrl + "wallet/getWallets", getType: type.GET } }, btc: { //请求所有钱包信息 WALLET_INFOS: { getUrl: btcUrl + "wallet/getWallets", getType: type.GET } }, eos: { //请求所有钱包信息 WALLET_INFOS: { getUrl: eosUrl + "wallet/selectWalletByWalletType", getType: type.GET } }, } function send(ajaxObj, data) { function Ajax(ajaxObj, data) { this.ajaxObj = ajaxObj; this.data = data; } Ajax.prototype.then = function(resolve, reject) { mui.ajax(this.ajaxObj.getUrl, { headers: { "X-Requested-Token": app.getTokenStorage(), // token头部 "locale": app.getLanguageLocalStorage() // 语种头部 }, data: this.data, dataType: 'json', type: this.ajaxObj.getType, timeout: 10000, success: function(rest) { resolve(rest); }, error: function(xhr, type, errorThrown) { reject ? reject() : function() { console.log(type + ":" + errorThrown); } } }); } return new Ajax(ajaxObj, data); } /** * 设置多个币种属于什么模块 * @param {Object} type * @param {Object} coins */ function moduleTypes(type, coins) { coins || (coins = []); for(var i = 0; i < coins.length; i++) { moduleType(type, coins[i]); } return coins; } /** * 设置币种属于什么模块 * @param {Object} type * @param {Object} coins */ function moduleType(type, coin) { coin.moduleType = type; coin.balance = app.formatValue(coin.balance); coin.freeBalance = app.formatValue(coin.freeBalance); coin.freezeBalance = app.formatValue(coin.freezeBalance); coin.src = '../img/coin/' + coin.tokenSymbol + '.png'; if('tokenId' in coin) { coin.tokenAddr = coin.tokenId; } if('tokenName' in coin) { coin.tokenAddr = coin.tokenName; } if(!('addr' in coin)) { coin.addr = ''; } return coin; } /** * 币种信息推送 * @param {Array} coins */ function coinsEvenPush(coins) { if(coins == null || coins.length == 0) return; var coinMap = {}; for(var i = 0; i < coins.length; i++) { coinMap[coins[i].tokenSymbol] = coins[i]; } return coinMap; } function baseGetWallet(type, fun) { // #请求数据 var obj = { btc: [], eth: [], eos: [] }; var count = 0; send(walletApi.eth.WALLET_INFOS, { walletType: type }).then(function(data) { obj.eth = moduleTypes('eth', data.data); count++; }, function() { count++; }); send(walletApi.btc.WALLET_INFOS, { walletType: type }).then(function(data) { obj.btc = moduleTypes('btc', data.data); count++; }, function() { count++; }); // EOS币种信息查询 send(walletApi.eos.WALLET_INFOS, { walletType: type }).then(function(data) { obj.eos = moduleTypes('eos', data.data); count++; }, function() { count++; }); // #数据同步处理,放进缓存 var time = 1000; setTimeout(function() { if(count >= Object.keys(obj).length) { var coins = obj.btc.concat(obj.eth).concat(obj.eos); for(var j = 0; j < coins.length; j++) { coins[j].coinIndex = j; } if(Object.is(type, walletType.CCT)) { app.setCoinWalletInfoLocalStorage(coins); } if(Object.is(type, walletType.C2C)) { app.setC2CCoinWalletInfoLocalStorage(coins); } fun(coinsEvenPush(coins)); } else { setTimeout(arguments.callee, time) } }, time); } /** * 获取CCT所有币种信息 * @param {Function} fun 回调方法 */ obj.getCCTWallets = function(fun) { baseGetWallet(walletType.CCT, fun) } /** * 获取C2C所有币种信息 * @param {Function} fun 回调方法 */ obj.getC2CWallets = function(fun) { baseGetWallet(walletType.C2C, fun) } /** * 加密密码 * @param {String} pass 密码 * @param {Function} fun 回调方法 */ obj.getPublicKey = function(pass, fun) { send(baseApi.AJAX_PULICKEY).then(function(data) { var pulicData = data.data; var crypt = new JSEncrypt({ default_key_size: 1024 }); crypt.setPublicKey(pulicData); fun(crypt.encrypt(pass)); }); } /** * 查询是否设置资金密码 */ obj.voidServieWalletPass = function(fun) { send(baseApi.AJAX_WALLETPASS).then(function(data) { app.setIsWalletPassLocalStorage(data.data); if(fun != null) { fun(); } }); } }(window.walletApi = {}));