小程序接入支付功能

This commit is contained in:
Yeomai committed 2026-09-29 01:14:24 +08:00
1 parent edf1e51846
commit 07a90e860d
38 files changed
+3644 -90

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
// 支付后端地址(已配到 MP 后台 request 合法域名);换域名/路径只改这一行
const API_BASE = 'https://mini.yeomai.site'
// 调自己的服务端,后端统一返回 { ok, ... }
function post(path, data) {
return new Promise((resolve, reject) => {
wx.request({
url: API_BASE + path,
method: 'POST',
data,
timeout: 10000, // 卡住也要有结论,别让页面一直停在「进行中」
success: (res) => {
const body = res.data || {}
if (body.ok) resolve(body)
else reject(new Error(body.error || '后端返回异常(HTTP ' + res.statusCode + ')'))
},
fail: (err) => reject(new Error('请求后端失败:' + (err.errMsg || '') + '(域名是否已加入 request 合法域名?)')),
})
})
}
// 拿 wx.login 的 code,服务端用它换 openid + sessionKey
function login() {
return new Promise((resolve, reject) => {
// wx.login 偶尔会不回调,加个兜底,避免整个流程卡死
const timer = setTimeout(() => reject(new Error('wx.login 超时,请重试')), 8000)
wx.login({
success: (res) => {
clearTimeout(timer)
resolve(res.code)
},
fail: () => {
clearTimeout(timer)
reject(new Error('wx.login 失败,拿不到 sessionKey'))
},
})
})
}
module.exports = { API_BASE, post, login }