payOrder.vue 12 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
<template>
  <view class="pay-order-container">

    <!-- 订单核心信息 -->
    <view class="order-info">
      <view class="info-item">
        <text class="label">缴费单位:</text>
        <text class="value normal">{{ displayPayName }}</text>
      </view>
      <view class="info-item">
        <text class="label">所属协会:</text>
        <text class="value normal">{{ displayAssoName }}</text>
      </view>
      <view class="info-item">
        <text class="label">人数合计:</text>
        <text class="value red">{{ formData.all ?? 0 }}</text>
      </view>
      <view class="info-item">
        <text class="label">新会员合计:</text>
        <text class="value red">{{ formData.new ?? 0 }}</text>
      </view>
      <view class="info-item">
        <text class="label">续费会员合计:</text>
        <text class="value red">{{ formData.old ?? 0 }}</text>
      </view>
      <view class="info-item total-price">
        <text class="label">支付总费用:</text>
        <text class="value red">{{ formData.price ?? 0 }}</text>
      </view>
    </view>

    <!-- 支付方式选择 -->
    <view class="pay-type-section">
      <view class="section-title">选择支付方式</view>
      <view class="payment-methods">
        <radio-group @change="handlePayTypeChange">
          <label :class="{ selected: payType === '0' }" class="payment-item">
            <radio :checked="payType =='0'" value="0"/>
            <image :src="config.baseUrl_api + '/fs/static/min.png'" class="icon ml10" mode="widthFix"></image>
            <text class="pay-name ml10">民生付</text>
          </label>
          <label :class="{ selected: payType === '3' }" class="payment-item">
            <radio :checked="payType === '3'" value="3"/>
            <image :src="config.baseUrl_api + '/fs/static/min.png'" class="icon ml10" mode="widthFix"></image>
            <text class="pay-name ml10">对公转账</text>
          </label>
        </radio-group>
      </view>
    </view>
    
    <!-- 对公转账表单 -->
    <view v-if="payType === '3'" class="transfer-form">
      <view class="form-item">
        <text class="form-label">联系人</text>
        <input v-model="form.contactPerson" class="form-input" placeholder="请输入联系人"/>
      </view>
      <view class="form-item">
        <text class="form-label">联系电话</text>
        <input v-model="form.contactTel" class="form-input" placeholder="请输入联系电话" type="number"/>
      </view>
    </view>
    
    <!-- 底部支付按钮 -->
    <view class="fixed-bottom">
      <button :loading="payLoading" class="pay-btn red-bg" @click="handlePay">立即支付</button>
    </view>
  </view>
</template>

<script setup>
import {
  computed,
  ref
} from 'vue'
import {
  onLoad
} from '@dcloudio/uni-app';
import * as api from '@/common/api.js'
import config from '@/config.js'
import {minShengPay} from "@/common/pay";

// 核心数据
const formData = ref({}) // 订单统计数据
const rangeId = ref('') // 核心业务ID
const orderId = ref('') // common/order 订单ID,用于订单列表再次支付
const payName = ref('')
const assoName = ref('')
const payType = ref('0') // 支付方式(默认0=民生付)
const payLoading = ref(false) // 支付按钮加载状态
const form = ref({
  contactPerson: '',
  contactTel: ''
})
const app = getApp()
const memberInfo = app.globalData?.memberInfo || {}
const deptInfo = app.globalData?.dept || app.globalData?.userInfo?.dept || {}

const pickFirst = (...values) => {
  const value = values.find(item => item !== undefined && item !== null && String(item).trim() !== '')
  return value ? String(value) : ''
}

const displayPayName = computed(() => {
  return pickFirst(
    memberInfo.name,
    memberInfo.deptName,
    deptInfo.deptName,
    formData.value.orderName,
    formData.value.payDeptName,
    formData.value.payMemName,
    formData.value.memberName,
    payName.value,
    formData.value.memName
  ) || '-'
})

const displayAssoName = computed(() => {
  return pickFirst(
    deptInfo.aname,
    deptInfo.associateName,
    memberInfo.aname,
    memberInfo.associateName,
    memberInfo.assoName,
    formData.value.assoName,
    formData.value.associateName,
    formData.value.associationName,
    formData.value.aname,
    formData.value.parentName,
    assoName.value
  ) || '-'
})

// 页面加载接收参数
onLoad(async (options) => {
  console.log('缴费范围ID:', options.rangeId, '订单ID:', options.orderId)
  payName.value = decodeURIComponent(options.payName || '')
  assoName.value = decodeURIComponent(options.assoName || '')
  if (options.orderId) {
    orderId.value = options.orderId
  }
  if (options.rangeId) {
    rangeId.value = options.rangeId
    await getCount()
  }
  if (options.orderId) {
    await getOrderDetail()
  }
})

async function getCount() {
  try {
    const res = await api.getNewCountByRangeId(rangeId.value)
    formData.value = {
      ...formData.value,
      ...(res.data || {
        all: 0,
        new: 0,
        old: 0
      })
    }
  } catch (e) {
    formData.value = {
      ...formData.value,
      all: 0,
      new: 0,
      old: 0
    }
  }
}

async function getOrderDetail() {
  try {
    const res = await api.orderDetail(orderId.value)
    const data = res.data || {}
    let content = {}
    if (data.content) {
      try {
        content = typeof data.content === 'string' ? JSON.parse(data.content) : data.content
      } catch (e) {
        content = {}
      }
    }
    formData.value = {
      ...formData.value,
      ...data,
      ...content,
      all: content.allPersonCount ?? data.allCount ?? data.all ?? 0,
      new: content.newPersonCount ?? data.newCount ?? data.new ?? 0,
      old: content.renewPersonCount ?? data.oldCount ?? data.old ?? 0,
      price: data.price ?? content.price ?? 0
    }
    payName.value = pickFirst(data.orderName, content.orderName, data.payDeptName, data.payMemName, content.payDeptName, content.payMemName, data.memberName, content.memberName, payName.value)
    assoName.value = pickFirst(data.assoName, data.associateName, data.associationName, data.aname, data.parentName, content.assoName, content.associateName, content.associationName, content.aname, content.parentName, assoName.value)
  } catch (e) {
    formData.value = {
      ...formData.value,
      all: 0,
      new: 0,
      old: 0
    }
  }
}

// 支付方式切换
function handlePayTypeChange(e) {
  payType.value = e.detail.value
  console.log('支付方式:', payType.value)
  if (payType.value === '3') {
    form.value.contactPerson = ''
    form.value.contactTel = ''
  }
}

// 立即支付核心逻辑
async function handlePay() {
  // 基础校验
  if ((!rangeId.value && !orderId.value) || rangeId.value === '-1') {
    return uni.showToast({
      title: '订单ID异常',
      icon: 'none'
    })
  }
  
  // 对公转账校验
  if (payType.value === '3') {
    if (!form.value.contactPerson) {
      return uni.showToast({title: '请输入联系人', icon: 'none'})
    }
    if (!form.value.contactTel) {
      return uni.showToast({title: '请输入联系电话', icon: 'none'})
    }
    // 手机号格式校验
    if (!/^1[3-9]\d{9}$/.test(form.value.contactTel)) {
      return uni.showToast({title: '请输入正确的手机号', icon: 'none'})
    }
  }
  
  try {
    payLoading.value = true
    uni.showLoading({
      title: '提交中...',
      mask: true
    })

    // 构建请求参数
    const params = {
      id: orderId.value || rangeId.value,
      payType: payType.value
    }
    if (payType.value === '3') {
      params.contactPerson = form.value.contactPerson
      params.contactTel = form.value.contactTel
    }

    const res = orderId.value ? await api.payForOrder(params) : await api.goPay(params)
    const resData = res.data
    
    // 对公转账 - 跳转转账信息页面
    if (resData.payFlag == 2) {
      uni.hideLoading()
      uni.redirectTo({
        url: `/myCenter/transferPay?orderId=${resData.orderId}`
      })
      return
    }
    
    // 民生付
    if (resData.payResult && resData.payResult.encryptedData) {
      const reason = await minShengPay(resData.orderId, resData.payResult.encryptedData)
      if (reason == 'OK') {
        uni.showToast({title: '支付成功', icon: 'success'})
        setTimeout(() => {
          uni.hideLoading()
          uni.redirectTo({
            url: `/personalVip/payment`
          })
        }, 1500)
      }
    }
    
  } catch (err) {
    console.log(err)
    const errMsg = err?.data?.msg || err?.message || '支付失败,请稍后重试'
    uni.showToast({
      title: errMsg,
      icon: 'none'
    })
  } finally {
    uni.hideLoading()
    payLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
.pay-order-container {
  padding: 30rpx;
  background-color: #fff;
  min-height: 100vh;
  box-sizing: border-box;
}

.icon {
  width: 30px;
}

// 页面头部
.page-header {
  text-align: center;
  padding: 20rpx 0;
  border-bottom: 1px solid #eee;
  margin-bottom: 40rpx;

  .title {
    font-size: 36rpx;
    font-weight: 600;
    color: #333;
  }
}

// 订单信息区域
.order-info {
  margin-bottom: 60rpx;
  border-top: 1px solid #f5f5f5;
  border-bottom: 1px solid #f5f5f5;

  .info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 25rpx 0;
    border-bottom: 1px solid #f5f5f5;
    font-size: 32rpx;

    .label {
      color: #666;
      flex-shrink: 0;
    }

    .value {
      font-weight: 600;
      font-size: 34rpx;
      text-align: right;
      word-break: break-all;
    }

    .normal {
      color: #111;
      font-size: 30rpx;
    }

    .red {
      color: #E60012;
    }
  }

  .total-price {
    border-bottom: none;
    margin-top: 10rpx;

    .label {
      font-size: 34rpx;
      color: #333;
    }

    .value {
      font-size: 38rpx;
    }
  }
}

// 支付方式区域
.pay-type-section {
  margin-bottom: 30rpx;

  .section-title {
    font-size: 32rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 20rpx;
    position: relative;
    padding-left: 20rpx;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 6rpx;
      height: 28rpx;
      background: linear-gradient(180deg, #FF755A, #F51722);
      border-radius: 3rpx;
    }
  }
}

.payment-methods {
  background: #f8f9fa;
  border-radius: 12rpx;
  padding: 20rpx;

  .payment-item {
    display: flex;
    align-items: center;
    padding: 16rpx;
    border-radius: 12rpx;
    border: 2rpx solid transparent;

    &.selected {
      border-color: #e4393c;
      background: #fff;
    }
    
    // 覆盖原生 radio 样式
    ::v-deep radio .wx-radio-input,
    ::v-deep radio .uni-radio-input {
      width: 36rpx;
      height: 36rpx;
      border-color: #ccc !important;
    }
    
    ::v-deep radio .wx-radio-input.wx-radio-input-checked,
    ::v-deep radio .uni-radio-input-checked {
      border-color: #e4393c !important;
      background: #e4393c !important;
    }
    
    .icon {
      width: 40rpx;
      height: 40rpx;
    }
    
    .pay-name {
      font-size: 30rpx;
      color: #333;
      font-weight: 500;
    }
    
    .ml10 {
      margin-left: 10rpx;
    }
  }
}

// 对公转账表单
.transfer-form {
  background: #f8f9fa;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 30rpx;
  
  .form-item {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
    border-bottom: 1rpx solid #eee;
    
    &:last-child {
      border-bottom: none;
    }
  }
  
  .form-label {
    font-size: 28rpx;
    color: #333;
    width: 140rpx;
    flex-shrink: 0;
  }
  
  .form-input {
    flex: 1;
    font-size: 28rpx;
    color: #333;
    text-align: right;
  }
}

// 底部支付按钮
.fixed-bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx 30rpx;
  background-color: #fff;
  border-top: 1px solid #eee;
  
  .pay-btn {
    width: 100%;
    height: 88rpx;
    line-height: 88rpx;
    border-radius: 44rpx;
    font-size: 34rpx;
    font-weight: 600;
  }
  
  .red-bg {
    background-color: #E60012;
    color: #fff;
  }
}
</style>