uni-app获取code发送到服务器上获取信息
- 首先先获取code,公众号分为静默授权(即无需用户点击)和非静默授权(弹出提示框,用户点击确认)
methods: {
getCode() { // 非静默授权,第一次有弹框
this.code = ''
var local = window.location.href // 获取页面url
var appid = '自己的appid'
this.code = this.getUrlCode() // 截取codeurlEncode
if (JSON.stringify(this.code) == "{}") { // 如果没有code,则去请求
window.location.href =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的appid&redirect_uri=https%3a%2f%2f自己的地址%2fh5&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
}
},
getUrlCode() { // 截取url中的code方法
var url = location.search
var theRequest = new Object()
if (url.indexOf("?") != -1) {
var str = url.substr(1)
var strs = str.split("&");
for (var i = 0; i < strs.lengt