mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-01 01:20:49 +03:00
Refactor HttpUtil and Msg: optimize methods
This commit is contained in:
parent
76fdfb2ef2
commit
3d7f13225a
@ -1,73 +1,57 @@
|
|||||||
class Msg {
|
class Msg {
|
||||||
constructor(success, msg, obj) {
|
constructor(success = false, msg = "", obj = null) {
|
||||||
this.success = false;
|
|
||||||
this.msg = "";
|
|
||||||
this.obj = null;
|
|
||||||
|
|
||||||
if (success != null) {
|
|
||||||
this.success = success;
|
this.success = success;
|
||||||
}
|
|
||||||
if (msg != null) {
|
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
}
|
|
||||||
if (obj != null) {
|
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HttpUtil {
|
class HttpUtil {
|
||||||
static _handleMsg(msg) {
|
static _handleMsg(msg) {
|
||||||
if (!(msg instanceof Msg)) {
|
if (!(msg instanceof Msg) || msg.msg === "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.msg === "") {
|
const messageType = msg.success ? 'success' : 'error';
|
||||||
return;
|
Vue.prototype.$message[messageType](msg.msg);
|
||||||
}
|
|
||||||
if (msg.success) {
|
|
||||||
Vue.prototype.$message.success(msg.msg);
|
|
||||||
} else {
|
|
||||||
Vue.prototype.$message.error(msg.msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static _respToMsg(resp) {
|
static _respToMsg(resp) {
|
||||||
const data = resp.data;
|
const { data } = resp;
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
return new Msg(true);
|
return new Msg(true);
|
||||||
} else if (typeof data === 'object') {
|
}
|
||||||
if (data.hasOwnProperty('success')) {
|
if (typeof data === 'object' && 'success' in data) {
|
||||||
return new Msg(data.success, data.msg, data.obj);
|
return new Msg(data.success, data.msg, data.obj);
|
||||||
} else {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return new Msg(false, 'unknown data:', data);
|
|
||||||
}
|
}
|
||||||
|
return typeof data === 'object' ? data : new Msg(false, 'unknown data:', data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async get(url, data, options) {
|
static async get(url, params, options = {}) {
|
||||||
let msg;
|
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(url, data, options);
|
const resp = await axios.get(url, { params, ...options });
|
||||||
msg = this._respToMsg(resp);
|
const msg = this._respToMsg(resp);
|
||||||
} catch (e) {
|
|
||||||
msg = new Msg(false, e.toString());
|
|
||||||
}
|
|
||||||
this._handleMsg(msg);
|
this._handleMsg(msg);
|
||||||
return msg;
|
return msg;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('GET request failed:', error);
|
||||||
|
const errorMsg = new Msg(false, error.response?.data?.message || error.message);
|
||||||
|
this._handleMsg(errorMsg);
|
||||||
|
return errorMsg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async post(url, data, options) {
|
static async post(url, data, options = {}) {
|
||||||
let msg;
|
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(url, data, options);
|
const resp = await axios.post(url, data, options);
|
||||||
msg = this._respToMsg(resp);
|
const msg = this._respToMsg(resp);
|
||||||
} catch (e) {
|
|
||||||
msg = new Msg(false, e.toString());
|
|
||||||
}
|
|
||||||
this._handleMsg(msg);
|
this._handleMsg(msg);
|
||||||
return msg;
|
return msg;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('POST request failed:', error);
|
||||||
|
const errorMsg = new Msg(false, error.response?.data?.message || error.message);
|
||||||
|
this._handleMsg(errorMsg);
|
||||||
|
return errorMsg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async postWithModal(url, data, modal) {
|
static async postWithModal(url, data, modal) {
|
||||||
|
Loading…
Reference in New Issue
Block a user