123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- var ReportType = require("../utils/GameEnum").ReportType;
- var Base64 = require("../lib/Base64").Base64;
- var HomeApi = require("../net/HomeApi");
- var GameModule = require('./GameModule');
- class DWTool {
- static coinParse(coin) {
- if (coin < 10000) {
- return coin.toString();
- } else if (coin >= 10000 && coin < 1000000) {
- let parseCoin = Math.floor((coin / 1000)).toFixed(0);
- return parseCoin.toString() + "K";
- } else if (coin >= 1000000 && coin < 1000000000) {
- let parseCoin = Math.floor((coin / 1000000)).toFixed(0);
- return parseCoin.toString() + "M";
- } else {
- let parseCoin = Math.floor((coin / 1000000000)).toFixed(0);
- return parseCoin.toString() + "B";
- }
- };
- static setGenderIcon(gender) {
- return this.getGenderIconPath(gender).then((filePath) => {
- return this.loadResSpriteFrame(filePath);
- });
- };
-
- static reportInfo(seq = 1, totalMoney = 0, stars = 0, recordModify = [], recordUnlockModify = []) {
- return new Promise((resolve, reject) => {
- let reportFormInfo = {};
- reportFormInfo[ReportType.Seq] = seq;
- reportFormInfo[ReportType.GrossIncome] = totalMoney;
- reportFormInfo[ReportType.Stars] = stars;
- reportFormInfo[ReportType.Timestamp] = Date.parse(new Date());
- if (recordUnlockModify.length > 0 || recordModify.length > 0) {
- reportFormInfo[ReportType.Event] = 1;
- if (recordUnlockModify.length > 0) {
- reportFormInfo[ReportType.UnLockBuild] = this.parseReportArray(recordUnlockModify);
- }
- if (recordModify.length > 0) {
- reportFormInfo[ReportType.Build] = this.parseReportArray(recordModify);
- }
- } else {
- reportFormInfo[ReportType.Event] = 0;
- }
-
- console.log("reportFormInfo: " + JSON.stringify(reportFormInfo));
- let ecData = Base64.encode(JSON.stringify(reportFormInfo));
- HomeApi.userReportGross(ecData, (rep) => {
- console.log("上报成功!");
- resolve();
- }, (code, msg) => {
- reject({ "code": code, "msg": msg });
- });
- });
- };
- static submitWechatStars(score) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- let key2 = Global.wechatScoreKey;
- if (window.wx != undefined) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- let myValue = {
- wxgame: {
- score: score,
- update_time: Date.parse(new Date()) / 1000
- },
- gender: Global.user.gender,
- jobName: GameModule.userInfo.levelName,
- jobLevel: GameModule.userInfo.level
- }
- var myValueString = JSON.stringify(myValue);
- window.wx.setUserCloudStorage({
- KVDataList: [{ key: key2, value: myValueString }],
- success: function (res) {
- console.log('setUserCloudStorage', 'success', res)
- },
- fail: function (res) {
- console.log('setUserCloudStorage', 'fail')
- },
- complete: function (res) {
- }
- });
-
-
-
-
-
-
-
- } else {
- cc.log("提交得分:" + key2 + " : " + score)
- }
- }
- static parseReportArray(array) {
- let tempArray = [];
-
- for (let i = 0; i < array.length; i++) {
- let model = array[i];
- let tempObj = {};
- tempObj[ReportType.ID] = model.buildingId;
- tempObj[ReportType.Level] = model.level
- tempObj[ReportType.NotPickupCoinCount] = model.coinCount;
- tempArray.push(tempObj);
- }
- return tempArray;
- };
- static getGenderIconPath(gender) {
- return new Promise((resolve, reject) => {
-
- let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
- resolve(filePath);
-
-
-
- })
- };
-
-
-
-
-
-
-
-
- static loadResPrefab(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(prefab)
- }
- });
- });
- return p;
- };
- static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- var spriteFrame = atlas.getSpriteFrame(subAsset);
- resolve(spriteFrame)
- }
- });
- });
- return p;
- };
- static loadResSpriteAtlas(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(atlas);
- }
- });
- });
- return p;
- };
- static loadResSpriteFrame(path) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(sf);
- }
- });
- });
- return p;
- }
- static loadResPrefabArray(pathArr) {
- let p = new Promise((resolve, reject) => {
- cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
- if (error) {
- console.log(error);
- reject(error);
- } else {
- resolve(resourceArr);
- }
- });
- });
- return p;
- }
-
- static timeParse(timestamp) {
- function zeroize(num) {
- return (String(num).length == 1 ? '0' : '') + num;
- }
- var curTimestamp = parseInt(new Date().getTime() / 1000);
- var timestampDiff = curTimestamp - timestamp;
- var curDate = new Date(curTimestamp * 1000);
- var tmDate = new Date(timestamp * 1000);
- var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
- var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
- if (timestampDiff < 60) {
- return "刚刚";
- } else if (timestampDiff < 3600) {
- return Math.floor(timestampDiff / 60) + "分钟前";
- } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
- return '今天' + H + ':' + zeroize(i);
- } else {
- var newDate = new Date((curTimestamp - 86400) * 1000);
- if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
- return '昨天' + H + ':' + zeroize(i);
- } else if (curDate.getFullYear() == Y) {
- return m + '月' + d + '日 ' + H + ':' + zeroize(i);
- } else {
- return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
- }
- }
- };
-
- static calculateTime(t) {
-
- if (t < 0) {
- t = 0;
- console.log("出现负数的时间");
- }
- let hour = Math.floor(t / 60 / 60);
- let minute = Math.floor(t / 60 % 60);
- let second = Math.floor(t % 60);
- if (hour != 0 && hour < 10) {
- hour = "0" + hour;
- }
- if (minute < 10) {
- minute = "0" + minute;
- }
- if (second < 10) {
- second = "0" + second;
- }
- if (hour != 0) {
- return `${hour}:${minute}:${second}`;
- } else {
- return `${minute}:${second}`;
- }
- };
-
- static getUrlParam(name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r != null) {
- return unescape(r[2]);
- } else {
- return null;
- }
- }
- }
- module.exports = DWTool;
|