ionic3+angular4的三方微信开发(登录and分享)

登录:

 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.com/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.com/sns/userinfo"

      url = url+"?access_token="+AccessToken+"&openid="+Openid;
    return this.http.get(url, {}).toPromise().then(response => {
    return response.json();
   }).catch(CommonService.handleError);
    }

分享:
  1、上面的1、2步需要操作一次
  2、代码
//标题
title: string = "分享的标题";
//描述
description: string = "简单描述!";
//分享链接
link: string = "分享后点击查的url";
//分享图片
image: string = "图片链接";
//分享的目标://0:微信好友,1:朋友圈
scene: number=0;
wxShare() {
try {
Wechat.share({
// text: msg,
message: {
title: this.title,
description: this.description,
thumb: "",
media: {
type: Wechat.Type.WEBPAGE,
webpageUrl: url
}
},
scene: scene == 0 ? Wechat.Scene.SESSION : Wechat.Scene.Timeline // share to Timeline
}, function () {
console.log("分享成功");
}, function (reason) {
    console.log("分享失败");
});
} catch (error) {
console.log(error);
}
}
//开发注意:APPID的一致性,需要做的功能在微信开发平台上相应的权限是否获取,还有就是该微信的相关操作不能使用真机联调,上面有看不明白的也可以查阅微信开发平台官方文档。
 

 

转载于:https://www.cnblogs.com/dreamwf/p/8743991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular是一个流行的JavaScript框架,用于构建Web应用程序。Ionic是基于Angular的开源移动应用开发框架,它提供了一套UI组件和具,帮助开发者构建跨平台的移动应用程序。 Swiper是一个流行的移动端滑动组件库,它提供了丰富的滑动效果和交互功能,可以用于创建漂亮的轮播图、图片浏览器等。 结合AngularIonic,你可以轻松地集成Swiper组件到你的移动应用中。首先,你需要在你的Angular项目中安装Swiper组件库。可以使用npm命令来安装: ``` npm install swiper --save``` 安装完成后,你可以在你的Ionic组件中引入Swiper组件,并在模板中使用它。以下是一个简单的示例: ```typescriptimport { Component } from '@angular/core'; import SwiperCore, { Navigation, Pagination } from 'swiper/core'; SwiperCore.use([Navigation, Pagination]); @Component({ selector: 'app-swiper', template: ` <swiper [navigation]="true" [pagination]="true"> <ng-template swiperSlide>Slide1</ng-template> <ng-template swiperSlide>Slide2</ng-template> <ng-template swiperSlide>Slide3</ng-template> </swiper> `, }) export class SwiperComponent {} ``` 在上面的示例中,我们首先引入了Swiper组件库,并注册了所需的Swiper模块(例如Navigation和Pagination)。然后,在组件的模板中,我们使用`<swiper>`标签创建了一个Swiper实例,并在内部添加了三个滑动的内容块。 你可以根据你的需求自定义Swiper的配置和样式。更多关于Swiper的用法和配置,你可以参考Swiper官方文档。 希望这可以帮助到你!如果你还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值