DWTool.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var ReportType = require("../utils/GameEnum").ReportType;
  2. var Base64 = require("../lib/Base64").Base64;
  3. var HomeApi = require("../net/HomeApi");
  4. var GameModule = require('./GameModule');
  5. class DWTool {
  6. static coinParse(coin) {
  7. if (coin < 99999) { // 1 ~ 99999 按实际显示
  8. return coin.toString();
  9. } else if (coin >= 100000 && coin < 999999) { // 以万为单位,保留2位小数,不需四舍五入
  10. let parseCoin = (coin / 10000).toFixed(3).slice(0, -1);
  11. return parseCoin.toString() + "万";
  12. } else if (coin >= 1000000 && coin < 9999999) { // 以万为单位,保留1位小数,不需四舍五入
  13. let parseCoin = (coin / 10000).toFixed(2).slice(0, -1);
  14. return parseCoin.toString() + "万";
  15. } else if (coin >= 10000000 && coin < 99999999) { // 以万为单位,保留0位小数,不需四舍五入
  16. let parseCoin = (coin / 10000).toFixed(1).slice(0, -2);
  17. return parseCoin.toString() + "万";
  18. } else if (coin >= 100000000 && coin < 999999999) { // 以亿为单位,保留3位小数,不需四舍五入
  19. let parseCoin = (coin / 100000000).toFixed(4).slice(0, -1);
  20. return parseCoin.toString() + "亿";
  21. } else if (coin >= 1000000000 && coin < 9999999999) { // 以亿为单位,保留2位小数,不需四舍五入
  22. let parseCoin = (coin / 100000000).toFixed(3).slice(0, -1);
  23. return parseCoin.toString() + "亿";
  24. } else if (coin >= 10000000000 && coin < 99999999999) { // 以亿为单位,保留1位小数,不需四舍五入
  25. let parseCoin = (coin / 100000000).toFixed(2).slice(0, -1);
  26. return parseCoin.toString() + "亿";
  27. } else if (coin >= 100000000000 && coin < 999999999999) { // 以亿为单位,保留0位小数,不需四舍五入
  28. let parseCoin = (coin / 100000000).toFixed(1).slice(0, -2);
  29. return parseCoin.toString() + "亿";
  30. } else if (coin >= 1000000000000 && coin < 9999999999999) { // 以兆为单位,保留3位小数,不需四舍五入
  31. let parseCoin = (coin / 1000000000000).toFixed(4).slice(0, -1);
  32. return parseCoin.toString() + "兆";
  33. } else if (coin >= 10000000000000 && coin < 99999999999999) { // 以兆为单位,保留2位小数,不需四舍五入
  34. let parseCoin = (coin / 1000000000000).toFixed(3).slice(0, -1);
  35. return parseCoin.toString() + "兆";
  36. } else if (coin >= 100000000000000 && coin < 999999999999999) { // 以兆为单位,保留1位小数,不需四舍五入
  37. let parseCoin = (coin / 1000000000000).toFixed(2).slice(0, -1);
  38. return parseCoin.toString() + "兆";
  39. } else if (coin >= 1000000000000000 && coin < 9999999999999999) { // 以兆为单位,保留0位小数,不需四舍五入
  40. let parseCoin = (coin / 1000000000000).toFixed(1).slice(0, -2);
  41. return parseCoin.toString() + "兆";
  42. }
  43. };
  44. static setGenderIcon(gender) {
  45. return this.getGenderIconPath(gender).then((filePath) => {
  46. return this.loadResSpriteFrame(filePath);
  47. });
  48. };
  49. /**
  50. * 上报方法, 抽成公用方法
  51. * @param {*} seq
  52. * @param {*} totalMoney 总金币
  53. * @param {*} stars 星星数
  54. * @param {*} recordModify 是否有修改的建筑
  55. * @param {*} recordUnlockModify 是否有解锁的建筑
  56. */
  57. static reportInfo(seq = 1, totalMoney = 0, stars = 0, recordModify = [], recordUnlockModify = []) {
  58. return new Promise((resolve, reject) => {
  59. let reportFormInfo = {};
  60. reportFormInfo[ReportType.Seq] = seq;
  61. reportFormInfo[ReportType.GrossIncome] = totalMoney;
  62. reportFormInfo[ReportType.Stars] = stars;
  63. reportFormInfo[ReportType.Timestamp] = Date.parse(new Date());
  64. if (recordUnlockModify.length > 0 || recordModify.length > 0) {
  65. reportFormInfo[ReportType.Event] = 1;
  66. if (recordUnlockModify.length > 0) {
  67. reportFormInfo[ReportType.UnLockBuild] = this.parseReportArray(recordUnlockModify);
  68. }
  69. if (recordModify.length > 0) {
  70. reportFormInfo[ReportType.Build] = this.parseReportArray(recordModify);
  71. }
  72. } else {
  73. reportFormInfo[ReportType.Event] = 0;
  74. }
  75. // 还是将上报信息log出来吧, 方便调试
  76. console.log("reportFormInfo: " + JSON.stringify(reportFormInfo));
  77. let ecData = Base64.encode(JSON.stringify(reportFormInfo));
  78. HomeApi.userReportGross(ecData, (rep) => {
  79. console.log("上报成功!");
  80. resolve();
  81. }, (code, msg) => {
  82. reject({ "code": code, "msg": msg });
  83. });
  84. });
  85. };
  86. static submitWechatStars(score) { //提交得分
  87. // if (window.wx != undefined) {
  88. // // 对用户托管数据进行写数据操作
  89. // window.wx.setUserCloudStorage({
  90. // KVDataList: [{ key: 'stars', value: "" + stars }],
  91. // success: function (res) {
  92. // console.log('setUserCloudStorage', 'success', res)
  93. // },
  94. // fail: function (res) {
  95. // console.log('setUserCloudStorage', 'fail')
  96. // },
  97. // complete: function (res) {
  98. // }
  99. // });
  100. // } else {
  101. // cc.log("提交星星数:" + stars)
  102. // }
  103. let key2 = Global.wechatScoreKey;
  104. if (window.wx != undefined) {
  105. // window.wx.getUserCloudStorage({
  106. // // 以key/value形式存储
  107. // keyList: [key2],
  108. // success: function (getres) {
  109. // if (getres.KVDataList.length != 0) {
  110. // let wxgame = JSON.parse(getres.KVDataList[0].value).wxgame;
  111. // if (wxgame) {
  112. // let preScore = wxgame.score;
  113. // if (preScore > score) {
  114. // return;
  115. // }
  116. // }
  117. // }
  118. // 对用户托管数据进行写数据操作
  119. let myValue = {
  120. wxgame: {
  121. score: score,
  122. update_time: Date.parse(new Date()) / 1000
  123. },
  124. gender: Global.user.gender,
  125. jobLevelName: GameModule.userInfo.userInfo.jobLevelName,
  126. jobLevel: GameModule.userInfo.userInfo.jobLevel
  127. }
  128. var myValueString = JSON.stringify(myValue);
  129. window.wx.setUserCloudStorage({
  130. KVDataList: [{ key: key2, value: myValueString }],
  131. success: function (res) {
  132. console.log('setUserCloudStorage', 'success', res)
  133. },
  134. fail: function (res) {
  135. console.log('setUserCloudStorage', 'fail')
  136. },
  137. complete: function (res) {
  138. }
  139. });
  140. // },
  141. // fail: function (res) {
  142. // console.log('getUserCloudStorage', 'fail')
  143. // },
  144. // complete: function (res) {
  145. // }
  146. // });
  147. } else {
  148. let myValue = {
  149. wxgame: {
  150. score: score,
  151. update_time: Date.parse(new Date()) / 1000
  152. },
  153. gender: Global.user.gender,
  154. jobLevelName: GameModule.userInfo.userInfo.jobLevelName,
  155. jobLevel: GameModule.userInfo.userInfo.jobLevel
  156. }
  157. var myValueString = JSON.stringify(myValue);
  158. console.log("提交得分:" + key2 + " : " + myValueString)
  159. }
  160. }
  161. static parseReportArray(array) {
  162. let tempArray = [];
  163. // 删除没用的字段
  164. for (let i = 0; i < array.length; i++) {
  165. let model = array[i];
  166. let tempObj = {};
  167. tempObj[ReportType.ID] = model.buildingId;
  168. tempObj[ReportType.Level] = model.level
  169. tempObj[ReportType.NotPickupCoinCount] = model.coinCount;
  170. tempArray.push(tempObj);
  171. }
  172. return tempArray;
  173. };
  174. static getGenderIconPath(gender) {
  175. return new Promise((resolve, reject) => {
  176. // if (gender === 0 || gender === 1) {
  177. let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
  178. resolve(filePath);
  179. // } else {
  180. // reject();
  181. // }
  182. })
  183. };
  184. // clickOnce(cb) {
  185. // if (this._clickDic === 0) { return; }
  186. // this._clickDic = 0;
  187. // setTimeout(() => {
  188. // this._clickDic = 1;
  189. // }, 1000);
  190. // cb && cb();
  191. // };
  192. static loadResPrefab(path) {
  193. let p = new Promise((resolve, reject) => {
  194. cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
  195. if (error) {
  196. console.log(error);
  197. reject(error);
  198. } else {
  199. resolve(prefab)
  200. }
  201. });
  202. });
  203. return p;
  204. };
  205. static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
  206. let p = new Promise((resolve, reject) => {
  207. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  208. if (error) {
  209. console.log(error);
  210. reject(error);
  211. } else {
  212. var spriteFrame = atlas.getSpriteFrame(subAsset);
  213. resolve(spriteFrame)
  214. }
  215. });
  216. });
  217. return p;
  218. };
  219. static loadResSpriteAtlas(path) {
  220. let p = new Promise((resolve, reject) => {
  221. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  222. if (error) {
  223. console.log(error);
  224. reject(error);
  225. } else {
  226. resolve(atlas);
  227. }
  228. });
  229. });
  230. return p;
  231. };
  232. static loadResSpriteFrame(path) {
  233. let p = new Promise((resolve, reject) => {
  234. cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
  235. if (error) {
  236. console.log(error);
  237. reject(error);
  238. } else {
  239. resolve(sf);
  240. }
  241. });
  242. });
  243. return p;
  244. }
  245. static loadResPrefabArray(pathArr) {
  246. let p = new Promise((resolve, reject) => {
  247. cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
  248. if (error) {
  249. console.log(error);
  250. reject(error);
  251. } else {
  252. resolve(resourceArr);
  253. }
  254. });
  255. });
  256. return p;
  257. }
  258. //时间戳格式化为对应文字时间显示
  259. static timeParse(timestamp) {
  260. function zeroize(num) {
  261. return (String(num).length == 1 ? '0' : '') + num;
  262. }
  263. var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  264. var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
  265. var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  266. var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
  267. var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
  268. var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
  269. if (timestampDiff < 60) { // 一分钟以内
  270. return "刚刚";
  271. } else if (timestampDiff < 3600) { // 一小时前之内
  272. return Math.floor(timestampDiff / 60) + "分钟前";
  273. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  274. return '今天' + H + ':' + zeroize(i);
  275. } else {
  276. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  277. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  278. return '昨天' + H + ':' + zeroize(i);
  279. } else if (curDate.getFullYear() == Y) {
  280. return m + '月' + d + '日 ' + H + ':' + zeroize(i);
  281. } else {
  282. return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
  283. }
  284. }
  285. };
  286. /**
  287. * 将时间戳转成00:00显示
  288. * @param {floor} t
  289. */
  290. static calculateTime(t) {
  291. // 好像经常会出现时间格式化出错的问题, 这里先这样解决一下
  292. if (t < 0) {
  293. t = 0;
  294. console.log("出现负数的时间");
  295. }
  296. let hour = Math.floor(t / 60 / 60);
  297. let minute = Math.floor(t / 60 % 60);
  298. let second = Math.floor(t % 60);
  299. if (hour != 0 && hour < 10) {
  300. hour = "0" + hour;
  301. }
  302. if (minute < 10) {
  303. minute = "0" + minute;
  304. }
  305. if (second < 10) {
  306. second = "0" + second;
  307. }
  308. if (hour != 0) {
  309. return `${hour}:${minute}:${second}`;
  310. } else {
  311. return `${minute}:${second}`;
  312. }
  313. };
  314. /**
  315. * 获取Url上指定参数
  316. * @param {String} name 参数名
  317. */
  318. static getUrlParam(name) {
  319. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  320. var r = window.location.search.substr(1).match(reg);
  321. if (r != null) {
  322. return unescape(r[2]);
  323. } else {
  324. return null;
  325. }
  326. }
  327. }
  328. module.exports = DWTool;