lock.uvue 3.2 KB

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