"""账户信息:积分余额、身份、免费额度。 tripocli balance | tripocli whoami | tripocli quota """ import time from ..auth import decode_jwt_payload def _balance(ctx): data = ctx.client.payment_profile() or {} wallet = data.get("wallet", {}) member = data.get("member", {}) ctx.out.data( data, human=lambda: ( ctx.out.line(f"剩余积分 : {wallet.get('total_credit', 0)}"), ctx.out.line(f"即将过期 : {wallet.get('expiring_credit', 0)} (到期 {wallet.get('expiring_date', '无')})"), ctx.out.line(f"会员等级 : {member.get('type', 'free')} (有效至 {member.get('valid_until', '无')})"), ), ) return data def _whoami(ctx): client = ctx.client info = { "user_id": None, "token_expires_at": None, "account": client.account_name, "token_source": client.token_source or ("account" if client.cookie else None), } payload = decode_jwt_payload(client.token or "") info["user_id"] = payload.get("sub") if payload.get("exp"): info["token_expires_at"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(payload["exp"])) profile = ctx.client.payment_profile() or {} info["member"] = profile.get("member", {}) info["wallet"] = profile.get("wallet", {}) ctx.out.data( info, human=lambda: ( ctx.out.line(f"用户 ID : {info.get('user_id')}"), ctx.out.line(f"Token 到期 : {info.get('token_expires_at')} ({info['token_source']})"), ctx.out.line(f"积分余额 : {info.get('wallet', {}).get('total_credit', 0)}"), ), ) return info def _quota(ctx): data = ctx.client.quota() or {} quotas = data.get("quota", {}) def human(): for group, items in quotas.items(): for name, value in items.items(): ctx.out.line(f"{group}/{name}: 剩余 {value.get('remaining', 0)} / 共 {value.get('total_count', 0)}") ctx.out.data(data, human=human) return data def register(subparsers): parser = subparsers.add_parser("balance", help="查询积分余额与会员状态") parser.set_defaults(func=_balance) parser = subparsers.add_parser("whoami", help="查询当前身份与 token 有效期") parser.set_defaults(func=_whoami) parser = subparsers.add_parser("quota", help="查询特权/试用免费额度") parser.set_defaults(func=_quota)