当前位置:网站首页>Wechat H5 official account to get openid climbing account
Wechat H5 official account to get openid climbing account
2022-07-05 08:47:00 【Front thought】

Before : The authorization of official account that has been done before is usually at the time of login , Recently, I came across a direct wechat to open the link sent by the enterprise wechat to enter the appointment page , When you need to enter, check whether the current wechat user has made an appointment for the corresponding application , If so, jump to index List page for , If not, leave it on the current appointment page ! Therefore, wechat users need to access wechat's internal applet / The unique identification of the user generated by the official account openid To query whether there is a corresponding data list !
In fact, when I didn't pay attention to the official account , User access to official account page , It will also produce a unique user and official account. OpenID
preparation :
1、 Certified official account ( Enterprise number can , Personal number cannot be bound H5 Of ), And get the official account appid
2、https Authentication domain name , The callback address cannot be configured in wechat without authenticated domain name , And the relevant domain name authorized by the webpage is configured in the background of official account , If not redirect_uri The domain name is still inconsistent with the background configuration 10003
3、 Wechat public platform certification passed , And create app
4、 Logic diagram :
Implementation steps :
1、 Finally, the address that needs to be spliced :
https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect}&response_type=code&scope=snsapi_base&state=#wechat_redirect
2、 Enter wechat to get code, It can only be initiated in wechat client , There are two ways ,scope=snsapi_base( Can only get openid, And it is obtained silently , There is no pop-up window ),scope=snsapi_userinfo( There are pop ups , You can not only get openid, You can also get the basic information of users' wechat )
3、 obtain code Intercept the link after success ! Use code Exchange with the backend openid!
The final code :
// This is a layer of judgment that must be processed in order to be compatible with other business logic
onLoad(options) {
var isMobile = window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i); // Whether the mobile terminal
var isWx = /micromessenger/i.test(navigator.userAgent); // Wechat or not
var isComWx = /wxwork/i.test(navigator.userAgent); // Is it enterprise wechat
if (options.id) {
this.id = options.id;
this.getDetail(options.id)
} else if (!options.id && !options.type && isWx && isMobile) {
this.getCode()
}
}
getCode() {
let appid = "XXXXXXXX"; // Personal developers appid
let redirect = encodeURIComponent(`https://XXXXXXX.com/pindex.html#/visitor/appointment/appointment`);
let wx_code = this.getUrlParam("code"); // Intercept url Medium code
if (!wx_code) {
// The return address will be in redirect On the basis of carrying a splice code Redirected back
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
appid}&redirect_uri=${
redirect}&response_type=code&scope=snsapi_base&state=#wechat_redirect`;
} else {
this.getOpenId(wx_code); // hold code Send it to the background to get users openid
}
},
getUrlParam: function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
},
Pit summary :
1、 The first mistake you will make : The background domain name is not configured 、appid Wrong writing 、redirect If you make a mistake, you will not be redirected back 、 Every time I come in, it's wrong wx_code Judgment will continue to flash …
2、480001:api Function not authorized :scope=snsapi_base Time is silent , The authorization interface does not pop up , Only users can be obtained openid, here api Not authorized by user , So you have to pull user information , It will pop up 480001. If you want to pull user information, please use snsapi_userinfo, When the user enters the page, the authorization confirmation interface will pop up
3、 obtain openid The requested backend domain name is http Of , Android system can adjust the background interface , And you can get openid Come back ! But in Apple system
Will request timeout ! Compared with most days , All parameters are the same , Later, it was changed back to https The agreement will do !
边栏推荐
猜你喜欢

每日一题——输入一个日期,输出它是该年的第几天

L298N module use
![[matlab] matlab reads and writes Excel](/img/80/78e4c7fcd27473526e480d4b930e2c.jpg)
[matlab] matlab reads and writes Excel

Guess riddles (10)

Business modeling of software model | vision

Daily question - input a date and output the day of the year

RT-Thread内核快速入门,内核实现与应用开发学习随笔记

Halcon color recognition_ fuses. hdev:classify fuses by color

Xrosstools tool installation for X-Series
![[Niuke brush questions day4] jz55 depth of binary tree](/img/f7/ca8ad43b8d9bf13df949b2f00f6d6c.png)
[Niuke brush questions day4] jz55 depth of binary tree
随机推荐
使用arm Neon操作,提高内存拷贝速度
U8g2 drawing
STM32 lights up the 1.8-inch screen under Arduino IDE
Shift operation of complement
【日常训练】1200. 最小绝对差
It cold knowledge (updating ing~)
微信H5公众号获取openid爬坑记
UE pixel stream, come to a "diet pill"!
特征工程
[noi simulation] juice tree (tree DP)
猜谜语啦(5)
Halcon: check of blob analysis_ Blister capsule detection
IT冷知识(更新ing~)
js异步错误处理
MATLAB小技巧(28)模糊综合评价
kubeadm系列-01-preflight究竟有多少check
暑假第一周
How can fresh students write resumes to attract HR and interviewers
Tips 1: Web video playback code
[Niuke brush questions day4] jz55 depth of binary tree