扫码关注官方订阅号
用sunny-ngrok穿透。可以直接访问。怎么接收微信服务器传来的GET值并判定?是前端VUE3接收再传到后端解析?还是直接发到后端,解析后把值传到前端吗?
实现微信授权流程
<?phpnamespace app\controller;use think\Controller;use think\Request; class WeChat extends Controller{ // 设置 AppID 和 AppSecret private $appId = 'your_app_id'; private $appSecret = 'your_app_secret'; // 获取 access_token public function getAccessToken() { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; $response = json_decode(file_get_contents($url), true); return $response['access_token']; } // 获取用户信息 public function getUserInfo($accessToken, $openid) { $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$accessToken}&openid={$openid}&lang=zh_CN"; $response = json_decode(file_get_contents($url), true); return $response; } // 处理微信回调 public function callback(Request $request) { $accessToken = $this->getAccessToken(); // 获取微信返回的 `code` 参数 $code = $request->param('code'); // 使用 `code` 获取 `openid` 和 `access_token` $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code={$code}&grant_type=authorization_code"; $response = json_decode(file_get_contents($url), true); $openid = $response['openid']; // 获取用户信息 $userInfo = $this->getUserInfo($accessToken, $openid); return json($userInfo); }}
前端 Vue 3 配置
export default { data() { return { userInfo: null, }; }, methods: { getUserInfo() { // 跳转到微信授权页面 window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=your_app_id&redirect_uri=your_redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`; }, }, mounted() { // 页面加载后获取用户信息 const code = new URLSearchParams(window.location.search).get('code'); if (code) { // 调用后端接口获取用户信息 this.$axios.get(`/wechat/callback?code=${code}`).then(response => { this.userInfo = response.data; }); } },};
你测试一下
不知道你
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
实现微信授权流程
前端 Vue 3 配置
你测试一下
不知道你