lock.uvue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script setup lang='ts'>
  2. import { wechatPay, wechatPayRequest, wechatPayQuery, wechatPayCancel } from '@/services/user'
  3. import { ref } from 'vue'
  4. const props = defineProps({
  5. record: {
  6. type: Object,
  7. default: () => ({})
  8. }
  9. })
  10. const visible = ref(false)
  11. function handleOpen() {
  12. visible.value = true
  13. }
  14. async function getPayStatus(outTradeNo: string) {
  15. const res = await wechatPayQuery({
  16. outTradeNo
  17. })
  18. if (res.code !== 1000) {
  19. getPayStatus(outTradeNo)
  20. }
  21. }
  22. const handlePay = async () => {
  23. console.log(props.record)
  24. try {
  25. const { prepayId, outTradeNo } = await wechatPay({
  26. subjectId: props.record.subjectId,
  27. })
  28. const { signature,timestamp,nonceStr } = await wechatPayRequest({
  29. prepayId, outTradeNo
  30. })
  31. console.log(signature)
  32. uni.requestPayment({
  33. provider: "wxpay",
  34. timeStamp: timestamp,
  35. nonceStr: nonceStr,
  36. package: "prepay_id="+prepayId,
  37. paySign: signature,
  38. signType: "RSA",
  39. success: (res) => {
  40. console.log('res: ', res)
  41. uni.showToast({
  42. title: "支付成功",
  43. icon: 'success'
  44. });
  45. // 支付成功后,查询支付结果
  46. getPayStatus(outTradeNo)
  47. },
  48. fail: (err) => {
  49. console.error("err", err);
  50. uni.hideLoading();
  51. // 支付失败后,取消订单
  52. wechatPayCancel({
  53. outTradeNo
  54. }).then(res => {
  55. if (res.code === 200) {
  56. uni.showToast({
  57. title: "订单已取消",
  58. icon: 'success'
  59. });
  60. }
  61. })
  62. }
  63. });
  64. } catch (err: any) {
  65. uni.showToast({
  66. title: err.message,
  67. icon: 'error'
  68. });
  69. }
  70. }
  71. </script>
  72. <template>
  73. <view class="absolute top-0 left-0 w-full h-full flex items-center justify-center bg1" @tap.stop="handleOpen">
  74. <cl-image src="https://oss.xiaoxiongcode.com/static/home/lock.png" mode="widthFix" width="30%" height="auto" />
  75. </view>
  76. <cl-popup v-model="visible" showClose :size="400" :show-header="false" direction="center">
  77. <view class="p-[40px] ">
  78. <cl-text class="text-center" color="#000" :size="20"> 小朋友,快叫爸爸妈妈帮忙,解锁更多精彩内容吧! </cl-text>
  79. <cl-button class="mt-4" size="large" type="warn" block @tap="handlePay"> 前往购课 </cl-button>
  80. </view>
  81. </cl-popup>
  82. </template>
  83. <style lang="scss" scoped>
  84. .bg1 {
  85. background-color: rgba(255, 255, 255, 0.5);
  86. }
  87. </style>