feat(invite): 添加邀请关系防重复处理功能
- 增加邀请关系存在性检查,避免重复处理 - 实现过期邀请关系记录清理功能 - 在建立邀请关系时保存记录,防止 future duplicates
This commit is contained in:
@@ -89,6 +89,18 @@ const Header = (props: any) => {
|
||||
const handleGetPhoneNumber = ({detail}: {detail: {code?: string, encryptedData?: string, iv?: string}}) => {
|
||||
const {code, encryptedData, iv} = detail
|
||||
|
||||
// 防重复登录检查
|
||||
const loginKey = 'login_in_progress'
|
||||
const loginInProgress = Taro.getStorageSync(loginKey)
|
||||
|
||||
if (loginInProgress && Date.now() - loginInProgress < 5000) { // 5秒内防重
|
||||
console.log('登录正在进行中,跳过重复请求')
|
||||
return
|
||||
}
|
||||
|
||||
// 标记登录开始
|
||||
Taro.setStorageSync(loginKey, Date.now())
|
||||
|
||||
// 获取存储的邀请参数
|
||||
const inviteParams = getStoredInviteParams()
|
||||
const refereeId = inviteParams?.inviter ? parseInt(inviteParams.inviter) : 0
|
||||
@@ -116,6 +128,9 @@ const Header = (props: any) => {
|
||||
TenantId
|
||||
},
|
||||
success: async function (res) {
|
||||
// 清除登录防重标记
|
||||
Taro.removeStorageSync('login_in_progress')
|
||||
|
||||
if (res.data.code == 1) {
|
||||
Taro.showToast({
|
||||
title: res.data.message,
|
||||
@@ -149,6 +164,10 @@ const Header = (props: any) => {
|
||||
Taro.reLaunch({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
},
|
||||
fail: function() {
|
||||
// 清除登录防重标记
|
||||
Taro.removeStorageSync('login_in_progress')
|
||||
}
|
||||
})
|
||||
} else {
|
||||
|
||||
@@ -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