xuruiqian
2025-02-25 e85ab165147c37b571ea67ce6f6ae9ae55a96070
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<template>
  <view class="content"></view>
</template>
 
<script>
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from "vue";
import { useBasicInfoStore } from "../../store/basicInfo"
import { PayUseWXViaMiniProgram, PaySuccess, walletRecharge } from "../../apis/api";
import { guid } from "../../common/common";
export default {
  setup() {
    const userStore = useBasicInfoStore()
    const { proxy } = getCurrentInstance();
    const state = reactive({
    })
 
    onMounted(() => {
      Init();
    });
 
    const Init = () => {
      getWXLoginCode()
    }
 
    const getWXLoginCode = () => {
      uni.login({
        provider: 'weixin',
        success: function (res) {
          doPay(res.code,);
        },
        fail: () => { },
        complete: () => { },
      })
    }
 
    const doPay = (code) => {
 
      const payData = {
        requesturl: "",
        code: code,
        orderid: guid(),
        totalprice: userStore.payInfo.payPrice,
      }
 
      PayUseWXViaMiniProgram(payData).then(payRes => {
 
        if (payRes.code == 200) {
          let pay = payRes.data;
 
          wx_pay(
            pay.timestamp,
            pay.nonceStr,
            pay.prepay_id,
            pay.signType,
            pay.paySign,
            doPaySuccess, doPayFailed, doPayCompleted);
        } else {
          //this.$messagebox.show(payRes.message, 5000);
        }
      })
    }
 
    const doPaySuccess = (res) => {
      uni.$u.toast('支付成功!');
      PaySuccess(userStore.payInfo).then(resp => {
        if (resp) {
          switch (userStore.routePage) {
            case 'list':
              uni.navigateTo({
                url: '/pages/question/type'
              });
              break;
            case 'recharge':
              let obj = {
                pageIndex: 1,
                pageSize: 101,
                masterid: userStore.userinfo.id,
                amount: userStore.payInfo.payPrice
              }
              walletRecharge(obj).then((res) => {
                if (res.code == 200) {
                  uni.navigateTo({
                    url: '/pages/my/recharge'
                  });
                } else {
                  uni.navigateTo({
                    url: '/pages/my/recharge'
                  });
                }
              })
              break;
 
            default:
              break;
          }
        }
      })
    }
 
 
    const doPayFailed = (res) => {
      uni.$u.toast('支付失败!');
      uni.navigateBack();
    }
    const doPayCompleted = (res) => {
      if (res.errMsg === 'requestPayment:ok') {
        // uni.navigateTo({
        //     url: `/pages/category/productstatus`
        // })
 
      } else if (res.errMsg === 'requestPayment:fail cancel') {
        // uni.navigateTo({
        //     url: `/pages/category/productstatus`
        // })
 
      } else {
 
        //this.$messagebox.show(res.errMsg, 5000);
      }
 
      uni.navigateBack();
    }
    const wx_pay = (timeStamp, nonceStr, pack, signType, paySign, successFun = null, failfun = null,
      completeFun = null) => {
      wx.requestPayment({
        provider: 'wxpay',
        orderInfo: "orderInfo",
        timeStamp: timeStamp,
        nonceStr: nonceStr,
        package: "prepay_id=" + pack,
        signType: signType,
        paySign: paySign,
        success(res) {
          successFun(res)
        },
        fail(err) {
          if (failfun) {
            failfun(err)
          }
        },
        complete(result) {
          if (completeFun) {
            completeFun(result)
          }
        }
      });
    }
 
 
 
 
    return { ...toRefs(state), userStore }
  }
}
 
 
 
</script>
 
<style scoped lang="scss">
.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  //justify-content: center;
}
 
.content {
  @extend .center;
  //background-color: #f5efd9;
  height: 100vh;
  width: 100%;
}
</style>