1
0

DWTool.js 11 KB

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