Files
MBTI_wang/miniprogram/pages/recharge/index.js
2026-03-17 12:01:20 +08:00

84 lines
2.3 KiB
JavaScript

const app = getApp()
const payment = require('../../utils/payment')
const { request } = require('../../utils/request')
Page({
data: {
enterpriseId: 0,
enterpriseName: '',
amountFen: 0,
amountYuan: '0.00',
paying: false
},
onLoad(options) {
const rawScene = options && options.scene ? decodeURIComponent(options.scene) : ''
const sceneParams = {}
if (rawScene) {
rawScene.split('&').forEach(pair => {
const [k, v] = pair.split('=')
if (k) sceneParams[k] = v || ''
})
}
const enterpriseId = parseInt(sceneParams.eid || options.eid || 0, 10) || 0
const amountFen = parseInt(sceneParams.a || options.amountFen || 0, 10) || 0
const amountYuan = (amountFen / 100).toFixed(2)
if (enterpriseId > 0) {
app.globalData.enterpriseIdFromScene = enterpriseId
}
this.setData({
enterpriseId,
enterpriseName: (app.globalData.userInfo && app.globalData.userInfo.enterpriseName) || '',
amountFen,
amountYuan
})
app.ensureLogin()
.then(() => {
if (enterpriseId > 0) {
request({
url: '/api/enterprise/bind',
method: 'POST',
data: { enterpriseId },
success: (res) => {
const payload = res && res.data && res.data.data ? res.data.data : {}
const enterpriseName = payload.enterpriseName || this.data.enterpriseName || ''
this.setData({ enterpriseName })
},
fail: () => {}
})
}
})
.catch(() => {
wx.showToast({ title: '请先登录', icon: 'none' })
})
},
submitRecharge() {
if (this.data.paying) return
if (!this.data.enterpriseId || !this.data.amountFen) {
wx.showToast({ title: '充值参数无效', icon: 'none' })
return
}
this.setData({ paying: true })
payment.recharge({
amountYuan: Number(this.data.amountYuan),
enterpriseId: this.data.enterpriseId,
success: () => {
this.setData({ paying: false })
wx.showToast({ title: '充值成功', icon: 'success' })
setTimeout(() => {
wx.navigateTo({ url: `/pages/enterprise/index?eid=${this.data.enterpriseId}` })
}, 1200)
},
fail: () => {
this.setData({ paying: false })
}
})
}
})