Files
soul-yongping/miniprogram/pages/mentor-detail/mentor-detail.js

114 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* Soul创业派对 - 导师详情stitch_soul
* 联系导师按钮 弹出 v2 弹窗选择咨询项目
*/
const app = getApp()
Page({
data: {
statusBarHeight: 44,
mentor: null,
loading: true,
showConsultModal: false,
consultOptions: [],
selectedType: '',
selectedAmount: 0,
creating: false,
},
onLoad(options) {
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 44 })
if (options.id) this.loadDetail(options.id)
},
async loadDetail(id) {
this.setData({ loading: true })
try {
const res = await app.request({ url: `/api/miniprogram/mentors/${id}`, silent: true })
if (res?.success && res.data) {
const d = res.data
if (d.judgmentStyle && typeof d.judgmentStyle === 'string') {
d.judgmentStyleArr = d.judgmentStyle.split(/[,]/).map(s => s.trim()).filter(Boolean)
} else {
d.judgmentStyleArr = []
}
const fmt = v => v != null ? String(Math.floor(v)).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : ''
d.priceSingleFmt = fmt(d.priceSingle)
d.priceHalfYearFmt = fmt(d.priceHalfYear)
d.priceYearFmt = fmt(d.priceYear)
const options = []
if (d.priceSingle != null) options.push({ type: 'single', label: '单次咨询', desc: '1小时深度沟通', price: d.priceSingle, priceFmt: fmt(d.priceSingle), original: null })
if (d.priceHalfYear != null) options.push({ type: 'half_year', label: '半年咨询', desc: '不限次数 · 关键节点陪伴', price: d.priceHalfYear, priceFmt: fmt(d.priceHalfYear), original: '98,000' })
if (d.priceYear != null) options.push({ type: 'year', label: '年度咨询', desc: '全年度战略顾问', price: d.priceYear, priceFmt: fmt(d.priceYear), original: '196,000', recommend: true })
this.setData({
mentor: d,
consultOptions: options,
loading: false,
})
} else {
this.setData({ loading: false })
}
} catch (e) {
this.setData({ loading: false })
}
},
onContactTap() {
if (!this.data.mentor || this.data.consultOptions.length === 0) {
wx.showToast({ title: '暂无咨询项目', icon: 'none' })
return
}
this.setData({
showConsultModal: true,
selectedType: this.data.consultOptions[0].type,
selectedAmount: this.data.consultOptions[0].price,
})
},
closeConsultModal() {
this.setData({ showConsultModal: false })
},
onSelectOption(e) {
const item = e.currentTarget.dataset.item
this.setData({ selectedType: item.type, selectedAmount: item.price })
},
async onConfirmConsult() {
const { mentor, selectedType } = this.data
const userId = app.globalData.userInfo?.id
if (!userId) {
wx.showToast({ title: '请先登录', icon: 'none' })
wx.navigateTo({ url: '/pages/my/my' })
return
}
this.setData({ creating: true })
try {
const res = await app.request({
url: `/api/miniprogram/mentors/${mentor.id}/book`,
method: 'POST',
data: { userId, consultationType: selectedType },
})
if (res?.success && res.data) {
this.setData({ showConsultModal: false, creating: false })
wx.showToast({ title: '预约创建成功', icon: 'success' })
// TODO: 调起支付 productType: mentor_consultation, productId: res.data.id
wx.showModal({
title: '预约成功',
content: '请联系客服完成后续对接',
showCancel: false,
})
} else {
wx.showToast({ title: res?.error || '创建失败', icon: 'none' })
}
} catch (e) {
wx.showToast({ title: '创建失败', icon: 'none' })
}
this.setData({ creating: false })
},
goBack() {
getApp().goBackOrToHome()
},
})