靜默授權 也就是 默默的授權哈哈
只能拿到已關注用戶的信息
簡單流程:
1.換取code.
我用的uniapp寫的
在create的時候就調用 getWechatCode()
頁面:
getWechatCode() { // 非靜默授權,第一次有彈框
const code = this.getUrlParam('code') // 截取路徑中的code,如果沒有就去微信授權,如果已經獲取到了就直接傳code給后台獲取openId
console.log(" code"+code)
alert("code"+code)
const local = window.location.href
if (code == null || code === '') {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + 'appID' + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
} else {
this.getOpenId(code); // 把code傳給后台獲取用戶信息這里根據官方文檔拿到code就能獲取到用戶信息了然后做你想要的操作
console.log(" code2"+code)
alert(" code2"+code);
}
},
getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
let url = window.location.href.split('#')[0]
let search = url.split('?')[1]
console.log(search)
if (search) {
var r = search.substr(0).match(reg)
if (r !== null)
return unescape(r[2])
return null
} else
return null
},
后台就不寫的吧!
這個方法說就是一個ajax getOpenId 將code穿到后台換取openid 然后再拿到用戶信息就行了
微信公眾號開發推薦可以看下這個:fastweixin https://gitee.com/pyinjava/fastweixin
還有這個
關於微信公眾號開發歡迎大家一起交流!!!