DWTool.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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() + "A";
  12. } else if (coin >= 1000000 && coin < 9999999) { // 以万为单位,保留1位小数,不需四舍五入
  13. let parseCoin = (coin / 10000).toFixed(2).slice(0, -1);
  14. return parseCoin.toString() + "A";
  15. } else if (coin >= 10000000 && coin < 99999999) { // 以万为单位,保留0位小数,不需四舍五入
  16. let parseCoin = (coin / 10000).toFixed(1).slice(0, -2);
  17. return parseCoin.toString() + "A";
  18. } else if (coin >= 100000000 && coin < 999999999) { // 以亿为单位,保留3位小数,不需四舍五入
  19. let parseCoin = (coin / 100000000).toFixed(4).slice(0, -1);
  20. return parseCoin.toString() + "B";
  21. } else if (coin >= 1000000000 && coin < 9999999999) { // 以亿为单位,保留2位小数,不需四舍五入
  22. let parseCoin = (coin / 100000000).toFixed(3).slice(0, -1);
  23. return parseCoin.toString() + "B";
  24. } else if (coin >= 10000000000 && coin < 99999999999) { // 以亿为单位,保留1位小数,不需四舍五入
  25. let parseCoin = (coin / 100000000).toFixed(2).slice(0, -1);
  26. return parseCoin.toString() + "B";
  27. } else if (coin >= 100000000000 && coin < 999999999999) { // 以亿为单位,保留0位小数,不需四舍五入
  28. let parseCoin = (coin / 100000000).toFixed(1).slice(0, -2);
  29. return parseCoin.toString() + "B";
  30. } else if (coin >= 1000000000000 && coin < 9999999999999) { // 以兆为单位,保留3位小数,不需四舍五入
  31. let parseCoin = (coin / 1000000000000).toFixed(4).slice(0, -1);
  32. return parseCoin.toString() + "C";
  33. } else if (coin >= 10000000000000 && coin < 99999999999999) { // 以兆为单位,保留2位小数,不需四舍五入
  34. let parseCoin = (coin / 1000000000000).toFixed(3).slice(0, -1);
  35. return parseCoin.toString() + "C";
  36. } else if (coin >= 100000000000000 && coin < 999999999999999) { // 以兆为单位,保留1位小数,不需四舍五入
  37. let parseCoin = (coin / 1000000000000).toFixed(2).slice(0, -1);
  38. return parseCoin.toString() + "C";
  39. } else if (coin >= 1000000000000000 && coin < 9999999999999999) { // 以兆为单位,保留0位小数,不需四舍五入
  40. let parseCoin = (coin / 1000000000000).toFixed(1).slice(0, -2);
  41. return parseCoin.toString() + "C";
  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, clickCount = 0, recordModify = [], recordUnlockModify = []) {
  72. return new Promise((resolve, reject) => {
  73. let reportFormInfo = {};
  74. reportFormInfo[ReportType.Seq] = seq;
  75. reportFormInfo[ReportType.Gold] = totalMoney;
  76. reportFormInfo[ReportType.Stars] = stars;
  77. reportFormInfo[ReportType.ClickCount] = clickCount;
  78. reportFormInfo[ReportType.Timestamp] = Date.parse(new Date());
  79. if (recordUnlockModify.length > 0 || recordModify.length > 0) {
  80. reportFormInfo[ReportType.Event] = 1;
  81. if (recordUnlockModify.length > 0) {
  82. reportFormInfo[ReportType.Build] = this.parseReportArray(recordUnlockModify);
  83. }
  84. if (recordModify.length > 0) {
  85. reportFormInfo[ReportType.Build] = this.parseReportArray(recordModify);
  86. }
  87. } else {
  88. reportFormInfo[ReportType.Event] = 0;
  89. }
  90. // 还是将上报信息log出来吧, 方便调试
  91. // console.log("reportFormInfo: " + JSON.stringify(reportFormInfo));
  92. let ecData = Base64.encode(JSON.stringify(reportFormInfo));
  93. HomeApi.userReportGross(ecData, (rep) => {
  94. // console.log("上报成功!");
  95. resolve();
  96. }, (code, msg) => {
  97. reject({ "code": code, "msg": msg });
  98. });
  99. });
  100. };
  101. static reportRoomsInfo(totalMoney = 0, buildingData = []) {
  102. let reportFormInfo = {};
  103. reportFormInfo[ReportType.Gold] = totalMoney;
  104. reportFormInfo[ReportType.Build] = this.parseReportArray(buildingData);
  105. let ecData = Base64.encode(JSON.stringify(reportFormInfo));
  106. HomeApi.userReportRooms(ecData, (rep) => {
  107. // console.log("上报成功!");
  108. }, (code, msg) => {
  109. console.log("上报失败!");
  110. });
  111. };
  112. //score对应总部等级buildingLevel,提交得分到微信后台
  113. static submitWechatScore(score) { //提交得分
  114. let key2 = Global.wechatScoreKey;
  115. if (window.wx != undefined) {
  116. // 对用户托管数据进行写数据操作
  117. let myValue = {
  118. wxgame: {
  119. score: score,
  120. update_time: Date.parse(new Date()) / 1000
  121. },
  122. gender: Global.user.gender,
  123. }
  124. var myValueString = JSON.stringify(myValue);
  125. window.wx.setUserCloudStorage({
  126. KVDataList: [{ key: key2, value: myValueString }],
  127. success: function (res) {
  128. console.log('setUserCloudStorage', 'success', res)
  129. },
  130. fail: function (res) {
  131. console.log('setUserCloudStorage', 'fail')
  132. },
  133. complete: function (res) {
  134. }
  135. });
  136. } else {
  137. let myValue = {
  138. wxgame: {
  139. score: score,
  140. update_time: Date.parse(new Date()) / 1000
  141. },
  142. gender: Global.user.gender,
  143. }
  144. var myValueString = JSON.stringify(myValue);
  145. // console.log("提交得分:" + key2 + " : " + myValueString)
  146. }
  147. }
  148. static parseReportArray(array) {
  149. let tempArray = [];
  150. // 删除没用的字段
  151. for (let i = 0; i < array.length; i++) {
  152. let model = array[i];
  153. let tempObj = {};
  154. tempObj[ReportType.ID] = model.roomId;
  155. if (model.level != undefined) {
  156. tempObj[ReportType.Level] = model.level;
  157. } else if (model.roomLevel != undefined) {
  158. tempObj[ReportType.Level] = model.roomLevel;
  159. }
  160. tempArray.push(tempObj);
  161. }
  162. return tempArray;
  163. };
  164. /**
  165. *根据url加载网络图片
  166. *
  167. * @static
  168. * @param {*} url
  169. * @memberof DWTool
  170. */
  171. static loadSpriteFrame(url) {
  172. return new Promise((resolve, reject) => {
  173. cc.loader.load(url, function (err, texture) {
  174. if (err) {
  175. reject(err);
  176. } else {
  177. let frame = new cc.SpriteFrame(texture);
  178. resolve(frame);
  179. }
  180. });
  181. })
  182. }
  183. static getGenderIconPath(gender) {
  184. return new Promise((resolve, reject) => {
  185. // if (gender === 0 || gender === 1) {
  186. let filePath = (gender === 1) ? './textures/icon_male' : './textures/icon_female';
  187. resolve(filePath);
  188. // } else {
  189. // reject();
  190. // }
  191. })
  192. };
  193. // clickOnce(cb) {
  194. // if (this._clickDic === 0) { return; }
  195. // this._clickDic = 0;
  196. // setTimeout(() => {
  197. // this._clickDic = 1;
  198. // }, 1000);
  199. // cb && cb();
  200. // };
  201. static loadResPrefab(path) {
  202. let p = new Promise((resolve, reject) => {
  203. cc.loader.loadRes(path, cc.Prefab, (error, prefab) => {
  204. if (error) {
  205. console.log(error);
  206. reject(error);
  207. } else {
  208. resolve(prefab)
  209. }
  210. });
  211. });
  212. return p;
  213. };
  214. static loadResSpriteAtlasToSpriteFrame(path, subAsset) {
  215. let p = new Promise((resolve, reject) => {
  216. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  217. if (error) {
  218. console.log(error);
  219. reject(error);
  220. } else {
  221. var spriteFrame = atlas.getSpriteFrame(subAsset);
  222. resolve(spriteFrame)
  223. }
  224. });
  225. });
  226. return p;
  227. };
  228. static loadResSpriteAtlas(path) {
  229. let p = new Promise((resolve, reject) => {
  230. cc.loader.loadRes(path, cc.SpriteAtlas, (error, atlas) => {
  231. if (error) {
  232. console.log(error);
  233. reject(error);
  234. } else {
  235. resolve(atlas);
  236. }
  237. });
  238. });
  239. return p;
  240. };
  241. static loadResSpriteFrame(path) {
  242. let p = new Promise((resolve, reject) => {
  243. cc.loader.loadRes(path, cc.SpriteFrame, (error, sf) => {
  244. if (error) {
  245. console.log(error);
  246. reject(error);
  247. } else {
  248. resolve(sf);
  249. }
  250. });
  251. });
  252. return p;
  253. }
  254. static loadResPrefabArray(pathArr) {
  255. let p = new Promise((resolve, reject) => {
  256. cc.loader.loadResArray(pathArr, cc.Prefab, (error, resourceArr) => {
  257. if (error) {
  258. console.log(error);
  259. reject(error);
  260. } else {
  261. resolve(resourceArr);
  262. }
  263. });
  264. });
  265. return p;
  266. }
  267. //时间戳格式化为对应文字时间显示
  268. static timeParse(timestamp) {
  269. function zeroize(num) {
  270. return (String(num).length == 1 ? '0' : '') + num;
  271. }
  272. var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
  273. var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
  274. var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
  275. var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
  276. var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
  277. var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
  278. if (timestampDiff < 60) { // 一分钟以内
  279. return "刚刚";
  280. } else if (timestampDiff < 3600) { // 一小时前之内
  281. return Math.floor(timestampDiff / 60) + "分钟前";
  282. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  283. return '今天' + H + ':' + zeroize(i);
  284. } else {
  285. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  286. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  287. return '昨天' + H + ':' + zeroize(i);
  288. } else if (curDate.getFullYear() == Y) {
  289. return m + '月' + d + '日 ' + H + ':' + zeroize(i);
  290. } else {
  291. return Y + '年' + m + '月' + d + '日 ' + H + ':' + zeroize(i);
  292. }
  293. }
  294. };
  295. /**
  296. * 将时间戳转成00:00显示
  297. * @param {floor} t
  298. */
  299. static calculateTime(t) {
  300. // 好像经常会出现时间格式化出错的问题, 这里先这样解决一下
  301. if (t < 0) {
  302. t = 0;
  303. console.log("出现负数的时间");
  304. }
  305. let hour = Math.floor(t / 60 / 60);
  306. let minute = Math.floor(t / 60 % 60);
  307. let second = Math.floor(t % 60);
  308. if (hour != 0 && hour < 10) {
  309. hour = "0" + hour;
  310. }
  311. if (minute < 10) {
  312. minute = "0" + minute;
  313. }
  314. if (second < 10) {
  315. second = "0" + second;
  316. }
  317. if (hour != 0) {
  318. return `${hour}:${minute}:${second}`;
  319. } else {
  320. return `${minute}:${second}`;
  321. }
  322. };
  323. static calculateTimeString(t) {
  324. if (t < 0) {
  325. t = 0;
  326. console.log("出现负数的时间");
  327. }
  328. let hour = Math.floor(t / 60 / 60);
  329. let minute = Math.floor(t / 60 % 60);
  330. let second = Math.floor(t % 60);
  331. if (hour != 0 && hour < 10) {
  332. hour = "0" + hour;
  333. }
  334. if (minute < 10) {
  335. minute = "0" + minute;
  336. }
  337. if (second < 10) {
  338. second = "0" + second;
  339. }
  340. if (hour != 0) {
  341. return `${hour}小时${minute}分${second}秒`;
  342. } else {
  343. return `${minute}分${second}秒`;
  344. }
  345. };
  346. /**
  347. * 获取Url上指定参数
  348. * @param {String} name 参数名
  349. */
  350. static getUrlParam(name) {
  351. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  352. var r = window.location.search.substr(1).match(reg);
  353. if (r != null) {
  354. return unescape(r[2]);
  355. } else {
  356. return null;
  357. }
  358. }
  359. /**
  360. * 判断当前用户是否走完引导教程
  361. */
  362. static checkIsOldUser() {
  363. // debugger;
  364. if (GameModule.userInfo && GameModule.userInfo.buildingLevel && GameModule.userInfo.buyStarCount) {
  365. // 1. 总部大楼大于25级
  366. // 2. 已拥有1个明星
  367. let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25;
  368. let unLockStatus2 = GameModule.userInfo.buyStarCount > 0;
  369. if (unLockStatus1 && unLockStatus2) {
  370. return true;
  371. } else {
  372. return false;
  373. }
  374. } else {
  375. return false;
  376. }
  377. }
  378. static isDot(num) {
  379. var result = (num.toString()).indexOf(".");
  380. if(result != -1) {
  381. return true;
  382. } else {
  383. return false;
  384. }
  385. }
  386. }
  387. module.exports = DWTool;