officialAccount/pages/pay/index.vue

147 lines
3.4 KiB
Vue
Raw Normal View History

2025-05-26 08:59:24 +08:00
<template>
<div class="container">
<div class="input-group">
<input type="number" v-model="amount" placeholder="请输入金额" class="amount-input" />
<button @click="initiatePayment" :disabled="loading || !amount" class="pay-btn">
支付
</button>
</div>
<div class="status-group" v-if="statusMessage">
<p>{{ statusMessage }}</p>
</div>
</div>
</template>
<script setup>
import {
ref
} from 'vue'
const amount = ref('')
const loading = ref(false)
const statusMessage = ref('')
// 硬编码支付参数
2025-05-26 16:48:12 +08:00
const payData = ref({
2025-05-26 08:59:24 +08:00
timeStamp: "1747983532",
package: "prepay_id=wx23145806465232c82870c59d2d41cf0000",
paySign: "0pUqj2JZ77BYchyJuthQyP4yRfqhjvwag78Q4IMnIyQ3/OQP6OyJreZfmj0GFSEMrRsKAHIdBBM7tVnot0aaRhI5qwSOWpzyvJCkYa4eqPgqlV4XYVMqE3zeB/Cx4C9bv4poMXnaGlfFPdkhMYbUcdtsw4gBXXhqUx//9x7eu9cOERRzLquM8Z8rewRpar/kkVKSCV6h8pX19kRj+KEkK5LZB8IUIG995g1lsVFOqdO4mVFBJ1wZCkwJczgVI+jdNGgnR2lpdjwIpJFm+5Hm0y9SwR0UFvgkm95NrmY+Sruty/Zf8ekQwF4+atubW8CE6i8wm2zZpMEnnfS4WFwAwg==",
appId: "wx8fc3e4305d2fbf0b",
signType: "RSA",
nonceStr: "DxpF2uIMl0VM7vPOG7pWnPHk2Dvi3V7K"
2025-05-26 16:48:12 +08:00
})
2025-05-26 08:59:24 +08:00
function callWeixinPay(data) {
const invokePay = () => {
window.WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
appId: data.appId,
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: data.signType,
paySign: data.paySign
},
function(res) {
loading.value = false
if (res.err_msg === 'get_brand_wcpay_request:ok') {
statusMessage.value = '支付成功'
} else {
statusMessage.value = '支付失败或取消'
}
}
)
}
if (typeof window.WeixinJSBridge === 'undefined') {
document.addEventListener('WeixinJSBridgeReady', invokePay, false)
} else {
invokePay()
}
}
const diaoqu = () =>{
2025-06-03 17:29:22 +08:00
// const urlpost = `${saveurl.value}/weiXinPay/native`;
const urlpost = `https://www.focusnu.com/nursing-unit_0010507/weiXinPay/native`
2025-05-26 08:59:24 +08:00
const payload = {
title: "测试",
openId: openid.value,
amountPrice:amount.value
};
console.log("???/",payload)
fetch(urlpost, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
.then(res => res.json())
.then(data => {
// secondArray.value = [...data.result];
2025-05-26 16:48:12 +08:00
// console.log("???",data)
callWeixinPay(data)
2025-05-26 08:59:24 +08:00
})
.catch(err => {
console.error('请求失败:', err);
});
}
const saveurl = ref("")
const openid = ref("")
function initiatePayment() {
if (!amount.value) return
loading.value = true
statusMessage.value = '拉起微信支付...'
uni.getStorage({
key: 'serverUrl',
success: function (res) {
console.log('读取缓存:', res.data.url);
saveurl.value = res.data.url;
}
});
uni.getStorage({
key: 'openid',
success: function (res) {
console.log('读取缓存:', res.data.openid);
openid.value = res.data.openid;
}
});
diaoqu()
2025-05-26 16:48:12 +08:00
2025-05-26 08:59:24 +08:00
}
</script>
<style scoped>
.container {
padding: 20px;
}
.input-group {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 20px;
}
.amount-input {
flex: 1;
height: 40px;
border: 1px solid #ddd;
padding: 0 10px;
border-radius: 4px;
}
.pay-btn {
padding: 0 20px;
height: 40px;
background-color: #1aad19;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
.status-group {
margin-top: 10px;
}
</style>