weappPayBridge.vue 6.47 KB
<template>
  <div class="weapp-pay-bridge-page">
    <div class="hero-card">
      <div class="eyebrow">WebView 支付测试</div>
      <h1 class="title">从 H5 跳转到小程序支付桥页</h1>
      <p class="desc">
        这个页面用于在小程序 WebView 中点击按钮后,直接跳到外层小程序的原生支付桥页,并在桥页里自动拉起微信支付弹框。
      </p>
    </div>

    <div class="panel">
      <div class="panel-head">
        <span class="panel-title">当前环境</span>
        <span :class="['env-tag', isMiniProgramWebView ? 'env-ok' : 'env-warn']">
          {{ isMiniProgramWebView ? '小程序 WebView 内' : '普通浏览器' }}
        </span>
      </div>
      <p class="hint">只有在小程序 WebView 中,点击下方按钮才会真正调用外层小程序页面。</p>
    </div>

    <div class="panel">
      <div class="panel-head">
        <span class="panel-title">支付参数</span>
      </div>
      <label class="field-label" for="orderId">测试订单 ID</label>
      <input
        id="orderId"
        v-model.trim="orderId"
        class="field-input"
        type="text"
        placeholder="请输入后端可生成支付参数的 order_id"
      />
      <p class="hint">建议使用未支付且可以正常返回支付参数的测试订单。</p>
      <div class="path-box">
        <div class="path-label">将跳转的小程序支付桥页</div>
        <div class="path-value">{{ payBridgePagePath }}</div>
      </div>
      <button class="primary-btn" @click="goToMiniProgramPayBridge">点击触发小程序支付测试</button>
    </div>

    <div class="panel">
      <div class="panel-head">
        <span class="panel-title">最近结果</span>
      </div>
      <div class="result-text">{{ resultText }}</div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue';
import { showToast } from 'vant';
import wx from 'weixin-js-sdk';

const orderId = ref('');
const resultText = ref('等待开始测试');

const isMiniProgramWebView = computed(() => {
  return navigator.userAgent.includes('miniProgram');
});

/**
 * 说明:
 * 当前页面只是「H5 -> 小程序支付桥页」的最小测试页,只负责把 order_id 传给外层小程序。
 * 小程序桥页支付完成后,会通过 navigateBack 返回 WebView,但不会把支付结果主动回调给当前 H5 页面。
 *
 * 如果后续要按正式业务接入
 * 1. H5 发起支付前记录当前 order_id,并标记“等待支付结果”;
 * 2. 从小程序桥页返回后,在 pageshow / visibilitychange / focus 等时机触发查单;
 * 3. 调后端订单状态接口,以服务端订单状态作为最终支付结果;
 * 4. 如后端存在支付落账延迟,可按 1~2 秒间隔短轮询 3~5 次。
 */
const payBridgePagePath = computed(() => {
  const normalizedOrderId = encodeURIComponent(orderId.value || '');
  return `/pages/pay-bridge/index?order_id=${normalizedOrderId}&auto_back=1&back_delay=2&back_mode=navigateBack&back_url=${encodeURIComponent('/pages/webview-preview/index')}&source=weapp-pay-bridge`;
});

const ensureOrderId = () => {
  if (orderId.value) return true;

  showToast('请先输入订单 ID');
  resultText.value = '未填写订单 ID,无法继续触发支付测试。';
  return false;
};

const goToMiniProgramPayBridge = () => {
  if (!ensureOrderId()) return;

  if (!isMiniProgramWebView.value) {
    resultText.value = '当前不是小程序 WebView 环境,无法跳转到外层小程序支付桥页。';
    showToast('请在小程序 WebView 中打开此页');
    return;
  }

  try {
    wx.miniProgram.navigateTo({
      url: payBridgePagePath.value,
      success: () => {
        resultText.value = `已请求跳转到小程序支付桥页:${payBridgePagePath.value}`;
      },
      fail: (error) => {
        const message = error?.errMsg || '跳转失败';
        resultText.value = `调用小程序支付桥页失败:${message}`;
        showToast(message);
      },
    });
  } catch (error) {
    const message = error?.message || '调用 wx.miniProgram.navigateTo 失败';
    resultText.value = message;
    showToast(message);
  }
};
</script>

<style scoped lang="less">
.weapp-pay-bridge-page {
  height: 100vh;
  padding: 24px 18px 40px;
  box-sizing: border-box;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(circle at top left, rgba(84, 171, 174, 0.18), transparent 28%),
    linear-gradient(180deg, #f7fbfb 0%, #eef4f6 100%);
  color: #16323a;
}

.hero-card,
.panel {
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(22, 50, 58, 0.08);
  border-radius: 22px;
  box-shadow: 0 18px 42px rgba(22, 50, 58, 0.08);
  padding: 22px 18px;
  box-sizing: border-box;
}

.hero-card {
  margin-bottom: 16px;
}

.eyebrow {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 999px;
  background: #e0f2f1;
  color: #0f766e;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
}

.title {
  margin: 14px 0 0;
  font-size: 28px;
  line-height: 1.25;
}

.desc,
.hint,
.result-text {
  margin: 12px 0 0;
  font-size: 14px;
  line-height: 1.7;
  color: #4f6470;
}

.panel {
  margin-bottom: 16px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-title {
  font-size: 18px;
  font-weight: 700;
  color: #16323a;
}

.env-tag {
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.env-ok {
  color: #166534;
  background: #dcfce7;
}

.env-warn {
  color: #9a3412;
  background: #ffedd5;
}

.field-label {
  display: block;
  margin-top: 16px;
  margin-bottom: 8px;
  font-size: 14px;
  color: #36505b;
}

.field-input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border: 1px solid #cfd8dc;
  border-radius: 14px;
  box-sizing: border-box;
  background: #f9fbfb;
  font-size: 15px;
  color: #16323a;
}

.path-box {
  margin-top: 14px;
  padding: 14px;
  border-radius: 14px;
  background: #f2f7f8;
  border: 1px dashed #b6c8cf;
}

.path-label {
  font-size: 12px;
  color: #68808a;
}

.path-value {
  margin-top: 6px;
  font-size: 13px;
  line-height: 1.6;
  word-break: break-all;
  color: #16323a;
}

.primary-btn,
.ghost-btn {
  width: 100%;
  height: 46px;
  border: none;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn {
  margin-top: 16px;
  color: #fff;
  background: linear-gradient(135deg, #0f766e, #155e75);
}

.ghost-btn {
  margin-top: 10px;
  color: #155e75;
  background: #e6f4f1;
}
</style>