vue3+tp6怎么加入微信公众号啊
凡人
凡人 2025-04-23 22:37:56
[PHP讨论组]

用sunny-ngrok穿透。可以直接访问。怎么接收微信服务器传来的GET值并判定?是前端VUE3接收再传到后端解析?还是直接发到后端,解析后把值传到前端吗?

凡人
凡人

全部回复(2)

实现微信授权流程

<?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中文网用户-7430067

不知道你

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号