当前位置:网站首页>微信小程序获取登录用户信息、openid和access_token
微信小程序获取登录用户信息、openid和access_token
2022-04-23 14:02:00 【1029179954】
1:获取登录用户信息
由于微信小程序提升用户体验,所以获取用户信息就有所提升,使用button按钮,添加open-type="getUserInfo"即可
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: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
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:获取openid和access_token
openid:
每个微信唯一的id
access_token:
access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需要使用access_token。
appid、secret:在微信公众平台里边,具体在下图
js(放在微信小程序onload函数即可)
//获取openid
wx.login({
success: function (res) {
var code1 = res.code
var appid1 = "自己的appid"
var secret1 = "自己的秘钥"
var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
//获取openid
wx.request({
url: ul,
method: 'GET',
success: function (e) {
var openid = e.data.openid
console.log('获取登录身份的唯一openid', openid)
that.openid=e.data.openid
wx.setStorageSync('openid', openid)
}
})
}
})
},
//获取access_token
const appid = "自己的appid" // 这里填写你的appid
const secret = "自己的秘钥" // 这里填写你的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微信小程序"+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)
}
})
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/121592280
边栏推荐
- Chapter 15 new technologies of software engineering
- _模_板_
- mysql通过binlog文件恢复数据
- 变长参数__VA_ARGS__ 和 写日志的宏定义
- PySide2
- 神经元与神经网络
- leetcode--977. Squares of a Sorted Array
- Wechat applet
- Qt Designer怎样加入资源文件
- Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
猜你喜欢
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
【项目】小帽外卖(八)
Basic knowledge learning record
groutine
JS brain burning interview question reward
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
JS 烧脑面试题大赏
Un modèle universel pour la construction d'un modèle d'apprentissage scikit
美联储数字货币最新进展
Interesting talk about network protocol
随机推荐
1256:献给阿尔吉侬的花束
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Oracle alarm log alert Chinese trace and trace files
BUG_me
mysql新表,自增id长达20位,原因竟是......
Express middleware ③ (custom Middleware)
JS 力扣刷题 102. 二叉树的层序遍历
2021年秋招,薪资排行NO
try --finally
leetcode--977. Squares of a Sorted Array
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
scikit-learn構建模型的萬能模板
JMeter pressure test tool
Tensorflow & pytorch common error reporting
分页SQL
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
json反序列化匿名数组/对象
linux安装mysql后修改密码
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
request模块