import SystemTools from "../../game/utils/SystemTools";
export default class WxSdk
{
private tempCode:string = ""; //临时登录code
private appid:string = "填写小游戏的APPID"; // 小程序 appId
private secret:string = "填写小游戏的秘钥"; // 小程序 appSecret
private grant_type:string = "authorization_code"; //填写为 authorization_code
private openId:string = ""; // 用户唯一id
private session_key:string = ""; //会话密钥
public nickName:string = ""; //名字
public avatarUrl:string = ""; //头像url
public login():void
{
this.checkSession();
}
/**
* 登录微信
*/
private loginWx():void
{
let obj:any =
{
success : (code:string)=>
{
this.tempCode = code;
this.getAccessToken();
},
fail:()=>
{
console.error(">>>>>登录失败");
}
};
wx.login(obj);
}
/**
* 检查当前登录状态是否过期
*/
private checkSession():void
{
let obj:any =
{
success : ()=>
{
//"登录还是有效的,不需要重新登录");
this.getUserInfo();
},
fail:()=>
{
//">>>>>登录状态过期了,需要重新登录");
this.loginWx();
}
};
wx.checkSession(obj);
}
/**
* 登入凭证校验
*/
private getAccessToken():void
{
let url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type={3}";
url = SystemTools.format(url,this.appid,this.secret,this.tempCode,this.grant_type); //拼接网址
let success = (res:any)=>{
let data = res.data;
this.openId = data.openid; //用户唯一标识
this.session_key = data.session_key; //回话密匙
let errcode:number = data.errcode; //错误码
let errMsg:string = data.errMsg; //错误信息
if (errcode == -1)
{
console.error(">>>系统繁忙,此时请开发者稍候再试");
}else if (errcode == 0)
{
//">>>请求成功");
this.getUserInfo();
}else if (errcode == 40029)
{
//">>>>code无效");
this.getUserInfo();
}
};
let fail = ()=>
{
console.error(">>>>>>>获取登录凭证失败");
}
this.request(url,success,fail);
}
/**
* 微信请求
* @param url 请求地址
* @param successCallBack 成功回调
* @param failBack 失败回调
*/
private request(url:string,successCallBack:(res:any)=>void,failBack:()=>void):void
{
let requestData:any =
{
url:url,
method:"GET",
dataType:"json",
success:(res:any)=>
{
if (successCallBack != null)
{
successCallBack(res);
}
},
fail:()=>
{
if (failBack != null)
{
failBack();
}
console.error(">>>>>>请求url失败:"+requestData.url);
}
};
let wxtem:any = wx;
wxtem.request(requestData);
}
/**
* 获取用户信息
*/
private getUserInfo():void
{
let data:any =
{
withCredentials:false,
lang:"zh_CN",
success:(res:any)=>
{
let userInfo:any = res.userInfo;
this.nickName = userInfo.nickName; //用户昵称
this.avatarUrl = userInfo.avatarUrl; //用户头像图片 url
let gender:number = userInfo.gender //性别 0:未知、1:男、2:女
let province:string = userInfo.province; //用户所在省份
let city:string = userInfo.city; //用户所在城市
let country:string = userInfo.country; // 用户所在国家
},
fail:()=>
{
this.checkSession(); //获取用户信息失败之后 继续去获取
console.error(">>>>获取用户信息失败:"+this.tempCode);
}
}
wx.getUserInfo(data);
}
}
SystemTools.format代码为:
public static format(str:string,...args:any[]):string
{
let result = str;
if(args.length > 0)
{
let value;
for (var key in args)
{
value = args[key];
if(value != null && value.length != 0)
{
var reg = new RegExp(`\\{${key}\\}`,"g");
result = result.replace(reg, value);
}
}
}
return result;
}
用法示例:
let wxSkd:WxSdk = new WxSdk();
wxSkd.login();
本文详细介绍了如何在CocosCreator游戏中接入微信登录功能,通过SystemTools.format代码实现获取并展示用户头像和昵称的完整流程,帮助开发者实现与微信社交平台的无缝对接。
7060

被折叠的 条评论
为什么被折叠?



