https://mp.weixin.qq.com/wiki 微信公众号开发文档
拿code是要在配置了appID,redirect_uri之后页面跳转回来的页面url上携带着code,需要截取。
将公众号代码让布到了服务器上 可以看效果,微信开发者工具很不方便,
getUrl() { let appID = "公众号的appId"; let redirectUri = encodeURIComponent("这里是f跳转回来的页面"); let strUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + "&redirect_uri=" + redirectUri + "%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; this.$nextTick(() => { window.location.href = strUrl; }); },
appId和response_type 是自己定义的参数
跳回的页面的url 需要加encodeURIComponent()方法来处理
截取code
//截取code getCode() { let url = window.location.search; let start = window.location.search.indexOf("="); let end = window.location.search.indexOf("&"); let code = url.substring(start + 1, end); return code; },
拿到code之后去请求后端,后端会返回openId 也可以拿到用户的昵称 头像
绑定手机号 另起页面 开通手机验证码服务
拿到数据发送给后端验证
getCode(formName){
this.msg = '';
const TIME_COUNT = 60;
if (this.inputPhone) {
this.$http.post('这里是调取的后端接口名?phone='+ this.inputPhone, this.inputPhone)
.then(({ data }) => {
if(data.succeed) {
this.count = TIME_COUNT;
this.show = false;
this.btnShow = true;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--;
} else {
this.btnShow = false;
this.show = true;
clearInterval(this.timer);
this.timer = null;
}
}, 1000)
} else {
//展示错误信息给用户
let errINFO = document.getElementById('errInfo')
this.msg = data.msg
this.changeMsg()
}
})
} else {
this.msg = '请输入手机号'
this.changeMsg();
}
},
//提交绑定
submitInfo() {
this.$http.post('这里是调取的后端接口名?phone='+ this.inputPhone +'&code='+ this.inputCode +'&openId='+ localStorage.getItem('openId'))
.then(({ data }) => {
if(data.succeed) {
this.$router.push('/production?userOK')
} else {
this.msg = data.msg
}
})
}
总结:
开发时需项目上线自动布项目,之后拿到域名跳域名测试,前端push代码,服务器自动布项目。这也是开发的不便。