获取微信code

本文详细介绍了微信授权过程,重点讲解如何获取微信code,这是连接微信用户与你的前端应用或小程序的关键步骤。首先,你需要配置微信开发者平台,然后通过特定URL发起请求,引导用户授权。成功授权后,微信将返回一个code,使用该code换取access_token和openid,从而实现用户身份的识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getCodes() { // 非静默授权,第一次有弹框
        var local = window.location.href // 获取页面url
        http.postList('', {}).then(res => {
          if (res.code == 1) {
            var appid = res.data;
            if (this.code == null || this.code === '') { // 如果没有code,则去请求
              window.location.href =
                `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect`
            } else {
              // 你自己的业务逻辑
              // console.log('已授权')
            }
          }
        })
      },
      getUrlCode() { // 截取url中的code方法
        // console.log(22)
        var url = location.search
        this.winUrl = url
        var theRequest = new Object()
        if (url.indexOf("?") != -1) {
          var str = url.substr(1)
          var strs = str.split("&")
          for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
          }
        }
        return theRequest
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值