DWTool.js 14 KB

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