DWTool.js 11 KB

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