下单支付需要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
补充一下
支付接口在测试环境是不支持的
诸君有用且点赞