From 3d7f13225affd82afad48951a48864c8c650be4c Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sun, 14 Jul 2024 00:01:13 +0200 Subject: [PATCH] Refactor HttpUtil and Msg: optimize methods --- web/assets/js/util/utils.js | 78 +++++++++++++++---------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/web/assets/js/util/utils.js b/web/assets/js/util/utils.js index f2f05f01..50ae4636 100644 --- a/web/assets/js/util/utils.js +++ b/web/assets/js/util/utils.js @@ -1,73 +1,57 @@ class Msg { - constructor(success, msg, obj) { - this.success = false; - this.msg = ""; - this.obj = null; - - if (success != null) { - this.success = success; - } - if (msg != null) { - this.msg = msg; - } - if (obj != null) { - this.obj = obj; - } + constructor(success = false, msg = "", obj = null) { + this.success = success; + this.msg = msg; + this.obj = obj; } } class HttpUtil { static _handleMsg(msg) { - if (!(msg instanceof Msg)) { + if (!(msg instanceof Msg) || msg.msg === "") { return; } - if (msg.msg === "") { - return; - } - if (msg.success) { - Vue.prototype.$message.success(msg.msg); - } else { - Vue.prototype.$message.error(msg.msg); - } + const messageType = msg.success ? 'success' : 'error'; + Vue.prototype.$message[messageType](msg.msg); } static _respToMsg(resp) { - const data = resp.data; + const { data } = resp; if (data == null) { return new Msg(true); - } else if (typeof data === 'object') { - if (data.hasOwnProperty('success')) { - return new Msg(data.success, data.msg, data.obj); - } else { - return data; - } - } else { - return new Msg(false, 'unknown data:', data); } + if (typeof data === 'object' && 'success' in data) { + return new Msg(data.success, data.msg, data.obj); + } + return typeof data === 'object' ? data : new Msg(false, 'unknown data:', data); } - static async get(url, data, options) { - let msg; + static async get(url, params, options = {}) { try { - const resp = await axios.get(url, data, options); - msg = this._respToMsg(resp); - } catch (e) { - msg = new Msg(false, e.toString()); + const resp = await axios.get(url, { params, ...options }); + const msg = this._respToMsg(resp); + this._handleMsg(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; } - this._handleMsg(msg); - return msg; } - static async post(url, data, options) { - let msg; + static async post(url, data, options = {}) { try { const resp = await axios.post(url, data, options); - msg = this._respToMsg(resp); - } catch (e) { - msg = new Msg(false, e.toString()); + const msg = this._respToMsg(resp); + this._handleMsg(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; } - this._handleMsg(msg); - return msg; } static async postWithModal(url, data, modal) {