lock.uvue 3.0 KB

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