var _path_php = 'https://fado.academy/start/init/php2/';
$(document).ready( function(){
$("#cmd-start").on('click',function(){
autorizator(function(){
console.log('апосля апосля афторизации');
});
});
$("#cmd-reg").on('click',function(){
// сбросить
registrate(function(){
console.log('апосля регистрации по кнопке');
});
});
$('#pwd').keydown(function(event){
if (event.which == 13) {
console.log('key enter');
autorizator(function(){
console.log('апосля апосля афторизации');
});
}
});
setTimeout(function() {
$('#uname').focus();
},700);
});
function gen_autch( uaccounts, after )
{
var s = '
';
$.each( uaccounts, function( j , account ){
if (! empty(account.icon)) {
style = "background-image: url('"+account.icon+"');";
} else {
style = "background-image: url('https://fado.academy/start/images/tuner.png');";
}
s += '
';
});
s += '
';
after(s);
}
function gen_content_menu( host, aprogs, after )
{
var s = '';
$.each( aprogs, function(i, rec ){
let style = '';
let title = rec.idru;
if (! empty(rec.avatar)) {
console.log('gen_content_menu rec=',rec);
style = "background-image: url('https://"+rec.subdomain+".fado.academy/media/"+rec.avatar+"');";
} else {
style = "background-image: url('https://fado.academy/start/images/tuner.png');";
title += ' Аватар программы не настроен';
}
s += '
'+
' '+
'
'+rec.idru+'
'+
'
'+rec.name+'
'+
'
';
});
s += '
';
after(s);
}
function registrate ( after )
{
let login = $('input#uname').val();
let pass = $('input#pwd').val();
$('input#uname-reg').val(login);
$('input#pwd-reg').val(pass);
$('#div-pass').css('display','none');
$('#div-reg').css('display','grid');
$('input#reg-fio').focus();
$("#cmd-save").on('click',function(){
save_reg(function(){
wait( '' , 'Регистрация выполнена успешно!', 5000 , 100,100, 50, 150, 15, '#008000' , '#FFFF00', 'solid 1px #ffff00', 0 );
// переносим значения логина и пароля в поля авторизации из полей регистрации
let login = $('input#uname-reg').val();
let pass = $('input#pwd-reg').val();
$('input#uname').val(login);
$('input#pwd').val(pass);
// и после этого авторизуемся
autorizator( function(){
wait( '' , 'Добро пожаловать!', 5000 , 100,100, 50, 150, 15, '#008000' , '#ffff00', 'solid 1px #ffff00', 0 );
});
});
});
}
function autorizator( after )
{
// здесь будет проверка пароля в БД
let username = $("#uname").val();
let userpass = $("#pwd").val();
//console.log('userpass=',userpass , 'account_id=', account_id );
if (username.length==0) {
alert('Ну хоть что ни-будь занесите в логин!');
$('#uname').focus();
return;
}
if (userpass.length==0) {
alert('Ну хоть что ни-будь занесите в пароль!');
$('#pwd').focus();
return;
}
//console.log('autorizator: username=',username,' userpass=', userpass , 'account=',account_id);
//console.log('_path_php=',_path_php);
$.post(_path_php+'start.php', { qswitch:'auth', login: username, pass: userpass , account: account_id }, function ( data ){
ret = jQuery.parseJSON(data);
if (data) {
if (ret.ok) {
console.log('Привет авторизация ответила: ret=',ret);
if (ret.auth) {
if (ret.uaccounts.length >0) {
if (ret.uaccounts.length==1) {
// всего один аккаунт
if (ret.uaccounts[0].progs.length>1) {
console.log('autorizator ret=',ret);
// есть программы и их больше чем одна
defi_prog_menu( ret, ret.uaccounts[0].progs, ret.uaccounts[0].id , ret.uaccounts[0].subdomain );
$('#div-pass').css('display','none');
$('#div-reg').css('display','none');
$('#div-account').css('display','block');
$('#div-account-list').focus();
} else {
if (ret.uaccounts[0].progs.length==1) {
// есть только одна программа и скоре всего это Админка
//load_account_prog(user_id, user_key, account_id, subdomain, after ) {
window.open(ret.uaccounts[0].progs[0].path+'/index.html?user_id='+ret.user_id+'&user_key='+ret.user_key+'&account_id='+ret.uaccounts[0].id+'&subdomain='+ret.uaccounts[0].subdomain, '_blank');
//});
} else {
// нет совсем ни одной прораммы
alert('Нет программ для аккаунта');
}
}
} else {
gen_autch( ret.uaccounts, function(content){
console.log('gen_autch ok');
$('#div-account-list').html(content);
$('#div-account-list div.line-account').on('click',function(){
let account_id = $(this).prop('id').replace('acnt-','');
var account=ret.uaccounts.filter( function(a){return a.id==account_id});
console.log('ret=',ret,'account_id=',account_id,' account=',account, account[0].progs, account[0].subdomain );
if (account[0].progs.length>0) {
defi_prog_menu( ret, account[0].progs, account_id , account[0].subdomain );
} else {
alert('Чота нет программ для выбранного аккаунта');
}
});
$('#div-pass').css('display','none');
$('#div-reg').css('display','none');
$('#div-account').css('display','block');
$('#div-account-list').focus();
}); // меню доступных программ
}
} else {
alert('Чота, нет доменов для авторизованного пользователя...');
}
} else {
if (ret.login) {
$('#cmd-reg').css('display','grid');
$('#uname-reg').prop('disabled',true);
$('#div-pass #cmd-reg').html('Сбросить');
$('#div-pass #reg-text').html('Если вы забыли пароль, обратитесь к администратору, получите код для смены пароля и сбросьте старый пароль');
$('#div-reg #reg-text').html('Сменить пароль');
$('#div-pass #login-text').html('Вы указали ошибочный пароль для существующей записи.');
$('#additive-info').css('display','none');
$('#login-text').css('color','#ff0000');
$('#login-reg').css('display','grid');
$('#reg-text').css('display','grid');
} else {
$('#cmd-reg').css('display','grid');
$('#login-text').html('Неверное сочетание логин и пароль, попробуйте еще раз.');
$('#div-pass #cmd-reg').html('Регистрация');
$('#div-pass #reg-text').html('Если вы впервые на этом ресурсе, пройдите регистрацию');
$('#login-text').css('color','#ff0000');
$('#login-reg').css('display','grid');
$('#reg-text').css('display','grid');
}
}
} else {
alert('Ошибка скрипта при авторизации:'+ret.mess);
}
} else {
alert('Cкрипт не отвечает, обратитесь к админу.');
}
});
}
function defi_account_menu()
{
$.post('../../start.php', { qswitch:'account_all', user_id: ret.user_id, user_key: ret.user_key }, function ( data ){
ret = jQuery.parseJSON(data);
if (ret.ok) {
console.log('defi_account_menu ret=',ret);
gen_autch( ret.uaccounts, function(content){
console.log('defi_account_menu gen_autch ok');
$('#div-account-list').html(content);
$('#div-account-list div.line-account').on('click',function(){
account_id = $(this).prop('id').replace('acnt-','');
});
$('#div-account-list').focus();
});
}
});
}
function defi_prog_menu( ret, aprogs, account_id , subdomain )
{
gen_content_menu( ret.host, aprogs, function(content) {
$('#div-prog-list').html(content);
$('#div-prog-list .div-option-prog').on('click',function(){
var prog_id = Number($(this).prop('id').replace('prog-',''));
var path = $(this).attr('path');
console.log('defi_prog_menu prog_id=',prog_id,' path=',path);
if (prog_id !=900) {
path += 'html/';
}
if ( prog_id == 1057) {
path = 'https://fado.academy/start/progs/fadocrm/';
}
console.log('опосля prog_id=',prog_id,' path=',path);
// что бы не передавать параметры get сохраняем сессию через post
// и отправляем редиректом на выполнение скрипту который ее прочтет тоже как post
//console.log('user_id=', ret.user_id,' user_key=', ret.user_key , 'account_id=',account_id, 'subdomain=',subdomain);
$.post('/../../init/php2/set_session.php', { user_id: ret.user_id, user_key: ret.user_key, account_id: account_id , subdomain: subdomain }, function ( data ){
ret1 = jQuery.parseJSON(data);
if (ret1.ok) {
console.log('path=',path,' account=',account_id,' subdomain=',subdomain,' user_id=',ret.user_id,' user_key=',ret.user_key);
console.log('--> ret1=',ret1);
//window.location.href = path+'index.html?user_id='+ret.user_id+'&user_key='+ret.user_key+'&account_id='+account_id;
window.open(path+'index.html?user_id='+ret1.user_id+'&user_key='+ret1.user_key+'&account_id='+ret1.account_id+'&subdomain='+ret1.subdomain, '_blank');
} else {
console.log('чейто пошло не так');
alert( ret.mess );
}
});
});
$('#div-account').css('display','none');
$('#div-prog').css('display','block');
$('#div-prog-list').focus();
});
}
function load_account_prog(user_id, user_key, account_id, subdomain, after ) {
$.post('/../../init/php2/set_session.php', { user_id: user_id, user_key: user_key, account_id: account_id , subdomain: subdomain }, function ( data ){
ret = jQuery.parseJSON(data);
if (ret.ok) {
after(ret);
} else {
console.log('чейто пошло не так');
alert( ret.mess );
}
});
}
function save_reg(after)
{
// здесь будет проверка пароля в БД
let username = $("#uname-reg").val();
let userpass = $("#pwd-reg").val();
let fio = $("#reg-fio").val();
let email = $("#reg-email").val();
let kod = $("#reg-kod").val();
let repass = $('#uname-reg').prop('disabled'); // признак что это не регистрация а смена пароля
console.log('repass=',repass);
if (kod.length<3) {
alert('Укажите код полученный от администратора');
$('#reg-kod').focus();
return;
}
if (username.length==0) {
alert('Ну хоть что ни-будь занесите в логин!');
$('#uname-reg').focus();
return;
}
if (userpass.length==0) {
alert('Ну хоть что ни-будь занесите в пароль!');
$('#pwd-reg').focus();
return;
}
if (empty(repass)) {
// дополнительные поля при регистрации
if (fio.length<3) {
alert('Занесите Фамилию Имя Отчество ');
$('#reg-fio').focus();
return;
}
if (email.length<3) {
alert('Занесите e-mail');
$('#reg-email').focus();
return;
}
}
console.log('--save_reg--',_path_php+'start.php account_id=',account_id,' kod=',kod);
$.post(_path_php+'start.php', {
qswitch:'regs',
login: username,
pass: userpass,
account: account_id,
fio: fio,
email: email,
kod: kod,
repass: repass }, function ( data ){
ret = jQuery.parseJSON(data);
if (data) {
if (ret.ok) {
console.log('Ответ регистрации ret=',ret);
if (ret.save) {
wait( '' ,ret.mess, 5000 , 100,100, 50, 250, 15, '#00ff00' , '#ffffff', 'double 3px #ffffff', 0 );
after();
} else {
switch (ret.err_login) {
case 1:
// повтор логина
wait( '' ,ret.mess, 5000 , 100,100, 50, 250, 15, '#FF0000' , '#ffffff', 'double 3px #ffffff', 0 );
$('#login-text').css('border','single 1px #ff0000');
$('#login-text').focus();
case 2:
// код регистрации устарел
wait( '' ,ret.mess, 5000 , 100,100, 50, 250, 15, '#00d0d0' , '#ffffff', 'double 3px #ffffff', 0 );
$('#login-reg').css('border','single 1px #ff0000');
$('#login-reg').focus();
default:
wait( '' ,ret.mess, 5000 , 100,100, 50, 250, 15, '#FF0000' , '#ffffff', 'double 3px #ffffff', 0 );
}
}
} else {
wait( '' , 'Ошибка при авторизации:'+ret.mess, 5000 , 100,100, 50, 150, 15, '#000000' , '#ff0000', 'solid 1px #d0d0d0', 0 );
}
} else {
wait( '' , 'Скрипт не отвечает, обратитесь к админу.', 10000 , 100,100, 50, 150, 15, '#000000' , '#ff0000', 'solid 1px #d0d0d0', 0 );
}
});
}