奶昔女孩 發表於 2022-10-27 16:35:00

微信小程序开通无感支付(微信支付分)--uni-app版

<p>1、打开微信支付分页面:首先需要将这个小程序绑定的商户号开通微信支付分服务,然后使用navigateTominiProgram跳转微信支付分小程序,跳转时需要传参,其中appId和path是固定的(为要跳转的小程序的参数,此时使用的是微信支付分的参数),然后在extraData中设置的是当前小程序的一些参数。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    uni.navigateToMiniProgram({
      appId: </span>'wxbcad394b3d99dac9'<span style="color: rgba(0, 0, 0, 1)">,
      path: </span>'/pages/auth-creditpay/auth-creditpay'<span style="color: rgba(0, 0, 0, 1)">,
      extraData: {
            mchid: uni.config.mchid,
            openid: uni.getStorageSync(</span>'openId'<span style="color: rgba(0, 0, 0, 1)">),
            plate_number: plateNumber, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 车牌号</span>
            plate_color: plateColor, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 车牌颜色</span>
            trade_scene: 'PARKING' <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用场景</span>
<span style="color: rgba(0, 0, 0, 1)">      }
    }); </span></pre>
</div>
<p>2、开通后会返回自身的小程序,在App.vue中的onShow中获取参数referrerInfo中的appId,判断是否从微信支付分页面返回,然后存储标识。</p>
<div class="cnblogs_code">
<pre> onShow: <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (res.scene === 1038) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 场景值1038:从被打开的小程序返回</span>
      const { appId, extraData } =<span style="color: rgba(0, 0, 0, 1)"> res.referrerInfo;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (appId == 'wxbcad394b3d99dac9') { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> appId为wxbcad394b3d99dac9:从车主小程序跳转回来</span>
      uni.setStorageSync('isWeixinBack', <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
      }
    }   
}</span></pre>
</div>
<p>3、最后在需要跳转微信支付分的页面的onShow中判断标识是否为true,如果是说明是从微信支付分小程序返回,可以进行编写开通后的一些接口调用代码。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">onShow() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (uni.getStorageSync('isWeixinBack'<span style="color: rgba(0, 0, 0, 1)">)) {
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可进行开通后的一些操作                        </span>
       uni.removeStorageSync('isWeixinBack'<span style="color: rgba(0, 0, 0, 1)">);
    }
}</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/bella99/p/16832569.html
頁: [1]
查看完整版本: 微信小程序开通无感支付(微信支付分)--uni-app版