可以自動獲取或點擊按鈕獲取文章來源:http://www.zghlxwxcb.cn/news/detail-736505.html
<view class="py-2 px-3">
<view @click="getUserInfo" class="flex align-center justify-center bg-primary p-2 rounded-circle text-white font-md" hover-class="bg-hover-primary" >點我登錄</view>
</view>
獲取openid
注意:一般都是將code值傳到后端去獲取openid,因為在前端可能會被抓包或爬取到你的appid和secret,不安全,如果放在后端獲取openid,除非你的服務(wù)器被攻擊了,不然就是安全的。下面的實例是在前端直接獲取的,這個明白后,可以直接改成后端的,是邏輯一樣的。文章來源地址http://www.zghlxwxcb.cn/news/detail-736505.html
methods: {
getUserInfo() {
uni.login({
success: res => {
//code值(5分鐘失效)
console.info(res.code);
//小程序appid
let appid = 'wx3599fe368a452c9'; //我瞎寫的
//小程序secret
let secret = '1a5567978saf65c43s8s2397er1332ce'; //我瞎寫的
//wx接口路徑 'https://api.weixin.qq.com/sns/jscode2session?appid=' + _this.globalData.appid + '&secret=' + _this.globalData.AppSecret + '&js_code=' + res.code + '&grant_type=authorization_code';
let url = 'https://api.weixin.qq.com/sns/jscode2session?appid='+ appid + '&secret='+ secret + '&js_code=' + res.code + '&grant_type=authorization_code';
uni.request({
url: url, // 請求路徑
data: {}, // 請求體
method: 'GET', //請求方法,
header: '', //請求頭
success: result => {
//響應(yīng)成功
//這里就獲取到了openid了
console.info(result.data.openid);
// uni.setStorage({
// key:'user',
// data: result.data.openid
// })
},
fail: err => {} //失敗
});
}
});
},
}
到了這里,關(guān)于uniapp微信小程序獲得openid的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!