set_params() { echo"当前 productId = $PRODUCT_ID" read -rp "输入新的 productId(直接回车保留当前): " p if [ -n "$p" ]; then PRODUCT_ID="$p" fi
echo"当前 quantity = $QUANTITY" read -rp "输入新的 quantity(整数,回车保留当前): " q if [ -n "$q" ]; then if [[ "$q" =~ ^[0-9]+$ ]]; then QUANTITY="$q" else echo"[-] quantity 必须为整数,已忽略修改" fi fi
echo"当前 couponId = $COUPON_ID" read -rp "输入新的 couponId(回车表示不使用优惠券): " c if [ -n "$c" ]; then COUPON_ID="$c" else COUPON_ID="" fi
echo"[+] 参数已更新" }
create_order() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi
local coupon_json if [ -n "$COUPON_ID" ]; then coupon_json=$(printf'"%s"'"$COUPON_ID") else coupon_json="null" fi
local payload payload=$(printf'{"productId":"%s","quantity":%s,"couponId":%s}' \ "$PRODUCT_ID""$QUANTITY""$coupon_json")
local resp resp=$(api_post "/api/order/create""$payload") pretty_print "创建订单结果""$resp"
ifcommand -v jq >/dev/null 2>&1; then local oid oid=$(echo"$resp" | jq -r '.data.order.id // empty' 2>/dev/null) if [ -n "$oid" ]; then LAST_ORDER_ID="$oid" echo"[+] 已保存最近订单 ID: $LAST_ORDER_ID" else echo"[-] 未从返回结果中解析到订单 ID" fi fi }
refund_order() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi
local oid read -rp "输入要退款的订单 ID(回车使用 last_order_id=$LAST_ORDER_ID): " oid if [ -z "$oid" ]; then oid="$LAST_ORDER_ID" fi if [ -z "$oid" ]; then echo"[-] 没有可用的订单 ID" return fi
local resp resp=$(api_post "/api/order/refund/${oid}""") pretty_print "订单退款结果""$resp" }
user_info() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi
local resp resp=$(api_get "/api/user/info") pretty_print "当前用户信息 (/api/user/info)""$resp" }
my_orders() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi
local resp resp=$(api_get "/api/order/my")
echo echo"===== 当前用户订单列表(只显示 id / status) ====="
ifcommand -v jq >/dev/null 2>&1; then echo"$resp" | jq -r ' if .data == null then "无 data 字段" elif (.data | type) == "array" then .data[] | "id=\(.id) status=\(.status)" else .data | "id=\(.id) status=\(.status)" end ' else echo"[!] 未安装 jq,无法只提取 id/status,原始响应如下:" echo"$resp" fi
threaded_once() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi if [ -z "$LAST_ORDER_ID" ]; then echo"[-] 请先创建至少一个订单(菜单 4),以便有 orderId 可以退款" return fi
local oid="$LAST_ORDER_ID" echo"[*] 开始一次并发:createOrder(无券) + refundOrder($oid)"
local tmp_create tmp_refund tmp_create=$(mktemp) tmp_refund=$(mktemp)
# ⚠ 并发下单不带优惠券,couponId 固定为 null local payload payload=$(printf'{"productId":"%s","quantity":%s,"couponId":null}' \ "$PRODUCT_ID""$QUANTITY")
echo"----- create_order 响应 -----" ifcommand -v jq >/dev/null 2>&1; then cat"$tmp_create" | jq . 2>/dev/null || cat"$tmp_create" else cat"$tmp_create" fi
echo"----- refund_order 响应 -----" ifcommand -v jq >/dev/null 2>&1; then cat"$tmp_refund" | jq . 2>/dev/null || cat"$tmp_refund" else cat"$tmp_refund" fi
rm -f "$tmp_create""$tmp_refund" }
threaded_loop() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi if [ -z "$LAST_ORDER_ID" ]; then echo"[-] 请先创建至少一个订单(菜单 4),以便有 orderId 可以退款" return fi
read -rp "输入循环轮数(例如 50 / 100): " rounds if ! [[ "$rounds" =~ ^[0-9]+$ ]]; then echo"[-] 轮数必须是整数" return fi
local i echo"[*] 开始循环并发测试,共 ${rounds} 轮" for ((i = 1; i <= rounds; i++)); do echo echo"---- Round ${i}/${rounds} ----"
local tmp_create tmp_refund tmp_create=$(mktemp) tmp_refund=$(mktemp)
# payload for create_order local coupon_json payload if [ -n "$COUPON_ID" ]; then coupon_json=$(printf'"%s"'"$COUPON_ID") else coupon_json="null" fi payload=$(printf'{"productId":"%s","quantity":%s,"couponId":%s}' \ "$PRODUCT_ID""$QUANTITY""$coupon_json")
# 选择要使用的那一张 read -rp "请选择要使用的优惠券序号(0 ~ $((count - 1)),直接回车选 0): " idx if [ -z "$idx" ]; then idx=0 fi
if ! [[ "$idx" =~ ^[0-9]+$ ]] || [ "$idx" -lt 0 ] || [ "$idx" -ge "$count" ]; then echo"[-] 序号不合法,取消设置 COUPON_ID" return fi
local cid cid=$(echo"$resp" | jq -r ".data[$idx].id" 2>/dev/null)
if [ -n "$cid" ]; then COUPON_ID="$cid" echo"[+] 已选择优惠券:$COUPON_ID" else echo"[-] 未能解析到优惠券 id" fi }
auto_refund_completed() { if [ -z "$TOKEN" ]; then echo"[-] 请先设置 token(菜单 1 或 2)" return fi
echo"[*] 从 /api/order/my 获取订单列表..." local resp resp=$(api_get "/api/order/my")
if ! command -v jq >/dev/null 2>&1; then echo"[!] 未安装 jq,无法解析订单状态,原始响应如下:" echo"$resp" return fi
# 提取所有 status == "COMPLETED" 的订单 id local ids ids=$(echo"$resp" | jq -r ' if .data == null then empty elif (.data | type) == "array" then .data[] | select(.status == "COMPLETED") | .id else # 如果 data 是单个对象 if .data.status == "COMPLETED" then .data.id else empty end end ')
if [ -z "$ids" ]; then echo"[*] 没有状态为 COMPLETED 的订单,无需操作。" return fi
echo"===== 将自动退款以下 COMPLETED 订单 =====" echo"$ids" | nl -ba echo"=======================================" read -rp "确认执行退款吗?(Y/n): " confirm if [[ "$confirm" =~ ^[Nn]$ ]]; then echo"[-] 已取消自动退款操作。" return fi