Files
mini-viewer/utils/pay-api.js
T
2026-09-29 01:14:24 +08:00

41 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 支付后端地址(已配到 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 }