uni-app微信小程序登录授权
<p><span style="font-size: 16px">微信小程序授权是非常简单和常用的功能,但为了方便,还是在此记录一下要点:</span></p><p><span style="font-size: 16px"><img src="https://img2020.cnblogs.com/blog/1157406/202004/1157406-20200408165741463-1083574942.png"></span></p>
<p> </p>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px">首先是需要用到一个授权按钮来触发获取用户信息授权: 关键在于 open-type 为 <span style="color: rgba(255, 0, 0, 1)">getUserInfo</span> , 然后有个@getuserinfo的事件,把获取授权接口写到该事件里面去</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px"><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="sys_btn"</span><span style="color: rgba(255, 0, 0, 1)"> open-type</span><span style="color: rgba(0, 0, 255, 1)">="getUserInfo"</span><span style="color: rgba(255, 0, 0, 1)"> lang</span><span style="color: rgba(0, 0, 255, 1)">="zh_CN"</span><span style="color: rgba(255, 0, 0, 1)"> @getuserinfo</span><span style="color: rgba(0, 0, 255, 1)">="appLoginWx"</span><span style="color: rgba(0, 0, 255, 1)">></span>小程序登录授权<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">></span></span></pre>
</div>
<p><span style="font-size: 16px">方法如下:</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 0, 0, 1)"> appLoginWx(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef MP-WEIXIN</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.getProvider({
service: </span>'oauth'<span style="color: rgba(0, 0, 0, 1)">,
success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (res) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (~res.provider.indexOf('weixin'<span style="color: rgba(0, 0, 0, 1)">)) {
uni.login({
provider: </span>'weixin'<span style="color: rgba(0, 0, 0, 1)">,
success: (res2) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 0, 1)">
uni.getUserInfo({
provider: </span>'weixin'<span style="color: rgba(0, 0, 0, 1)">,
success: (info) </span>=><span style="color: rgba(0, 0, 0, 1)"> {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里请求接口</span>
<span style="color: rgba(0, 0, 0, 1)"> console.log(res2);
console.log(info);</span><span style="color: rgba(0, 0, 0, 1)">
},
fail: () </span>=><span style="color: rgba(0, 0, 0, 1)"> {
uni.showToast({title:</span>"微信登录授权失败",icon:"none"<span style="color: rgba(0, 0, 0, 1)">});
}
})
},
fail: () </span>=><span style="color: rgba(0, 0, 0, 1)"> {
uni.showToast({title:</span>"微信登录授权失败",icon:"none"<span style="color: rgba(0, 0, 0, 1)">});
}
})
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
uni.showToast({
title: </span>'请先安装微信或升级版本'<span style="color: rgba(0, 0, 0, 1)">,
icon:</span>"none"<span style="color: rgba(0, 0, 0, 1)">
});
}
}
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#endif</span>
}</span></pre>
</div>
<p> </p>
<p><span style="font-size: 16px">在 uni.login 和 uni.getUserInfo 被调用后,你可以获取到以下值用于继续请求后端给你的接口:</span></p>
<p><span style="font-size: 16px">常用的值大概有:<span style="font-size: 18px; color: rgba(255, 0, 0, 1)">code 、iv 、encryptedData</span> 和 个人基本信息,这些可以传给后端交换得到openid。</span></p>
<p> </p>
<p><span style="font-size: 16px"><img src="https://img2020.cnblogs.com/blog/1157406/202004/1157406-20200408165531645-747004200.png"></span></p>
<p> </p>
<p><span style="font-size: 16px">如果需要知道用户当前是否已经授权,则可以使用如下代码:</span></p>
<p><span style="font-size: 16px">uniapp的授权文档,可以判断不同的授权类型:https://uniapp.dcloud.io/api/other/authorize?id=authorize</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef MP-WEIXIN</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.getSetting({
success(res) {
console.log(</span>"授权:"<span style="color: rgba(0, 0, 0, 1)">,res);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!res.authSetting['scope.userInfo'<span style="color: rgba(0, 0, 0, 1)">]) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里调用授权</span>
console.log("当前未授权"<span style="color: rgba(0, 0, 0, 1)">);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用户已经授权过了</span>
console.log("当前已授权"<span style="color: rgba(0, 0, 0, 1)">);
}
}
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">#endif</span></span></pre>
</div>
<p> </p>
<p><span style="font-size: 16px"> </span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
有人住高楼,有人处深沟。
有人光万丈,有人一生绣。
时光是匆匆,回首无旧梦。
人生若几何,凡尘事非多。
深情总遗却,妄自也洒脱。<br><br>
来源:https://www.cnblogs.com/nanyang520/p/12661104.html
頁:
[1]