Loading.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. LoadingNode: {
  5. default: null,
  6. type: cc.Node,
  7. serializable: true,
  8. },
  9. StartButton: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. },
  15. start () {
  16. // this.Host = 'https://www.9527fun.cn/Idol/';
  17. this.LoadingNode.active = false;
  18. //如果不是微信环境
  19. if (typeof wx === 'undefined') {
  20. return;
  21. }
  22. // this.CreateAuthorizeBtn(this.StartButton);
  23. },
  24. OnStartButtonClick:function () {
  25. this.LoadingNode.active = true;
  26. this.StartButton.active = false;
  27. // var animCtrl = this.node.getComponent(cc.Animation);
  28. // animCtrl.play("linear");
  29. this.LoadingNode.getChildByName('label').getComponent(cc.Animation).play();
  30. this.LoadingNode.getChildByName('SparklersParticle').getComponent(cc.Animation).play();
  31. // let URL = this.Host+'UserInfo/CheckUserInfo';
  32. // let Data = 'NickName=TestUser'+'&'+'AvatarURL=https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTI8a5gibDPIrdlScaNkw6jaGQd1kalEPHBKib2J9H8pKOfJszSb0sIIspWKgGL6Fz2LLIr2WicFvXPSg/132';
  33. // this.SendPostUserInfo(URL,Data);
  34. cc.director.loadScene("Game");
  35. },
  36. CreateAuthorizeBtn(BtnNode) {
  37. let FrameSize = cc.view.getFrameSize();
  38. let WinSize = cc.director.getWinSize();
  39. // get btn size
  40. let BtnSize = cc.size(BtnNode.width+10,BtnNode.height+10);
  41. // console.log("winSize: ",winSize);
  42. // console.log("frameSize: ",frameSize);
  43. //适配不同机型来创建微信授权按钮 get position
  44. let Left = (WinSize.width*0.5+BtnNode.x-BtnNode.width*0.5)/WinSize.width*FrameSize.width;
  45. let Top = (WinSize.height*0.5-BtnNode.y-BtnNode.height*0.5)/WinSize.height*FrameSize.height;
  46. let Width = BtnNode.width/WinSize.width*FrameSize.width;
  47. let Height = BtnNode.height/WinSize.height*FrameSize.height;
  48. // console.log("button pos: ",cc.v2(left,top));
  49. // console.log("button size: ",cc.size(width,height));
  50. //let Self = this;
  51. this.BtnAuthorize = wx.createUserInfoButton({
  52. type: 'text',
  53. text: '',
  54. style: {
  55. left: Left,
  56. top: Top,
  57. width: Width,
  58. height: Height
  59. }
  60. });
  61. this.BtnAuthorize.onTap((Uinfo) => {
  62. // console.log("onTap uinfo: ",Uinfo);
  63. if (Uinfo.userInfo) {
  64. // console.log("wxLogin auth success:",Uinfo.userInfo);
  65. console.log("avatarUrl: ",Uinfo.userInfo.avatarUrl);
  66. // let aNickName = Uinfo.userInfo.nickName.replace(/[^0-9a-z\u4e00-\u9fa5]/ig,"口");
  67. let aNickName = Uinfo.userInfo.nickName
  68. var strArr = aNickName.split(""),
  69. result = "",
  70. totalLen = 0;
  71. for(var idx = 0; idx < strArr.length; idx ++) {
  72. // 超出长度,退出程序
  73. if(totalLen >= 16) break;
  74. var val = strArr[idx];
  75. // 英文,增加长度1
  76. if(/[a-zA-Z]/.test(val)) {
  77. totalLen = 1 + (+totalLen);
  78. result += val;
  79. }
  80. // 中文,增加长度2
  81. else if(/[\u4e00-\u9fa5]/.test(val)) {
  82. totalLen = 2 + (+totalLen);
  83. result += val;
  84. }
  85. // 遇到代理字符,将其转换为 "口", 不增加长度
  86. else if(/[\ud800-\udfff]/.test(val)) {
  87. // 代理对长度为2,
  88. if(/[\ud800-\udfff]/.test(strArr[idx + 1])) {
  89. // 跳过下一个
  90. idx ++;
  91. }
  92. // 将代理对替换为 "口"
  93. result += "口";
  94. }
  95. };
  96. console.log("nickName: ",result);
  97. cc.sys.localStorage.setItem('nickName', result);
  98. // cc.sys.localStorage.setItem('nickName', aNickName);
  99. cc.sys.localStorage.setItem('avatarUrl', Uinfo.userInfo.avatarUrl);
  100. this.LoadingNode.active = true;
  101. this.StartButton.active = false;
  102. // var animCtrl = this.node.getComponent(cc.Animation);
  103. // animCtrl.play("linear");
  104. this.LoadingNode.getChildByName('label').getComponent(cc.Animation).play();
  105. this.LoadingNode.getChildByName('SparklersParticle').getComponent(cc.Animation).play();
  106. let URL = this.Host+'UserInfo/Login/';
  107. // let Data = 'NickName='+Uinfo.userInfo.nickName+'&'+'AvatarURL='+Uinfo.userInfo.avatarUrl;
  108. this.SendUserInfo(URL,Uinfo.userInfo.nickName,Uinfo.userInfo.avatarUrl);
  109. }else {
  110. console.log("wxLogin auth fail");
  111. }
  112. });
  113. },
  114. SendUserInfo(URL,NickName,AvatarUrl)
  115. {
  116. wx.login({
  117. //获取code
  118. success: function(res) {
  119. let Code = res.code;//返回code
  120. // console.log("返回code:", code);
  121. let Data = 'Code='+Code+'&'+'NickName='+NickName+'&'+'AvatarURL='+AvatarUrl;
  122. // console.log("AvatarUrl", AvatarUrl);
  123. let xhr = new XMLHttpRequest();
  124. xhr.onreadystatechange = function () {
  125. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  126. let response = xhr.responseText;
  127. console.log("http data返回:", response);
  128. cc.sys.localStorage.setItem('Openid', xhr.responseText);
  129. cc.director.loadScene("Game");
  130. }
  131. };
  132. xhr.open("POST", URL, true);
  133. xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  134. xhr.send(Data);
  135. }
  136. });
  137. }
  138. });