- https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index 申请公众号测试号
- 网页服务——网页账号——网页授权获取用户基本信息 设置重定向域名
调用微信API 跳转授权页面,授权成功后,跳转重定向页面
<template> <div> <el-button @click="getWeChatCode">公众号登录</el-button> </div> </template> <script> export default { methods: { getWeChatCode() { // let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址 let appid = 'wxa31dae82514157b9' let local = 'http%3A%2F%2F127.0.0.1%3A8080%2F' //通过微信官方接口获取code之后,会重新刷新设置的回调地址【redirect_uri】 window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa31dae82514157b9' + '&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2F%23%2Fabout' + '&response_type=code' + '&scope=snsapi_userinfo' + '&state=123#wechat_redirect' } } } </script>重定向页面的url中能拿到code
通过 code 获取网页授权access_token
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxa31dae82514157b9&secret=1edcf8ed9b98f8cfcb342c8c2053620b&code=061qKLml2i1tfa4HKgnl2tGaBv2qKLmc&grant_type=authorization_code通过access_token拉取用户信息(需 scope 为 snsapi_userinfo)
https://api.weixin.qq.com/sns/userinfo?access_token=62_f9OLnoslXklfbcExFGgNds86JVqsd9aKcy0Zs5CRksaiVHbVmvKKPKFJfm_c-coJulKlFCm5F8aOE4GQNJjWy0uz6nMOIzYzSdM-SbIJPFg&openid=ol_O45sjngYWypE8P9wvoaWz0_bE&lang=zh_CN
snsapi_userinfo 和snsapi_base的区别
- snsapi_base 用户无感知,不能拿到用户信息,但是能拿到openid
- snsapi_userinfo 用户有感知,需要用户在授权页面授权,能拿到用户信息。