Files
MBTI_wang/miniprogram/pages/recharge/index.js
卡若 aca2e263bb feat: 管理端聚合页、小程序/抖音埋点与统计、飞书线索 webhook、API 迁移与路由
- admin:OrdersHub/UsersHub、Commerce/Ops/Enterprise Hub、MpAnalytics、Feishu/小程序配置面板、鉴权存储
- api:Analytics、DataMigration、FeishuLeadWebhook、mp 事件迁移 SQL
- 微信/抖音小程序:analytics 上报与相关页面调整
- 开发文档与 scripts 补充

Made-with: Cursor
2026-03-27 17:22:04 +08:00

113 lines
3.3 KiB
JavaScript

const app = getApp()
const payment = require('../../utils/payment')
const { request } = require('../../utils/request')
const { getEffectiveEnterpriseId } = require('../../utils/enterpriseContext.js')
let analyticsMod = null
try {
analyticsMod = require('../../utils/analytics')
} catch (e) {}
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
}
const resolveEnterpriseId = () => {
if (enterpriseId > 0) return enterpriseId
return getEffectiveEnterpriseId() || 0
}
this.setData({
enterpriseId: resolveEnterpriseId(),
enterpriseName: (app.globalData.userInfo && app.globalData.userInfo.enterpriseName) || '',
amountFen,
amountYuan
})
app.ensureLogin()
.then((ok) => {
if (!ok) {
wx.showToast({ title: '请先登录', icon: 'none' })
return Promise.reject(new Error('no login'))
}
return app.getRuntimeConfig().catch(() => {})
})
.then(() => {
const finalEid = resolveEnterpriseId()
if (finalEid > 0 && enterpriseId <= 0) {
this.setData({ enterpriseId: finalEid })
}
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(() => {})
},
submitRecharge() {
if (this.data.paying) return
if (!this.data.enterpriseId || !this.data.amountFen) {
wx.showToast({ title: '充值参数无效', icon: 'none' })
return
}
this.setData({ paying: true })
if (analyticsMod && typeof analyticsMod.track === 'function') {
analyticsMod.track('click_recharge', {
action: '充值页确认充值',
enterpriseId: this.data.enterpriseId,
amountFen: this.data.amountFen
})
if (typeof analyticsMod.flush === 'function') {
analyticsMod.flush()
}
}
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 })
}
})
}
})