index.vue 5.75 KB
<template>
  <view class="pay-test-page">
    <view class="hero-card">
      <text class="hero-title">测试中心</text>
      <text class="hero-desc">
        这里统一承接当前首页里的测试能力,包括 WebView 预览和微信支付拉起测试。
      </text>
    </view>

    <view class="panel">
      <view class="panel-head">
        <text class="panel-title">调试入口</text>
      </view>
      <text class="panel-tip">
        如需验证 H5 通过小程序桥页发起支付,可先从这里进入 WebView 预览页。
      </text>
      <button class="outline-btn" @tap="goToWebviewPreview">
        打开 WebView 预览页
      </button>
    </view>

    <view class="panel">
      <view class="panel-head">
        <text class="panel-title">授权状态</text>
        <text class="auth-tag" :class="{ authed: is_authed }">
          {{ is_authed ? '已授权' : '未授权' }}
        </text>
      </view>
      <text class="panel-tip">
        当前 sessionid:{{ sessionid_preview }}
      </text>
      <button class="outline-btn" :loading="auth_loading" @tap="handleRefreshAuth">
        重新静默授权
      </button>
    </view>

    <view class="panel">
      <view class="panel-head">
        <text class="panel-title">支付测试</text>
      </view>
      <text class="field-label">测试订单 ID</text>
      <input
        class="pay-input"
        type="text"
        :value="order_id"
        placeholder="请输入 meihuaApp 后端里的测试订单 ID"
        @input="handleOrderIdInput"
      />
      <text class="panel-tip">
        说明:这里只要求拉起微信支付弹框,不要求支付成功;请使用后端可生成支付参数的未支付订单。
      </text>
      <button class="primary-btn" :loading="pay_loading" @tap="handlePay">
        点击测试拉起微信支付
      </button>
    </view>

    <view class="panel">
      <view class="panel-head">
        <text class="panel-title">最近结果</text>
      </view>
      <text class="result-text">{{ result_text }}</text>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'
import Taro, { useDidShow, useLoad } from '@tarojs/taro'
import { useWechatMiniPay } from '@/composables/useWechatMiniPay'

const order_id = ref('')
const should_auto_pay = ref(false)
const has_auto_started = ref(false)

const {
  auth_loading,
  pay_loading,
  is_authed,
  sessionid_preview,
  last_result_text,
  sync_auth_state,
  refresh_auth,
  pay_by_order_id,
} = useWechatMiniPay()

const result_text = ref(last_result_text.value)

watch(last_result_text, (value) => {
  result_text.value = value
})

const handleOrderIdInput = (event) => {
  order_id.value = event?.detail?.value || ''
}

const handleRefreshAuth = async () => {
  await refresh_auth({
    force_refresh: true,
    show_loading: true,
    on_status: (text) => {
      result_text.value = text
    },
  })
}

const goToWebviewPreview = () => {
  Taro.navigateTo({
    url: '/pages/webview-preview/index',
  })
}

const handlePay = async () => {
  await pay_by_order_id(order_id.value, {
    auto_auth: false,
    on_status: (text) => {
      result_text.value = text
    },
  })
}

useLoad((options) => {
  const route_order_id = String(options?.order_id || '').trim()
  if (route_order_id) {
    order_id.value = route_order_id
    result_text.value = '已接收到来自 H5/WebView 的订单参数,准备测试支付。'
  }

  should_auto_pay.value = String(options?.auto_pay || '') === '1'
})

useDidShow(() => {
  sync_auth_state()

  if (should_auto_pay.value && order_id.value && !has_auto_started.value) {
    has_auto_started.value = true

    setTimeout(async () => {
      await handlePay()
    }, 300)
  }
})
</script>

<style lang="less">
.pay-test-page {
  min-height: 100vh;
  padding: 32rpx 24rpx 48rpx;
  box-sizing: border-box;
  background:
    radial-gradient(circle at top left, rgba(255, 214, 165, 0.45), transparent 36%),
    linear-gradient(180deg, #fff9f0 0%, #f4f6fb 100%);

  .hero-card,
  .panel {
    background: rgba(255, 255, 255, 0.92);
    border: 2rpx solid rgba(17, 24, 39, 0.05);
    border-radius: 28rpx;
    padding: 32rpx;
    box-sizing: border-box;
    box-shadow: 0 20rpx 60rpx rgba(15, 23, 42, 0.06);
    backdrop-filter: blur(10rpx);
  }

  .hero-card {
    margin-bottom: 24rpx;
  }

  .hero-title {
    display: block;
    font-size: 40rpx;
    font-weight: 700;
    color: #111827;
  }

  .hero-desc {
    display: block;
    margin-top: 16rpx;
    font-size: 26rpx;
    line-height: 1.7;
    color: #6b7280;
  }

  .panel {
    margin-bottom: 24rpx;
  }

  .panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20rpx;
  }

  .panel-title {
    font-size: 32rpx;
    font-weight: 600;
    color: #111827;
  }

  .auth-tag {
    padding: 8rpx 18rpx;
    border-radius: 999rpx;
    font-size: 24rpx;
    color: #b45309;
    background: #fef3c7;
  }

  .authed {
    color: #166534;
    background: #dcfce7;
  }

  .field-label {
    display: block;
    margin-bottom: 16rpx;
    font-size: 26rpx;
    color: #4b5563;
  }

  .panel-tip,
  .result-text {
    display: block;
    font-size: 24rpx;
    line-height: 1.7;
    color: #6b7280;
  }

  .panel-tip {
    margin-top: 16rpx;
  }

  .pay-input {
    width: 100%;
    height: 88rpx;
    padding: 0 24rpx;
    border-radius: 20rpx;
    background: #f9fafb;
    border: 2rpx solid #e5e7eb;
    box-sizing: border-box;
    font-size: 28rpx;
    color: #111827;
  }

  .primary-btn,
  .outline-btn {
    margin-top: 24rpx;
    border-radius: 999rpx;
    font-size: 30rpx;
    line-height: 88rpx;
  }

  .primary-btn {
    color: #fff;
    background: linear-gradient(135deg, #0f766e, #115e59);
  }

  .outline-btn {
    color: #0f172a;
    background: #fff;
    border: 2rpx solid #d1d5db;
  }
}
</style>