Files
wordpress-theme-aksrt/src/Auth/ServicePart1b.php
T

199 lines
6.8 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Auth;
defined('ABSPATH') || exit;
trait ServicePart1b {
/**
* 手机号注册
*
* @param array $post POST 数据 (phone, code, password, ref)
* @return array{success: bool, user_id?: int, error?: string, redirect?: string}
*/
public static function register_by_phone(array $post): array {
$phone = sanitize_text_field((string) ($post['phone'] ?? ''));
$pass = (string) ($post['password'] ?? '');
$confirm = (string) ($post['password_confirm'] ?? '');
$username = sanitize_user(wp_unslash((string) ($post['user_login'] ?? '')), true);
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
return ['success' => false, 'error' => __('手机号格式不正确', 'aksrt')];
}
if (empty($post['_auth_code_verified'])) {
return ['success' => false, 'error' => __('请先完成手机验证', 'aksrt')];
}
if ($username === '' || !validate_username($username) || mb_strlen($username) < 3 || mb_strlen($username) > 60) {
return ['success' => false, 'error' => __('请输入 3-60 个字符的有效用户名', 'aksrt')];
}
if (username_exists($username)) {
return ['success' => false, 'error' => __('该用户名已被使用', 'aksrt')];
}
if (class_exists('\AKSRT\Settings\Settings')) {
foreach (\AKSRT\Settings\Settings::nickname_blacklist() as $keyword) {
if ($keyword !== '' && mb_stripos($username, $keyword) !== false) {
return ['success' => false, 'error' => __('用户名包含禁止使用的关键词', 'aksrt')];
}
}
if (\AKSRT\Settings\Settings::signup_omit_password_confirmation()) {
$confirm = $pass;
}
$agreement = \AKSRT\Settings\Settings::user_agreement_settings();
if ((!empty($agreement['enabled']) || !empty($agreement['privacy'])) && !empty($agreement['required']) && empty($post['agree_terms'])) {
return ['success' => false, 'error' => __('请先同意服务条款', 'aksrt')];
}
}
$password_check = self::validate_password($pass, $confirm);
if ($password_check !== true) {
return ['success' => false, 'error' => $password_check];
}
// 检查手机号是否已注册
$existing_user = self::get_user_by_phone($phone);
if ($existing_user) {
return ['success' => false, 'error' => __('该手机号已注册', 'aksrt')];
}
$ref_service = class_exists('\AKSRT\Payment\ReferralAttribution')
? \AKSRT\Payment\ReferralAttribution::get_instance()
: null;
if ($ref_service) {
$ref_service->clear_registration_context();
}
$registration = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_referral_registration_settings()
: ['enabled' => false, 'register_field' => 'hidden', 'reject_invalid' => true];
$submitted_ref = sanitize_key((string) ($post['ref'] ?? ''));
$field_mode = !empty($registration['enabled'])
? (string) ($registration['register_field'] ?? 'hidden')
: 'hidden';
$accept_submitted = !empty($registration['enabled']) && $field_mode !== 'hidden';
$ref_context = $ref_service
? $ref_service->prepare_registration($accept_submitted ? $submitted_ref : '')
: ['valid' => false, 'submitted_valid' => false, 'conflict' => false, 'source' => 'none'];
if ($field_mode === 'required' && empty($ref_context['valid'])) {
if ($submitted_ref === '') {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('请填写推广码', 'aksrt')];
}
if (!empty($registration['reject_invalid'])) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('推广码无效,请检查后重试', 'aksrt')];
}
}
if ($submitted_ref !== '' && empty($ref_context['valid']) && !empty($registration['reject_invalid'])) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => __('推广码无效,请检查后重试', 'aksrt')];
}
// 创建用户
$user_id = wp_create_user($username, $pass, '');
if (is_wp_error($user_id)) {
if ($ref_service) {
$ref_service->clear_registration_context();
}
return ['success' => false, 'error' => $user_id->get_error_message()];
}
// 绑定手机号
update_user_meta($user_id, 'aksrt_phone', $phone);
update_user_meta($user_id, 'aksrt_phone_verified', '1');
self::mark_email_verified((int) $user_id);
// 自动登录
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id, true);
$user = get_userdata($user_id);
if ($user instanceof \WP_User) {
do_action('wp_login', $user->user_login, $user);
}
$redirect = function_exists('aksrt_get_user_center_url')
? aksrt_get_user_center_url()
: home_url('/');
return [
'success' => true,
'user_id' => $user_id,
'redirect' => $redirect,
'message' => __('注册成功', 'aksrt'),
'notice' => $ref_service ? $ref_service->take_registration_notice() : '',
];
}
/**
* 绑定手机号到当前用户
*
* @param array $post (phone, code)
* @return array
*/
public static function bind_phone(array $post): array {
if (!is_user_logged_in()) {
return ['success' => false, 'error' => __('请先登录', 'aksrt')];
}
$phone = sanitize_text_field((string) ($post['phone'] ?? ''));
$code = sanitize_text_field((string) ($post['code'] ?? ''));
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
return ['success' => false, 'error' => __('手机号格式不正确', 'aksrt')];
}
if (function_exists('aksrt_require_captcha')) {
$captcha_result = aksrt_require_captcha('bind_phone', $post);
if ($captcha_result !== true) {
return ['success' => false, 'error' => $captcha_result];
}
}
// 验证 SMS 验证码
if (class_exists('\AKSRT\Sms\SMS')) {
$verify_result = \AKSRT\Sms\SMS::get_instance()->verify_code($phone, $code);
if (empty($verify_result['success'])) {
return ['success' => false, 'error' => $verify_result['message'] ?? __('验证码不正确', 'aksrt')];
}
} else {
return ['success' => false, 'error' => __('短信服务不可用', 'aksrt')];
}
// 检查手机号是否已被其他用户绑定
$existing = self::get_user_by_phone($phone);
if ($existing && (int) $existing->ID !== get_current_user_id()) {
return ['success' => false, 'error' => __('该手机号已被其他账号绑定', 'aksrt')];
}
$user_id = get_current_user_id();
update_user_meta($user_id, 'aksrt_phone', $phone);
update_user_meta($user_id, 'aksrt_phone_verified', '1');
return [
'success' => true,
'message' => __('手机号绑定成功', 'aksrt'),
];
}
/**
* 根据手机号查找用户
*/
private static function get_user_by_phone(string $phone): ?\WP_User {
$users = get_users([
'meta_key' => 'aksrt_phone',
'meta_value' => $phone,
'number' => 1,
'fields' => 'all',
]);
return !empty($users) ? $users[0] : null;
}
}