293 lines
9.4 KiB
JavaScript
293 lines
9.4 KiB
JavaScript
const app = getApp()
|
||
|
||
const MATCH_TYPES = [
|
||
{ id: 'partner', label: '创业合伙', matchLabel: '创业伙伴', icon: '⭐' },
|
||
{ id: 'investor', label: '资源对接', matchLabel: '资源对接', icon: '👥' },
|
||
{ id: 'mentor', label: '导师顾问', matchLabel: '商业顾问', icon: '❤️' },
|
||
{ id: 'team', label: '团队招募', matchLabel: '加入项目', icon: '🎮' }
|
||
]
|
||
const FREE_MATCH_LIMIT = 1
|
||
|
||
function getStoredContact() {
|
||
try {
|
||
return {
|
||
phone: wx.getStorageSync('user_phone') || '',
|
||
wechat: wx.getStorageSync('user_wechat') || ''
|
||
}
|
||
} catch (e) { return { phone: '', wechat: '' } }
|
||
}
|
||
|
||
function getTodayMatchCount() {
|
||
try {
|
||
const today = new Date().toISOString().split('T')[0]
|
||
const stored = wx.getStorageSync('match_count_data')
|
||
if (stored) {
|
||
const data = typeof stored === 'string' ? JSON.parse(stored) : stored
|
||
if (data.date === today) return data.count || 0
|
||
}
|
||
} catch (e) {}
|
||
return 0
|
||
}
|
||
|
||
function saveTodayMatchCount(count) {
|
||
try {
|
||
const today = new Date().toISOString().split('T')[0]
|
||
wx.setStorageSync('match_count_data', JSON.stringify({ date: today, count }))
|
||
} catch (e) {}
|
||
}
|
||
|
||
function saveContact(phone, wechat) {
|
||
try {
|
||
if (phone) wx.setStorageSync('user_phone', phone)
|
||
if (wechat) wx.setStorageSync('user_wechat', wechat)
|
||
} catch (e) {}
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
statusBarHeight: 44,
|
||
navBarHeight: 88,
|
||
matchEnabled: false,
|
||
matchTypes: MATCH_TYPES,
|
||
selectedType: 'partner',
|
||
currentMatchLabel: '创业伙伴',
|
||
hasPurchased: false,
|
||
todayMatchCount: 0,
|
||
totalMatchesAllowed: 1,
|
||
matchesRemaining: 0,
|
||
needPayToMatch: false,
|
||
isMatching: false,
|
||
currentMatch: null,
|
||
matchAttempts: 0,
|
||
showUnlockModal: false,
|
||
showJoinModal: false,
|
||
joinType: null,
|
||
phoneNumber: '',
|
||
wechatId: '',
|
||
contactType: 'phone',
|
||
isJoining: false,
|
||
joinSuccess: false,
|
||
joinError: '',
|
||
isUnlocking: false
|
||
},
|
||
|
||
onLoad() {
|
||
this.setNavBarHeight()
|
||
const contact = getStoredContact()
|
||
this.setData({ phoneNumber: contact.phone, wechatId: contact.wechat })
|
||
app.loadFeatureConfig().then(() => this.syncState())
|
||
},
|
||
|
||
onShow() {
|
||
if (typeof this.getTabBar === 'function' && this.getTabBar()) this.getTabBar().setData({ selected: 2 })
|
||
this.setNavBarHeight()
|
||
app.loadFeatureConfig().then(() => this.syncState())
|
||
},
|
||
|
||
setNavBarHeight() {
|
||
const statusBarHeight = app.globalData.statusBarHeight || 44
|
||
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
|
||
this.setData({ statusBarHeight, navBarHeight })
|
||
},
|
||
|
||
syncState() {
|
||
const matchEnabled = app.globalData.matchEnabled === true
|
||
const user = app.globalData.userInfo
|
||
const hasFullBook = !!app.globalData.hasFullBook
|
||
const purchasedSections = app.globalData.purchasedSections || []
|
||
const hasPurchased = hasFullBook || purchasedSections.length > 0
|
||
const todayMatchCount = getTodayMatchCount()
|
||
const totalMatchesAllowed = hasFullBook ? 999999 : FREE_MATCH_LIMIT + purchasedSections.length
|
||
const matchesRemaining = hasFullBook ? 999999 : Math.max(0, totalMatchesAllowed - todayMatchCount)
|
||
const needPayToMatch = !hasFullBook && matchesRemaining <= 0
|
||
if (user && user.phone) this.setData({ phoneNumber: user.phone })
|
||
this.setData({
|
||
matchEnabled,
|
||
hasPurchased,
|
||
todayMatchCount,
|
||
totalMatchesAllowed,
|
||
matchesRemaining,
|
||
needPayToMatch
|
||
})
|
||
},
|
||
|
||
selectType(e) {
|
||
const id = e.currentTarget.dataset.id
|
||
const t = MATCH_TYPES.find(x => x.id === id)
|
||
this.setData({ selectedType: id, currentMatchLabel: t ? t.matchLabel : '创业伙伴' })
|
||
},
|
||
|
||
startMatch() {
|
||
if (!this.data.hasPurchased) {
|
||
wx.showToast({ title: '购买书籍后可使用', icon: 'none' })
|
||
return
|
||
}
|
||
if (this.data.needPayToMatch) {
|
||
this.setData({ showUnlockModal: true })
|
||
return
|
||
}
|
||
this.setData({ isMatching: true, currentMatch: null, matchAttempts: 0 })
|
||
let attempts = 0
|
||
const timer = setInterval(() => {
|
||
attempts++
|
||
this.setData({ matchAttempts: attempts })
|
||
}, 1000)
|
||
const delay = 3000 + Math.random() * 3000
|
||
setTimeout(() => {
|
||
clearInterval(timer)
|
||
const matched = this.getMockMatch()
|
||
const newCount = this.data.todayMatchCount + 1
|
||
saveTodayMatchCount(newCount)
|
||
this.reportMatchToCKB(matched)
|
||
const currentType = MATCH_TYPES.find(t => t.id === this.data.selectedType)
|
||
const showJoinAfter = currentType && (currentType.id === 'investor' || currentType.id === 'mentor' || currentType.id === 'team')
|
||
this.setData({
|
||
isMatching: false,
|
||
currentMatch: matched,
|
||
todayMatchCount: newCount,
|
||
matchesRemaining: this.data.hasFullBook ? 999999 : Math.max(0, this.data.totalMatchesAllowed - newCount),
|
||
needPayToMatch: !this.data.hasFullBook && (this.data.totalMatchesAllowed - newCount) <= 0
|
||
})
|
||
if (showJoinAfter) {
|
||
this.setData({ showJoinModal: true, joinType: this.data.selectedType, joinSuccess: false, joinError: '' })
|
||
}
|
||
}, delay)
|
||
},
|
||
|
||
getMockMatch() {
|
||
const nicknames = ['创业先锋', '资源整合者', '私域专家', '商业导师', '连续创业者']
|
||
const concepts = [
|
||
'专注私域流量运营5年,帮助100+品牌实现从0到1的增长。',
|
||
'连续创业者,擅长商业模式设计和资源整合。',
|
||
'在Soul分享真实创业故事,希望找到志同道合的合作伙伴。'
|
||
]
|
||
const wechats = ['soul_partner_1', 'soul_business_2024', 'soul_startup_fan']
|
||
const i = Math.floor(Math.random() * nicknames.length)
|
||
const typeLabel = MATCH_TYPES.find(t => t.id === this.data.selectedType)
|
||
return {
|
||
id: 'user_' + Date.now(),
|
||
nickname: nicknames[i],
|
||
avatar: '',
|
||
tags: ['创业者', '私域运营', typeLabel ? typeLabel.label : ''],
|
||
matchScore: 80 + Math.floor(Math.random() * 20),
|
||
concept: concepts[i % concepts.length],
|
||
wechat: wechats[i % wechats.length],
|
||
commonInterests: [
|
||
{ icon: '📚', text: '都在读《创业实验》' },
|
||
{ icon: '💼', text: '对私域运营感兴趣' },
|
||
{ icon: '🎯', text: '相似的创业方向' }
|
||
]
|
||
}
|
||
},
|
||
|
||
reportMatchToCKB(matched) {
|
||
app.request('/api/ckb/match', {
|
||
method: 'POST',
|
||
data: {
|
||
matchType: this.data.selectedType,
|
||
phone: this.data.phoneNumber || (app.globalData.userInfo && app.globalData.userInfo.phone) || '',
|
||
wechat: this.data.wechatId || (app.globalData.userInfo && app.globalData.userInfo.wechat) || '',
|
||
userId: (app.globalData.userInfo && app.globalData.userInfo.id) || '',
|
||
nickname: (app.globalData.userInfo && app.globalData.userInfo.nickname) || '',
|
||
matchedUser: { id: matched.id, nickname: matched.nickname, matchScore: matched.matchScore }
|
||
}
|
||
}).catch(() => {})
|
||
},
|
||
|
||
cancelMatch() {
|
||
this.setData({ isMatching: false })
|
||
},
|
||
|
||
nextMatch() {
|
||
if (this.data.needPayToMatch) {
|
||
this.setData({ showUnlockModal: true })
|
||
return
|
||
}
|
||
this.setData({ currentMatch: null })
|
||
this.startMatch()
|
||
},
|
||
|
||
addWechat() {
|
||
const m = this.data.currentMatch
|
||
if (!m || !m.wechat) return
|
||
wx.setClipboardData({
|
||
data: m.wechat,
|
||
success: () => {
|
||
wx.showModal({
|
||
title: '已复制微信号',
|
||
content: '请打开微信添加好友,备注"创业合作"即可。',
|
||
showCancel: false
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
closeUnlockModal() {
|
||
if (!this.data.isUnlocking) this.setData({ showUnlockModal: false })
|
||
},
|
||
|
||
goBuySection() {
|
||
this.setData({ showUnlockModal: false })
|
||
wx.switchTab({ url: '/pages/chapters/chapters' })
|
||
},
|
||
|
||
closeJoinModal() {
|
||
if (!this.data.isJoining) this.setData({ showJoinModal: false })
|
||
},
|
||
|
||
setContactType(e) {
|
||
const t = e.currentTarget.dataset.type
|
||
this.setData({ contactType: t })
|
||
},
|
||
|
||
onPhoneInput(e) {
|
||
this.setData({ phoneNumber: (e.detail && e.detail.value) || '' })
|
||
},
|
||
|
||
onWechatInput(e) {
|
||
this.setData({ wechatId: (e.detail && e.detail.value) || '' })
|
||
},
|
||
|
||
submitJoin() {
|
||
const { contactType, phoneNumber, wechatId } = this.data
|
||
if (contactType === 'phone' && (!phoneNumber || phoneNumber.length !== 11)) {
|
||
this.setData({ joinError: '请输入正确的11位手机号' })
|
||
return
|
||
}
|
||
if (contactType === 'wechat' && (!wechatId || wechatId.length < 6)) {
|
||
this.setData({ joinError: '请输入正确的微信号(至少6位)' })
|
||
return
|
||
}
|
||
this.setData({ isJoining: true, joinError: '' })
|
||
app.request('/api/ckb/join', {
|
||
method: 'POST',
|
||
data: {
|
||
type: this.data.joinType,
|
||
phone: contactType === 'phone' ? phoneNumber : '',
|
||
wechat: contactType === 'wechat' ? wechatId : '',
|
||
userId: app.globalData.userInfo && app.globalData.userInfo.id
|
||
}
|
||
}).then((res) => {
|
||
if (res && res.success) {
|
||
saveContact(phoneNumber, wechatId)
|
||
this.setData({ joinSuccess: true })
|
||
setTimeout(() => this.setData({ showJoinModal: false, joinSuccess: false }), 2000)
|
||
} else {
|
||
this.setData({ joinError: (res && res.message) || '加入失败,请稍后重试' })
|
||
}
|
||
}).catch(() => {
|
||
this.setData({ joinError: '网络错误,请检查网络后重试' })
|
||
}).finally(() => {
|
||
this.setData({ isJoining: false })
|
||
})
|
||
},
|
||
|
||
goToChapters() {
|
||
wx.switchTab({ url: '/pages/chapters/chapters' })
|
||
},
|
||
|
||
goToIndex() {
|
||
wx.switchTab({ url: '/pages/index/index' })
|
||
}
|
||
})
|