lock.uvue 4.4 KB

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