vue h5微信公众号授权获取用户信息

1.申请测试账号

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2.修改网页授权,填写自己需要申请授权的前端首页

(比如我这里xuexiao.zhangdengzhen.top,是我的登录页面,我在登录页面申请授权)



3填写申请授权的前端url,token随便写。

提交时,提示错误也不用管它

具体流程

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

前端代码

在登录页面获取用户权限

<template><div class="login"><van-row><van-col span="10" offset="4"><van-imageroundwidth="10rem"height="10rem"src="https://img1.baidu.com/it/u=1565027473,2004361185&fm=26&fmt=auto&gp=0.jpg"/></van-col>
</van-row><!-- 居中 -->
<van-row type="flex" justify="center" ><van-col span="20" style="margin-top:20px;box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)"><van-field v-model="text" label="学号"   placeholder="请输学号" /><van-field v-model="password" type="password" label="密码"   placeholder="请输入密码" /><van-fieldv-model="sms"centerclearablelabel="验证码"placeholder="请输入验证码"><template #button><van-imagewidth="4.096rem"src="https://img01.yzcdn.cn/vant/cat.jpeg"/></template></van-field></van-col>
</van-row><!-- 居中 -->
<!-- <van-row type="flex" justify="center" style="margin-top:10px;" > --><van-row type="flex" justify="center" style="margin-top:0.64rem;" ><van-col span="6"><van-button @click="tohome" type="primary" size="large" style="box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)">登录</van-button></van-col>
</van-row></div>
</template>
<style scoped></style>
<script>import axios from 'axios'
export default {data(){return{text:"",password:"",sms:"",search:"",urlpost:""}},methods:{tohome(){localStorage.setItem('password', this.password);localStorage.setItem('text', this.text);axios({method:"post",url:this.urlpost,data:{zhanghao:this.text,mima:this.search,yanzhengma:this.sms,openid:JSON.parse(localStorage.getItem("userinfo")).openid}}).then(res=>{console.log(res.data)Toast("登录成功")})this.$router.push('/index') },getCode(){axios.get('http://if:3000/getCode').then(response => (window.location.href=response.data)).catch(function (error) { // 请求失败处理console.log(error);});},isCode(){return  window.location.search.includes('code=')},getUserinfo(){this.search=window.location.searchconst url_params=Object.fromEntries(window.location.search.slice(1).split('&').map(v=>v.split('=')))console.log(url_params)console.log(this.search)axios({method:'post',url:"http://ip:3000/getUserInfo",data:{code:url_params.code,search:this.search}}).then(res=>{console.log(res.data)localStorage.setItem("userinfo",JSON.stringify(res.data))})}},created(){//  this.getCode()this.urlpost=window.g.pachongif(this.isCode()){this.getUserinfo()console.log(this.isCode())}else{this.getCode()}},//  updated(){//    this.getUserinfo()//      if(this.isCode){//             this.getUserinfo()//             console.log(this.isCode())//          }else{//              this.getCode()//          }//  }
}
</script>

后端代码

1.目录结构

2.env文件内容(填写你测试号的APPID和APPSECRET)

3.serve.js内容

const app=require("express")()
const bodyParser=require("body-parser")
const cors=require("cors")
const axios=require("axios")
app.use(bodyParser.json())require('dotenv').config()
app.use(cors())app.get('/',async(req,res)=>{res.send("ok")
})
app.post('/ceshi',async(req,res)=>{console.log("账号",req.body.zhanghao)console.log("密码",req.body.mima)console.log("验证码",req.body.yanzhengma)console.log("openid",req.body.openid)res.send("ok")
})
app.get('/getCode',async(req,res)=>{let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${process.env.APPID}&redirect_uri=${encodeURIComponent(`http://xuexiao.zhangdengzhen.top`)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`//console.log(url)res.send(url)
})
app.post('/getUserInfo',async(req,res)=>{let code=req.body.code;// console.log(req.body.search,"mysearch")// console.log(code)let accessaccess_token_url= `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${process.env.APPID}&secret=${process.env.APPSECRET}&code=${code}&grant_type=authorization_code`let token=(await axios.get(accessaccess_token_url)).data.access_tokenlet userInfo=`https://api.weixin.qq.com/sns/userinfo?access_token=${token}&openid=OPENID&lang=zh_CN`let user=(await axios.get(userInfo)).data//  console.log(accessaccess_token_url)console.log(user,'user')res.send(user)
})
app.listen(3000,()=>{console.log("http://localhost:3000")
})

后端返回的数据情况

vue h5微信公众号授权获取用户信息相关推荐

  1. 微信公众号授权获取用户OpenID和UnionId然后发生消息通知

    微信公众号授权获取用户OpenID和UnionId然后发生消息通知 1.获取微信公众号code 1.微信公众拿取公众号appid和appSecret 2.网页域名授权,这里的域名拼接上html所在位置 ...

  2. 【微信公众号】获取用户信息

    author:咔咔 wechat:fangkangfk 已经很久没有在写公众号了,今天在回顾一下 首先获取测试账号 然后配置域名 打开文档看看步骤:https://mp.weixin.qq.com/w ...

  3. 微信公众号开发--获取用户信息中文乱码的解决方案

    在微信开发中我们会经常需要获取用户的信息. 微信给我们提供了获取用户信息的api, 地址为 https://api.weixin.qq.com/cgi-bin/user/info?access_tok ...

  4. 微信公众号网页获取用户信息

    微信小程序开发交流qq群   173683895    承接微信小程序开发.扫码加微信. 效果图 html 代码: <!DOCTYPE html> <html><head ...

  5. 企业微信接收消息服务器配置php,微信企业号配置及在公众号里获取用户信息

    微信企业号配置及在公众号里获取用户信息 1.1.企业公众号里新建应用,回掉模式里自定义回调URL及密钥拿到,创建应答数据 配置成功后显示: 已成功配置企业服务器 URL http://75271.co ...

  6. 微信小程序授权获取用户信息和手机号码

    微信小程序授权获取用户信息和手机号码 1.微信官方文档 登录:https://developers.weixin.qq.com/miniprogram/dev/framework/open-abili ...

  7. 微信小程序授权 获取用户信息

    微信小程序授权 获取用户信息 小程序昵称突然变成了"微信用户",头像也不显示, <!-- 近期很多小伙伴通过该方法获取头像和昵称,代码也没有做改变,突然就变成了下面这样子 - ...

  8. H5 微信公众号 授权登录 前后端分离篇(资料准备+前端01)

    实现微信公众号授权登录,很简单,但是注意的地方要细心,小伙伴们跟着我的思路一起实现吧! 文章目录 一.帐号申请 1. 正式账号 2. 测试帐号 二.微信文档 2.1. 文档主页 2.2. 授权流程 2 ...

  9. H5微信公众号授权登录

    目录 一.封装获取code的函数.存储token函数.获取token函数 二.点击请求微信公众号授权并跳转 三.重定向跳转会当前链接获取code登录 四.整体流程的描述: 一.封装获取code的函数. ...

最新文章

  1. Typescript,Vue you enjoy it ?
  2. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第20篇]Merkle-Damgaard hash函数如何构造
  3. 第二百一十九天 how can I 坚持
  4. php文件夹转换网页,PHP转换文件夹下所有文件的编码 适合发布网站的其他编码版本...
  5. Server Tomcat Server at localhost failed to start解决方案
  6. 从一个微型例子看“C/C++的内存分配机制”和“数组变量名与指针变量名”
  7. 通过JAVA操作SAE上的MY SQL数据库
  8. gbase导入sql文件_GBase数据库——常用命令
  9. 继承者来了!CentOS 创始人开辟新项目 Rocky Linux
  10. iOS Programming - Disallow Alphabetic Characters
  11. java 调用odi_[转]ODI定时任务
  12. 计算机网络发展的第四阶段特点,计算机网络基础考试重点.doc
  13. CoreJava(第二章)Java基础
  14. java中char数据类型的使用
  15. 2012年10月【美国】自驾游
  16. 去除 csv 文件中的重复行
  17. Java响应式流框架Reactor中的Mono和Flux
  18. LeetCode:求全由1构成的最大正方形面积
  19. 婚恋网站有风险,网上交友须谨慎
  20. 全网最全Python项目练习500例(附源代码),练完可就业

热门文章

  1. 调制解调通信系统实现
  2. 第二届国信蓝点模拟试题
  3. CyclicBarrier 使用详解
  4. 在win7下删除Windows服务的方法
  5. 009仓储物流中心里的人机工程学设计
  6. 安卓日程表毕业设计源码
  7. XenServer 挂载其它硬盘及错误原因分析
  8. 服务器稳定性和可靠性区别,在服务器、软件里面,稳定性和可靠性有什么区别?...
  9. 三年内赶超特斯拉?这家车企的底气是什么?
  10. 剑与远征服务器无限换,剑与远征兑换码2021年5月最新 剑与远征兑换码永久有效整理...