Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 40s
feat(pay): 添加支付API基础结构 feat(miniapp): 创建支付测试小程序页面与配置 feat(wechatpay): 配置微信支付参数与证书 fix(guild): 修复成员列表查询条件 docs: 更新代码规范文档与需求文档 style: 统一前后端枚举显示与注释格式 refactor(admin): 重构用户奖励发放接口参数处理 test(title): 添加称号效果参数验证测试
103 lines
3.3 KiB
JavaScript
103 lines
3.3 KiB
JavaScript
const BASE_URL = 'http://localhost:9991/api'
|
|
|
|
function apiRequest(opts) {
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
...opts,
|
|
success: (res) => resolve(res),
|
|
fail: (err) => reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
async function ensureLogin(ctx) {
|
|
const loginRes = await wx.login()
|
|
const code = loginRes.code
|
|
const res = await apiRequest({
|
|
url: `${BASE_URL}/app/users/weixin/login`,
|
|
method: 'POST',
|
|
header: { 'content-type': 'application/json' },
|
|
data: { code }
|
|
})
|
|
const { token, openid, nickname, avatar } = (res && res.data) || {}
|
|
ctx.setData({ token: token || '', openid: openid || '', nickname: nickname || '', avatar: avatar || '' })
|
|
wx.setStorageSync('TOKEN', token || '')
|
|
wx.setStorageSync('OPENID', openid || '')
|
|
return { token: token || '', openid: openid || '' }
|
|
}
|
|
|
|
async function callWithAuth(ctx, opts) {
|
|
let token = ctx.data.token || wx.getStorageSync('TOKEN')
|
|
const res = await apiRequest({
|
|
...opts,
|
|
header: { ...(opts.header || {}), Authorization: `${token}` }
|
|
})
|
|
if (res && (res.statusCode === 401 || (res.data && res.data.code === 10103))) {
|
|
const info = await ensureLogin(ctx)
|
|
token = info.token
|
|
const retry = await apiRequest({
|
|
...opts,
|
|
header: { ...(opts.header || {}), Authorization: `${token}` }
|
|
})
|
|
return retry
|
|
}
|
|
return res
|
|
}
|
|
|
|
Page({
|
|
data: { logging: false, paying: false, token: '', openid: '', orderNo: '', nickname: '', avatar: '' },
|
|
onOrderNoInput(e) { this.setData({ orderNo: e.detail.value }) },
|
|
async doLogin() {
|
|
if (this.data.logging) return
|
|
this.setData({ logging: true })
|
|
try {
|
|
await ensureLogin(this)
|
|
} finally {
|
|
this.setData({ logging: false })
|
|
}
|
|
},
|
|
async doPay() {
|
|
const token = this.data.token || wx.getStorageSync('TOKEN')
|
|
const openid = this.data.openid || wx.getStorageSync('OPENID')
|
|
const orderNo = this.data.orderNo
|
|
if (!token) { wx.showToast({ title: '请先登录', icon: 'none' }); return }
|
|
if (!orderNo) { wx.showToast({ title: '请输入订单号', icon: 'none' }); return }
|
|
this.setData({ paying: true })
|
|
try {
|
|
const res = await callWithAuth(this, {
|
|
url: `${BASE_URL}/app/pay/wechat/jsapi/preorder`,
|
|
method: 'POST',
|
|
header: {
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
'Authorization': `${token}`
|
|
},
|
|
data: { order_no: orderNo, openid }
|
|
})
|
|
const p = (res && res.data) || {}
|
|
await wx.requestPayment({
|
|
timeStamp: String(p.timeStamp),
|
|
nonceStr: p.nonceStr,
|
|
package: p.package,
|
|
signType: p.signType,
|
|
paySign: p.paySign
|
|
})
|
|
wx.showToast({ title: '支付成功' })
|
|
} catch (e) {
|
|
wx.showToast({ title: '支付失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ paying: false })
|
|
}
|
|
}
|
|
,
|
|
async doCreateOrder() {
|
|
const token = this.data.token || wx.getStorageSync('TOKEN')
|
|
if (!token) { wx.showToast({ title: '请先登录', icon: 'none' }); return }
|
|
const res = await callWithAuth(this, {
|
|
url: `${BASE_URL}/app/orders/test/create`,
|
|
method: 'POST',
|
|
header: { 'Authorization': `${token}` }
|
|
})
|
|
const { order_no } = (res && res.data) || {}
|
|
if (order_no) { this.setData({ orderNo: order_no }); wx.showToast({ title: '已生成', icon: 'success' }) }
|
|
}
|
|
}) |