UserInfoJS.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Global UserInfo
  3. */
  4. window.UserInfo = {
  5. /*
  6. 积分
  7. */
  8. shareLimit : 3, //分享次数限制
  9. shareAddGold : 10, //分享增加金币数
  10. platform : CC_WECHATGAME ? 1 : 0, //1为微信小游戏模式
  11. shareUrl : 'https://pub.dwstatic.com/wxgame/budingGame/share/shareImage.jpg',//分享图片地址
  12. shareDesc : '超级可爱的布丁,快来跟我一起玩吧',
  13. shareDescChanllenge : '顽强的布丁',
  14. wxversion : '6.6.3',
  15. localversion : '1.0.1',
  16. ewmurl: 'https://pub.dwstatic.com/wxgame/escape/share/2weima.jpg',//公众号二维码
  17. GameCount : 2,
  18. getItem (key) {
  19. if(CC_QQPLAY) {
  20. return BK.localStorage.getItem(key)
  21. } else {
  22. return cc.sys.localStorage.getItem(key)
  23. }
  24. },
  25. setItem (key, value) {
  26. if(CC_QQPLAY) {
  27. BK.localStorage.setItem(key, value)
  28. } else {
  29. cc.sys.localStorage.setItem(key, value)
  30. }
  31. },
  32. // 获取每周排行榜分数
  33. getScore: function () {
  34. let score = this.getItem('score');
  35. return score ? parseInt(score) : 0;
  36. },
  37. setScore: function (value) {
  38. this.setItem('score', "" + value);
  39. },
  40. getCurrLocalversion: function () {
  41. if (this.IsEmpty(this.localversion))
  42. return "";
  43. return this.localversion;
  44. },
  45. setLocalversion: function (version) {
  46. this.setItem("localversion", "" + version);
  47. },
  48. getLocalversion: function () {
  49. let version = this.getItem('localversion');
  50. if (this.IsEmpty(version)) {
  51. this.setItem('localversion', '');
  52. version = '';
  53. }
  54. return version;
  55. },
  56. addScore: function (value) {
  57. let score = this.getItem(this.getCurWeekScoreValue());
  58. let inputScore = (score ? parseInt(score) : 0) + parseInt(value) + "";
  59. this.setItem(this.getCurWeekScoreValue(), inputScore);
  60. },
  61. /*
  62. 金币
  63. */
  64. getGold: function () {
  65. let gold = this.getItem('gold');
  66. return gold ? parseInt(gold) : 0;
  67. },
  68. setGold: function (value) {
  69. this.setItem('gold', "" + value);
  70. },
  71. addGold: function (value) {
  72. let gold = this.getItem('gold');
  73. let inputGold = (gold ? parseInt(gold) : 0) + parseInt(value) + "";
  74. this.setItem('gold', inputGold);
  75. this.addTotalGold(value);
  76. },
  77. addTotalGold: function (value) {
  78. let goldTotal = this.getItem('goldTotal');
  79. let inputGold = (goldTotal ? parseInt(goldTotal) : 0) + parseInt(value) + "";
  80. this.setItem('goldTotal', inputGold);
  81. },
  82. getTotalGold: function () {
  83. let goldTotal = this.getItem('goldTotal');
  84. return goldTotal ? parseInt(goldTotal) : 0;
  85. },
  86. addStageProp: function(value) {//累计道具数
  87. let propcnt = this.getItem('propcnt');
  88. let inputCnt = (propcnt ? parseInt(propcnt) : 0) + parseInt(value) + "";
  89. this.setItem('propcnt', inputCnt);
  90. },
  91. setStageProp: function(value) {//累计道具数
  92. let inputCnt = parseInt(value) + "";
  93. this.setItem('propcnt', inputCnt);
  94. },
  95. getStageProp: function () {
  96. let propcnt = this.getItem('propcnt');
  97. return propcnt ? parseInt(propcnt) : 0;
  98. },
  99. getCurrentLevel: function () {
  100. let level = this.getItem('levelSign');
  101. if (this.IsEmpty(level)) {
  102. this.setItem('levelSign', '0');
  103. level = '0';
  104. }
  105. return parseInt(level);
  106. },
  107. setCurrentLevel: function (value) {
  108. this.setItem('levelSign', value + "");
  109. },
  110. setFlyCnt: function(cnt) {
  111. this.setItem('flycnt', cnt + "");
  112. },
  113. getFlyCnt: function() {
  114. let level = this.getItem('flycnt');
  115. if (this.IsEmpty(level)) {
  116. this.setItem('flycnt', '0');
  117. level = '0';
  118. }
  119. return parseInt(level);
  120. },
  121. setSkinIndex: function(skinIndex){
  122. this.setItem('skinIndex',skinIndex + "");
  123. },
  124. getSkinIndex: function()
  125. {
  126. let skinIndex = this.getItem('skinIndex');
  127. if (this.IsEmpty(skinIndex))
  128. {
  129. this.setItem('skinIndex','0');
  130. skinIndex = '0';
  131. }
  132. return parseInt(skinIndex);
  133. },
  134. /*
  135. 当前等级
  136. */
  137. getCurrentPlane: function () {
  138. let level = this.getItem('level');
  139. if (this.IsEmpty(level)) {
  140. this.setItem('level', '1');
  141. level = '1';
  142. }
  143. var intLevel = parseInt(level);
  144. if (intLevel <= 0) {
  145. intLevel = 1;
  146. }
  147. return intLevel;
  148. },
  149. setCurrentPlane: function (value) {
  150. this.setItem('level', value + "");
  151. },
  152. getLevelStage: function () {
  153. let level = this.getItem('levelStage');
  154. if (this.IsEmpty(level)) {
  155. this.setItem('levelStage', '');
  156. level = '';
  157. }
  158. return level;
  159. },
  160. setLevelStage: function (value) {
  161. this.setItem('levelStage', value + "");
  162. },
  163. /*
  164. 设置自定义数据
  165. */
  166. setString: function (str, value) {
  167. this.setItem(str, "" + value);
  168. },
  169. getString: function (str) {
  170. return this.getItem(str);
  171. },
  172. addString: function (str, value) {
  173. let data = this.getItem(str);
  174. let inputData = (data ? parseInt(data) : 0) + parseInt(value) + "";
  175. this.setItem(str, inputData);
  176. },
  177. removeString: function (str) {
  178. cc.sys.localStorage.removeItem(str);
  179. },
  180. IsEmpty(obj) {//判断是否为空
  181. if (obj == "undefined" || obj == null || obj == "") {
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. },
  187. getMaxTime() {//历史最长时间
  188. var score = this.getString('maxtime');
  189. if (this.IsEmpty(score)) {
  190. this.setItem('maxtime', "0");
  191. score = "0";
  192. }
  193. return parseInt(score);
  194. },
  195. setMaxTime: function (value) {
  196. this.setItem('maxtime', value+"");
  197. },
  198. getHistoryScore() {//历史最高分
  199. var score = this.getString('historyScore');
  200. if (this.IsEmpty(score)) {
  201. this.setItem('historyScore', "0");
  202. score = "0";
  203. }
  204. return parseInt(score);
  205. },
  206. // 获取用户保存在QQ轻游戏云端的数据
  207. getUserGameData(callback) {
  208. if(CC_QQPLAY) {
  209. BK.QQ.loadGameData(function(errCode, cmd, data) {
  210. // BK.UI.showAlert({
  211. // title: 'loadGameData',
  212. // content: JSON.stringify(data)
  213. // })
  214. callback && callback(data)
  215. });
  216. } else {
  217. callback && callback({
  218. maxScore: 0
  219. })
  220. }
  221. },
  222. // 设置用户在QQ轻游戏的云端数据
  223. setUserGameData(score) {
  224. var data = {
  225. maxScore: score
  226. }
  227. if(CC_QQPLAY) {
  228. // 保存个人数据
  229. BK.QQ.saveGameData(data, function(errCode, cmd, data) {
  230. // BK.UI.showAlert({
  231. // title: 'saveGameData',
  232. // content: JSON.stringify(data)
  233. // })
  234. });
  235. }
  236. },
  237. getCurWeekScoreValue() {
  238. return this.getNowFormatDate();
  239. },
  240. getNowFormatDate() {
  241. var date = new Date();
  242. var seperator1 = "-";
  243. var year = date.getFullYear();
  244. // var month = date.getMonth() + 1;
  245. // if (month >= 1 && month <= 9) {
  246. // month = "0" + month;
  247. // }
  248. var week = this.Getweek();
  249. var currentdate = year + seperator1 + week;
  250. return currentdate;
  251. },
  252. getMonthWeek() {
  253. var date = new Date(),
  254. w = date.getDay(),
  255. d = date.getDate();
  256. return Math.ceil(
  257. (d + 7 - w) / 7
  258. );
  259. },
  260. Getweek(dateString) {
  261. var da = '';
  262. if (dateString == undefined) {
  263. var now = new Date();
  264. var now_m = now.getMonth() + 1;
  265. now_m = (now_m < 10) ? '0' + now_m : now_m;
  266. var now_d = now.getDate();
  267. now_d = (now_d < 10) ? '0' + now_d : now_d;
  268. da = now.getFullYear() + '-' + now_m + '-' + now_d;
  269. } else {
  270. da = dateString; //日期格式2015-12-30
  271. }
  272. var date1 = new Date(da.substring(0, 4), parseInt(da.substring(5, 7)) - 1, da.substring(8, 10)); //当前日期
  273. var date2 = new Date(da.substring(0, 4), 0, 1); //1月1号
  274. //获取1月1号星期(以周一为第一天,0周一~6周日)
  275. var dateWeekNum = date2.getDay() - 1;
  276. if (dateWeekNum < 0) {
  277. dateWeekNum = 6;
  278. }
  279. if (dateWeekNum < 4) {
  280. //前移日期
  281. date2.setDate(date2.getDate() - dateWeekNum);
  282. } else {
  283. //后移日期
  284. date2.setDate(date2.getDate() + 7 - dateWeekNum);
  285. }
  286. var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
  287. var year = date1.getFullYear();
  288. var week = Math.ceil((d + 1) / 7);
  289. return week;
  290. },
  291. };