Merge pull request 'fix: 身份认证身份证号码校验' (#349) from worktree-identity-form-simplify into main

Reviewed-on: CYL/wordpress-theme-aksrt#349
This commit is contained in:
2026-08-01 22:19:54 +08:00
2 changed files with 19 additions and 3 deletions
@@ -84,7 +84,7 @@ $config = [
<label class="uc-auth-field"><span><?php esc_html_e('统一社会信用代码', 'aksrt'); ?></span><input class="form-control" name="company_code" maxlength="32" autocomplete="off"></label>
</div>
<label class="uc-auth-field"><span data-auth-person-label><?php esc_html_e('真实姓名', 'aksrt'); ?></span><input class="form-control" name="person_name" maxlength="50" required autocomplete="name"></label>
<label class="uc-auth-field"><span data-auth-id-label><?php esc_html_e('身份证号码', 'aksrt'); ?></span><input class="form-control" name="id_number" maxlength="20" required autocomplete="off"></label>
<label class="uc-auth-field"><span data-auth-id-label><?php esc_html_e('身份证号码', 'aksrt'); ?></span><input class="form-control" name="id_number" maxlength="18" pattern="[0-9]{15}|[0-9]{17}[0-9Xx]" required autocomplete="off"></label>
<label class="uc-auth-consent"><input type="checkbox" name="consent" value="1" required><span><?php esc_html_e('我同意为身份认证目的将必要身份信息提交给站点当前配置的云服务商;站点不保存人脸素材,并仅保留脱敏结果。', 'aksrt'); ?></span></label>
<input type="hidden" name="return_url" value="<?php echo esc_url(aksrt_get_user_center_tab_url('auth')); ?>">
<button type="submit" class="btn-primary uc-auth-submit"><?php esc_html_e('创建认证会话', 'aksrt'); ?></button>
+18 -2
View File
@@ -54,10 +54,10 @@ final class ApplicationService {
}
$person_name = sanitize_text_field((string) ($input['person_name'] ?? ''));
$id_number = preg_replace('/\s+/', '', sanitize_text_field((string) ($input['id_number'] ?? ''))) ?: '';
$id_number = strtoupper(preg_replace('/\s+/', '', sanitize_text_field((string) ($input['id_number'] ?? ''))) ?: '');
$company_name = $type === 'enterprise' ? sanitize_text_field((string) ($input['company_name'] ?? '')) : '';
$company_code = $type === 'enterprise' ? (preg_replace('/\s+/', '', sanitize_text_field((string) ($input['company_code'] ?? ''))) ?: '') : '';
if ($person_name === '' || $this->length($person_name) > 50 || $id_number === '' || $this->length($id_number) > 20) {
if ($person_name === '' || $this->length($person_name) > 50 || !$this->is_china_id_number($id_number)) {
return new \WP_Error('aksrt_identity_person_fields', __('请填写有效的姓名和身份证号码', 'aksrt'));
}
if ($type === 'enterprise' && ($company_name === '' || $this->length($company_name) > 191 || $company_code === '' || $this->length($company_code) > 32)) {
@@ -298,4 +298,20 @@ final class ApplicationService {
private function truncate(string $value, int $length): string {
return function_exists('mb_substr') ? mb_substr($value, 0, $length) : substr($value, 0, $length);
}
private function is_china_id_number(string $value): bool {
if (!preg_match('/^(\d{17}[0-9X]|\d{15})$/', $value)) {
return false;
}
if (strlen($value) === 15) {
return true;
}
$weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
$codes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
$sum = 0;
for ($i = 0; $i < 17; $i++) {
$sum += (int) $value[$i] * $weights[$i];
}
return $codes[$sum % 11] === $value[17];
}
}