diff --git a/src/views/smsManage.vue b/src/views/smsManage.vue index 0029b75..3b2de20 100644 --- a/src/views/smsManage.vue +++ b/src/views/smsManage.vue @@ -118,8 +118,13 @@ import {getUUID} from '@/utils' const APPID = "wx7135dd84735bc392"; + // 静默授权:scope=snsapi_base + // const WX_AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + // + '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=0#wechat_redirect'; + + // 非静默授权:scope=snsapi_userinfo const WX_AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID - + '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=0#wechat_redirect'; + + '&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'; export default { data() { @@ -144,6 +149,8 @@ codeUrl: "", //验证码 isSmsLogin: false, //是否手机验证码 computeTime: 0, + headimgurl: "", + nickname: "", }; }, created() { @@ -158,10 +165,7 @@ this.showMessage = !this.showMessage }, wxAuth() { - // let code = this.getUrlParam('code');//获取URL中的code参数 - // if (code) return code; - // let currentUrl = encodeURIComponent(window.location.href); - // window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl)); + // 非静默授权:snsapi_userinfo,有弹框弹出需要用户手动点击确认授权。可以获取openId,用户的头像、昵称等 return new Promise((resolve, reject) => { let openid = this.$cookie.get('openid'); if (openid) { @@ -170,26 +174,65 @@ } let code = this.getUrlParam('code'); if (!code) {//未经过微信授权 + /** + * 获取网页授权登录code + * 如果有直接返回,如果没有重定向到腾讯授权页面,腾讯会再次重定向回来并带有code参数 + **/ + let code = this.getUrlParam('code');//获取URL中的code参数 + if (code) return code; let currentUrl = encodeURIComponent(window.location.href); window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl)); } else { this.$cookie.set('appid', APPID); //接口服务端从cookie中读取openid this.$http({ - url: this.$http.adornUrl('/wxAuth/codeToOpenid'), + url: this.$http.adornUrl('/wxAuth/codeToUserInfo'), method: 'post', data: this.$http.adornParams({ 'code': code }) }).then(res => { - if (res.code == 200) { - console.log("微信授权完成" + res.data); - resolve(res.data); + if (res.data.code == 200) { + console.log("微信授权完成"); + this.headimgurl = res.data.headimgurl; + this.nickname = res.data.nickname; + console.log(this.headimgurl); + console.log(this.nickname); } else { console.log("换取openid失败"); } }); } }); + + // 静默授权:snsapi_base,没有弹窗,只能获取用户的openId。 + // return new Promise((resolve, reject) => { + // let openid = this.$cookie.get('openid'); + // if (openid) { + // resolve(openid); + // return; + // } + // let code = this.getUrlParam('code'); + // if (!code) {//未经过微信授权 + // let currentUrl = encodeURIComponent(window.location.href); + // window.location.replace(WX_AUTH_URL.replace('REDIRECT_URI', currentUrl)); + // } else { + // this.$cookie.set('appid', APPID); //接口服务端从cookie中读取openid + // this.$http({ + // url: this.$http.adornUrl('/wxAuth/codeToOpenid'), + // method: 'post', + // data: this.$http.adornParams({ + // 'code': code + // }) + // }).then(res => { + // if (res.code == 200) { + // console.log("微信授权完成" + res.data); + // resolve(res.data); + // } else { + // console.log("换取openid失败"); + // } + // }); + // } + // }); }, getUrlParam(key) {//获取当前页面url中的参数 var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"); @@ -264,6 +307,8 @@ method: 'post', data: this.$http.adornParams({ 'openId': this.$cookie.get('openid'), + 'headimgurl': this.headimgurl, + 'nickname': this.nickname, 'mobile': this.formData.mobile, 'code': this.formData.smsCode, 'codeTime': this.formData.codeTime