refactor: harden client runtime and exports

This commit is contained in:
ANG-Server
2026-07-23 00:59:00 +08:00
parent 5d2339a29b
commit fc872fe8af
27 changed files with 902 additions and 856 deletions

View File

@@ -20,7 +20,6 @@ const MALL_BADGE_TEXTURE: Texture2D = preload("res://assets/ui/mall/branding/bra
const COIN_TEXTURE: Texture2D = preload("res://assets/ui/mall/branding/branding_whale_coin.png")
const CATALOG_SCRIPT: Script = preload("res://Config/MallCatalog.gd")
const ITEM_CARD_SCRIPT: Script = preload("res://scenes/ui/mall/MallItemCard.gd")
const NetworkConfig = preload("res://_Core/utils/NetworkConfig.gd")
var _overlay: ColorRect
var _panel: Control
@@ -39,8 +38,6 @@ var _toastLabel: Label
var _confirmDialog: Control
var _confirmLabel: Label
var _currencyLabel: Label
var _purchaseRequest: HTTPRequest
var _catalogRequest: HTTPRequest
var _categoryButtons: Dictionary = {}
var _cards: Array[Button] = []
@@ -175,8 +172,6 @@ func _build_ui() -> void:
_build_toast()
_build_confirm_dialog()
_build_purchase_request()
_build_catalog_request()
_update_panel_alpha(0.0, 0.0)
func _build_header() -> Control:
@@ -859,20 +854,6 @@ func _format_item_price(item: Dictionary) -> String:
func _is_skin_item(item: Dictionary) -> bool:
return not str(item.get("skinId", "")).strip_edges().is_empty()
func _build_purchase_request() -> void:
_purchaseRequest = HTTPRequest.new()
_purchaseRequest.name = "mallPurchaseRequest"
_purchaseRequest.timeout = 12.0
_purchaseRequest.request_completed.connect(_on_purchase_request_completed)
add_child(_purchaseRequest)
func _build_catalog_request() -> void:
_catalogRequest = HTTPRequest.new()
_catalogRequest.name = "mallCatalogRequest"
_catalogRequest.timeout = 12.0
_catalogRequest.request_completed.connect(_on_catalog_request_completed)
add_child(_catalogRequest)
func _connect_auth_state() -> void:
var authManager := get_node_or_null("/root/AuthManager")
if authManager == null or not authManager.has_signal("auth_state_changed"):
@@ -907,10 +888,6 @@ func _current_account_generation() -> int:
return -1
func _cancel_account_bound_requests() -> void:
if is_instance_valid(_catalogRequest):
_catalogRequest.cancel_request()
if is_instance_valid(_purchaseRequest):
_purchaseRequest.cancel_request()
_catalogRequestAccountGeneration = -1
_purchaseRequestAccountGeneration = -1
@@ -926,14 +903,6 @@ func _reset_account_bound_state() -> void:
_selectedItem = {}
_hide_confirm_dialog()
func _auth_headers() -> PackedStringArray:
var authManager := get_node_or_null("/root/AuthManager")
var accessToken := str(authManager.call("get_access_token")).strip_edges() if authManager != null and authManager.has_method("get_access_token") else ""
return PackedStringArray([
"Content-Type: application/json",
"Authorization: Bearer %s" % accessToken,
])
func _submit_backend_purchase(item: Dictionary) -> void:
var authManager := get_node_or_null("/root/AuthManager")
var accessToken := str(authManager.call("get_access_token")).strip_edges() if authManager != null else ""
@@ -949,43 +918,24 @@ func _submit_backend_purchase(item: Dictionary) -> void:
"item_id": str(item.get("id", "")),
}
_purchaseRequestAccountGeneration = _current_account_generation()
var err := _purchaseRequest.request("%s/shop/purchases" % NetworkConfig.get_api_base_url(), _auth_headers(), HTTPClient.METHOD_POST, JSON.stringify(payload))
if err != OK:
var api_client := get_node_or_null("/root/ApiClient")
if api_client == null or not api_client.has_method("post_json"):
_purchaseRequestAccountGeneration = -1
_hide_confirm_dialog()
_emit_event(EventNames.MALL_PURCHASE_FAILED, {"item": item, "reason": "request_failed"})
_show_toast("购买请求发送失败")
_show_toast("购买服务不可用")
_update_purchase_button(_selectedItem)
return
api_client.call("post_json", "/shop/purchases", payload, _on_purchase_request_completed, true)
func _on_purchase_request_completed(result: int, responseCode: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
func _on_purchase_request_completed(success: bool, response: Dictionary, error_info: Dictionary) -> void:
if _purchaseRequestAccountGeneration != _current_account_generation():
return
_purchaseRequestAccountGeneration = -1
if result != HTTPRequest.RESULT_SUCCESS:
_hide_confirm_dialog()
_emit_event(EventNames.MALL_PURCHASE_FAILED, {"item": _selectedItem, "reason": "network_failed"})
_show_toast("购买失败,请稍后再试")
_update_purchase_button(_selectedItem)
return
var json := JSON.new()
if json.parse(body.get_string_from_utf8()) != OK:
_hide_confirm_dialog()
_emit_event(EventNames.MALL_PURCHASE_FAILED, {"item": _selectedItem, "reason": "invalid_response"})
_show_toast("购买响应解析失败")
_update_purchase_button(_selectedItem)
return
var responseVariant: Variant = json.data
if not (responseVariant is Dictionary):
_hide_confirm_dialog()
_emit_event(EventNames.MALL_PURCHASE_FAILED, {"item": _selectedItem, "reason": "invalid_response"})
_show_toast("购买响应格式错误")
_update_purchase_button(_selectedItem)
return
var response: Dictionary = responseVariant
if responseCode < 200 or responseCode >= 300 or not bool(response.get("success", true)):
if not success:
_hide_confirm_dialog()
_emit_event(EventNames.MALL_PURCHASE_FAILED, {"item": _selectedItem, "reason": "backend_rejected"})
_show_toast(str(response.get("message", "购买失败")))
_show_toast(str(error_info.get("message", "购买失败")))
_update_purchase_button(_selectedItem)
return
var dataVariant: Variant = response.get("data", {})
@@ -1030,37 +980,19 @@ func _fetch_catalog() -> void:
_walletError = ""
_update_currency()
_catalogRequestAccountGeneration = _current_account_generation()
var err := _catalogRequest.request("%s/shop/catalog" % NetworkConfig.get_api_base_url(), _auth_headers(), HTTPClient.METHOD_GET, "")
if err != OK:
var api_client := get_node_or_null("/root/ApiClient")
if api_client == null or not api_client.has_method("get_json"):
_catalogRequestAccountGeneration = -1
_catalogLoading = false
_catalogLoaded = false
_catalogError = "商城数据请求发送失败"
_walletLoading = false
_walletLoaded = false
_walletError = _catalogError
_update_currency()
_render_grid([])
push_warning("MallPanel: 商城数据请求发送失败: %s" % error_string(err))
_apply_catalog_error("商城服务不可用")
return
api_client.call("get_json", "/shop/catalog", _on_catalog_request_completed, true)
func _on_catalog_request_completed(result: int, responseCode: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
func _on_catalog_request_completed(success: bool, response: Dictionary, error_info: Dictionary) -> void:
if _catalogRequestAccountGeneration != _current_account_generation():
return
_catalogRequestAccountGeneration = -1
if result != HTTPRequest.RESULT_SUCCESS or responseCode < 200 or responseCode >= 300:
_apply_catalog_error("商城数据读取失败")
return
var json := JSON.new()
if json.parse(body.get_string_from_utf8()) != OK:
_apply_catalog_error("商城响应解析失败")
return
var responseVariant: Variant = json.data
if not (responseVariant is Dictionary):
_apply_catalog_error("商城响应格式错误")
return
var response: Dictionary = responseVariant
if not bool(response.get("success", true)):
_apply_catalog_error(str(response.get("message", "商城数据读取失败")))
if not success:
_apply_catalog_error(str(error_info.get("message", "商城数据读取失败")))
return
var dataVariant: Variant = response.get("data", {})
if not (dataVariant is Dictionary):