微信网页登录

封装接口页:

const wxLogin = (appid, url) => {
  let redirect_uri = encodeURIComponent(url)
  window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATUS#wechat_redirect`
}
参数说明:

1、let redirect_uri = encodeURIComponent(url)  ——  授权后重定向的回调链接地址,并把字符串编码为 URI 组件。

2、windows.location.href="/url"  ——  当前页面打开URL页面
3、appid  -  公众号的唯一标识(必填)
4、redirect_uri  -  授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理(必填)
5、response_type  -  返回类型,请填写code(必填)
6、scope  -  应用授权作用域,snsapi_base(静默登录,不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )(必填)
7、state  -  重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节(非必填)
8、#wechat_redirect  -  无论直接打开还是做页面302重定向时候,必须带此参数(必填)

1、第一个参数用?号隔开,之后的参数用&隔开,最后的#wechat_redirect不用加&,
2、open.weixin.qq.com/connect/oauth2/authorize  微信默认地址


页面中使用:
template:
 <div @click="wxlogin">
     <p>使用微信登录</p>
 </div>

 

script
import {wxLogin} from "@/api/apis";

// methods
wxlogin(){
        wxLogin('wx546563fe38304393', 'http://whc.mouhua.cn/mobile/v-accredit')
}
 第一个参数是公众号的appid,第二个是跳转的默认空白页。

猜你喜欢

转载自www.cnblogs.com/lzuku/p/11304590.html