Files
zhenxun_bot-sdgb_api/charge.py
2025-10-15 19:42:27 +08:00

178 lines
6.7 KiB
Python

__all__ = ["handle_buy_ticket", "handle_wipe_ticket", "handle_query_ticket"]
bind_lock = asyncio.Lock()
async def _ensure_login(userId: int):
ts = generateTimestamp()
loginResult = apiLogin(ts, userId)
return ts, loginResult
async def handle_buy_ticket(bot: Bot, message: UniMsg, session: EventSession):
user_qq = str(session.id1)
async with bind_lock:
if user_qq not in bind_data:
await MessageUtils.build_message("请先绑定 /bind").send(reply_to=True)
return
userId = bind_data[user_qq]
text = message.extract_plain_text().strip().lower()
match = re.match(r'^/发票\s*([2356])$', text)
if not match:
await MessageUtils.build_message("命令格式错误,请使用 /发票2356").send(reply_to=True)
return
ticket_type = int(match.group(1))
ts, loginResult = await _ensure_login(userId)
return_code = loginResult.get("returnCode")
if return_code == 1:
pass
elif return_code == 100:
await MessageUtils.build_message("用户正在上机游玩,请下机后再试,或等待 15 分钟。").send(reply_to=True)
return
elif return_code == 102:
await MessageUtils.build_message("zako~zako~又不获取二维码吗").send(reply_to=True)
return
elif return_code == 103:
await MessageUtils.build_message("登录的账号 UID 无效,请检查账号是否正确。").send(reply_to=True)
return
else:
error_details = loginResult.get("message", str(loginResult))
await MessageUtils.build_message(f"登录失败!这不应该发生,请反馈此问题。\n错误详情:{error_details}").send(reply_to=True)
return
try:
try:
result_str = implBuyTicket(userId, ticket_type)
try:
result_json = json.loads(result_str)
if result_json.get("returnCode") == 1:
msg = f"✅ 发票 {ticket_type}成功"
else:
msg = f"❌ 发票失败: {result_json.get('message', result_str)}"
except:
msg = f"发票 {ticket_type} 返回: {result_str}"
await MessageUtils.build_message(msg).send(reply_to=True)
except Exception as e:
await MessageUtils.build_message(f"发票失败: {e}").send(reply_to=True)
logger.error(f"发票异常: {e}")
finally:
try:
apiLogout(ts, userId)
except Exception as e:
logger.warning(f"登出失败: {e}")
async def handle_wipe_ticket(bot: Bot, session: EventSession):
user_qq = str(session.id1)
async with bind_lock:
if user_qq not in bind_data:
await MessageUtils.build_message("请先绑定 /bind").send(reply_to=True)
return
userId = bind_data[user_qq]
ts, loginResult = await _ensure_login(userId)
return_code = loginResult.get("returnCode")
if return_code == 1:
pass
elif return_code == 100:
await MessageUtils.build_message("用户正在上机游玩,请下机后再试,或等待 15 分钟。").send(reply_to=True)
return
elif return_code == 102:
await MessageUtils.build_message("zako~zako~又不获取二维码吗").send(reply_to=True)
return
elif return_code == 103:
await MessageUtils.build_message("登录的账号 UID 无效,请检查账号是否正确。").send(reply_to=True)
return
else:
error_details = loginResult.get("message", str(loginResult))
await MessageUtils.build_message(f"登录失败!这不应该发生,请反馈此问题。\n错误详情:{error_details}").send(reply_to=True)
return
try:
try:
result = implWipeTickets(userId, ts, loginResult)
if isinstance(result, dict) and result.get("returnCode") == 1:
await MessageUtils.build_message("✅ 清票成功!").send(reply_to=True)
else:
await MessageUtils.build_message(f"❌ 清票失败: {result}").send(reply_to=True)
except Exception as e:
await MessageUtils.build_message(f"❌ 清票失败: {e}").send(reply_to=True)
logger.error(f"清票异常: {e}")
finally:
try:
apiLogout(ts, userId)
except Exception as e:
logger.warning(f"登出失败: {e}")
async def handle_query_ticket(bot: Bot, session: EventSession):
user_qq = str(session.id1)
async with bind_lock:
if user_qq not in bind_data:
await MessageUtils.build_message("请先绑定 /bind").send(reply_to=True)
return
userId = bind_data[user_qq]
ts, loginResult = await _ensure_login(userId)
return_code = loginResult.get("returnCode")
if return_code == 1:
pass
elif return_code == 100:
await MessageUtils.build_message("用户正在上机游玩,请下机后再试,或等待 15 分钟。").send(reply_to=True)
return
elif return_code == 102:
await MessageUtils.build_message("zako~zako~又不获取二维码吗").send(reply_to=True)
return
elif return_code == 103:
await MessageUtils.build_message("登录的账号 UID 无效,请检查账号是否正确。").send(reply_to=True)
return
else:
error_details = loginResult.get("message", str(loginResult))
await MessageUtils.build_message(f"登录失败!这不应该发生,请反馈此问题。\n错误详情:{error_details}").send(reply_to=True)
return
try:
try:
result = apiQueryTicket(userId)
if isinstance(result, str):
import json
try:
result = json.loads(result)
except Exception:
pass
msg_lines = []
userChargeList = result.get("userChargeList", []) if isinstance(result, dict) else []
if not userChargeList:
msg_lines.append("当前没有任何记录。")
else:
charge_name_map = {2: "2倍票", 3: "3倍票", 5: "5倍票", 6: "6倍票"}
for charge in userChargeList:
charge_id = charge.get("chargeId")
stock = charge.get("stock", 0)
valid_date = charge.get("validDate", "未知")
msg_lines.append(f"{charge_name_map.get(charge_id, charge_id)}: 持有 {stock}, 有效期 {valid_date}")
msg_text = "📋 当前票状态:\n" + "\n".join(msg_lines)
await MessageUtils.build_message(msg_text).send(reply_to=True)
except Exception as e:
await MessageUtils.build_message(f"❌ 查票失败: {e}").send(reply_to=True)
logger.error(f"查票异常: {e}")