<script>
export default {
data() {
return {
code: "",
},
}
methods: {
getParams() {
// 获取跳转回来的参数,里面包含了我们需要的code
var url = window.location.search;
var objParams = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
objParams[strs[i].split("=")[0]] = strs[i].split("=")[1];
}
}
return objParams;
},
authenticatedLogin() {
// 微信公众号或企业微信的appid
let APPID = "wxbxxxxxxxxxxxxxx";
// 跳转回来的路径
let REDIRECT_URI = encodeURIComponent(
"https://xxx.cn/h5/index.html#/"
);
// 拿用户的某种信息
let SCOPE = "snsapi_userinfo";
// 微信公众号或者企业微信的请求地址
location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=STATE#wechat_redirect `;
return;
},
},
onShow() {
let getCode = this.getParams();
this.code = getCode.code;
// 获取code 如果没有就走微信公众号或者企业微信的请求链接,如果有就走后续登录流程
if (this.code) {
// 后续登录流程
} else {
this.authenticatedLogin();
}
},
};
</script>
微信公众号或企业微信登录
于 2023-04-14 15:03:21 首次发布