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 }
this.register(s.authResult.openid) console.log(this.Params) }, error => { plus.nativeUI.alert(JSON.stringify(error)); this.$toast('失敗報錯'); }) } },
//第三方登錄
register(openid){
let sign;//標記
if(this.oauth == 'weixin'){ sign = 2; } if(this.oauth == 'sinaweibo'){ sign = 3; } if(this.oauth == 'qq'){ sign = 4; } let params = { type:sign,//1手機 2微信 3微博 4qq wx_openid:sign == 2?openid:'', wb_openid:sign == 3?openid:'', qq_openid:sign == 4?openid:'', } this.$post("/user/user/register", params).then(res => { if (!res.error_code) { console.log(res.response_data,'res') localStorage.setItem('access_token',res.response_data.access_token); this.getUserInfo() } else { this.$toast(res.error_msg); console.log(res.error_msg,'error') } }) },