Micro-channel user authorization

Micro-channel user information
This function also requires public No authentication is successful to use, obtain user information is to get the user's openId and picture and name, then deposit into the database, openId is the need for absolute confidentiality, access to user information in two ways corresponding to the two design type species code

Two design patterns

Every licensed pop design patterns

// 当用户访问地址url,url不能是hash地址,也就是地址栏不能有【#】
// 前端js判断地址栏url有没有code参数,没有就重定向
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo#wechat_redirect
// 注意 scope=snsapi_userinfo,还有appid要带上,其他几个值是固定的
// 微信会自动的获取一个code,并跳回redirect_uri后面的地址,并把code接到地址上
// 也就是说虽然打开的页面没变,但是地址栏上多了个code参数,并且页面刷新了两次
// 把code传给后端,后端把code拿去调用接口跟微信换去用户的access_token和openId,跟上一个笔记的access_token不是一样的
// 再拿用户access_token和openId调用接口跟微信换去用户的信息
// 再把数据和openId一起存进数据库

Silent authorized design patterns

// 当用户访问地址url,url不能是hash地址,也就是地址栏不能有【#】
// 前端js判断地址栏url有没有code参数,没有就重定向
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_base#wechat_redirect
// 注意 scope=snsapi_base,还有appid要带上,其他几个值是固定的
// 微信会自动的获取一个code,并跳回redirect_uri后面的地址,并把code接到地址上
// 也就是说虽然打开的页面没变,但是地址栏上多了个code参数,并且页面刷新了两次
// 把code传给后端,后端把code拿去调用接口跟微信换去用户的openId【没有access_token】
// 因为这种方式没有access_token,所以无法获取用户信息,把openId存进数据库就行
// 我要用户信息怎么办,在需要用户信息的地方,跟后端请求数据,后端查一下数据库表示也没有,前端提示您还没有关注公众号,让用户去关注公众号
// 用户只要关注了公众号,上一个笔记有关注公众号的请求,能够拿到用户的access_token和openId,通过这两个值可以用微信的接口获取用户信息,然后存进对应openId的空数据库里就行

It can be seen that the two modes access_token difference is the presence or absence, and the first one is directly authorized, but every time the user is authorized [offensive] authorized, and the second is silent authorized, but requires user attention, I recommend first two kinds, follow the above code stickers out, so stay tuned ....

Both are implemented under the trailing edge separating the former case, if not before and after the end of the separation, the back-end server page is returned, the operation authorization page is redirected back end to do, because the address bar appears after the code, or will it again request a server, this time a code, the code does not need to take the initiative to pass up the front end, we can say that the entire process implemented by the backend

Supplement
the notes and the notes of the code may be a number of plug-in co-wechat, co-wechat-api, co-wechat-oauthin place of, the relevant articles from
the micro-channel is turned
oAuth authentication

Guess you like

Origin www.cnblogs.com/pengdt/p/12072517.html