lock.uvue 3.9 KB

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