Files
MBTI_wang/miniprogram/pages/purchase/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

251 lines
7.7 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.

// pages/purchase/index.js - 开通会员(深度服务价格:个人/企业区分,类目由后端配置可新增)
const app = getApp()
const payment = require('../../utils/payment')
const { hasPhone, bindPhoneByCode } = require('../../utils/phoneAuth.js')
Page({
data: {
activeTab: 'personal',
personalCategories: [],
enterpriseCategories: [],
loading: true,
purchasing: false,
hasPhone: false,
successModal: {
visible: false,
title: '',
content: '',
wechat: ''
}
},
onLoad(options) {
const tab = (options && options.tab === 'enterprise') ? 'enterprise' : 'personal'
this.setData({ activeTab: tab })
wx.setNavigationBarTitle({ title: '深度服务' })
this.loadDeepPricing()
},
onShow() {
// 不在此页强制跳转资料页:避免与付费按钮上的手机号授权打架导致死循环;未绑手机由按钮 open-type 引导
this.setData({ hasPhone: hasPhone() })
},
loadDeepPricing() {
const apiBase = app.globalData.apiBase || ''
if (!apiBase) {
this.setData({ loading: false })
return
}
this.setData({ loading: true })
Promise.all([
this.requestDeepPricing('personal'),
this.requestDeepPricing('enterprise')
]).then(([personal, enterprise]) => {
this.setData({
personalCategories: personal || [],
enterpriseCategories: enterprise || [],
loading: false
})
}).catch(() => {
this.setData({ loading: false })
})
},
requestDeepPricing(scope) {
return new Promise((resolve) => {
wx.request({
url: `${app.globalData.apiBase.replace(/\/$/, '')}/api/config/deep-pricing`,
method: 'GET',
data: { scope },
success: (res) => {
if (res.statusCode === 200 && res.data && res.data.code === 200 && Array.isArray(res.data.data && res.data.data.categories)) {
resolve(res.data.data.categories)
} else {
resolve([])
}
},
fail: () => resolve([])
})
})
},
switchTab(e) {
const tab = e.currentTarget.dataset.tab
if (tab !== 'personal' && tab !== 'enterprise') return
this.setData({ activeTab: tab })
wx.setNavigationBarTitle({ title: '深度服务' })
},
// 无需再次授权时,直接点击按钮执行购买/咨询
handlePurchaseTap(e) {
const tab = e.currentTarget.dataset.tab
const index = e.currentTarget.dataset.index
this.handlePurchase(tab, index)
},
// 实际执行购买/咨询逻辑(手机号由外层 getPhoneNumber 或 hasPhone 分支保证)
handlePurchase(tab, index) {
if (index === undefined || index === null) return
const list = tab === 'enterprise' ? this.data.enterpriseCategories : this.data.personalCategories
const category = list[index]
if (!category) return
if (category.actionType === 'buy' && category.productKey) {
this.purchasePersonal(category)
} else {
this.applyConsult(category)
}
},
// 购买/企业咨询按钮:就地触发微信系统手机号授权,然后执行 handlePurchase
onGetPhoneNumberForPurchase(e) {
const tab = e.currentTarget.dataset.tab
const index = e.currentTarget.dataset.index
const { code, errMsg } = e.detail || {}
if (errMsg && errMsg.indexOf('getPhoneNumber:fail') === 0) {
if (!hasPhone()) {
wx.showToast({ title: '需要授权手机号才能继续', icon: 'none' })
return
}
// 用户拒绝但之前已授权过,本地已有手机号,则直接继续
this.handlePurchase(tab, index)
return
}
if (!code) {
if (hasPhone()) {
this.handlePurchase(tab, index)
} else {
wx.showToast({ title: '获取手机号失败', icon: 'none' })
}
return
}
bindPhoneByCode(code)
.then(() => {
this.setData({ hasPhone: true })
this.handlePurchase(tab, index)
})
.catch(() => {
// 失败时只提示,不阻塞后续再次点击
})
},
purchasePersonal(category) {
if (this.data.purchasing) return
this.setData({ purchasing: true })
wx.showLoading({ title: '处理中...', mask: true })
const deepProductId = category.id || category.productKey || ''
const title = category.title || '个人深度服务1v1深度解读'
payment.purchasePersonalDeepService({
deepProductId,
description: title,
success: () => {
wx.hideLoading()
this.setData({ purchasing: false })
this._reportCrmLead(category, 'buy')
const successMsg = (category.successMessage || '购买成功!我们的顾问会尽快与您联系,为您提供专属深度解读服务。').trim()
const wechat = (category.serviceWechat || '').trim()
this._showSuccessModal('购买成功', successMsg, wechat)
},
fail: () => {
wx.hideLoading()
this.setData({ purchasing: false })
}
})
},
applyConsult(category) {
// serviceWechat 展示给用户consultWechat 是存客宝 API key
const wechat = (category.serviceWechat || '').trim()
const apiKey = (category.consultWechat || '').trim()
const successMsg = (category.successMessage || '感谢您的申请,我们的顾问会尽快与您联系!').trim()
wx.showLoading({ title: '提交中...', mask: true })
if (apiKey) {
this._reportCrmLead(category, 'consult')
}
setTimeout(() => {
wx.hideLoading()
this._showSuccessModal('申请成功', successMsg, wechat)
}, 600)
},
_showSuccessModal(title, content, wechat) {
this.setData({
successModal: {
visible: true,
title: title || '成功',
content: content || '',
wechat: wechat || ''
}
})
},
catchTap() {},
closeSuccessModal() {
this.setData({ 'successModal.visible': false })
},
copyWechat() {
const wechat = this.data.successModal.wechat
if (!wechat) return
wx.setClipboardData({
data: wechat,
success: () => wx.showToast({ title: '已复制微信号', icon: 'success' })
})
},
/**
* 向后端上报存客宝线索,后端负责签名和调用存客宝 API
* @param {Object} category 深度服务类目对象(需含 consultWechat / title
* @param {string} actionType 'buy'(付款完成)| 'consult'(申请咨询)
*/
_reportCrmLead(category, actionType) {
const apiKey = category.consultWechat || ''
if (!apiKey) return
const apiBase = app.globalData.apiBase || ''
if (!apiBase) return
const isEnterprise = this.data.activeTab === 'enterprise'
const source = (isEnterprise ? '企业深度服务' : '个人深度服务') + (category.title ? `-${category.title}` : '')
const remark = actionType === 'buy' ? '完成付款' : '申请咨询'
wx.request({
url: `${apiBase.replace(/\/$/, '')}/api/crm/report`,
method: 'POST',
header: {
Authorization: `Bearer ${wx.getStorageSync('token') || ''}`,
'Content-Type': 'application/json',
},
data: {
apiKey,
source,
remark,
siteTags: category.title || '',
},
success(res) {
console.log('[CRM] 线索上报结果', res.data)
},
fail(err) {
console.warn('[CRM] 线索上报请求失败', err)
},
})
},
onShareAppMessage() {
const { getSharePath } = require('../../utils/share')
return { title: '神仙团队性格测试 - 发现你的内在潜能', path: getSharePath('/pages/purchase/index') }
},
onShareTimeline() {
const { buildShareQuery } = require('../../utils/share')
return {
title: '神仙团队性格测试 - 发现你的内在潜能',
query: buildShareQuery()
}
}
})