Skip to content

Instantly share code, notes, and snippets.

@1c7
Last active October 31, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1c7/e837693185284a06dfd2c4010b6ad13f to your computer and use it in GitHub Desktop.
Save 1c7/e837693185284a06dfd2c4010b6ad13f to your computer and use it in GitHub Desktop.
微信桌面端二维码登录的 redirect_url 地址
<!-- 微信登录:电脑端的 redirect_uri, 接收到来自微信的 code, 然后这个页面负责做下一步操作(把 code 传给后端)-->
<template lang='pug'>
#root
.hint
h3
| {{message}}
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import API_session from '@/api/session'
import API_user from '@/api/user'
export default {
data() {
return {
message: '正在向微信获取您的信息...请稍等',
}
},
mounted(){
// 如果拿不到 code 参数,说明用户没有授权微信登录
let code = this.$route.query.code;
if(code == undefined){
// this.$router.push('/');
// 直接返回首页
}else{
this.wechatLogin(code);
}
},
computed: {
...mapGetters(['currentUser','isLoggedIn'])
},
methods: {
// code 是用户授权登录后,微信传递过来的
wechatLogin(code){
var self = this;
let state = this.$route.query.state;
API_user.wechatLogin(code).then(result=>{
if (result['token']) {
self.$store.dispatch('logIn', result); // 登录
// 如果用户没绑定手机,就提示绑定
if (self.currentUser.phone == null){
self.$router.push('/bind_phone');
}else{
self.$router.push('/');
}
}else{
if(result['errcode'] == '40163'){
var msg = 'code 只能使用一次,已失效';
self.message = msg;
}
}
}).catch(error=>{
alert('服务器出错');
console.log(error);
});
},
},
}
</script>
<style lang="scss" scoped>
省略
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment