登录:
1、在微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。
2、通过cordova添加微信插件;进入项目的目录下,运行命令
? ionic cordova plugin add cordova-plugin-wechat ?--variable wechatappid=AppID(就是你申请的AppID)
??? 3、微信需要在编译文件中声明变量,declare let Wechat;
?????????
??? 4、 微信授权登录,也是最重要的一步 ,获取code,为获取access_token提供参数
wechatLogin(){
?????? let loading = this.loadingCtrl.create({
???????????? content: "跳转微信登录中...",//loading框显示的内容
???????????? dismissOnPageChange: true, // 是否在切换页面之后关闭loading框
???????????? showBackdrop: true //是否显示遮罩层
?????? });
????? loading.present();
????? try {
?????????? let scope = "snsapi_userinfo",
?????????? state = "_" + (+new Date());
????????? Wechat.auth(scope, state, (response) => {
???????? ? ? ? ? let res = response;
?? console.log(JSON. stringify ( res ))
???????? ? ? ?? //在调用service的 getAccessToken 方法获取 access_token ,和 acopenid,
//然后再 service的 getWechatUserInfo 方法获取微信用户的基本信息,最后保存登录,完成
???? }, (reason) => {
??????? console.log(reason);
??? ? });
??? } catch (error) {
?????? console.log(error);
??? } finally {
??????? loading.dismiss();
??? }
}
? 5、获取 access_token和 openid ,这个方法接口放在service .ts 文件里, 由ts调用
?????????? getAccessToken(data){
//这里注意参数 grant_type ,官方文档上说的值填写 authorization_code ,实际上就是填写的值是:" authorization_code ";就行,不要理解成其他什么code
let url =" https://api.weixin.qq测试数据/sns/oauth2/access_token?appid=??&secret=??&grant_type=authorization_code";
???????????????? url = url + "&code="+data. code;
??????????????? return this. http. get( url, {}). toPromise(). then(response => {
???????????????????????? return response. json();
?????????????? }). catch(CommonService.handleError);
????????? }
6、 通过 access_token和 openid 获取用户信息, 这个方法接口放在service.ts文件里, 由ts调用
getWechatUserInfo(AccessToken,Openid){
let? url= "https://api.weixin.qq测试数据/sns/userinfo"
url = url + "?access_token="+AccessToken+ "&open>微信开发平台 官方文档。
?
?
查看更多关于ionic3+angular4的三方微信开发(登录and分享)的详细内容...