微信公众号开发
功能:自动登录,获取个人信息,上传图片
超多麻烦的情况,怎样获取openid呢?
以下我给大家提供源码,文本,视频资料
保证让你看了就明白哈
1.拉起用户授权

2.后台获取到的openid

3.前端展示用户信息

不懂的可以先看看这个视频:
https://www.bilibili.com/video/BV1XL411T73G/?vd_source=125d808bbbad2b8400f221b816a0f674
/**
// 获取用户信息,三部曲
// 感谢网友的文章 https://blog.csdn.net/qq_39506978/article/details/109410343
// 参考微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#3
app.get("/login.html", (req, res) => {console.log(Date.now()+':login.html页面响应--用户,有信息 :', req.body, req.query);// 如果是获取用户信息的回调,就重定向 if(req.query && req.query.code){let code = req.query.coderes.redirect(`/${staticUrl}?code=`+code)}
})// 通过后台,获取用户信息1
app.get("/getUserInfo", (req,res)=>{let code = req.query.code// 通过code,获取access_tokenlet url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${wxConfig.appId}&secret=${wxConfig.appsecret}&code=${code}&grant_type=authorization_code`request(url, async function (error, response, body) {if (!error) {console.log('openId_成功_用户信息:error, response, body', typeof body)// 通过access_token,最后获取用户信息let userInfo = await getUserInfo(JSON.parse(body))console.log('userInfo',userInfo);res.send(userInfo)} else {console.log('error:', error)res.send(JSON.parse(error))}});
})
如何运行:
你需要安装nodejs环境,
直接去nodejs官网https://nodejs.org/en/
https://gitee.com/618859/WeChat-official-account-openid.git
在本项目根目录运行以下命令
一定要进入到:项目根目录,否则无法正确安装依赖
npm install
npm install request
node app.js
可以安装了nodemon
npm i nodemon -g
nodemon app.js
在微信开发者工具中访问你内网穿透的地址
