diff --git a/backend/internal/router/app/dynamic.go b/backend/internal/router/app/dynamic.go index 4417500..a673482 100644 --- a/backend/internal/router/app/dynamic.go +++ b/backend/internal/router/app/dynamic.go @@ -173,13 +173,8 @@ func handleExecuteDynamicCode(c *gin.Context) { userVariables := map[string]interface{}{} devices := []map[string]interface{}{} - targetUserID := claims.UserID - if req.UserID != nil { - targetUserID = *req.UserID - } - var user model.AppUser - if err := database.DB.Where("id = ? AND application_id = ?", targetUserID, app.ID).First(&user).Error; err == nil { + if err := database.DB.Where("id = ? AND application_id = ?", claims.UserID, app.ID).First(&user).Error; err == nil { userData = map[string]interface{}{ "id": user.ID, "username": user.Username, @@ -222,6 +217,57 @@ func handleExecuteDynamicCode(c *gin.Context) { } } + targetUserData := map[string]interface{}{} + targetSubscription := map[string]interface{}{} + targetUserVariables := map[string]interface{}{} + targetDevices := []map[string]interface{}{} + + if req.UserID != nil && *req.UserID != claims.UserID { + var targetUser model.AppUser + if err := database.DB.Where("id = ? AND application_id = ?", *req.UserID, app.ID).First(&targetUser).Error; err == nil { + targetUserData = map[string]interface{}{ + "id": targetUser.ID, + "username": targetUser.Username, + "email": targetUser.Email, + "status": targetUser.Status, + "device_id": targetUser.DeviceID, + "avatar": targetUser.Avatar, + "created_at": targetUser.CreatedAt, + "last_login_at": targetUser.LastLoginAt, + } + + targetSubscription = map[string]interface{}{ + "balance": targetUser.Balance, + "is_trial_user": targetUser.IsTrialUser, + "trial_start_at": targetUser.TrialStartAt, + "trial_end_at": targetUser.TrialEndAt, + "expiry_at": targetUser.ExpiryAt, + "is_expired": targetUser.ExpiryAt != nil && targetUser.ExpiryAt.Before(time.Now()), + "is_lifetime": targetUser.Balance == -1, + "days_remaining": calculateDaysRemaining(targetUser.ExpiryAt, targetUser.Balance), + } + + var targetUserDevices []model.UserDevice + database.DB.Where("user_id = ? AND application_id = ?", targetUser.ID, app.ID).Find(&targetUserDevices) + for _, d := range targetUserDevices { + targetDevices = append(targetDevices, map[string]interface{}{ + "id": d.ID, + "device_id": d.DeviceID, + "device_name": d.DeviceName, + "device_type": d.DeviceType, + "status": d.Status, + "created_at": d.CreatedAt, + }) + } + + var targetUserVars []model.UserVariable + database.DB.Where("user_id = ? AND app_id = ?", targetUser.ID, app.ID).Find(&targetUserVars) + for _, v := range targetUserVars { + targetUserVariables[v.VarName] = v.VarValue + } + } + } + if err := vm.Set("user", userData); err != nil { response.Error(c, 500, "用户数据设置失败") return @@ -239,6 +285,23 @@ func handleExecuteDynamicCode(c *gin.Context) { return } + if err := vm.Set("targetUser", targetUserData); err != nil { + response.Error(c, 500, "目标用户数据设置失败") + return + } + if err := vm.Set("targetSubscription", targetSubscription); err != nil { + response.Error(c, 500, "目标用户订阅数据设置失败") + return + } + if err := vm.Set("targetUserVariables", targetUserVariables); err != nil { + response.Error(c, 500, "目标用户变量设置失败") + return + } + if err := vm.Set("targetDevices", targetDevices); err != nil { + response.Error(c, 500, "目标用户设备数据设置失败") + return + } + for k, v := range req.Params { if err := vm.Set(k, v); err != nil { response.Error(c, 500, "参数设置失败")