微信公众号(或微信浏览器)获取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

补充一下

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

诸君有用且点赞

相关推荐
张居斜2 天前
Obsidian + Claude Code + 微信AI,我把这三个系统缝进了一个软件
微信·obsidian·claude code·molio
Jinkey5 天前
要用户手机号真的是为了打骚扰电话吗?浅谈微信生态会员账号体系与资产合并
后端·微信·微信小程序
2601_9618451510 天前
粉笔行测题库|系统班|刷题
网络·百度·微信·微信公众平台·facebook·新浪微博
weikecms10 天前
聚合返利CPS小程序快速搭建教程
人工智能·微信·小程序
沉默王二12 天前
用Codex+iLink Bot API给Agent接入微信,基于这个开源Skill
微信·agent·claude
Szime13 天前
小批量电子元器件采购为什么更难?从研发打样到试产交付
微信
五月君_13 天前
安卓也支持了!微信链接 Claude Code 保姆级教程
android·微信
人道领域13 天前
为什么iPhone微信聊天记录搜不到“?“,而安卓可以。
android·微信·iphone
weikecms16 天前
消费返物业费 + 小区本地生活 CPS 系统|微客云(物业 / 社区 / 本地服务商首选)
人工智能·微信·微客云
Thomas_YXQ17 天前
Unity3D Addressable 深度优化热更性能消耗
开发语言·3d·unity·微信