在线咨询
微信咨询
服务热线
服务热线:15639912513
TOP
当前位置:
首页 > 新闻中心> 前端设计>微信小程序wx.login()获取openid整套流…

微信小程序wx.login()获取openid整套流程

发布时间:2019-09-04 浏览:7580次

微信小程序wx.login()获取openid整套流程是怎么样的?下面是兴弘海科技列出的整套完整流程代码,可以直接复制,直接用。

代码如下:

wx.login({

success: res => {

// 发送 res.code 到后台换取 openId, sessionKey, unionId

if (res.code) {

var that = this;

//发起网络请求

wx.request({

url: 'https://api.weixin.qq.com/sns/jscode2session',

data: {

appid: '微信小程序的appid',

secret: '微信小程序的秘钥',

js_code: res.code,

grant_type: 'authorization_code'

},

success: function (response) {

if (response.data.openid != null && response.data.openid != undefined) {

that.globalData.openid = response.data.openid

}

},

fail: function (error) {

console.log('获取openid失败');

}

})

} else {

console.log('登录失败!' + res.errMsg)

}

}

})

上面代码其中有一行做下解释:

success: function (response) {

if (response.data.openid != null && response.data.openid != undefined) {

that.globalData.openid = response.data.openid

}

这行代码的意思是成功返回函数,如果response.data.openid不为空或者response.data.openid不为undefined未定义,那么,就把response.data.openid这个openid传给globalData.openid。

补充:

这个函数主要的应用场景是用户登录注册的时候,会需要微信用户的openid。尤其是微信小程序定制开发,很多客户会需要用户注册信息,那么这个时候就需要用户登录注册,那么openid的获取就显得重要了。

TAG
7580
该内容对我有帮助