"""User-facing lifecycle; a display state is never authorization to provision.""" from django.utils import timezone STEPS = ("选择套餐", "付款确认", "审核开通", "使用中", "到期续费") def lifecycle(stage, title, next_step, *, tone="normal", free=False): labels = list(STEPS) if free: labels[1] = "无需付款" return {"title": title, "next_step": next_step, "tone": tone, "steps": [{"label": label, "state": "done" if index < stage else "current" if index == stage else "future"} for index, label in enumerate(labels)]} def personal_lifecycle(user, subscription=None, application=None): free = user.identity_type == "employee" if not user.plan_application_allowed: return lifecycle(0, "账号已停用或暂停", "联系平台管理员核实账号;购买套餐不会解除停权。", tone="attention", free=free) if subscription is not None: if subscription.status == "cancelled": return lifecycle(0, "服务已取消", "选择新的套餐;取消不是已经走完付款和使用流程。", tone="attention", free=free) if subscription.status == "expired" or (subscription.expires_at and subscription.expires_at <= timezone.now()): return lifecycle(4, "已到期 / 已结束", "选择续费或新的套餐;不会自动扣款。", tone="attention", free=free) if subscription.status == "suspended": return lifecycle(3 if subscription.starts_at else 2, "服务已暂停", "查看暂停原因并联系管理员;当前不可使用。", tone="attention", free=free) if subscription.online_payment_pending: return lifecycle(1, "待付款 · 支付暂未开放", "支付接通后再完成付款;未扣款、未开通。", tone="attention") if subscription.is_usable: if subscription.expires_at and (subscription.expires_at - timezone.now()).total_seconds() <= 7 * 86400: return lifecycle(3, "即将到期 · 当前可用", "到期前申请续费;当前期限以套餐到期时间为准。", tone="attention", free=free) return lifecycle(3, "使用中", "可查看代理用量及设备;实际连接状态以客户端为准。", free=free) if subscription.traffic_limit_bytes and subscription.used_bytes >= subscription.traffic_limit_bytes: return lifecycle(3, "流量已用完", "当前不可用;申请调整额度或联系管理员,不会自动加购扣费。", tone="attention", free=free) return lifecycle(2, "待开通 / 正在下发", "等待审核或节点下发完成;当前还不能使用。", free=free) if application is not None: if application.status in ("rejected", "cancelled"): return lifecycle(0, "申请未通过 / 已取消", "核对申请原因后重新选择套餐。", tone="attention", free=free) amount = application.plan.price_for_user(user) if amount > 0 and application.plan.online_payment_required: return lifecycle(1, "待付款 · 支付暂未开放", "仅登记了申请,未扣款;支付接通后再继续。", tone="attention") if amount > 0 and application.payment_status != "paid": return lifecycle(1, "待付款确认", "联系管理员确认付款和申请进度;本页不会自动扣款。") return lifecycle(2, "等待审核开通", "审核通过后自动关联个人订阅。", free=(amount == 0)) return lifecycle(0, "尚未选择套餐", "先选择个人套餐,或切换到团队查看你的团队服务。", free=free) def team_lifecycle(team): if team.plan_draft_id: return lifecycle(1, "意向套餐 · 待开通", "团队支付、共享用量及代理授权尚未接通;目前只做团队管理,不收费。", tone="attention") return lifecycle(0, "团队筹备中 · 尚未选套餐", "由所有者联系平台管理员配置意向套餐;创建团队不等于购买成功。")