UserInfoJS.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/shareImage2.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(value) {
  27. if(CC_QQPLAY) {
  28. BK.localStorage.setItem(key, value.toString())
  29. } else {
  30. cc.sys.localStorage.setItem(key, value.toString())
  31. }
  32. }
  33. },
  34. // 获取每周排行榜分数
  35. getScore: function () {
  36. let score = this.getItem('score');
  37. return score ? parseInt(score) : 0;
  38. },
  39. setScore: function (value) {
  40. this.setItem('score', "" + value);
  41. },
  42. getCurrLocalversion: function () {
  43. if (this.IsEmpty(this.localversion))
  44. return "";
  45. return this.localversion;
  46. },
  47. setLocalversion: function (version) {
  48. this.setItem("localversion", "" + version);
  49. },
  50. getLocalversion: function () {
  51. let version = this.getItem('localversion');
  52. if (this.IsEmpty(version)) {
  53. this.setItem('localversion', '');
  54. version = '';
  55. }
  56. return version;
  57. },
  58. addScore: function (value) {
  59. let score = this.getItem(this.getCurWeekScoreValue());
  60. let inputScore = (score ? parseInt(score) : 0) + parseInt(value) + "";
  61. this.setItem(this.getCurWeekScoreValue(), inputScore);
  62. },
  63. /*
  64. 金币
  65. */
  66. getGold: function () {
  67. let gold = this.getItem('gold');
  68. return gold ? parseInt(gold) : 0;
  69. },
  70. setGold: function (value) {
  71. this.setItem('gold', "" + value);
  72. },
  73. addGold: function (value) {
  74. let gold = this.getItem('gold');
  75. let inputGold = (gold ? parseInt(gold) : 0) + parseInt(value) + "";
  76. this.setItem('gold', inputGold);
  77. this.addTotalGold(value);
  78. },
  79. addTotalGold: function (value) {
  80. let goldTotal = this.getItem('goldTotal');
  81. let inputGold = (goldTotal ? parseInt(goldTotal) : 0) + parseInt(value) + "";
  82. this.setItem('goldTotal', inputGold);
  83. },
  84. getTotalGold: function () {
  85. let goldTotal = this.getItem('goldTotal');
  86. return goldTotal ? parseInt(goldTotal) : 0;
  87. },
  88. addStageProp: function(value) {//累计道具数
  89. let propcnt = this.getItem('propcnt');
  90. let inputCnt = (propcnt ? parseInt(propcnt) : 0) + parseInt(value) + "";
  91. this.setItem('propcnt', inputCnt);
  92. },
  93. setStageProp: function(value) {//累计道具数
  94. let inputCnt = parseInt(value) + "";
  95. this.setItem('propcnt', inputCnt);
  96. },
  97. getStageProp: function () {
  98. let propcnt = this.getItem('propcnt');
  99. return propcnt ? parseInt(propcnt) : 0;
  100. },
  101. getCurrentLevel: function () {
  102. let level = this.getItem('levelSign');
  103. if (this.IsEmpty(level)) {
  104. this.setItem('levelSign', '0');
  105. level = '0';
  106. }
  107. return parseInt(level);
  108. },
  109. setCurrentLevel: function (value) {
  110. this.setItem('levelSign', value + "");
  111. },
  112. setFlyCnt: function(cnt) {
  113. this.setItem('flycnt', cnt + "");
  114. },
  115. getFlyCnt: function() {
  116. let level = this.getItem('flycnt');
  117. if (this.IsEmpty(level)) {
  118. this.setItem('flycnt', '0');
  119. level = '0';
  120. }
  121. return parseInt(level);
  122. },
  123. setSkinIndex: function(skinIndex){
  124. this.setItem('skinIndex',skinIndex + "");
  125. },
  126. getSkinIndex: function()
  127. {
  128. let skinIndex = this.getItem('skinIndex');
  129. if (this.IsEmpty(skinIndex))
  130. {
  131. this.setItem('skinIndex','0');
  132. skinIndex = '0';
  133. }
  134. return parseInt(skinIndex);
  135. },
  136. /*
  137. 当前等级
  138. */
  139. getCurrentPlane: function () {
  140. let level = this.getItem('level');
  141. if (this.IsEmpty(level)) {
  142. this.setItem('level', '1');
  143. level = '1';
  144. }
  145. var intLevel = parseInt(level);
  146. if (intLevel <= 0) {
  147. intLevel = 1;
  148. }
  149. return intLevel;
  150. },
  151. setCurrentPlane: function (value) {
  152. this.setItem('level', value + "");
  153. },
  154. getLevelStage: function () {
  155. let level = this.getItem('levelStage');
  156. if (this.IsEmpty(level)) {
  157. this.setItem('levelStage', '');
  158. level = '';
  159. }
  160. return level;
  161. },
  162. setLevelStage: function (value) {
  163. this.setItem('levelStage', value + "");
  164. },
  165. /*
  166. 设置自定义数据
  167. */
  168. setString: function (str, value) {
  169. this.setItem(str, "" + value);
  170. },
  171. getString: function (str) {
  172. return this.getItem(str);
  173. },
  174. addString: function (str, value) {
  175. let data = this.getItem(str);
  176. let inputData = (data ? parseInt(data) : 0) + parseInt(value) + "";
  177. this.setItem(str, inputData);
  178. },
  179. removeString: function (str) {
  180. cc.sys.localStorage.removeItem(str);
  181. },
  182. IsEmpty(obj) {//判断是否为空
  183. if (obj == "undefined" || obj == null || obj == "") {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. },
  189. getMaxTime() {//历史最长时间
  190. var score = this.getString('maxtime');
  191. if (this.IsEmpty(score)) {
  192. this.setItem('maxtime', "0");
  193. score = "0";
  194. }
  195. return parseInt(score);
  196. },
  197. setMaxTime: function (value) {
  198. this.setItem('maxtime', value+"");
  199. },
  200. getHistoryScore() {//历史最高分
  201. var score = this.getString('historyScore');
  202. if (this.IsEmpty(score)) {
  203. this.setItem('historyScore', "0");
  204. score = "0";
  205. }
  206. return parseInt(score);
  207. },
  208. // 获取用户保存在QQ轻游戏云端的数据
  209. getUserGameData(callback) {
  210. if(CC_QQPLAY) {
  211. BK.QQ.loadGameData(function(errCode, cmd, data) {
  212. // BK.UI.showAlert({
  213. // title: 'loadGameData',
  214. // content: JSON.stringify(data)
  215. // })
  216. callback && callback(data)
  217. });
  218. } else {
  219. callback && callback({
  220. maxScore: 0
  221. })
  222. }
  223. },
  224. // 设置用户在QQ轻游戏的云端数据
  225. setUserGameData(score) {
  226. var data = {
  227. maxScore: score
  228. }
  229. if(CC_QQPLAY) {
  230. // 保存个人数据
  231. BK.QQ.saveGameData(data, function(errCode, cmd, data) {
  232. // BK.UI.showAlert({
  233. // title: 'saveGameData',
  234. // content: JSON.stringify(data)
  235. // })
  236. });
  237. }
  238. },
  239. getCurWeekScoreValue() {
  240. return this.getNowFormatDate();
  241. },
  242. getNowFormatDate() {
  243. var date = new Date();
  244. var seperator1 = "-";
  245. var year = date.getFullYear();
  246. // var month = date.getMonth() + 1;
  247. // if (month >= 1 && month <= 9) {
  248. // month = "0" + month;
  249. // }
  250. var week = this.Getweek();
  251. var currentdate = year + seperator1 + week;
  252. return currentdate;
  253. },
  254. getMonthWeek() {
  255. var date = new Date(),
  256. w = date.getDay(),
  257. d = date.getDate();
  258. return Math.ceil(
  259. (d + 7 - w) / 7
  260. );
  261. },
  262. Getweek(dateString) {
  263. var da = '';
  264. if (dateString == undefined) {
  265. var now = new Date();
  266. var now_m = now.getMonth() + 1;
  267. now_m = (now_m < 10) ? '0' + now_m : now_m;
  268. var now_d = now.getDate();
  269. now_d = (now_d < 10) ? '0' + now_d : now_d;
  270. da = now.getFullYear() + '-' + now_m + '-' + now_d;
  271. } else {
  272. da = dateString; //日期格式2015-12-30
  273. }
  274. var date1 = new Date(da.substring(0, 4), parseInt(da.substring(5, 7)) - 1, da.substring(8, 10)); //当前日期
  275. var date2 = new Date(da.substring(0, 4), 0, 1); //1月1号
  276. //获取1月1号星期(以周一为第一天,0周一~6周日)
  277. var dateWeekNum = date2.getDay() - 1;
  278. if (dateWeekNum < 0) {
  279. dateWeekNum = 6;
  280. }
  281. if (dateWeekNum < 4) {
  282. //前移日期
  283. date2.setDate(date2.getDate() - dateWeekNum);
  284. } else {
  285. //后移日期
  286. date2.setDate(date2.getDate() + 7 - dateWeekNum);
  287. }
  288. var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
  289. var year = date1.getFullYear();
  290. var week = Math.ceil((d + 1) / 7);
  291. return week;
  292. },
  293. showIndexBanner() {
  294. this.indexBanner = BK.Advertisement.createBannerAd({
  295. viewId:1001,
  296. style: {x: 0, y: 0}
  297. });
  298. this.indexBanner.show()
  299. },
  300. destroyIndexBanner() {
  301. try{
  302. this.indexBanner.destory()
  303. } catch(e) {}
  304. },
  305. showDeadBanner() {
  306. this.deadBanner = BK.Advertisement.createBannerAd({
  307. viewId:1002,
  308. style: {x: 0, y: 0}
  309. });
  310. this.deadBanner.show()
  311. },
  312. destroyDeadBanner() {
  313. try{
  314. this.deadBanner.destory()
  315. } catch(e) {}
  316. },
  317. };