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

补充一下

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

诸君有用且点赞

相关推荐
追逐时光者1 天前
推荐 2 款简洁美观的微信公众号 Markdown 编辑器,让你不再为微信内容排版而发愁!
微信
陈思杰系统思考Jason4 天前
系统思考:经济反馈的循环
百度·微信·微信公众平台·新浪微博·微信开放平台
天朝八阿哥5 天前
PC上多开微信的方法
linux·windows·微信
悟空码字5 天前
微信公众号开发文档,这谁写的,要扣绩效吧
微信
avoidaily6 天前
vue2使用v-viewer图片预览:打开页面自动预览,禁止关闭预览,解决在微信浏览器的页面点击事件老是触发预览初始化的问题
微信
Kiri霧8 天前
Kotlin内联函数
android·开发语言·微信·kotlin
Kiri霧8 天前
Kotlin比较接口
android·java·前端·微信·kotlin
sunly_10 天前
Flutter:上传图片,选择相机或相册:wechat_assets_picker
数码相机·flutter·微信
陈思杰系统思考Jason11 天前
系统思考场景应用
百度·微信·微信公众平台·新浪微博·微信开放平台
泉城老铁11 天前
Spring Boot + Vue 实现微信扫码登录的详细攻略
vue.js·后端·微信