当前位置:网站首页>Wechat applet obtains login user information, openid and access_ token
Wechat applet obtains login user information, openid and access_ token
2022-04-23 14:04:00 【one billion twenty-nine million one hundred and seventy-nine th】
1: Get login user information
Because wechat applet improves user experience , Therefore, obtaining user information is improved , Use button Button , add to open-type="getUserInfo" that will do
wxml
<view class='loginHeader'>
<image src='{
{userHead}}' class='userHead' mode='aspectFill' bindtap='previewHead' />
<button class='userTitle' open-type="getUserInfo" bindtap='loginTap'>{
{userTitle}} </button>
<!-- <text class='userTitle' bindtap='loginTap'>{
{userTitle}}</text> -->
</view>
js
loginTap: function () {
wx.getUserProfile({
desc: ' Display user information ', // Declare the purpose of obtaining the user's personal information , The follow-up will be shown in the pop-up window , Please fill in carefully
success: (res) => {
console.log(res.userInfo)
app.userTitle=res.userInfo.nickName
app.userHead=res.userInfo.avatarUrl
this.setData({
userHead: app.userHead,
userTitle: app.userTitle
})
}
})
},
2: obtain openid and access_token
openid:
Each wechat is unique id
access_token:
access_token The official account is the only global interface invoke credential. , The official account calls for interfaces. access_token.
appid、secret: Inside the wechat public platform , See the figure below for details
js( Put it in wechat applet onload Function )
// obtain openid
wx.login({
success: function (res) {
var code1 = res.code
var appid1 = " Their own appid"
var secret1 = " My secret key "
var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
// obtain openid
wx.request({
url: ul,
method: 'GET',
success: function (e) {
var openid = e.data.openid
console.log(' Get the unique of login identity openid', openid)
that.openid=e.data.openid
wx.setStorageSync('openid', openid)
}
})
}
})
},
// obtain access_token
const appid = " Their own appid" // Here's your appid
const secret = " My secret key " // Here's your secret
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`,
header: {
'content-type': 'application/json'
},
success(res) {
console.log("at Wechat applet "+res.data.access_token)
that.access_token=res.data.access_token
console.log("onload:"+that.access_token)
wx.setStorageSync('at',res.data.access_token)
},
fail(error){
console.log(error)
}
})
版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401298099.html
边栏推荐
猜你喜欢
随机推荐
log4j 输出日志信息到文件中
MySQL 修改主数据库
Force deduction brush question 101 Symmetric binary tree
微信小程序 input隐藏和不可操作的设置
websocket
Problems encountered in the project (V) understanding of operating excel interface poi
基于CM管理的CDH集群集成Phoenix
Port occupied 1
STM32学习记录0007——新建工程(基于寄存器版)
腾讯根据ip解析地址
scikit-learn构建模型的万能模板
scikit-learn構建模型的萬能模板
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
Oracle alarm log alert Chinese trace and trace files
Neuron and neural network
New关键字的学习和总结
crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
Wechat applet
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)