重构匹配页面,通过新增登录和手机号绑定模态窗口来提升用户体验。在添加好友前,增加了通过微信和手机号绑定登录的功能。更新了API配置,并调整了用户联系信息的本地存储处理方式。改进了模态窗口的用户界面元素,并确保用户交互的数据流正确。
This commit is contained in:
@@ -66,6 +66,17 @@ Page({
|
||||
// 解锁弹窗
|
||||
showUnlockModal: false,
|
||||
|
||||
// 手机号绑定弹窗(一键加好友前校验)
|
||||
showBindPhoneModal: false,
|
||||
pendingAddWechatAfterBind: false,
|
||||
bindPhoneInput: '',
|
||||
showMatchPhoneManualInput: false,
|
||||
|
||||
// 登录弹窗(未登录时点击匹配弹出)
|
||||
showLoginModal: false,
|
||||
isLoggingIn: false,
|
||||
agreeProtocol: false,
|
||||
|
||||
// 匹配价格(可配置)
|
||||
matchPrice: 1,
|
||||
extraMatches: 0
|
||||
@@ -90,6 +101,7 @@ Page({
|
||||
tabBar.setData({ selected: 2 })
|
||||
}
|
||||
}
|
||||
this.loadStoredContact()
|
||||
this.initUserStatus()
|
||||
},
|
||||
|
||||
@@ -123,10 +135,11 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
// 加载本地存储的联系方式
|
||||
// 加载本地存储的联系方式(含用户资料的手机号、微信号)
|
||||
loadStoredContact() {
|
||||
const phone = wx.getStorageSync('user_phone') || ''
|
||||
const wechat = wx.getStorageSync('user_wechat') || ''
|
||||
const ui = app.globalData.userInfo || {}
|
||||
const phone = wx.getStorageSync('user_phone') || ui.phone || ''
|
||||
const wechat = wx.getStorageSync('user_wechat') || ui.wechat || ui.wechatId || ''
|
||||
this.setData({
|
||||
phoneNumber: phone,
|
||||
wechatId: wechat,
|
||||
@@ -192,25 +205,16 @@ Page({
|
||||
|
||||
// 点击匹配按钮
|
||||
handleMatchClick() {
|
||||
// 检测是否登录,未登录则弹出登录弹窗
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.setData({ showLoginModal: true, agreeProtocol: false })
|
||||
return
|
||||
}
|
||||
|
||||
const currentType = MATCH_TYPES.find(t => t.id === this.data.selectedType)
|
||||
|
||||
// 资源对接类型需要登录+购买章节才能使用
|
||||
// 资源对接类型需要购买章节才能使用
|
||||
if (currentType && currentType.id === 'investor') {
|
||||
// 检查是否登录
|
||||
if (!this.data.isLoggedIn) {
|
||||
wx.showModal({
|
||||
title: '需要登录',
|
||||
content: '请先登录后再使用资源对接功能',
|
||||
confirmText: '去登录',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
wx.switchTab({ url: '/pages/my/my' })
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否购买过章节
|
||||
const hasPurchased = app.globalData.purchasedSections?.length > 0 || app.globalData.hasFullBook
|
||||
if (!hasPurchased) {
|
||||
@@ -235,7 +239,8 @@ Page({
|
||||
const hasWechat = !!this.data.wechatId
|
||||
|
||||
if (!hasPhone && !hasWechat) {
|
||||
// 没有绑定联系方式,先显示绑定提示
|
||||
// 没有绑定联系方式,先显示绑定提示(仍尝试加载已有资料填充)
|
||||
this.loadStoredContact()
|
||||
this.setData({
|
||||
showJoinModal: true,
|
||||
joinType: currentType.id,
|
||||
@@ -279,6 +284,8 @@ Page({
|
||||
const delay = Math.random() * 2000 + 1000
|
||||
setTimeout(() => {
|
||||
clearInterval(timer)
|
||||
// 打开弹窗前调取用户资料填充手机号、微信号
|
||||
this.loadStoredContact()
|
||||
this.setData({
|
||||
isMatching: false,
|
||||
showJoinModal: true,
|
||||
@@ -435,26 +442,78 @@ Page({
|
||||
this.setData({ currentMatch: null })
|
||||
},
|
||||
|
||||
// 添加微信好友
|
||||
// 添加微信好友(先校验手机号绑定)
|
||||
handleAddWechat() {
|
||||
if (!this.data.currentMatch) return
|
||||
|
||||
// 未登录需先登录
|
||||
if (!app.globalData.isLoggedIn) {
|
||||
wx.showModal({
|
||||
title: '需要登录',
|
||||
content: '请先登录后再添加好友',
|
||||
confirmText: '去登录',
|
||||
success: (res) => {
|
||||
if (res.confirm) wx.switchTab({ url: '/pages/my/my' })
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否已绑定手机号(本地缓存或用户资料)
|
||||
const hasPhone = !!(
|
||||
wx.getStorageSync('user_phone') ||
|
||||
app.globalData.userInfo?.phone
|
||||
)
|
||||
|
||||
if (!hasPhone) {
|
||||
this.setData({
|
||||
showBindPhoneModal: true,
|
||||
pendingAddWechatAfterBind: true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.doCopyWechat()
|
||||
},
|
||||
|
||||
// 执行复制联系方式(优先微信号,无则复制手机号)
|
||||
doCopyWechat() {
|
||||
if (!this.data.currentMatch) return
|
||||
const wechat = (this.data.currentMatch.wechat || this.data.currentMatch.wechatId || '').trim()
|
||||
const phone = (this.data.currentMatch.phone || '').trim()
|
||||
const toCopy = wechat || phone
|
||||
if (!toCopy) {
|
||||
wx.showModal({
|
||||
title: '暂无可复制',
|
||||
content: '该用户未提供微信号或手机号,请通过其他方式联系',
|
||||
showCancel: false,
|
||||
confirmText: '知道了'
|
||||
})
|
||||
return
|
||||
}
|
||||
const label = wechat ? '微信号' : '手机号'
|
||||
wx.setClipboardData({
|
||||
data: this.data.currentMatch.wechat,
|
||||
data: toCopy,
|
||||
success: () => {
|
||||
wx.showModal({
|
||||
title: '微信号已复制',
|
||||
content: `微信号:${this.data.currentMatch.wechat}\n\n请打开微信添加好友,备注"创业合作"即可`,
|
||||
title: wechat ? '微信号已复制' : '手机号已复制',
|
||||
content: wechat
|
||||
? `${label}:${toCopy}\n\n请打开微信添加好友,备注"创业合作"即可`
|
||||
: `${label}:${toCopy}\n\n可通过微信搜索该手机号添加好友`,
|
||||
showCancel: false,
|
||||
confirmText: '知道了'
|
||||
})
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({ title: '复制失败,请重试', icon: 'none' })
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 切换联系方式类型
|
||||
// 切换联系方式类型(同步刷新用户资料填充)
|
||||
switchContactType(e) {
|
||||
const type = e.currentTarget.dataset.type
|
||||
this.loadStoredContact()
|
||||
this.setData({ contactType: type, joinError: '' })
|
||||
},
|
||||
|
||||
@@ -565,6 +624,149 @@ Page({
|
||||
this.setData({ showJoinModal: false, joinError: '' })
|
||||
},
|
||||
|
||||
// 关闭手机绑定弹窗
|
||||
closeBindPhoneModal() {
|
||||
this.setData({
|
||||
showBindPhoneModal: false,
|
||||
pendingAddWechatAfterBind: false,
|
||||
bindPhoneInput: '',
|
||||
showMatchPhoneManualInput: false
|
||||
})
|
||||
},
|
||||
|
||||
// 关闭登录弹窗
|
||||
closeLoginModal() {
|
||||
if (this.data.isLoggingIn) return
|
||||
this.setData({ showLoginModal: false })
|
||||
},
|
||||
|
||||
// 切换协议勾选
|
||||
toggleAgree() {
|
||||
this.setData({ agreeProtocol: !this.data.agreeProtocol })
|
||||
},
|
||||
|
||||
// 打开用户协议
|
||||
openUserProtocol() {
|
||||
wx.navigateTo({ url: '/pages/agreement/agreement' })
|
||||
},
|
||||
|
||||
// 打开隐私政策
|
||||
openPrivacy() {
|
||||
wx.navigateTo({ url: '/pages/privacy/privacy' })
|
||||
},
|
||||
|
||||
// 微信登录(匹配页)
|
||||
async handleMatchWechatLogin() {
|
||||
if (!this.data.agreeProtocol) {
|
||||
wx.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ isLoggingIn: true })
|
||||
try {
|
||||
const result = await app.login()
|
||||
if (result) {
|
||||
this.initUserStatus()
|
||||
this.setData({ showLoginModal: false, agreeProtocol: false })
|
||||
wx.showToast({ title: '登录成功', icon: 'success' })
|
||||
} else {
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Match] 微信登录错误:', e)
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ isLoggingIn: false })
|
||||
}
|
||||
},
|
||||
|
||||
// 一键获取手机号(匹配页加好友前绑定)
|
||||
async onMatchGetPhoneNumber(e) {
|
||||
if (e.detail.errMsg !== 'getPhoneNumber:ok') {
|
||||
wx.showToast({ title: '授权失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const code = e.detail.code
|
||||
if (!code) {
|
||||
this.setData({ showMatchPhoneManualInput: true })
|
||||
return
|
||||
}
|
||||
try {
|
||||
wx.showLoading({ title: '获取中...', mask: true })
|
||||
const userId = app.globalData.userInfo?.id
|
||||
const res = await app.request('/api/miniprogram/phone', {
|
||||
method: 'POST',
|
||||
data: { code, userId }
|
||||
})
|
||||
wx.hideLoading()
|
||||
if (res.success && res.phoneNumber) {
|
||||
await this.saveMatchPhoneAndContinue(res.phoneNumber)
|
||||
} else {
|
||||
this.setData({ showMatchPhoneManualInput: true })
|
||||
}
|
||||
} catch (err) {
|
||||
wx.hideLoading()
|
||||
this.setData({ showMatchPhoneManualInput: true })
|
||||
}
|
||||
},
|
||||
|
||||
// 切换为手动输入
|
||||
onMatchShowManualInput() {
|
||||
this.setData({ showMatchPhoneManualInput: true })
|
||||
},
|
||||
|
||||
// 手动输入手机号
|
||||
onMatchPhoneInput(e) {
|
||||
this.setData({
|
||||
bindPhoneInput: e.detail.value.replace(/\D/g, '').slice(0, 11)
|
||||
})
|
||||
},
|
||||
|
||||
// 确认手动绑定手机号
|
||||
async confirmMatchPhoneBind() {
|
||||
const { bindPhoneInput } = this.data
|
||||
if (!bindPhoneInput || bindPhoneInput.length !== 11) {
|
||||
wx.showToast({ title: '请输入正确的11位手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!/^1[3-9]\d{9}$/.test(bindPhoneInput)) {
|
||||
wx.showToast({ title: '请输入正确的手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
await this.saveMatchPhoneAndContinue(bindPhoneInput)
|
||||
},
|
||||
|
||||
// 保存手机号到本地+服务器,并继续加好友
|
||||
async saveMatchPhoneAndContinue(phone) {
|
||||
wx.setStorageSync('user_phone', phone)
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.phone = phone
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
this.setData({
|
||||
phoneNumber: phone,
|
||||
userPhone: phone,
|
||||
bindPhoneInput: ''
|
||||
})
|
||||
this.loadStoredContact()
|
||||
try {
|
||||
const userId = app.globalData.userInfo?.id
|
||||
if (userId) {
|
||||
await app.request('/api/miniprogram/user/profile', {
|
||||
method: 'POST',
|
||||
data: { userId, phone }
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('[Match] 同步手机号到服务器失败:', e)
|
||||
}
|
||||
const pending = this.data.pendingAddWechatAfterBind
|
||||
this.closeBindPhoneModal()
|
||||
if (pending) {
|
||||
wx.showToast({ title: '绑定成功', icon: 'success' })
|
||||
setTimeout(() => this.doCopyWechat(), 500)
|
||||
}
|
||||
},
|
||||
|
||||
// 显示解锁弹窗
|
||||
showUnlockModal() {
|
||||
this.setData({ showUnlockModal: true })
|
||||
|
||||
Reference in New Issue
Block a user