微信公众号(或微信浏览器)获取openId(网页授权)

下单支付需要openId 首先授权去拿到code --然后调用后太换取openId

1.去拿取code

下图中执行到window.location.href ( redirect_uri 传入当前路径-)–执行后重新跳转到当前页面–但是路径上会带上code参数
在这里插入图片描述

//然后调用后台方法–将code传给后台得到 openId

   if (this.is_wx()) {
      if (!uni.getStorageSync("openId")) {
        if (this.getCode() == "") {
			// const currenturl = location.href.split("#")[0];
		  uni.setStorageSync("currentUrl",window.location.href.split("#")[0])
          const currenturl = encodeURIComponent(window.location.href.split("#")[0]);
		  //访问这个链接 redirect_uri 传入当前路径---执行后重新跳转到当前页面--但是路径上会带上code参数
          window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${currenturl}&response_type=code&scope=snsapi_base#wechat_redirect`;
        } else {
          console.log("code");
          console.log(this.getCode());
          let code = this.getCode();
          //拿着code 传输给后台

          this.$request
            .get({
              url: `mp/api/code2accesstoken/${code}`,
            //   loadingTip: "加载中...",
              data: {},
            })
            .then((res) => {
				//将openId参数放进缓存
				uni.setStorageSync('openId',res.data.openid)
			});
        }
      }
    }

–至此支付下单接口需要的参数openId已经得到

官网参考链接
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0

补充一下
支付接口在测试环境是不支持的

诸君有用且点赞

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_39016177

你的鼓励将是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值