209 lines
7.4 KiB
PHP
209 lines
7.4 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, bool $verification_completed = false, bool $defer_login = false): 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 (!\AKSRT\Sms\SMS::is_phonenumber($phone)) {
|
|
return ['success' => false, 'error' => __('手机号格式不正确', 'aksrt')];
|
|
}
|
|
if (!$verification_completed) {
|
|
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')];
|
|
}
|
|
|
|
$invite_service = class_exists('\AKSRT\Payment\InviteCode')
|
|
? \AKSRT\Payment\InviteCode::get_instance()
|
|
: null;
|
|
if ($invite_service) {
|
|
$invite_service->clear_registration_context();
|
|
$invite_mode = $invite_service->field_mode();
|
|
$submitted_invite = sanitize_key((string) ($post['invite_code'] ?? ''));
|
|
if ($invite_mode === 'required' && $submitted_invite === '') {
|
|
return ['success' => false, 'error' => __('请填写邀请码', 'aksrt')];
|
|
}
|
|
if ($submitted_invite !== '') {
|
|
if (!$invite_service->is_valid($submitted_invite)) {
|
|
return ['success' => false, 'error' => __('邀请码无效或已用完,请检查后重试', 'aksrt')];
|
|
}
|
|
$invite_service->set_registration_context($submitted_invite);
|
|
}
|
|
}
|
|
|
|
// 创建用户
|
|
$user_id = wp_create_user($username, $pass, '');
|
|
if (is_wp_error($user_id)) {
|
|
if ($ref_service) {
|
|
$ref_service->clear_registration_context();
|
|
}
|
|
if ($invite_service) {
|
|
$invite_service->clear_registration_context();
|
|
}
|
|
return ['success' => false, 'error' => $user_id->get_error_message()];
|
|
}
|
|
|
|
// 绑定手机号
|
|
update_user_meta($user_id, 'phone_number', $phone);
|
|
update_user_meta($user_id, 'aksrt_phone_verified', '1');
|
|
|
|
// 自动登录
|
|
$user = get_userdata($user_id);
|
|
if (!$defer_login) {
|
|
wp_set_current_user($user_id);
|
|
wp_set_auth_cookie($user_id, true);
|
|
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, bool $verification_completed = false): array {
|
|
if (!is_user_logged_in()) {
|
|
return ['success' => false, 'error' => __('请先登录', 'aksrt')];
|
|
}
|
|
|
|
$phone = sanitize_text_field((string) ($post['phone'] ?? ''));
|
|
if (!\AKSRT\Sms\SMS::is_phonenumber($phone)) {
|
|
return ['success' => false, 'error' => __('手机号格式不正确', 'aksrt')];
|
|
}
|
|
|
|
if (!$verification_completed) {
|
|
return ['success' => false, 'error' => __('验证码错误或已过期', 'aksrt')];
|
|
}
|
|
|
|
$user_id = get_current_user_id();
|
|
$existing = self::get_user_by_phone($phone);
|
|
if ($existing && (int) $existing->ID !== $user_id) {
|
|
return ['success' => false, 'error' => __('该手机号已被其他账号绑定', 'aksrt')];
|
|
}
|
|
if ($existing && (int) $existing->ID === $user_id) {
|
|
return ['success' => false, 'error' => __('手机号不能与当前绑定号码相同', 'aksrt')];
|
|
}
|
|
|
|
$old_phone = (string) get_user_meta($user_id, 'phone_number', true);
|
|
update_user_meta($user_id, 'phone_number', $phone);
|
|
update_user_meta($user_id, 'aksrt_phone_verified', '1');
|
|
do_action('aksrt_user_phone_changed', $user_id, $phone, $old_phone);
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => $old_phone !== '' ? __('手机号修改成功', 'aksrt') : __('手机号绑定成功', 'aksrt'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 根据手机号查找用户
|
|
*/
|
|
private static function get_user_by_phone(string $phone): ?\WP_User {
|
|
$users = get_users([
|
|
'meta_key' => 'phone_number',
|
|
'meta_value' => $phone,
|
|
'number' => 1,
|
|
'fields' => 'all',
|
|
]);
|
|
return !empty($users) ? $users[0] : null;
|
|
}
|
|
}
|