fix(users): restore WeChat template notification flow

This commit is contained in:
kate522 committed 2026-11-10 20:54:24 +08:00
1 parent 30ceab4615
commit 7fe26d3fff
18 files changed
+1051 -763

No files matched your search

+57 -35
View File
@@ -256,6 +256,12 @@ class UserController extends ControllerBase {
'permission_callback' => [$this, 'check_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/wechat/test', [
'methods' => 'POST',
'callback' => [$this, 'test_wechat_template'],
'permission_callback' => [$this, 'check_permission'],
]);
// ── User Restrictions / Ban ──
// List restrictions for a user (admin)
@@ -324,11 +330,7 @@ class UserController extends ControllerBase {
}
/**
* GET /wechat/status — 微信模板消息配置状态。
*
* 状态形状与 WeChatNotify::get_status() 对齐(该方法的既定用途即 REST 与 AJAX 复用)。
*/
/** GET /wechat/status */
public function get_wechat_status(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\Jio\Payment\WeChatNotify')) {
return $this->error(__('微信通知组件未加载', 'jio'), 500);
@@ -337,39 +339,59 @@ class UserController extends ControllerBase {
return $this->success(['data' => \Jio\Payment\WeChatNotify::get_instance()->get_status()]);
}
/**
* POST /wechat/settings — 更新微信模板消息配置。
*
* 前端在密钥留空且已配置时发送掩码 ***,此时保留已存密钥;
* 未提供的字段沿用当前值。键白名单由 Integration::update_wechat_settings() 收敛。
*/
/** POST /wechat/settings */
public function update_wechat_settings(\WP_REST_Request $request): \WP_REST_Response {
$params = $this->get_json_params($request);
$current = \Jio\Settings\Settings::get_wechat_settings();
$enabled = array_key_exists('enabled', $params) ? !empty($params['enabled']) : !empty($current['enabled']);
$app_id = array_key_exists('app_id', $params)
? sanitize_text_field((string) $params['app_id'])
: (string) ($current['app_id'] ?? '');
$template_id = array_key_exists('template_id', $params)
? sanitize_text_field((string) $params['template_id'])
: (string) ($current['template_id'] ?? '');
// 掩码 *** 表示「不修改密钥」;仅在显式传入其他值时才覆盖。
if (array_key_exists('app_secret', $params) && (string) $params['app_secret'] !== '***') {
$app_secret = sanitize_text_field((string) $params['app_secret']);
} else {
$app_secret = (string) ($current['app_secret'] ?? '');
$params = $this->get_json_params($request);
if (!isset($params['templates']) || !is_array($params['templates'])) {
return $this->error(__('模板配置格式无效', 'jio'), 400);
}
foreach ($params['templates'] as $type => $item) {
if (!isset(\Jio\Settings\WechatTemplates::definitions()[$type]) || !is_array($item)) {
return $this->error(__('通知类型无效', 'jio'), 400);
}
if (isset($item['id']) && !is_string($item['id'])) {
return $this->error(__('模板 ID 格式无效', 'jio'), 400);
}
if (isset($item['keys']) && !is_array($item['keys'])) {
return $this->error(__('模板参数格式无效', 'jio'), 400);
}
foreach (($item['keys'] ?? []) as $field => $key) {
if (!isset(\Jio\Settings\WechatTemplates::definitions()[$type]['fields'][$field])
|| !is_string($key)
|| ($key !== '' && !preg_match('/^[a-zA-Z][a-zA-Z0-9_]{0,63}$/', $key))) {
return $this->error(__('模板参数名无效', 'jio'), 400);
}
}
}
$config = \Jio\Settings\WechatTemplates::normalize($params);
foreach ($config['templates'] as $item) {
$used_keys = [];
$index = 1;
foreach ($item['keys'] as $key) {
$effective_key = $key !== '' ? $key : 'keyword' . $index;
if (isset($used_keys[$effective_key])) {
return $this->error(__('同一模板的参数名不能重复', 'jio'), 400);
}
$used_keys[$effective_key] = true;
$index++;
}
}
\Jio\Settings\Settings::update_wechat_settings($config);
return $this->success(['data' => $config, 'message' => __('微信模板消息设置已保存', 'jio')]);
}
\Jio\Settings\Settings::update_wechat_settings([
'enabled' => $enabled,
'app_id' => $app_id,
'app_secret' => $app_secret,
'template_id' => $template_id,
]);
return $this->success(['message' => __('微信通知配置已保存', 'jio')]);
/** POST /wechat/test */
public function test_wechat_template(\WP_REST_Request $request): \WP_REST_Response {
$params = $this->get_json_params($request);
$type = sanitize_key((string) ($params['type'] ?? ''));
if (!isset(\Jio\Settings\WechatTemplates::definitions()[$type])) {
return $this->error(__('请选择需要测试的模板', 'jio'), 400);
}
$result = \Jio\Payment\WeChatNotify::get_instance()->test_send(get_current_user_id(), $type);
if (is_wp_error($result)) {
return $this->error($result->get_error_message(), (int) ($result->get_error_data()['status'] ?? 400));
}
return $this->success(['message' => __('测试消息已发送', 'jio')]);
}
public function rest_handle_credit_settings(\WP_REST_Request $request): \WP_REST_Response {
@@ -229,6 +229,14 @@ trait UserControllerReportOperations {
return $this->error(__('处理失败', 'jio'));
}
if ($updated > 0 && $status !== (string) ($row['status'] ?? '')) {
do_action('jio_report_status_changed', $report_id, (int) ($row['reporter_id'] ?? 0), $status, $note, [
'category' => (string) ($row['category'] ?? ''),
'reason' => (string) ($row['reason'] ?? ''),
'created_at' => (string) ($row['created_at'] ?? ''),
]);
}
return $this->success([
'message' => __('举报工单已处理', 'jio'),
]);