| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- cc.Class({
- extends: cc.Component,
- properties: {
- LoadingNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- StartButton: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- },
- start () {
- // this.Host = 'https://www.9527fun.cn/Idol/';
- this.LoadingNode.active = false;
- //如果不是微信环境
- if (typeof wx === 'undefined') {
- return;
- }
- // this.CreateAuthorizeBtn(this.StartButton);
- },
- OnStartButtonClick:function () {
- this.LoadingNode.active = true;
- this.StartButton.active = false;
- // var animCtrl = this.node.getComponent(cc.Animation);
- // animCtrl.play("linear");
- this.LoadingNode.getChildByName('label').getComponent(cc.Animation).play();
- this.LoadingNode.getChildByName('SparklersParticle').getComponent(cc.Animation).play();
- // let URL = this.Host+'UserInfo/CheckUserInfo';
- // let Data = 'NickName=TestUser'+'&'+'AvatarURL=https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTI8a5gibDPIrdlScaNkw6jaGQd1kalEPHBKib2J9H8pKOfJszSb0sIIspWKgGL6Fz2LLIr2WicFvXPSg/132';
- // this.SendPostUserInfo(URL,Data);
- cc.director.loadScene("Game");
- },
- CreateAuthorizeBtn(BtnNode) {
- let FrameSize = cc.view.getFrameSize();
- let WinSize = cc.director.getWinSize();
- // get btn size
- let BtnSize = cc.size(BtnNode.width+10,BtnNode.height+10);
- // console.log("winSize: ",winSize);
- // console.log("frameSize: ",frameSize);
- //适配不同机型来创建微信授权按钮 get position
- let Left = (WinSize.width*0.5+BtnNode.x-BtnNode.width*0.5)/WinSize.width*FrameSize.width;
- let Top = (WinSize.height*0.5-BtnNode.y-BtnNode.height*0.5)/WinSize.height*FrameSize.height;
- let Width = BtnNode.width/WinSize.width*FrameSize.width;
- let Height = BtnNode.height/WinSize.height*FrameSize.height;
- // console.log("button pos: ",cc.v2(left,top));
- // console.log("button size: ",cc.size(width,height));
- //let Self = this;
- this.BtnAuthorize = wx.createUserInfoButton({
- type: 'text',
- text: '',
- style: {
- left: Left,
- top: Top,
- width: Width,
- height: Height
- }
- });
- this.BtnAuthorize.onTap((Uinfo) => {
- // console.log("onTap uinfo: ",Uinfo);
- if (Uinfo.userInfo) {
- // console.log("wxLogin auth success:",Uinfo.userInfo);
- console.log("avatarUrl: ",Uinfo.userInfo.avatarUrl);
- // let aNickName = Uinfo.userInfo.nickName.replace(/[^0-9a-z\u4e00-\u9fa5]/ig,"口");
- let aNickName = Uinfo.userInfo.nickName
- var strArr = aNickName.split(""),
- result = "",
- totalLen = 0;
- for(var idx = 0; idx < strArr.length; idx ++) {
- // 超出长度,退出程序
- if(totalLen >= 16) break;
- var val = strArr[idx];
- // 英文,增加长度1
- if(/[a-zA-Z]/.test(val)) {
- totalLen = 1 + (+totalLen);
- result += val;
- }
- // 中文,增加长度2
- else if(/[\u4e00-\u9fa5]/.test(val)) {
- totalLen = 2 + (+totalLen);
- result += val;
- }
- // 遇到代理字符,将其转换为 "口", 不增加长度
- else if(/[\ud800-\udfff]/.test(val)) {
- // 代理对长度为2,
- if(/[\ud800-\udfff]/.test(strArr[idx + 1])) {
- // 跳过下一个
- idx ++;
- }
- // 将代理对替换为 "口"
- result += "口";
- }
- };
- console.log("nickName: ",result);
- cc.sys.localStorage.setItem('nickName', result);
- // cc.sys.localStorage.setItem('nickName', aNickName);
- cc.sys.localStorage.setItem('avatarUrl', Uinfo.userInfo.avatarUrl);
- this.LoadingNode.active = true;
- this.StartButton.active = false;
- // var animCtrl = this.node.getComponent(cc.Animation);
- // animCtrl.play("linear");
- this.LoadingNode.getChildByName('label').getComponent(cc.Animation).play();
- this.LoadingNode.getChildByName('SparklersParticle').getComponent(cc.Animation).play();
- let URL = this.Host+'UserInfo/Login/';
- // let Data = 'NickName='+Uinfo.userInfo.nickName+'&'+'AvatarURL='+Uinfo.userInfo.avatarUrl;
- this.SendUserInfo(URL,Uinfo.userInfo.nickName,Uinfo.userInfo.avatarUrl);
- }else {
- console.log("wxLogin auth fail");
- }
- });
- },
- SendUserInfo(URL,NickName,AvatarUrl)
- {
- wx.login({
- //获取code
- success: function(res) {
- let Code = res.code;//返回code
- // console.log("返回code:", code);
- let Data = 'Code='+Code+'&'+'NickName='+NickName+'&'+'AvatarURL='+AvatarUrl;
- // console.log("AvatarUrl", AvatarUrl);
- let xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
- let response = xhr.responseText;
- console.log("http data返回:", response);
- cc.sys.localStorage.setItem('Openid', xhr.responseText);
- cc.director.loadScene("Game");
- }
- };
- xhr.open("POST", URL, true);
- xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- xhr.send(Data);
- }
- });
- }
- });
|