feat(payment): 卡密管理改为独立页面并对齐参考主题

- 移除「余额充值」设置中的充值卡(card_pass_*)配置组,PHP schema 与 SPA mirror 双轨同步
- 重写后台卡密管理页:列表过滤/排序/搜索/批量删除/同款分页/点击复制、自动生成与导入、text/xls 导出
- ChargeCard 服务新增 generate_cards/rand_number/rand_password/delete_by_ids/get_export_rows,get_list 支持参考主题排序别名与标识过滤
- 删除无消费方的 /payment/charge-cards REST 路由及 batch_create/disable 死代码,数据层零迁移
This commit is contained in:
2026-09-16 22:05:37 +08:00
parent ba1c8c2822
commit 678143e7b9
9 changed files with 1227 additions and 788 deletions
-351
View File
@@ -41,74 +41,6 @@ class PaymentController extends ControllerBase {
'note' => ['type' => 'string', 'default' => '', 'maxLength' => 1000, 'sanitize_callback' => 'sanitize_textarea_field'],
],
]);
// ── Charge Card ──
// Charge card list
register_rest_route(self::API_NAMESPACE, '/payment/charge-cards', [
'methods' => 'GET',
'callback' => [$this, 'get_charge_cards'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'page' => ['type' => 'integer', 'minimum' => 1, 'default' => 1],
'per_page' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20],
'status' => ['type' => 'string', 'enum' => ['', 'active', 'used', 'expired', 'disabled']],
'keyword' => ['type' => 'string', 'maxLength' => 191, 'sanitize_callback' => 'sanitize_text_field'],
'date_from' => ['type' => 'string', 'format' => 'date'],
'date_to' => ['type' => 'string', 'format' => 'date'],
],
]);
register_rest_route(self::API_NAMESPACE, '/payment/charge-cards/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => [$this, 'get_charge_card_detail'],
'permission_callback' => [$this, 'check_permission'],
'args' => ['id' => ['type' => 'integer', 'minimum' => 1, 'required' => true]],
]);
// Charge card create
register_rest_route(self::API_NAMESPACE, '/payment/charge-cards', [
'methods' => 'POST',
'callback' => [$this, 'create_charge_card'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'type' => ['type' => 'string', 'required' => true, 'enum' => ['balance', 'vip', 'credits']],
'password' => ['type' => 'string', 'required' => true, 'minLength' => 1, 'maxLength' => 80, 'sanitize_callback' => 'sanitize_text_field'],
'amount' => ['type' => 'string', 'pattern' => '^(0|[1-9]\\d*)(?:\\.\\d{1,2})?$'],
'points' => ['type' => 'integer', 'minimum' => 1],
'level' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 2],
'days' => ['type' => 'integer', 'minimum' => 0],
'expires_at' => ['type' => 'string', 'maxLength' => 32, 'sanitize_callback' => 'sanitize_text_field'],
'note' => ['type' => 'string', 'maxLength' => 1000, 'sanitize_callback' => 'sanitize_textarea_field'],
],
]);
// Charge card batch create
register_rest_route(self::API_NAMESPACE, '/payment/charge-cards/batch', [
'methods' => 'POST',
'callback' => [$this, 'batch_create_charge_cards'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'type' => ['type' => 'string', 'required' => true, 'enum' => ['balance', 'vip', 'credits']],
'count' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'required' => true],
'amount' => ['type' => 'string', 'pattern' => '^(0|[1-9]\\d*)(?:\\.\\d{1,2})?$'],
'points' => ['type' => 'integer', 'minimum' => 1],
'level' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 2],
'days' => ['type' => 'integer', 'minimum' => 0],
'prefix' => ['type' => 'string', 'maxLength' => 20, 'sanitize_callback' => 'sanitize_text_field'],
'length' => ['type' => 'integer', 'minimum' => 8, 'maximum' => 32, 'default' => 16],
'expires_at' => ['type' => 'string', 'maxLength' => 32, 'sanitize_callback' => 'sanitize_text_field'],
'note' => ['type' => 'string', 'maxLength' => 1000, 'sanitize_callback' => 'sanitize_textarea_field'],
],
]);
// Charge card disable
register_rest_route(self::API_NAMESPACE, '/payment/charge-cards/(?P<id>\d+)/disable', [
'methods' => 'POST',
'callback' => [$this, 'disable_charge_card'],
'permission_callback' => [$this, 'check_permission'],
'args' => ['id' => ['type' => 'integer', 'minimum' => 1, 'required' => true]],
]);
}
public function get_withdrawals(\WP_REST_Request $request): \WP_REST_Response {
@@ -178,287 +110,4 @@ class PaymentController extends ControllerBase {
return $this->error(__('更新失败', 'aksrt'));
}
/* ------------------------------------------------------------------
* Charge Card
* ----------------------------------------------------------------*/
/**
* GET /payment/charge-cards — 充值卡列表
*/
public function get_charge_cards(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
return $this->success(['data' => ['items' => [], 'total' => 0, 'page' => 1, 'max_pages' => 1]]);
}
global $wpdb;
$table = \AKSRT\Core\DBMigrator::table('charge_card');
$page = max(1, (int) ($request->get_param('page') ?: 1));
$per_page = max(1, min(100, (int) ($request->get_param('per_page') ?: 20)));
$status = sanitize_key((string) $request->get_param('status'));
$keyword = trim(sanitize_text_field((string) $request->get_param('keyword')));
$date_from = sanitize_text_field((string) $request->get_param('date_from'));
$date_to = sanitize_text_field((string) $request->get_param('date_to'));
$where = [];
$params = [];
if ($status !== '') {
$where[] = 'status = %s';
$params[] = $status;
}
if ($date_from !== '') {
$where[] = 'created_at >= %s';
$params[] = $date_from . ' 00:00:00';
}
if ($date_to !== '') {
$where[] = 'created_at <= %s';
$params[] = $date_to . ' 23:59:59';
}
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
$sql = "SELECT * FROM {$table} {$where_sql} ORDER BY created_at DESC LIMIT 500";
$rows = $params ? $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A) : $wpdb->get_results($sql, ARRAY_A);
$items = [];
foreach ((array) $rows as $row) {
$item = $this->build_charge_card_admin_item($row);
if ($keyword !== '' && mb_stripos(implode(' ', array_filter([
(string) ($item['password'] ?? ''),
(string) ($item['card'] ?? ''),
(string) ($item['type_label'] ?? ''),
(string) ($item['note'] ?? ''),
(string) ($item['used_by_user']['display_name'] ?? ''),
(string) ($item['used_by_user']['email'] ?? ''),
])), $keyword) === false) {
continue;
}
$items[] = $item;
}
$total = count($items);
$offset = ($page - 1) * $per_page;
$stats = \AKSRT\Payment\ChargeCard::get_instance()->get_stats();
return $this->success([
'data' => [
'items' => array_values(array_slice($items, $offset, $per_page)),
'total' => $total,
'page' => $page,
'per_page' => $per_page,
'max_pages' => max(1, (int) ceil(max(1, $total) / $per_page)),
'stats' => $stats,
'options' => [
'statuses' => [
['label' => __('全部状态', 'aksrt'), 'value' => ''],
['label' => __('未使用', 'aksrt'), 'value' => 'active'],
['label' => __('已使用', 'aksrt'), 'value' => 'used'],
['label' => __('已过期', 'aksrt'), 'value' => 'expired'],
['label' => __('已禁用', 'aksrt'), 'value' => 'disabled'],
],
],
],
]);
}
/**
* POST /payment/charge-cards — 创建充值卡
*/
public function create_charge_card(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
return $this->error('充值卡系统未启用', 500);
}
$params = $this->get_json_params($request);
$type = sanitize_key((string) ($params['type'] ?? 'balance'));
if (!in_array($type, ['balance', 'vip', 'credits'], true)) {
$type = 'balance';
}
$password = sanitize_text_field((string) ($params['password'] ?? ''));
if ($password === '') {
return $this->error('卡密不能为空');
}
$id = \AKSRT\Payment\ChargeCard::get_instance()->create(array_merge($params, [
'type' => $type,
'password' => $password,
]));
if ($id > 0) {
return $this->success([
'message' => __('充值卡已创建', 'aksrt'),
'data' => ['id' => $id],
], 201);
}
return $this->error(__('创建失败(可能卡密已存在或参数无效)', 'aksrt'));
}
/**
* POST /payment/charge-cards/batch — 批量生成充值卡
*/
public function batch_create_charge_cards(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
return $this->error('充值卡系统未启用', 500);
}
$params = $this->get_json_params($request);
$type = sanitize_key((string) ($params['type'] ?? 'balance'));
if (!in_array($type, ['balance', 'vip', 'credits'], true)) {
$type = 'balance';
}
$count = max(1, min(100, (int) ($params['count'] ?? 1)));
$length = max(8, min(32, (int) ($params['length'] ?? 16)));
$prefix = sanitize_text_field((string) ($params['prefix'] ?? ''));
$note = sanitize_textarea_field((string) ($params['note'] ?? ''));
$expires = sanitize_text_field((string) ($params['expires_at'] ?? ''));
$raw_amount = $params['amount'] ?? '';
$meta_fields = [
'amount' => is_string($raw_amount) ? $raw_amount : '',
'level' => (int) ($params['level'] ?? 1),
'days' => (int) ($params['days'] ?? 30),
'points' => (int) ($params['points'] ?? 0),
];
if ($type === 'balance') {
try {
$card_amount = \AKSRT\Ledger\AssetAmount::balance($meta_fields['amount']);
} catch (\InvalidArgumentException $e) {
return $this->error('余额卡金额格式无效');
}
if ($card_amount->minor() <= 0) return $this->error('余额卡金额必须大于 0');
$meta_fields['amount'] = $card_amount->decimal();
}
if ($type === 'credits' && $meta_fields['points'] <= 0) {
return $this->error('积分卡积分必须大于 0');
}
$codes = \AKSRT\Payment\ChargeCard::get_instance()->batch_create(
$type,
$meta_fields,
$count,
$length,
$prefix,
$expires,
$note
);
return $this->success([
'message' => sprintf(__('已生成 %d 张充值卡', 'aksrt'), count($codes)),
'data' => ['codes' => $codes, 'count' => count($codes), 'type' => $type],
]);
}
/**
* POST /payment/charge-cards/{id}/disable — 禁用充值卡
*/
public function disable_charge_card(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\ChargeCard')) {
return $this->error('充值卡系统未启用', 500);
}
$id = (int) $request->get_param('id');
if ($id <= 0) {
return $this->error('参数无效');
}
$success = \AKSRT\Payment\ChargeCard::get_instance()->disable($id);
if ($success) {
return $this->success(['message' => __('充值卡已禁用', 'aksrt')]);
}
return $this->error(__('操作失败', 'aksrt'));
}
public function get_charge_card_detail(\WP_REST_Request $request): \WP_REST_Response {
global $wpdb;
$id = (int) $request->get_param('id');
$table = \AKSRT\Core\DBMigrator::table('charge_card');
if ($id <= 0 || !$table) {
return $this->error(__('充值卡不存在', 'aksrt'), 404);
}
$row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $id), ARRAY_A);
if (!$row) {
return $this->error(__('充值卡不存在', 'aksrt'), 404);
}
return $this->success([
'data' => [
'card' => $this->build_charge_card_admin_item($row),
],
]);
}
private function build_charge_card_admin_item(array $row): array {
$used_by = (int) ($row['used_by'] ?? 0);
$user = $used_by > 0 ? get_userdata($used_by) : null;
$status = sanitize_key((string) ($row['status'] ?? 'active'));
$type = sanitize_key((string) ($row['type'] ?? 'balance'));
$statusMap = [
'active' => __('未使用', 'aksrt'),
'used' => __('已使用', 'aksrt'),
'expired' => __('已过期', 'aksrt'),
'disabled' => __('已禁用', 'aksrt'),
];
$typeMap = [
'balance' => __('余额卡', 'aksrt'),
'vip' => __('会员卡', 'aksrt'),
'credits' => __('积分卡', 'aksrt'),
];
$meta = [];
if (!empty($row['meta'])) {
if (is_array($row['meta'])) {
$meta = $row['meta'];
} else {
$decoded = json_decode((string) $row['meta'], true);
if (is_array($decoded)) {
$meta = $decoded;
}
}
}
$password = (string) ($row['password'] ?? '');
$card_no = (string) ($row['card'] ?? '');
return [
'id' => (int) ($row['id'] ?? 0),
'password' => $password,
'card' => $card_no,
'type' => $type,
'type_label' => $typeMap[$type] ?? $type,
'amount' => $this->cash_decimal((string) ($meta['amount'] ?? $row['amount'] ?? '0')),
'balance' => $this->cash_decimal((string) ($row['balance'] ?? '0')),
'meta' => $meta,
'level' => (int) ($meta['level'] ?? 0),
'days' => (int) ($meta['days'] ?? 0),
'points' => (int) ($meta['points'] ?? 0),
'order_number' => (string) ($row['order_number'] ?? ''),
'status' => $status,
'status_label' => $statusMap[$status] ?? $status,
'used_by' => $used_by,
'used_at' => (string) ($row['used_at'] ?? ''),
'expires_at' => (string) ($row['expires_at'] ?? ''),
'created_at' => (string) ($row['created_at'] ?? ''),
'note' => (string) ($row['note'] ?? ''),
'used_by_user' => $user instanceof \WP_User ? [
'id' => (int) $user->ID,
'display_name' => $user->display_name,
'email' => $user->user_email,
] : null,
];
}
private function cash_decimal(string $value): string {
try {
return \AKSRT\Ledger\AssetAmount::balance($value)->decimal();
} catch (\InvalidArgumentException $e) {
return '0.00';
}
}
}