注意,我使用的Hbuilder
1、配置manifest.json

2、拉起授權頁面
/* 這里的that是因為下面獲取不到this了,需要用that替換 */
let that = this;
/* 獲取當前服務商 */
uni.getProvider({
service: 'oauth',
success: function (res) {
/* 進行登錄 發送請求給對應的服務商 */
uni.login({
provider: res.provider,
success: function (loginRes) {
/* 發送成功 服務商回傳code為獲取微信token做準備 */
console.log(loginRes)
/* 拉起服務商授權頁面 */
uni.getUserProfile({
desc: "登錄",
lang:"zh_CN",
success:function(profileRes){
// 用戶授權通過
that.name = profileRes.userInfo.nickName
that.avatarUrl = profileRes.userInfo.avatarUrl
/*
請求獲取 用戶唯一標識 會話密鑰
注意這里不能直接請求微信獲取
而是需要通過請求后臺去給微信發請求
*/
uni.request({
url:"http://localhost:8080/api",
data:{code:loginRes.code},
success:(keyRes)=>{
console.log(keyRes)
}
})
},
fail:function(err){
// 用戶拒絕了授權
that.$refs.uToast.show({
title: "您取消了微信授權",
type: "warning" })
}
})
},
fail: function(err){
// 服務商授權頁面拉起失敗,可能是由于沒有安裝服務商的客戶端
that.$refs.uToast.show({
title: "授權拉取失敗!",
type: "error" })
}
});
}
});
注:后臺使用Apache的HttpClient就可以了,關于它是怎么用的,可參看:HttpClient詳細使用示例
3、說明
目前移動應用上微信登錄只提供原生的登錄方式,需要用戶安裝微信客戶端才能配合使用,
所以開發的時候,微信開發工具沒有反應,不妨試試真機除錯,

附上微信登錄官方檔案:檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293574.html
標籤:其他
上一篇:AIoT讓一個屏有了100種用法
下一篇:html初學 2021.8.12
