Реализация функций на VB.net
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Collections.Generic
Imports Newtonsoft.Json
Public Class PSCWebApi
Private ReadOnly _baseUrl As String = "https://pscw.autoxp.ru/api.aspx"
Private _sessionCode As String
Private ReadOnly _keyNumber As String
Private ReadOnly _codeSync As String
Public Sub New(keyNumber As String, codeSync As String)
_keyNumber = keyNumber
_codeSync = codeSync
End Sub
Public Function GetSession() As String
' Получение кода сессии
Dim parameters = New Dictionary(Of String, String) From {
{"keynumber", _keyNumber},
{"codesync", _codeSync}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка авторизации: {result}")
End If
Dim jsonResponse = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(result)
_sessionCode = jsonResponse("ses")
Return result
End Function
Public Sub SetSession(sessionCode As String)
' Установка сессии вручную (если уже есть)
_sessionCode = sessionCode
End Sub
Public Function GetCurrentSession() As String
' Получение текущей сессии
Return _sessionCode
End Function
Public Function GetListOfLastDocuments(Optional fields As String = Nothing, Optional countOfDocs As Integer = 200) As List(Of List(Of String))
' Получение списка документов как объект
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"task", "listoflastdocs"}
}
If Not String.IsNullOrEmpty(fields) Then
parameters.Add("fields", fields)
End If
parameters.Add("countofdocs", countOfDocs.ToString())
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка получения списка документов: {response}")
End If
Return ParseTableToList(response)
End Function
Public Function CreateDocument(blankCode As String) As String
' Создание нового документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"bnk", blankCode}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка создания документа: {result}")
End If
Dim jsonResponse = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(result)
Return jsonResponse("doc")
End Function
Public Function GetFieldValue(documentIndex As String, fieldIndex As String) As String
' Получение содержимого поля документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"frmfield", fieldIndex}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка получения поля: {result}")
End If
Return If(result = "null", Nothing, result)
End Function
Public Function GetMultipleFields(documentIndex As String, fieldIndexes As List(Of String)) As Dictionary(Of String, String)
' Получение содержимого нескольких полей документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"frmfields", String.Join(",", fieldIndexes)}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка получения полей: {response}")
End If
Return ParseFieldListToDictionary(response)
End Function
Public Function SetFieldValue(documentIndex As String, fieldIndex As String, value As String) As Boolean
' Изменение содержимого поля документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"frmfield", fieldIndex},
{"val", If(String.IsNullOrEmpty(value), "null", value)}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка изменения поля: {response}")
End If
Return response = "OK"
End Function
Public Function SetMultipleFields(documentIndex As String, fieldsValues As Dictionary(Of String, String)) As Boolean
' Изменение нескольких полей документа
CheckSession()
Dim fieldIndexes = New List(Of String)()
Dim values = New List(Of String)()
For Each kvp In fieldsValues
fieldIndexes.Add(kvp.Key)
values.Add(If(String.IsNullOrEmpty(kvp.Value), "null", kvp.Value))
Next
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"frmfields", String.Join(",", fieldIndexes)},
{"vals", String.Join("[s]", values)}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка изменения полей: {response}")
End If
Return response = "OK"
End Function
Public Function GetTableRows(documentIndex As String, tableIndex As String) As List(Of List(Of String))
' Получение содержимого таблицы документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "tblrows"},
{"table", tableIndex}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка получения таблицы: {response}")
End If
Return ParseTableToList(response)
End Function
Public Function AddTableRowsFrom(documentIndex As String, tableIndex As String, rowsJson As String) As Boolean
' Добавление записей в таблицу документа из JSON
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "tbladdrows"},
{"table", tableIndex},
{"rows", rowsJson}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка добавления строк: {response}")
End If
Return response = "OK"
End Function
Public Function DeleteTableRows(documentIndex As String, tableIndex As String, rowIndexes As List(Of String)) As Boolean
' Удаление записей из таблицы документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "tbldelrows"},
{"table", tableIndex},
{"rows", String.Join(",", rowIndexes)}
}
Dim response = SendPostRequest(parameters)
If response.StartsWith("!") Then
Throw New Exception($"Ошибка удаления строк: {response}")
End If
Return response = "OK"
End Function
Public Function GetDocumentStatus(documentIndex As String) As String
' Получение статуса документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "getdocstatus"}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка получения статуса: {result}")
End If
Return If(result = "null", Nothing, result)
End Function
Public Function GetDocumentStatusWithTime(documentIndex As String) As Dictionary(Of String, String)
' Получение статуса документа со временем
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "getdocstatuswtime"}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка получения статуса: {result}")
End If
Dim statusInfo As New Dictionary(Of String, String)()
' Парсинг ответа (формат: status|time или status)
Dim parts = result.Split("|"c)
statusInfo("status") = If(parts(0) = "null", Nothing, parts(0))
statusInfo("time") = If(parts.Length > 1 AndAlso parts(1) <> "null", parts(1), Nothing)
Return statusInfo
End Function
Public Function SetDocumentStatus(documentIndex As String, status As String) As Boolean
' Изменение статуса документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "setdocstatus"},
{"val", If(String.IsNullOrEmpty(status), "null", status)}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка изменения статуса: {result}")
End If
Return result = "OK"
End Function
Public Function GetDocumentSymbol(documentIndex As String) As String
' Получение символа признака документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "getdocsymbol"}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка получения символа: {result}")
End If
Return If(result = "null", Nothing, result)
End Function
Public Function SetDocumentSymbol(documentIndex As String, symbol As String) As Boolean
' Изменение символа признака документа
CheckSession()
Dim validSymbols = {"alert", "express", "phone", "print", "flag", "light"}
If Not validSymbols.Contains(symbol) Then
Throw New ArgumentException("Неверный символ. Допустимые значения: alert, express, phone, print, flag, light")
End If
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "setdocsymbol"},
{"val", symbol}
}
Dim queryString = BuildQueryString(parameters)
Dim result = SendGetRequest($"{_baseUrl}{queryString}")
If result.StartsWith("!") Then
Throw New Exception($"Ошибка изменения символа: {result}")
End If
Return result = "OK"
End Function
Public Function GetImagesList(documentIndex As String, gallery As String) As List(Of List(Of String))
' Получение списка изображений из документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"task", "listofimages"},
{"gallery", gallery}
}
Dim queryString = BuildQueryString(parameters)
Dim response = SendGetRequest($"{_baseUrl}{queryString}")
If response.StartsWith("!") Then
Throw New Exception($"Ошибка получения списка изображений: {response}")
End If
Return ParseTableToList(response)
End Function
Public Function GetImage(documentIndex As String, imageIndex As String) As Byte()
' Получение изображения из документа
CheckSession()
Dim parameters = New Dictionary(Of String, String) From {
{"ses", _sessionCode},
{"doc", documentIndex},
{"image", imageIndex}
}
Dim queryString = BuildQueryString(parameters)
Return SendGetRequestBytes($"{_baseUrl}{queryString}")
End Function
Public Sub SaveImageToFile(documentIndex As Strin