当前位置:网站首页>H5页面调用微信授权获取code
H5页面调用微信授权获取code
2022-08-02 00:16:00 【m0_67401382】
H5调用微信授权获取code(微信公众号微信页面开发网页授权)
一、首先你要有一个公众号(公众号、订阅号),微信公众平台配置。
1、获取基础信息(APPID(前后端都需要)、APPsecret(后端需要)、IP白名单(后端提供,配置在公众号这里))。
1647488321959-5071dd31-c39b-4894-843a-c8987361d5ed.png
2、配置网页授权域名(切记域名格式,不需要带上https://)
WechatIMG92.png
3、有校验文件需要下载下来给后端开发
WeChatf70182f098421638f795a47be284c194.png
二、前端开发
1、判断是否微信浏览器
// 判断是否为公众号模拟器环境
const isWechat = () => {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
}
2、访问特定微信链接来获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
WeChat82135db29abd0e7497fe776df02201e7.png
3、截取code方法
getUrlCode() {
// 截取url中的code方法
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
}
}
console.log(theRequest);
return theRequest;
},
4、上demo
// 判断是否为微信环境
const isWechat = () => {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
}
export default {
data() {
return {
code: ''
}
},
onLoad() {
this.getWxCode()
},
methods: {
getWechatCode() {
if (isWechat) {
let appid = "公众号后台获取的APPID"; //微信APPid
let code = this.getUrlCode().code; //是否存在code
let local = window.location.href;
if (code == null || code === "") {
//不存在就打开上面的地址进行授权
window.location.href =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
appid +
"&redirect_uri=" +
encodeURIComponent(local) +
"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
} else {
this.code = code;
}
}
},
getUrlCode() {
// 截取url中的code方法
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
}
}
console.log(theRequest);
return theRequest;
},
}
}
5、调试工具(微信开发者工具)
WeChat1aeabd56b19142682a4e26695e8df80b.png
WeChatc272536731536f5b233523bc563f23eb.png
三、总结踩坑敲黑板
1、公众号后台重定向域名一定要配置和你开发测试环境一致
2、APPID、前后端一致
3、真机测试,微信右上角弹出框的复制链接是拿不到带有code链接的地址的,可以分享到QQ就能看到带有code的链接了。
WeChatcdd03618bde7e7586245969af9651314.png
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 管理基础知识9
- 【软件工程之美 - 专栏笔记】34 | 账号密码泄露成灾,应该怎样预防?
- Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ
- 【HCIP】BGP小型实验(联邦,优化)
- 思维导图,UML在线画图工具
- 基于注意力机制的多特征融合人脸活体检测
- Difference between JSP out.print() and out.write() methods
- els block deformation judgment.
- bgp aggregation reflector federation experiment
- These 4 computer notepad software, you have to try
猜你喜欢
Unknown CMake command "add_action_files"
C语言:打印整数二进制的奇数位和偶数位
好的期货公司开户让人省心省钱
NodeJs, all kinds of path
How to find new potential projects?Tools recommended
nodeJs--mime模块
测试用例:四步测试设计法
渗透测试与攻防对抗——渗透测试基础
Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ
Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)
随机推荐
nodeJs--mime模块
JS中对事件代理的理解及其应用场景
Angr(十二)——官方文档(Part3)
How does JSP use request to get the real IP of the current visitor?
ICML 2022 | GraphFM:通过特征Momentum提升大规模GNN的训练
C语言:打印整数二进制的奇数位和偶数位
22.卷积神经网络实战-Lenet5
IO stream basics
C语言函数详解(1)【库函数与自定义函数】
Routing strategy
CVPR 2022 | SharpContour:一种基于轮廓变形 实现高效准确实例分割的边缘细化方法
Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ
管理基础知识13
PowerBI商学院佐罗BI真经连续剧
What is the function of the JSP out.println() method?
DFS详解
Don't concatenate strings with jOOQ
Industrial control network intrusion detection based on automatic optimization of hyperparameters
Constructor, this keyword, method overloading, local variables and member variables
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core