288 lines
11 KiB
PHP
288 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT admin auth and phone operations support.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait AKSRT_Admin_User_Controller_Auth_Phone_Operations {
|
|
|
|
/**
|
|
* POST /auth/modal-login — 弹窗登录
|
|
*/
|
|
public function modal_login(WP_REST_Request $request): WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$payload = is_array($params) ? $params : [];
|
|
$login = sanitize_text_field((string) ($payload['log'] ?? $payload['login'] ?? ''));
|
|
$password = (string) ($payload['pwd'] ?? $payload['password'] ?? '');
|
|
|
|
if ($login === '' || $password === '') {
|
|
return $this->error('请输入用户名和密码');
|
|
}
|
|
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用', 500);
|
|
}
|
|
|
|
$result = AKSRT_Auth_Service::login_user([
|
|
...$payload,
|
|
'log' => $login,
|
|
'pwd' => $password,
|
|
'rememberme' => !empty($payload['rememberme']) ? (string) $payload['rememberme'] : 'forever',
|
|
], true, true);
|
|
|
|
if (empty($result['success'])) {
|
|
$error = $result['error'] ?? '登录失败';
|
|
$verify_url = $result['verify_url'] ?? '';
|
|
$response = ['success' => false, 'message' => $error];
|
|
if ($verify_url) {
|
|
$response['verify_url'] = $verify_url;
|
|
}
|
|
return new WP_REST_Response($response, 401);
|
|
}
|
|
|
|
$user = wp_get_current_user();
|
|
return $this->success([
|
|
'message' => __('登录成功', 'aksrt'),
|
|
'redirect' => $result['redirect'] ?? home_url('/'),
|
|
'user' => [
|
|
'id' => $user->ID,
|
|
'display_name' => $user->display_name,
|
|
'avatar_url' => function_exists('aksrt_get_avatar_url')
|
|
? aksrt_get_avatar_url($user->ID, ['size' => 48])
|
|
: get_avatar_url($user->ID, ['size' => 48]),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* POST /auth/modal-register — 弹窗注册
|
|
*/
|
|
public function modal_register(WP_REST_Request $request): WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$payload = is_array($params) ? $params : [];
|
|
$username = sanitize_user((string) ($payload['user_login'] ?? $payload['username'] ?? ''), true);
|
|
$email = sanitize_email((string) ($payload['user_email'] ?? $payload['email'] ?? ''));
|
|
$password = (string) ($payload['user_pass'] ?? $payload['password'] ?? '');
|
|
$confirm = (string) ($payload['user_pass_confirm'] ?? $payload['password_confirm'] ?? $payload['confirm_password'] ?? $password);
|
|
$invite_code = sanitize_text_field((string) ($payload['invite_code'] ?? ''));
|
|
$register_nonce = sanitize_text_field((string) ($payload['_register_nonce'] ?? ''));
|
|
$agree_terms = !empty($payload['agree_terms']) ? '1' : '';
|
|
|
|
if ($username === '' || $email === '' || $password === '') {
|
|
return $this->error('请填写所有必填项');
|
|
}
|
|
|
|
if (!is_email($email)) {
|
|
return $this->error('邮箱格式不正确');
|
|
}
|
|
|
|
if (strlen($password) < 6) {
|
|
return $this->error('密码至少 6 位');
|
|
}
|
|
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用', 500);
|
|
}
|
|
|
|
$payload['user_login'] = $username;
|
|
$payload['user_email'] = $email;
|
|
$payload['user_pass'] = $password;
|
|
$payload['user_pass_confirm'] = $confirm;
|
|
$payload['invite_code'] = $invite_code;
|
|
$payload['_register_nonce'] = $register_nonce;
|
|
$payload['agree_terms'] = $agree_terms;
|
|
|
|
$result = AKSRT_Auth_Service::register_user($payload);
|
|
|
|
if (empty($result['success'])) {
|
|
return $this->error($result['error'] ?? '注册失败');
|
|
}
|
|
|
|
$verification_required = !empty($result['verification_required']);
|
|
$login_url = function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url();
|
|
$registered_user = null;
|
|
$current_user = wp_get_current_user();
|
|
if (
|
|
$current_user instanceof WP_User
|
|
&& $current_user->ID > 0
|
|
&& !empty($result['user_id'])
|
|
&& (int) $result['user_id'] === (int) $current_user->ID
|
|
) {
|
|
$registered_user = $current_user;
|
|
}
|
|
$user = $current_user;
|
|
$needs_verify = $verification_required;
|
|
|
|
return $this->success([
|
|
'message' => $needs_verify ? __('注册成功,请验证邮箱', 'aksrt') : __('注册成功', 'aksrt'),
|
|
'needs_verification' => $needs_verify,
|
|
'verify_url' => $result['verify_url'] ?? '',
|
|
'redirect' => $verification_required ? ($result['verify_url'] ?? '') : $login_url,
|
|
'verification_required' => $verification_required,
|
|
'login_url' => $login_url,
|
|
'user_login' => $result['user_login'] ?? $username,
|
|
'user_email' => $email,
|
|
'user' => $registered_user ? [
|
|
'id' => $registered_user->ID,
|
|
'display_name' => $registered_user->display_name,
|
|
'avatar_url' => function_exists('aksrt_get_avatar_url')
|
|
? aksrt_get_avatar_url($registered_user->ID, ['size' => 48])
|
|
: get_avatar_url($registered_user->ID, ['size' => 48]),
|
|
] : ($user->ID ? [
|
|
'id' => $user->ID,
|
|
'display_name' => $user->display_name,
|
|
'avatar_url' => function_exists('aksrt_get_avatar_url')
|
|
? aksrt_get_avatar_url($user->ID, ['size' => 48])
|
|
: get_avatar_url($user->ID, ['size' => 48]),
|
|
] : null),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /auth/me — 当前用户信息(用于 AJAX 刷新)
|
|
*/
|
|
public function get_current_user_info(): WP_REST_Response {
|
|
$user = wp_get_current_user();
|
|
if (!$user->ID) {
|
|
return $this->error('未登录', 401);
|
|
}
|
|
|
|
$unread = 0;
|
|
if (class_exists('AKSRT_Private_Message')) {
|
|
$unread = AKSRT_Private_Message::get_instance()->get_unread_count($user->ID);
|
|
}
|
|
|
|
$credits = 0;
|
|
if (class_exists('AKSRT_Credits')) {
|
|
$credits = AKSRT_Credits::balance($user->ID);
|
|
}
|
|
|
|
return $this->success([
|
|
'data' => [
|
|
'id' => $user->ID,
|
|
'display_name' => $user->display_name,
|
|
'user_email' => $user->user_email,
|
|
'avatar_url' => function_exists('aksrt_get_avatar_url')
|
|
? aksrt_get_avatar_url($user->ID, ['size' => 48])
|
|
: get_avatar_url($user->ID, ['size' => 48]),
|
|
'is_admin' => current_user_can('manage_options'),
|
|
'unread' => $unread,
|
|
'credits' => $credits,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* POST /auth/send-sms-code — 发送短信验证码
|
|
*/
|
|
public function auth_send_sms_code(WP_REST_Request $request): WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$phone = sanitize_text_field((string) ($params['phone'] ?? ''));
|
|
|
|
if (!class_exists('AKSRT_SMS')) {
|
|
return $this->error('短信服务不可用');
|
|
}
|
|
|
|
$result = AKSRT_SMS::get_instance()->send_code($phone);
|
|
|
|
if (!empty($result['success'])) {
|
|
return $this->success([
|
|
'message' => $result['message'],
|
|
'remaining_time' => $result['remaining_time'] ?? 60,
|
|
]);
|
|
}
|
|
|
|
$response = [
|
|
'success' => false,
|
|
'message' => $result['message'],
|
|
];
|
|
if (isset($result['remaining_time'])) {
|
|
$response['data'] = ['remaining_time' => $result['remaining_time']];
|
|
}
|
|
return new WP_REST_Response($response, 400);
|
|
}
|
|
|
|
/**
|
|
* POST /auth/phone-register — 手机号注册
|
|
*/
|
|
public function auth_phone_register(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用');
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$result = AKSRT_Auth_Service::register_by_phone($params);
|
|
|
|
if (!empty($result['success'])) {
|
|
return $this->success([
|
|
'message' => $result['message'] ?? '注册成功',
|
|
'redirect' => $result['redirect'] ?? home_url('/'),
|
|
'user_id' => $result['user_id'] ?? 0,
|
|
]);
|
|
}
|
|
|
|
return $this->error($result['error'] ?? '注册失败');
|
|
}
|
|
|
|
/**
|
|
* POST /auth/phone-login — 手机号 + 密码登录
|
|
*/
|
|
public function auth_phone_login(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用');
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$result = AKSRT_Auth_Service::login_by_phone_password($params);
|
|
|
|
if (!empty($result['success'])) {
|
|
return $this->success([
|
|
'redirect' => $result['redirect'] ?? home_url('/'),
|
|
]);
|
|
}
|
|
|
|
return $this->error($result['error'] ?? '登录失败');
|
|
}
|
|
|
|
/**
|
|
* POST /auth/phone-code-login — 手机号 + 验证码免密登录
|
|
*/
|
|
public function auth_phone_code_login(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用');
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$result = AKSRT_Auth_Service::login_by_phone_code($params);
|
|
|
|
if (!empty($result['success'])) {
|
|
return $this->success([
|
|
'redirect' => $result['redirect'] ?? home_url('/'),
|
|
]);
|
|
}
|
|
|
|
return $this->error($result['error'] ?? '登录失败');
|
|
}
|
|
|
|
/**
|
|
* POST /auth/bind-phone — 绑定手机号
|
|
*/
|
|
public function auth_bind_phone(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Auth_Service')) {
|
|
return $this->error('认证服务不可用');
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$result = AKSRT_Auth_Service::bind_phone($params);
|
|
|
|
if (!empty($result['success'])) {
|
|
return $this->success(['message' => $result['message']]);
|
|
}
|
|
|
|
return $this->error($result['error'] ?? '绑定失败');
|
|
}
|
|
}
|