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 = { export const Ajax = {
post(url, data, cb){ post(url, data, cb, cfg){
let stringData=''; let stringData='';
try{ try{
stringData = JSON.stringify(data); stringData = JSON.stringify(data);
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("POST", url, true); cfg = transformCfg(cfg || {});
xhr.setRequestHeader("Content-Type", "application/json"); cfg.url = url;
xhr.onreadystatechange = function () { xhrProceed("POST", xhr, cfg, cb);
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;
}
};
xhr.send(stringData); xhr.send(stringData);
}catch (e) { }catch (e) {
/* HeaderModel.statusNotification.statuses.push({type: 'error'}); /* HeaderModel.statusNotification.statuses.push({type: 'error'});
...@@ -46,39 +63,14 @@ export const Ajax = { ...@@ -46,39 +63,14 @@ export const Ajax = {
} }
}, },
get(url, cb){ get(url, cb, cfg){
try{ try{
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
cfg = transformCfg(cfg || {});
cfg.url = url;
xhr.open("GET", url, true); xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json"); xhrProceed(xhr, url, cb);
xhr.onreadystatechange = function () { xhrProceed("GET", xhr, cfg, cb);
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"});
}
};
xhr.send(null); xhr.send(null);
}catch (e) { }catch (e) {
/*HeaderModel.statusNotification.statuses.push({type: 'error'}); /*HeaderModel.statusNotification.statuses.push({type: 'error'});
...@@ -91,7 +83,7 @@ export const Ajax = { ...@@ -91,7 +83,7 @@ export const Ajax = {
} }
}; };
export const AsyncAjax = { export const AsyncAjax = {
get(url) { get(url, cfg) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
Ajax.get(url, function(err, data) { Ajax.get(url, function(err, data) {
if(err){ if(err){
...@@ -99,10 +91,10 @@ export const AsyncAjax = { ...@@ -99,10 +91,10 @@ export const AsyncAjax = {
}else{ }else{
resolve(data); resolve(data);
} }
}) }, cfg)
}); });
}, },
post(url, data){ post(url, data, cfg){
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
Ajax.post(url, data, function(err, data) { Ajax.post(url, data, function(err, data) {
if(err){ if(err){
...@@ -110,7 +102,7 @@ export const AsyncAjax = { ...@@ -110,7 +102,7 @@ export const AsyncAjax = {
}else{ }else{
resolve(data); resolve(data);
} }
}) }, cfg)
}); });
} }
}; };
\ No newline at end of file
...@@ -3,8 +3,10 @@ const store = new Store({ ...@@ -3,8 +3,10 @@ const store = new Store({
current: 'login' current: 'login'
}, },
'account': { 'account': {
userID: 5,
name: 'Диогроген Курославович', 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'; ...@@ -11,7 +11,7 @@ import LoginCode from './LoginCode.jsx';
const {AND, OR, IF} = Store; const {AND, OR, IF} = Store;
export default D.declare('view.page.Login', () => { export default D.declare('view.page.Login', () => {
const loginStore = new Store({ const loginStore = new Store({
phone: '79991112233', phone: '79999877415',
code: '', code: '',
codeValid: false, codeValid: false,
active: 'enterPhone', active: 'enterPhone',
......
...@@ -7,6 +7,7 @@ const {AND, OR, IF} = Store; ...@@ -7,6 +7,7 @@ const {AND, OR, IF} = Store;
export default D.declare('view.page.LoginCode', ({loginStore}) => { export default D.declare('view.page.LoginCode', ({loginStore}) => {
const checkCode = function () { const checkCode = function () {
loginStore.set('codeChecking', true); loginStore.set('codeChecking', true);
loginStore.set('codeError', false);
try{ try{
}catch(e){ }catch(e){
......
...@@ -8,6 +8,8 @@ export default D.declare('view.page.LoginPhone', ({loginStore}) => { ...@@ -8,6 +8,8 @@ export default D.declare('view.page.LoginPhone', ({loginStore}) => {
const checkPhone = async function() { const checkPhone = async function() {
loginStore.set('phoneChecking', true); loginStore.set('phoneChecking', true);
loginStore.set('phoneError', false);
try{ try{
const result = await AsyncAjax.post( API.ENDPOINTS.CHECK_PHONE(), { const result = await AsyncAjax.post( API.ENDPOINTS.CHECK_PHONE(), {
phone: loginStore.get('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