[ 'username' => $email, 'password' => $password ] ]); if (is_wp_error($response)) { wp_send_json_error('连接服务器失败'); } $body = json_decode($response['body'], true); if (isset($body['token'])) { // 保存用户信息到选项 update_option('story321_token', $body['token']); update_option('story321_email', $email); update_option('story321_last_login', time()); update_option('story321_password', $password); update_option('story321_member_point', $body['member_point'] ?? 0); update_option('story321_point', $body['point'] ?? 0); // 获取用户积分信息 /* $user_info = wp_remote_get('https://story321.com/wp-json/wp/v2/users/me', [ 'headers' => [ 'Authorization' => 'Bearer ' . $body['token'] ] ]); if (!is_wp_error($user_info)) { $user_data = json_decode($user_info['body'], true); update_option('ai_cloud_points_member', $user_data['member_points'] ?? 0); update_option('ai_cloud_points_standard', $user_data['standard_points'] ?? 0); } */ wp_send_json_success(); } else { wp_send_json_error($body['message'] ?? '登录失败'); } } // 在WordPress的admin-ajax处理中添加 add_action('wp_ajax_handle_cloud_disconnect', 'story321_handle_cloud_disconnect'); function story321_handle_cloud_disconnect() { // 修改nonce检查,使用更具体的nonce action名称 check_ajax_referer('story321_disconnect_action', 'nonce'); if (current_user_can('manage_options')) { delete_option('story321_token'); delete_option('story321_email'); delete_option('story321_password'); delete_option('story321_last_login'); delete_option('story321_member_point'); delete_option('story321_point'); wp_send_json_success(); } else { wp_send_json_error( __('Insufficient permissions', 'ai-generator')); } } add_action('rest_api_init', function () { register_rest_route('story321/v1', '/generate_text', array( 'methods' => 'POST', 'callback' => 'story321_handle_ai_generation', 'permission_callback' => function () { return current_user_can('edit_posts'); }, 'args' => array( 'prompt' => array( 'required' => true, 'validate_callback' => function ($param) { return !empty($param); } ) ) )); }); function story321_handle_ai_generation($request) { if ($request->get_param('stream', false)) { // 设置纯文本流响应头 header('Content-Type: text/plain; charset=utf-8'); header('Cache-Control: no-cache'); header('X-Accel-Buffering: no'); } // 获取用户认证信息 $token = get_option('story321_token'); $memberPoint = get_option('story321_member_point'); $point = get_option('story321_point'); // 设置请求参数 $url = 'https://studio.story321.com/api/tm/generate-text'; $args = [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ], 'body' => json_encode([ 'prompt' => $request->get_param('prompt'), 'token' => $token, 'memberPoint' => $memberPoint, 'point' => $point ]), 'timeout' => 120 ]; // 发送请求并直接输出响应 $response = wp_remote_post($url, $args); if (is_wp_error($response)) { status_header(500); echo "Error: " . esc_html($response->get_error_message()); exit; } echo wp_remote_retrieve_body($response); exit; } // 注册图片生成API端点 add_action('rest_api_init', function () { register_rest_route('story321/v1', '/generate-text2image', array( 'methods' => 'POST', 'callback' => 'story321_handle_generate_image', 'permission_callback' => function () { return current_user_can('edit_posts'); } )); }); // 处理图片生成请求 function story321_handle_generate_image($request) { // 获取请求参数 $prompt = $request->get_param('prompt'); if (empty($prompt)) { return new WP_Error('empty_prompt', '提示词不能为空', array('status' => 400)); } // 获取认证信息 $token = get_option('story321_token'); $memberPoint = get_option('story321_member_point'); $point = get_option('story321_point'); // 发送请求到外部API $response = wp_remote_post('https://studio.story321.com/api/tm/generate-text2image', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json' // 添加这行,明确要求JSON响应 ), 'body' => json_encode(array( 'prompt' => $prompt, 'token' => $token, 'memberPoint' => $memberPoint, 'point' => $point, 'stream' => false // 添加这个参数,告诉API不要返回流式数据 )), 'timeout' => 120 )); if (is_wp_error($response)) { return new WP_Error('api_error', '请求失败:' . esc_html($response->get_error_message()), array('status' => 500)); } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['imageUrl'])) { return new WP_Error('api_error', '图片生成失败', array('status' => 500)); } // 检查返回的图片URL格式 $imageUrl = $body['imageUrl']; if (strpos($imageUrl, 'data:image/') === 0) { // 如果是完整的Data URL格式,需要提取base64部分 $imageUrl = explode(',', $imageUrl)[1]; } // 处理base64图片数据 $image_data = base64_decode($imageUrl, true); if ($image_data === false) { return new WP_Error('decode_error', 'Base64解码失败:无效的base64数据', array('status' => 500)); } // 验证图片数据的有效性 if (!getimagesizefromstring($image_data)) { return new WP_Error('invalid_image', '解码后的数据不是有效的图片格式', array('status' => 500)); } $upload_dir = wp_upload_dir(); $filename = 'ai-generated-' . time() . '.png'; $file = $upload_dir['path'] . '/' . $filename; if (file_put_contents($file, $image_data) === false) { return new WP_Error('save_error', '保存图片失败', array('status' => 500)); } $wp_filetype = wp_check_filetype($filename, null); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => $prompt, 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $file); if (is_wp_error($attach_id)) { return new WP_Error('upload_error', '图片上传失败', array('status' => 500)); } require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $file); wp_update_attachment_metadata($attach_id, $attach_data); return array( 'success' => true, 'data' => array( 'id' => $attach_id, 'url' => wp_get_attachment_url($attach_id) ) ); }