@@ -1,6 +1,7 @@
/**
* Soul 创业派对 - 用户旅程规则引擎
* 卡若 创业派对 - 用户旅程规则引擎
* 从后端 /api/miniprogram/user-rules 读取启用的规则,按场景触发引导
* 稳定版兼容: readCount 用 getReadCount(), hasPurchasedFull 用 hasFullBook, 完善头像跳 avatar-nickname
*
* trigger → scene 映射:
* 注册 → after_login
@@ -15,7 +16,14 @@
* 浏览导师页 → browse_mentor
*/
const app = getApp ( )
function getAppInstance ( ) {
try {
const a = getApp ( )
return a && a . globalData ? a : null
} catch ( e ) {
return null
}
}
const RULE _COOLDOWN _KEY = 'rule_engine_cooldown'
const COOLDOWN _MS = 60 * 1000
@@ -59,11 +67,14 @@ function setCooldown(ruleId) {
}
function getUserInfo ( ) {
return app . globalData . userInfo || { }
const app = getAppInstance ( )
return app ? ( app . globalData . userInfo || { } ) : { }
}
async function loadRules ( ) {
if ( _cachedRules && Date . now ( ) - _cacheTs < CACHE _TTL ) return _cachedRules
const app = getAppInstance ( )
if ( ! app ) return _cachedRules || [ ]
try {
const res = await app . request ( { url : '/api/miniprogram/user-rules' , method : 'GET' , silent : true } )
if ( res && res . success && res . rules ) {
@@ -85,8 +96,12 @@ function getRuleInfo(rules, triggerName) {
return rules . find ( r => r . trigger === triggerName )
}
// 稳定版:跳转 avatar-nickname( 专注头像+昵称,首次登录由 app.login 强制 redirect)
// VIP 用户不触发:统一由 checkVipContactRequiredAndGuide 引导到 profile-edit, 避免与主流程冲突
function checkRule _FillAvatar ( rules ) {
if ( ! isRuleEnabled ( rules , '注册' ) ) return null
const app = getAppInstance ( )
if ( app && app . globalData . isVip ) return null
const user = getUserInfo ( )
if ( ! user . id ) return null
const avatar = user . avatar || user . avatarUrl || ''
@@ -100,7 +115,7 @@ function checkRule_FillAvatar(rules) {
title : info ? . title || '完善个人信息' ,
message : info ? . description || '设置头像和昵称,让其他创业者更容易认识你' ,
action : 'navigate' ,
target : '/pages/profile-edit/profile-edit '
target : '/pages/avatar-nickname/avatar-nickname '
}
}
@@ -138,11 +153,13 @@ function checkRule_FillProfile(rules) {
}
}
// 稳定版兼容: readCount 用 getReadCount()
function checkRule _ShareAfter5Chapters ( rules ) {
if ( ! isRuleEnabled ( rules , '累计浏览5章节' ) ) return null
const user = getUserInfo ( )
if ( ! user . id ) return null
const readCount = app . globalData . readCount || 0
const app = getAppInstance ( )
const readCount = app ? ( typeof app . getReadCount === 'function' ? app . getReadCount ( ) : ( app . globalData . readCount || 0 ) ) : 0
if ( readCount < 5 ) return null
if ( isInCooldown ( 'share_after_5' ) ) return null
setCooldown ( 'share_after_5' )
@@ -156,11 +173,13 @@ function checkRule_ShareAfter5Chapters(rules) {
}
}
// 稳定版兼容: hasPurchasedFull 用 hasFullBook
function checkRule _FillVipInfo ( rules ) {
if ( ! isRuleEnabled ( rules , '完成付款' ) ) return null
const user = getUserInfo ( )
if ( ! user . id ) return null
if ( ! app . globalData . hasPurchasedFull ) return null
const app = getAppInstance ( )
if ( ! app || ! ( app . globalData . hasFullBook || app . globalData . hasPurchasedFull ) ) return null
if ( user . wechatId && user . address ) return null
if ( isInCooldown ( 'fill_vip_info' ) ) return null
setCooldown ( 'fill_vip_info' )
@@ -212,7 +231,8 @@ function checkRule_Withdraw(rules) {
if ( ! isRuleEnabled ( rules , '收益满50元' ) ) return null
const user = getUserInfo ( )
if ( ! user . id ) return null
const earnings = app . globalData . totalEarnings || 0
const app = getAppInstance ( )
const earnings = app ? ( app . globalData . totalEarnings || 0 ) : 0
if ( earnings < 50 ) return null
if ( isInCooldown ( 'withdraw_50' ) ) return null
setCooldown ( 'withdraw_50' )
@@ -274,6 +294,8 @@ function executeRule(rule, pageInstance) {
function _trackRuleAction ( ruleId , action ) {
const userId = getUserInfo ( ) . id
if ( ! userId ) return
const app = getAppInstance ( )
if ( ! app ) return
app . request ( {
url : '/api/miniprogram/track' ,
method : 'POST' ,