feat(invite): 添加邀请关系防重复处理功能
- 增加邀请关系存在性检查,避免重复处理 - 实现过期邀请关系记录清理功能 - 在建立邀请关系时保存记录,防止 future duplicates
This commit is contained in:
@@ -189,6 +189,16 @@ export async function handleInviteRelation(userId: number): Promise<boolean> {
|
||||
return false
|
||||
}
|
||||
|
||||
// 防重复检查:检查是否已经处理过这个邀请关系
|
||||
const relationKey = `invite_relation_${inviterId}_${userId}`
|
||||
const existingRelation = Taro.getStorageSync(relationKey)
|
||||
|
||||
if (existingRelation) {
|
||||
console.log(`邀请关系已存在,跳过重复处理: ${inviterId} -> ${userId}`)
|
||||
clearInviteParams() // 清除邀请参数
|
||||
return true // 返回true表示关系已存在
|
||||
}
|
||||
|
||||
console.log(`准备建立邀请关系: ${inviterId} -> ${userId}`)
|
||||
|
||||
// 使用新的绑定推荐关系接口
|
||||
@@ -199,6 +209,14 @@ export async function handleInviteRelation(userId: number): Promise<boolean> {
|
||||
scene: inviteParams.source === 'qrcode' ? `uid_${inviterId}` : `inviter=${inviterId}&source=${inviteParams.source}&t=${inviteParams.t}`
|
||||
})
|
||||
|
||||
// 标记邀请关系已处理(设置过期时间为7天)
|
||||
Taro.setStorageSync(relationKey, {
|
||||
inviterId,
|
||||
userId,
|
||||
timestamp: Date.now(),
|
||||
source: inviteParams.source || 'qrcode'
|
||||
})
|
||||
|
||||
// 清除本地存储的邀请参数
|
||||
clearInviteParams()
|
||||
|
||||
@@ -355,6 +373,9 @@ export function debugInviteInfo() {
|
||||
*/
|
||||
export async function checkAndHandleInviteRelation(): Promise<boolean> {
|
||||
try {
|
||||
// 清理过期的防重记录
|
||||
cleanExpiredInviteRelations()
|
||||
|
||||
// 打印调试信息
|
||||
debugInviteInfo()
|
||||
|
||||
@@ -410,6 +431,34 @@ export async function manualHandleInviteRelation(userId: number): Promise<boolea
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期的邀请关系防重记录
|
||||
*/
|
||||
export function cleanExpiredInviteRelations() {
|
||||
try {
|
||||
const keys = Taro.getStorageInfoSync().keys
|
||||
const expireTime = 7 * 24 * 60 * 60 * 1000 // 7天
|
||||
const now = Date.now()
|
||||
|
||||
keys.forEach(key => {
|
||||
if (key.startsWith('invite_relation_')) {
|
||||
try {
|
||||
const data = Taro.getStorageSync(key)
|
||||
if (data && data.timestamp && (now - data.timestamp > expireTime)) {
|
||||
Taro.removeStorageSync(key)
|
||||
console.log('清理过期的邀请关系记录:', key)
|
||||
}
|
||||
} catch (error) {
|
||||
// 如果读取失败,直接删除
|
||||
Taro.removeStorageSync(key)
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('清理过期邀请关系记录失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接绑定推荐关系
|
||||
* 用于直接调用绑定推荐关系接口
|
||||
|
||||
Reference in New Issue
Block a user