goPay.vue 9.62 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
<template>
  <view class="container">
    <view class="content">
      <view class="card">
        <view class="yearRow">
          <view class="label">缴费年限</view>
          <view class="control">
            <image v-if="form.renewYear > 1" class="icon" mode="widthFix" :src="config.baseUrl_api + '/fs/static/slices/dd_02.png'"
                   @click="minusYear"></image>
            <image v-else class="icon" mode="widthFix" :src="config.baseUrl_api + '/fs/static/slices/dd_02_g.png'"></image>
            <text class="num">{{ form.renewYear }}</text>
            <image v-if="form.renewYear < 5" class="icon" mode="widthFix" :src="config.baseUrl_api + '/fs/static/slices/btn_03.png'"
                   @click="plusYear"></image>
            <image v-else class="icon" mode="widthFix" :src="config.baseUrl_api + '/fs/static/slices/btn_03_g.png'"></image>
          </view>
        </view>
      </view>
      
      <view class="card ">
        <!-- 费用合计 -->
        <view class="row ">
          <text class="label">费用合计</text>
          <text class="value red">{{ (form.renewYear * memberFee).toFixed(2) }}</text>
        </view>
        
        <view v-if="preferentialPolicy" class="hintRow">
          <text
            class="hintText">温馨提示:根据中国跆协{{ preferentialData.name || '优惠' }}政策减免一年费用,每个单位在政策有效期内只享受一次
          </text>
        </view>
      </view>
      
      <view class="payRow ">
        <radio-group @change="onPayTypeChange">
          <label class="radioItem">
            <radio :checked="payType === '1'" class="custom-radio" value="1"/>
            <view class="payInfo">
              <image class="icon" mode="widthFix" :src="config.baseUrl_api + '/fs/static/slices/min.png'"></image>
              <text>民生付</text>
            </view>
          </label>
        </radio-group>
      </view>
      
      <view class="totalRow ">
        <text class="label">支付费用合计</text>
        <text class="value redBig">{{ memberTotalFee }}</text>
      </view>
    
    </view>
    
    <view class="bottomBtn">
      <view class="deductRow">
        <text class="label">减免费用</text>
        <text class="value red">-{{ memberFee.toFixed(2) }}</text>
      </view>
      <button :loading="isPaying" class="payBtn" @click="handelPay">立即支付 ¥{{ memberTotalFee }}</button>
    </view>
  
  </view>
</template>

<script setup>
import {
  ref,
  computed
} from 'vue'
import {
  onLoad
} from '@dcloudio/uni-app';
import to from 'await-to-js'
import * as api from '@/common/api.js'
import {minShengPay} from '@/common/pay.js'
import config from '@/config.js'
const form = ref({
  renewYear: 1
})
const memberFee = ref(0)
const preferentialPolicy = ref(false)
const preferentialData = ref({
  name: '优惠'
})
const payType = ref('1')
const isPaying = ref(false)

const memberTotalFee = computed(() => {
  if (preferentialPolicy.value) {
    return (memberFee.value * form.value.renewYear - memberFee.value * 1).toFixed(2)
  } else {
    return (memberFee.value * form.value.renewYear).toFixed(2)
  }
})

// 年限减
const minusYear = () => {
  if (form.value.renewYear > 1) {
    form.value.renewYear--
  }
}
// 年限加
const plusYear = () => {
  if (form.value.renewYear < 6) {
    form.value.renewYear++
  }
}

// 支付方式切换
const onPayTypeChange = (e) => {
  payType.value = e.detail.value
}

// 支付操作
const handelPay = async () => {
  if (memberTotalFee.value < 0) {
    uni.showToast({
      title: '支付金额异常',
      icon: 'none'
    })
    return
  }
  
  // 显示 loading
  uni.showLoading({
    title: '支付中...',
    mask: true
  })
  isPaying.value = true
  
  // 创建订单
  const [orderErr, orderRes] = await to(api.certifiedNew(form.value.renewYear))
  if (orderErr) {
    uni.hideLoading()
    isPaying.value = false
    // uni.showToast({
    // 	title: '创建订单失败',
    // 	icon: 'none'
    // })
    return
  }
  
  const data = orderRes.data
  // 无需支付,直接成功
  if (data.payFlag == 0) {
    uni.hideLoading()
    isPaying.value = false
    uni.redirectTo({
      url: `/myCenter/sucPay?orderId=${data.orderId}`
    })
    return
  }
  if (data.payResult.encryptedData) {
    const res = minShengPay(data.orderId, data.payResult.encryptedData)
    console.log(res)
  }
  // 需要支付回调
  // if (data.orderId) {
  //   await to(api.callBack2(data.orderId))
  //   uni.hideLoading()
  //   isPaying.value = false
  //
  //   uni.redirectTo({
  //     url: `/myCenter/sucPay?orderId=${data.orderId}`
  //   })
  // }
  
  
}

onLoad((option) => {
  // 接收年限
  form.value.renewYear = Number(option.renewYear || 1)
  // 初始化获取费用和优惠
  init()
})

// 初始化接口
async function init() {
  uni.showLoading({
    title: '加载中...'
  })
  const [err] = await to(Promise.all([
    getMyMemberCertUnitFeeApi(),
    canUseDiscountApi(),
    getZtxDiscountPolicyApi()
  ]))
  uni.hideLoading()
  if (err) {
    console.error('初始化失败:', err)
  }
}

// 获取会员单价
async function getMyMemberCertUnitFeeApi() {
  const [err, res] = await to(api.getMyMemberCertUnitFee())
  if (!err && res.data) {
    memberFee.value = Number(res.data || 1500)
  }
}

// 是否可用优惠
async function canUseDiscountApi() {
  const [err, res] = await to(api.canUseDiscount())
  if (!err && res.data !== undefined) {
    preferentialPolicy.value = res.data
  }
}

// 获取优惠政策详情
async function getZtxDiscountPolicyApi() {
  const [err, res] = await to(api.getZtxDiscountPolicy())
  if (!err && res.data) {
    preferentialData.value = res.data
  }
}
</script>

<style scoped>
/* 整体容器 */
.container {
  min-height: 100vh;
  background-color: #f7f7f7;
}

/* 内容区域 */
.content {
  padding: 20rpx 20rpx 120rpx;
}

/* 卡片 */
.card {
  background: #fff;
  border-radius: 8rpx;
  padding: 25rpx 20rpx;
  margin-bottom: 20rpx;
}

/* 缴费年限行 */
.yearRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20rpx;
}

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

.yearRow .control {
  display: flex;
  align-items: center;
}

.control image {
  width: 50rpx;
  height: 50rpx;
}

/* 加减按钮样式 */
.num-btn {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border: 1rpx solid #C4121B;
}

.num-btn.disabled {
  border-color: #ccc;
}

.num-btn.disabled .btn-icon {
  color: #ccc;
}

.btn-icon {
  font-size: 24rpx;
  color: #C4121B;
  font-weight: bold;
}

.yearRow .num {
  font-size: 28rpx;
  color: #333;
  min-width: 80rpx;
  text-align: center;
  margin: 0 10rpx;
}


/* 通用行 */
.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
}

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

.row .value {
  font-size: 30rpx;
  color: #C4121B;
  font-weight: 500;
}

/* 优惠提示 */
.hintRow {
  display: flex;
  align-items: flex-start;
  font-size: 24rpx;
  line-height: 1.4;
}

.hint-icon {
  width: 24rpx;
  height: 24rpx;
  border-radius: 50%;
  background-color: #C4121B;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10rpx;
  flex-shrink: 0;
  margin-top: 2rpx;
}

.icon {
  width: 30px;
}

.icon-check {
  color: #fff;
  font-size: 16rpx;
}

.hintRow .hintText {
  color: #FF8124;
  flex: 1;
  margin-top: 10rpx;
}

/* 减免费用 */
.deductRow {
  background: #fff;
  padding: 20rpx 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10rpx;
  border-radius: 8rpx;
}

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

.deductRow .value {
  font-size: 30rpx;
  color: #C4121B;
}

/* 支付方式行 */
.payRow {
  background: #fff;
  border-radius: 8rpx;
  padding: 20rpx 20rpx;
  margin-bottom: 20rpx;
}

.radioItem {
  display: flex;
  align-items: center;
}

/* 自定义红色单选框 */
::v-deep .custom-radio .wx-radio-input {
  width: 30rpx;
  height: 30rpx;
  border-radius: 50%;
  border: 2rpx solid #ccc;
}

::v-deep .custom-radio .wx-radio-input.wx-radio-input-checked {
  border-color: #C4121B !important;
  background: #C4121B !important;
}


.payInfo {
  display: flex;
  align-items: center;
  margin-left: 15rpx;
}

.payInfo .icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 10rpx;
}

.payInfo text {
  font-size: 28rpx;
  color: #333;
}

/* 总费用行(突出显示) */
.totalRow {
  background: #fff;
  border-radius: 8rpx;
  padding: 20rpx 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10rpx;
}

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

.redBig {
  font-size: 32rpx;
  color: #C4121B;
  font-weight: bold;
}

/* 底部按钮 */
.bottomBtn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 20rpx;
  background: #fff;
  border-top: 1rpx solid #eee;
}

.payBtn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  background-color: #C4121B;
  color: #fff;
  border-radius: 8rpx;
  font-size: 32rpx;
  text-align: center;
  border: none;
}

.payBtn[disabled] {
  background-color: #ccc;
  color: #999;
}

/* 通用红色文字 */
.red {
  color: #C4121B;
}
</style>