lock.uvue 2.5 KB

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