web微信扫码登录(unionid)
2022-04-06 本文已影响0人
秀萝卜
实现目标:
点击微信登录按钮,打开标题为微信登录的新标签页,上面有微信二维码,手机扫码跳转到登录页面,显示登录成功。
1.调用后台接口front/thirdparty/wxlogin 获取路径,返回如下url
h5调用
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
web调用
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_login&state=1#wechat_redirect
参数说明:
appid:自己的appid
redirect_uri:扫码成功后的回调地址,该路径需要在微信平台进行配置。
response_type=code:写死的,扫码成功后,会给你返回code参数
state=1:这个1是自己定的,这里作为区分是微信unionId登录。扫码成功后的回调地址会带上这个参数。
2.window.open打开新标签页,上面有微信二维码
3.手机扫码登录
4.页面跳转回redirect_uri的路径,该url多了一个参数code,前端获取参数code,调用后台接口front/thirdparty/wxcallback,
5.如果用户已经注册过,数据库有用户信息,那么成功登陆,返回用户信息
6.如果用户没有注册过,返回参数ifReg=false和unionId,数据库没有用户信息,需要用户绑定unionId。调用接口bindWeixin
通过参数已有账号的手机号,密码,unionId,type(标识是微信unoinId登陆),进行绑定,绑定成功,返回用户信息。