Commit cac92781 by Иван Кубота

Remove Error status on phone\code submitting.

new AuthAjax + old Ajax refactor
parent 92a580d0
import { AsyncAjax } from "/core/Ajax.jsx";
export { AsyncAjax } from "/core/Ajax.jsx";
export const AsyncAuthAjax = {
async get(url) {
if(!store.get('account.token'))
return false;
return await AsyncAjax.get(url, {
authorization: 'Bearer '+ store.get('account.token')
})
},
async post(url, data) {
if(!store.get('account.token'))
return false;
return await AsyncAjax.post(url, data, {
authorization: 'Bearer '+ store.get('account.token')
})
}
};
\ No newline at end of file
const transformCfg = function(cfg) {
return {
...cfg,
headers: {
'Content-Type': 'application/json',
...(cfg && cfg.headers || {})
}
}
};
const xhrProceed = function(method, xhr, cfg, cb) {
xhr.open(method, cfg.url, true);
for(let key in cfg.headers){
xhr.setRequestHeader(key, cfg.headers[key]);
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let json;
try {
json = JSON.parse(xhr.responseText);
cb && cb(false, json);
}catch (e) {
console.error('AJAX:'+ method +' Incorrect Response ← '+cfg.url, xhr.responseText, e);
cb && cb(true, e);
}
}else if(xhr.status === 404){
cb && cb(true);
}else if(xhr.status === 401){
//Пользователь не авторизован
window.location.href = "/login";
return;
}else if(xhr.status === 403){
//Нет доступа к ассистенту
window.location.href = "/";
return;
}else if (xhr.status >= 400){
/*HeaderModel.statusNotification.statuses.push({type: 'error'});
setTimeout(function () {
HeaderModel.statusNotification.statuses.length = 0;
}, 3000);*/
cb && cb(true, {"status":"error"});
return;
}
};
};
export const Ajax = {
post(url, data, cb){
post(url, data, cb, cfg){
let stringData='';
try{
stringData = JSON.stringify(data);
const xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let json;
try {
json = JSON.parse(xhr.responseText);
cb && cb(false, json);
}catch (e) {
console.error('AJAX:POST Incorrect Response ← '+url, xhr.responseText, e);
cb && cb(true, e);
}
}else if(xhr.status === 404){
cb && cb(true);
}else if(xhr.status === 401){
//Пользователь не авторизован
window.location.href = "/login";
return;
}else if(xhr.status === 403){
//Нет доступа к ассистенту
window.location.href = "/";
return;
}else if (xhr.status >= 400){
/*HeaderModel.statusNotification.statuses.push({type: 'error'});
setTimeout(function () {
HeaderModel.statusNotification.statuses.length = 0;
}, 3000);*/
cb && cb(true, {"status":"error"});
return;
}
};
cfg = transformCfg(cfg || {});
cfg.url = url;
xhrProceed("POST", xhr, cfg, cb);
xhr.send(stringData);
}catch (e) {
/* HeaderModel.statusNotification.statuses.push({type: 'error'});
......@@ -46,39 +63,14 @@ export const Ajax = {
}
},
get(url, cb){
get(url, cb, cfg){
try{
const xhr = new XMLHttpRequest();
cfg = transformCfg(cfg || {});
cfg.url = url;
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let json;
try {
json = JSON.parse(xhr.responseText);
cb && cb(false, json);
}catch (e) {
console.error('AJAX:GET Incorrect Response ← '+url, xhr.responseText, e);
cb && cb(true, e);
}
}else if(xhr.status === 404){
cb && cb(true);
}else if(xhr.status === 401){
//Пользователь не авторизован
window.location.href = "/login";
return;
}else if(xhr.status === 403){
//Нет доступа к ассистенту
window.location.href = "/";
return;
} else if (xhr.status >= 400){
/*HeaderModel.statusNotification.statuses.push({type: 'error'});
setTimeout(function () {
HeaderModel.statusNotification.statuses.length = 0;
}, 3000);*/
cb && cb(true, {"status":"error"});
}
};
xhrProceed(xhr, url, cb);
xhrProceed("GET", xhr, cfg, cb);
xhr.send(null);
}catch (e) {
/*HeaderModel.statusNotification.statuses.push({type: 'error'});
......@@ -91,7 +83,7 @@ export const Ajax = {
}
};
export const AsyncAjax = {
get(url) {
get(url, cfg) {
return new Promise(function(resolve, reject) {
Ajax.get(url, function(err, data) {
if(err){
......@@ -99,10 +91,10 @@ export const AsyncAjax = {
}else{
resolve(data);
}
})
}, cfg)
});
},
post(url, data){
post(url, data, cfg){
return new Promise(function(resolve, reject) {
Ajax.post(url, data, function(err, data) {
if(err){
......@@ -110,7 +102,7 @@ export const AsyncAjax = {
}else{
resolve(data);
}
})
}, cfg)
});
}
};
\ No newline at end of file
......@@ -3,8 +3,10 @@ const store = new Store({
current: 'login'
},
'account': {
userID: 5,
name: 'Диогроген Курославович',
phone: '79999877414'
phone: '79999877414',
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1ODAzMDg0NTcsImV4cCI6MTU4MDM5NDg1Nywicm9sZXMiOlsiUk9MRV9BRE1JTiIsIlJPTEVfRU1QTE9ZRUUiXSwicGhvbmUiOiI3OTk5OTg3NzQxNCIsImlkIjo1fQ.yt7LqaBkHZ_YFXg4WV8LRrm6eW9RdBjFbWNlLBYJXDyFPKjeVuR9PTGwnZdztqRxnGEKc60FnZfqpf52rfTdgkZWuuy_jXQbxfUzRvuD2GHPd4Ds12ucQnbij71-Rv0d8mA4L3EzbEQzMfSwoQsjhY6PXmhHRft4mSuXm0Cx4cn7ms92gTBFGbSWA-Ru395jTcaTpWdpAyIQFCMwAyQ1zQDRwsYc4RPPVKTH2BhkGVLVWlMDh_D22kduV-zcEzxEAFvOf6HhctsYBaasByhXIHVKQUcSKjA5bz_eftW7XQWeer0gQNR3OLRP9Qo7INL8GnhVhGEuu5UL6x0JJ9FeutOhZ4xa0jOqwzmYRcTMP63LbWNoSGHgn-UY14yG_O31Ij0wDNS-VqhCoVB1IdGawWX_p3eKLuUvCX38ZLs3cAIU1vmHlF--0KNSwo3OeYj-U9R8tWPi-zkUDw29ZAM1wu38uXI4_bw_UG2g53cSdjxuIQw8sLmAp7EF-6R1q28YQecqVKm-d8VgVeVAh8ueicFeejSzgax-3GRjHR9Kudqgxescas6rm6g5iV_-73VUXrn-8rvg2vXuB3nF-7X-SWV2gKFBP9u0hTGMBeseODyjohN3bWjnfPWU0rWhwNUsPIJabzg0iICv4a5li507TCaHg025HEa2V2mayj0wkk8'
}
});
......
......@@ -11,7 +11,7 @@ import LoginCode from './LoginCode.jsx';
const {AND, OR, IF} = Store;
export default D.declare('view.page.Login', () => {
const loginStore = new Store({
phone: '79991112233',
phone: '79999877415',
code: '',
codeValid: false,
active: 'enterPhone',
......
......@@ -7,6 +7,7 @@ const {AND, OR, IF} = Store;
export default D.declare('view.page.LoginCode', ({loginStore}) => {
const checkCode = function () {
loginStore.set('codeChecking', true);
loginStore.set('codeError', false);
try{
}catch(e){
......
......@@ -8,6 +8,8 @@ export default D.declare('view.page.LoginPhone', ({loginStore}) => {
const checkPhone = async function() {
loginStore.set('phoneChecking', true);
loginStore.set('phoneError', false);
try{
const result = await AsyncAjax.post( API.ENDPOINTS.CHECK_PHONE(), {
phone: loginStore.get('phone')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment