vue+h5 plus 第三方登录

时间:2020-05-14 14:59:55   收藏:0   阅读:80

html:

<div class="box" style="margin-top:0;" @click="getLogin(‘weixin‘)">微信登录</div>
<div class="box" style="margin-top:0;" @click="getLogin(‘qq‘)">QQ登录</div>
<div class="box" style="margin-top:0;" @click="getLogin(‘sinaweibo‘)">微博登录</div>

 




data:

oauth: null,
auths: !window.localStorage.getItem(‘auths‘) ? null : JSON.parse(window.localStorage.getItem(‘auths‘)),
Params: {}, // 第三方登录参数

 



 methods:

//第三方登录
      getLogin(type) {
        this.oauth = type;
        plus.oauth.getServices(success => {
          this.auths = success
          window.localStorage.setItem(‘auths‘, JSON.stringify(success))
          this.$toast(`正在获取第三方登录...`);
          this.oauthLogin(type)
        }, error => {
          plus.nativeUI.alert("获取登录授权服务列表失败:" + JSON.stringify(error));
        })
      },
      // 获取登录认证
      oauthLogin(type) {
        console.log(‘======type‘ + type);
        var s;
        this.auths.forEach((item, index) => {
          if (this.auths[index].id == type) {
            s = this.auths[index]
          }
        })
        if (!s.authResult) {
          s.login(res => {
            // this.$toast(‘第一个微信登录‘)
            this.authUserInfo(type);
          }, error => {
            plus.nativeUI.alert(‘error‘ + JSON.stringify(error));
          })
        } else {
          this.authUserInfo(type);
          // this.$toast(‘第二个微信登录,已授权登录‘);
        }
      },
      // 获取授权用户信息
      authUserInfo(type) {
        var s;
        this.auths.forEach((item, index) => {
          if (this.auths[index].id == type) {
            s = this.auths[index]
          }
        })
        // console.log(‘===========s‘ + JSON.stringify(s));
        if (s.authResult) {
          s.getUserInfo(res => {
            this.Params = {
              username: s.userInfo.nickname,
              avatar: s.userInfo.headimgurl,
              sex: s.userInfo.sex,
              openid: s.userInfo.openid,
              longitude: this.longitude,
              latitude: this.latitude,
              type: 2
            }
            console.log(this.Params)
          }, error => {
            plus.nativeUI.alert(JSON.stringify(error));
            this.$toast(‘失败报错‘);
          })
        }
      },

 



原文:https://www.cnblogs.com/fanqiuzhuji/p/12888224.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!