<template>
<div>
登陆页面内容
<!-- #ifdef MP-WEIXIN -->
<button @getuserinfo='getUserInfo' open-type="getUserInfo" type="primary" class='login'>微信授权登录</button>
<!-- #endif -->
</div>
</template>
<script>
export default {
data() {
return {
};
},
methods: {
getUserInfo(e) {
let _this = this;
let ret = e.detail;
console.log(ret,'------------点击按钮')
if (ret.errMsg == "getUserInfo:fail auth deny") {
uni.showToast({
title: '为了更好的服务,请同意授权',
duration: 2000
});
} else if (ret.errMsg == "getUserInfo:ok") {
_this.userLogin(ret);
} else {
uni.showToast({
title: '授权失败',
duration: 2000
});
}
},
userLogin(e) {
let _this = this;
uni.login({
success: res => {
let code = res.code;
if (code) {
//登录参数
let d={
appid:'wx4b6f4b1e5883f743',
secret:'75ceb047014166507d9b5505ed2a278c',
code:code
}
let data ='https://api.weixin.qq.com/sns/jscode2session?appid='+d.appid+'&secret='+d.secret+'&js_code='+res.code+'&grant_type=authorization_code';
//调登录接口
uni.request({
url: data, //仅为示例,并非真实接口地址。
data: {},
header: {},
success: (res) => {
console.log(res,'---请求接口返回能力');
}
});
}
},
fail: () => {
uni.showToast({
title: '没有获取到登陆信息,请重试',
duration: 2000
});
}
});
}
}
}
</script>
<style>
.xj{
border-radius: 8px;
}
</style>
注意这里一定要配置,不然在开发者攻击中运行会报错 forceUpdate