import $ from 'jquery' let apiSpec = null let currentUser = null async function loadUserProfile() { try { const requestData = { jsonrpc: '2.0', method: 'Profile.get', params: {}, id: Date.now() } const response = await fetch('http://localhost:8080/api', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(requestData) }) const result = await response.json() if (result.result) { currentUser = result.result $('#username').text(currentUser.name || 'неизвестный пользователь') } else { currentUser = null $('#username').text('не авторизован') } } catch (error) { console.error('Ошибка загрузки профиля:', error) currentUser = null $('#username').text('не авторизован') } } async function loadApiSpec() { try { const response = await fetch('/api.spec.json', { method: 'GET', headers: { 'Cache-Control': 'no-cache', 'Accept': 'application/json' } }) if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`) } apiSpec = await response.json() console.log('API Spec loaded:', apiSpec) let moduleSpecs = {}; apiSpec.modules.forEach(m => moduleSpecs[m.name] = m); return moduleSpecs } catch (error) { console.error('Ошибка загрузки API спецификации:', error) $('#result').text('Ошибка загрузки API спецификации: ' + error.message) } } function populateServices() { const $serviceSelect = $('#service') $serviceSelect.empty() $serviceSelect.append('') Object.keys(apiSpec).forEach(serviceName => { $serviceSelect.append(``) }) } function populateMethods(serviceName) { const $methodSelect = $('#method') $methodSelect.empty() $methodSelect.append('') if (serviceName && apiSpec[serviceName] && apiSpec[serviceName].methods) { apiSpec[serviceName].methods.forEach(method => { $methodSelect.append(``) }) } } function createParamInputs(methodName, serviceName) { const $paramsContainer = $('#params-container') $paramsContainer.empty() if (!serviceName || !methodName) return const service = apiSpec[serviceName] if (!service || !service.methods) return const method = service.methods.find(m => m.name === methodName) if (!method || !method.params) return method.params.forEach(param => { const required = !param.optional ? ' (обязательно)' : ' (необязательно)' const isOptional = param.optional const isObjectOrArray = param.type === 'object' || param.type === 'array' const defaultValue = param.type === 'object' ? '{}' : (param.type === 'array' ? '[]' : '') const inputElement = isObjectOrArray ? `` : `` if (isOptional) { const $paramDiv = $(`
${inputElement}
`) $paramsContainer.append($paramDiv) $(`#defined-${param.name}`).on('change', function () { const $input = $(`#param-${param.name}`) const isChecked = $(this).is(':checked') $input.prop('disabled', !isChecked) if (!isChecked) { $input.val(isObjectOrArray ? defaultValue : '') } }) // Initially disable optional fields $(`#param-${param.name}`).prop('disabled', true) } else { const $paramDiv = $(`
${inputElement}
`) $paramsContainer.append($paramDiv) } }) } async function sendRequest() { const serviceName = $('#service').val() const methodName = $('#method').val() if (!serviceName || !methodName) { $('#result').text('Выберите сервис и метод') return } const params = {} let parseError = null $('.param').each(function () { const $checkbox = $(this).find('.defined-checkbox') const $input = $(this).find('input[type="text"], textarea') const paramName = $input.data('param') const paramType = $input.attr('id').replace('param-', '') // Find parameter type from method definition const service = apiSpec[serviceName] const method = service.methods.find(m => m.name === methodName) const paramDef = method.params.find(p => p.name === paramName) const isObjectOrArray = paramDef && (paramDef.type === 'object' || paramDef.type === 'array') if ($checkbox.length > 0) { // Optional parameter if ($checkbox.is(':checked') && $input.val().trim() !== '') { const value = $input.val().trim() if (isObjectOrArray) { try { params[paramName] = JSON.parse(value) } catch (e) { parseError = `Ошибка парсинга JSON для поля "${paramName}": ${e.message}` } } else { params[paramName] = value } } } else { // Required parameter const value = $input.val().trim() if (value !== '') { if (isObjectOrArray) { try { params[paramName] = JSON.parse(value) } catch (e) { parseError = `Ошибка парсинга JSON для поля "${paramName}": ${e.message}` } } else { params[paramName] = value } } } }) if (parseError) { $('#result').text(parseError) $('#request').text(JSON.stringify(requestData, null, 2)) return } const requestData = { jsonrpc: '2.0', method: `${serviceName}.${methodName}`, params: params, id: Date.now() } $('#result').text('Отправка запроса...') $('#request').text('') try { const response = await fetch('http://localhost:8080/api', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(requestData) }) const result = await response.json() $('#result').text(JSON.stringify(result, null, 2)) $('#request').text(JSON.stringify(requestData, null, 2)) // Проверяем, нужно ли обновить профиль пользователя if (result.result && ( methodName.startsWith('authenticateBy') || methodName === 'logout' )) { await loadUserProfile() } } catch (error) { $('#result').text(`Ошибка: ${error.message}`) $('#request').text(JSON.stringify(requestData, null, 2)) } } $(document).ready(async () => { await loadApiSpec() if (apiSpec) { populateServices() // Автоматически загружаем профиль пользователя await loadUserProfile() $('#service').on('change', function () { const serviceName = $(this).val() populateMethods(serviceName) $('#params-container').empty() }) $('#method').on('change', function () { const serviceName = $('#service').val() const methodName = $(this).val() createParamInputs(methodName, serviceName) }) $('#send-btn').on('click', sendRequest) } })