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

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

1.去拿取code

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

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

javascript 复制代码
   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

补充一下

支付接口在测试环境是不支持的

诸君有用且点赞

相关推荐
找不到对象就NEW一个16 小时前
wechatapi,微信二次开发-连载篇(二)通讯录模块
后端·微信
FeiHuo5651518 小时前
微信个人号开发中如何高效实现API二次开发
java·开发语言·python·微信
Gerardisite3 天前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
winfredzhang9 天前
用 Python + wxPython 打造一个万能网页图片一键下载神器(附完整源码 + 详细解析)
python·微信·pdf·遍历·图片·网页·保存
旭日跑马踏云飞10 天前
一文搞定腾讯开发生态-微信上传图片和视频
微信·serverless
llq_35010 天前
如何用 GitHub Pages 完成企业微信的域名归属认证?(适合没有服务器的开发者)
微信
今天也在研究公众号10 天前
Apple同款SVG,怎么写出来?手写+编辑器,两张方法都能搞定!
微信
开发加微信:hedian11612 天前
短剧小程序开发全攻略:技术选型与实现思路
微信·小程序·架构·aigc·交互
AiXed13 天前
PC微信协议之nid算法
python·网络协议·算法·微信