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

补充一下

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

诸君有用且点赞

相关推荐
微信开发api5 天前
微信iPad协议怎么用?基于协议的二次开发实践
微信·机器人·ipad
wechatbot8885 天前
极客互动企业微信 SCRM 自动运营平台效果实测
java·微信·企业微信·rpa
懂软件的胡子个哥5 天前
微信机器人为什么需要“回复候选”而不是所有 AI 内容直接发送
运维·微信·自动化·wechatapi·个人微信号二次开发
微信开发api5 天前
微信机器人接口:REST API vs WebSocket 选型建议
微信·机器人·ipad
极客互动API5 天前
极客互动-企业微信基于外部API接口实现AI客服自动接管外部联系人消息收发
java·微信·企业微信·ai编程·rpa
随性而行3605 天前
企业微信二次开发实战:基于企业微信API构建自动化营销触达系统
微信
前端小白乘风6 天前
VS Code 终于能连微信了!发现一个硬核开源神仙插件:WeChat AHP
微信·visual studio code·deepseek
M哥支付6 天前
商户池是什么?
服务器·网络·其他·微信·金融
孙严Pay6 天前
代付 VS 纯代付
笔记·科技·计算机网络·其他·微信
iPad协议个微协议6 天前
微信消息进入工单系统为什么需要候选层:WechatApi 接入后的服务流程设计
微信·自动化·微信开发·wechatapi·个人微信号二次开发·微信api·微信二次开发