From 7a71bb7e015f92e0b71574f719c1cec335e81c50 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 4 Jun 2023 21:50:36 +0300 Subject: [PATCH] v0.0.8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Реализовано: просмотр параметров сервера и возможность редактирования --- database/script.sql | 67 ++++++++++++++++----------------- js/script.js | 67 +++++++++++++++++++++++++++++++-- public/style.css | 19 ++++++++-- source/daster.d | 7 ++++ source/requests/server.d | 30 +++++++++++++++ source/sql.d | 74 +++++++++++++++++++++++++++++-------- source/structures.d | 7 ++++ source/version_.d | 2 +- views/index.dt | 2 - views/list_number_groups.dt | 4 +- views/list_sms_groups.dt | 4 +- views/list_ussd_groups.dt | 4 +- views/server.dt | 23 ++++++++++++ 13 files changed, 247 insertions(+), 63 deletions(-) create mode 100644 source/requests/server.d create mode 100644 views/server.dt diff --git a/database/script.sql b/database/script.sql index 1e4a1cd..beecc0f 100644 --- a/database/script.sql +++ b/database/script.sql @@ -1,38 +1,38 @@ create table if not exists da_groups ( - da_name varchar(20) not null, - da_comment varchar(100) default null, - constraint da_groups_pk primary key (da_name) + da_name varchar(20) not null, + da_comment varchar(100) default null, + constraint da_groups_pk primary key (da_name) ); insert into da_groups (da_name, da_comment) - values - ('general', 'Общие контакты'), - ('work', 'Рабочие контакты'), - ('personal', 'Личные контакты'); + values + ('general', 'Общие контакты'), + ('work', 'Рабочие контакты'), + ('personal', 'Личные контакты'); create table if not exists da_lists ( - da_name varchar(20) not null, - da_comment varchar(100) default null, - constraint da_lists_pk primary key (da_name) + da_name varchar(20) not null, + da_comment varchar(100) default null, + constraint da_lists_pk primary key (da_name) ); insert into da_lists (da_name, da_comment) - values - ('general', 'Общий'), - ('whitelist', 'Белый'), - ('blacklist', 'Черный'); + values + ('general', 'Общий'), + ('whitelist', 'Белый'), + ('blacklist', 'Черный'); create table if not exists da_numbers ( - da_number varchar(12) not null, - da_group varchar(20) not null default 'general', - da_list varchar(20) not null default 'general', - da_all_cc int not null default 0, - da_white_cc int not null default 0, - da_black_cc int not null default 0, - da_comment varchar(100) default null, - constraint da_numbers_pk primary key (da_number), - foreign key (da_group) references da_groups (da_name) on delete set null on update cascade, - foreign key (da_list) references da_lists (da_name) on delete set null on update cascade + da_number varchar(12) not null, + da_group varchar(20) not null default 'general', + da_list varchar(20) not null default 'general', + da_all_cc int not null default 0, + da_white_cc int not null default 0, + da_black_cc int not null default 0, + da_comment varchar(100) default null, + constraint da_numbers_pk primary key (da_number), + foreign key (da_group) references da_groups (da_name) on delete set null on update cascade, + foreign key (da_list) references da_lists (da_name) on delete set null on update cascade ); create table if not exists da_sms ( @@ -51,13 +51,13 @@ create table if not exists da_ussd_type ( ); insert into da_ussd_type (da_id, da_comment) - values - ('0', 'Уведомление'), - ('1', 'Запрос'), - ('2', 'Прервано сетью'), - ('3', 'Ответ другого локального клиента'), - ('4', 'Операция не поддерживается'), - ('5', 'Тайм-аут сети'); + values + ('0', 'Уведомление'), + ('1', 'Запрос'), + ('2', 'Прервано сетью'), + ('3', 'Ответ другого локального клиента'), + ('4', 'Операция не поддерживается'), + ('5', 'Тайм-аут сети'); create table if not exists da_ussd ( da_id bigserial not null, @@ -69,9 +69,10 @@ create table if not exists da_ussd ( ); create table if not exists da_server ( - da_address varchar(50) not null, + da_id int not null default 1, da_transparent_mode bool not null default false, da_internal_number varchar(12) not null, da_external_number varchar(12) not null, - constraint da_server_pk primary key (da_address) + da_external_number_on bool not null default true, + constraint da_server_pk primary key (da_id) ); diff --git a/js/script.js b/js/script.js index 1a444f2..583147f 100644 --- a/js/script.js +++ b/js/script.js @@ -9,7 +9,7 @@ $(document).ready(function () { 0: () => { loadNumbers() }, 1: () => { loadSMS() }, 2: () => { loadUSSD() }, - 3: () => {} + 3: () => { loadServerInfo() } }; let lists = { @@ -23,7 +23,7 @@ $(document).ready(function () { 0: () => { generateListGroupNumbers($("#accordion-numbers .ui-accordion-content-active")) }, 1: () => { generateListGroupSMS($("#accordion-sms .ui-accordion-content-active")) }, 2: () => { generateListGroupUSSD($("#accordion-ussd .ui-accordion-content-active")) }, - 3: () => { noticer.success('Вкладка "Сервер"') } + 3: () => {} }; $("button").button(); @@ -36,7 +36,7 @@ $(document).ready(function () { activate: function( event, ui ) { lists[$(this).tabs( "option", "active" )](); $("#add-number").button( "option", "disabled", $(this).tabs( "option", "active" ) > 0 ); - // $("#search").attr( "disabled", $("#search").val("") && $(this).tabs( "option", "active" ) > 1 ); + $("#update-group").button( "option", "disabled", $(this).tabs( "option", "active" ) > 2 ); } }); @@ -61,6 +61,7 @@ $(document).ready(function () { loadNumbers(); loadSMS(); loadUSSD(); + loadServerInfo(); $("body").fadeTo(500, 1); }) @@ -608,3 +609,63 @@ function delUSSD(panel, currentWindow) { noticer.error(error.message); }); } + +/************************************************************************************ + + Обработка таблицы информации о сервере + +************************************************************************************/ + +function loadServerInfo() { + request('serverinfo', 'text').then(data => { + data.error ? noticer.error(data.message) : showServerInfo(data); + }).catch(error => { + noticer.error(error.message); + }); +} + +function showServerInfo(data) { + $("#tabs-server").html(data); + $("#server-external-number-on").checkboxradio(); + $("#server-transparent-mode").checkboxradio(); + $("#server-button").button({ icon: "ui-icon-disk", disabled: true }); + + $(".server-input").on("change paste cut keydown", () => { + if ($("#server-button").button( "option", "disabled")) { + noticer.warning('Некоторые параметры сервера были изменены'); + $("#server-button").button( "option", "disabled", false); + } + }); + + $("#server-button").click(() => { + writeServerInfo(); + }); +} + +function writeServerInfo() { + let pattern_number = /^\+7\d{10}$/g; + + let internal_number = $("#server-internal-number").val(); + let external_number = $("#server-external-number").val(); + let external_number_on = $("#server-external-number-on").is(":checked"); + let transparent_mode = $("#server-transparent-mode").is(":checked"); + + let error = false; + + if (external_number.match(pattern_number) === null) { noticer.warning("Внешний номер не соответствует формату +7XXXXXXXXXX"); error = true; } + + if (error) return; + + request('writeserverinfo', 'json', { + internal_number: internal_number, + external_number: external_number, + external_number_on: external_number_on, + transparent_mode: transparent_mode + }).then(data => { + data.error ? + noticer.error(data.message) : + $("#server-button").button( "option", "disabled", true) && noticer.success("Параметры сервера были сохранены") + }).catch(error => { + noticer.error(error.message); + }); +} diff --git a/public/style.css b/public/style.css index 79134bf..71ea28c 100644 --- a/public/style.css +++ b/public/style.css @@ -71,7 +71,7 @@ input { margin-top: 20px } -.content table { +.table-content { width: 100%; border-collapse: collapse; border-spacing: 0; @@ -125,15 +125,28 @@ tr.row:hover, tr.row:nth-child(even):hover { /* EDIT NUMBER */ -.number-label, .sms-label, .ussd-label { +.number-label, .sms-label, .ussd-label, .server-label { color: #333; text-align: right; } -.number-value { +.number-value, .server-value { height: 30px; } +.server-value { + text-align: left; + padding-left: 10px; +} + +.server-input { + height: 100%; +} + +.server-button { + margin-top: 30px; +} + .sms-label-text, .ussd-label-text { vertical-align:top } diff --git a/source/daster.d b/source/daster.d index 9f55970..29609de 100644 --- a/source/daster.d +++ b/source/daster.d @@ -19,6 +19,7 @@ import structures; import requests.numbers; import requests.sms; import requests.ussd; +import requests.server; static ServerInfo serverInfo; @@ -223,6 +224,12 @@ void postReq(HTTPServerRequest req, HTTPServerResponse res) { case "delussd": sendDelUSSD(req, res); break; + case "serverinfo": + getServerInfo(req, res); + break; + case "writeserverinfo": + sendWriteServerInfo(req, res); + break; default: res.redirect("/"); } diff --git a/source/requests/server.d b/source/requests/server.d new file mode 100644 index 0000000..7b66583 --- /dev/null +++ b/source/requests/server.d @@ -0,0 +1,30 @@ +module requests.server; + +import vibe.vibe; +import response; +import structures; +import sql; +import singlog; + +import std.regex; + +// Получить информацию о сервере +void getServerInfo(HTTPServerRequest req, HTTPServerResponse res) { + auto dataServer = sqlGetServerInfo(); + render!("server.dt", dataServer)(res); +} + +void sendWriteServerInfo(HTTPServerRequest req, HTTPServerResponse res) { + ServerDB server = deserializeJson!ServerDB(req.json); + + if (!server.external_number.matchFirst(regex(r"^\+7\d{10}$", "g"))) { + res.send(true, "Внешний номер не соответствует формату +7XXXXXXXXXX"); + return; + } + + if (!sqlUpdateServerInfo(server)) { + res.send(true, "Не удалось записать параметры сервера"); + return; + } + res.send(); +} diff --git a/source/sql.d b/source/sql.d index 5f14461..a488407 100644 --- a/source/sql.d +++ b/source/sql.d @@ -6,11 +6,9 @@ import structures; import std.conv; -/* - - Запросы для таблицы номеров телефонов - -*/ +/*********************************************************** + Запросы для таблицы номеров телефонов +***********************************************************/ GroupDB[] sqlGetListGroups() { GroupDB[] groups; @@ -207,11 +205,9 @@ bool sqlInsertNumber(NumberDB number) { return true; } -/* - - Запросы для таблицы SMS - -*/ +/*********************************************************** + Запросы для таблицы SMS +***********************************************************/ SMSDB[] sqlGetSMSNumbers() { SMSDB[] numbers; @@ -306,11 +302,9 @@ bool sqlDeleteSMS(int idsms) { return true; } -/* - - Запросы для таблицы USSD - -*/ +/*********************************************************** + Запросы для таблицы USSD +***********************************************************/ USSDDB[] sqlGetUSSDNumbers() { USSDDB[] numbers; @@ -408,3 +402,53 @@ bool sqlDeleteUSSD(int idussd) { } return true; } + +/*********************************************************** + Запросы для таблицы информации о сервере +***********************************************************/ + +ServerDB sqlGetServerInfo() { + ServerDB server; + try { + auto queryResult = pgsql.sql( + "select + case when da_transparent_mode then 1 else 0 end da_transparent_mode, + da_internal_number, + da_external_number, + case when da_external_number_on then 1 else 0 end da_external_number_on + from da_server + where da_id = 1" + ); + foreach (row; queryResult) { + server.transparent_mode = row["da_transparent_mode"].to!int.to!bool; + server.internal_number = row["da_internal_number"]; + server.external_number = row["da_external_number"]; + server.external_number_on = row["da_external_number_on"].to!int.to!bool; + } + } catch (Exception e) { + log.e("Не удалось выполнить запрос к БД. " ~ e.msg); + } + + return server; +} + +bool sqlUpdateServerInfo(ServerDB server) { + try { + pgsql.sql( + "update da_server set + da_transparent_mode = ?, + da_internal_number = ?, + da_external_number = ?, + da_external_number_on = ? + where da_id = 1", + server.transparent_mode, + server.internal_number, + server.external_number, + server.external_number_on + ); + } catch (Exception e) { + log.e("Ошибка обновления параметров сервера в БД. " ~ e.msg); + return false; + } + return true; +} diff --git a/source/structures.d b/source/structures.d index ec79c5a..04c2a95 100644 --- a/source/structures.d +++ b/source/structures.d @@ -53,3 +53,10 @@ struct USSDDB { string type_comment; string text; } + +struct ServerDB { + bool transparent_mode; + string internal_number; + string external_number; + bool external_number_on; +} diff --git a/source/version_.d b/source/version_.d index fd2e40b..296864d 100644 --- a/source/version_.d +++ b/source/version_.d @@ -1,3 +1,3 @@ module version_; -enum dasterVersion = "v0.0.6"; +enum dasterVersion = "v0.0.8"; diff --git a/views/index.dt b/views/index.dt index c6e2f47..60cf645 100644 --- a/views/index.dt +++ b/views/index.dt @@ -35,6 +35,4 @@ body div#tabs-numbers div#tabs-sms div#tabs-ussd - p Список результатов USSD запросов div#tabs-server - p Информация о сервере diff --git a/views/list_number_groups.dt b/views/list_number_groups.dt index 67052e8..9a907a8 100644 --- a/views/list_number_groups.dt +++ b/views/list_number_groups.dt @@ -2,7 +2,7 @@ div#accordion-numbers - foreach (group; listGroups) h3 #{group.comment} div.group-content(data-group-name='#{group.name}') - table + table.table-content thead.head tr th Номер @@ -12,5 +12,5 @@ div#accordion-numbers th Черные звонки th Комментарий div.body-rows - table + table.table-content tbody.body diff --git a/views/list_sms_groups.dt b/views/list_sms_groups.dt index a511995..c4f7270 100644 --- a/views/list_sms_groups.dt +++ b/views/list_sms_groups.dt @@ -2,12 +2,12 @@ div#accordion-sms - foreach (number; numbers) h3 На номер #{number.to} div.group-content(data-to='#{number.to}') - table + table.table-content thead.head tr th.sms-content-width Дата th.sms-content-width От кого th Текст сообщения div.body-rows - table + table.table-content tbody.body diff --git a/views/list_ussd_groups.dt b/views/list_ussd_groups.dt index 249fa78..4cd5b44 100644 --- a/views/list_ussd_groups.dt +++ b/views/list_ussd_groups.dt @@ -2,12 +2,12 @@ div#accordion-ussd - foreach (number; numbers) h3 На номер #{number.to} div.group-content(data-to='#{number.to}') - table + table.table-content thead.head tr th.ussd-content-width Дата th.ussd-content-width-type Тип th Текст сообщения div.body-rows - table + table.table-content tbody.body diff --git a/views/server.dt b/views/server.dt new file mode 100644 index 0000000..f17ecaf --- /dev/null +++ b/views/server.dt @@ -0,0 +1,23 @@ +div#server-data + table + tbody + tr + td.server-label Внутренний номер: + td.server-value + input.input-focus.server-input#server-internal-number(type="text", value="#{dataServer.internal_number}") + td.server-value + tr + td.server-label Внешний номер: + td.server-value + input.input-focus.server-input#server-external-number(type="text", value="#{dataServer.external_number}") + td.server-value + label(for="server-external-number-on") Использовать + input.server-input#server-external-number-on(name="server-external-number-on", type="checkbox", checked="#{dataServer.external_number_on}") + tr + td.server-label Прозрачный режим: + td.server-value + label(for="server-transparent-mode") Включен + input.server-input#server-transparent-mode(name="server-transparent-mode", type="checkbox", checked="#{dataServer.transparent_mode}") + td.server-value +div.server-button + button#server-button Сохранить изменения