Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authormhsanaei <ho3ein.sanaei@gmail.com>2024-07-14 01:01:13 +0300
committermhsanaei <ho3ein.sanaei@gmail.com>2024-07-14 01:01:13 +0300
commit3d7f13225affd82afad48951a48864c8c650be4c (patch)
treed0c94f07e45856702452953420137e85fdd7632f /web
parent76fdfb2ef2083563cebaca0f05d823653df18e05 (diff)
Refactor HttpUtil and Msg: optimize methods
Diffstat (limited to 'web')
-rw-r--r--web/assets/js/util/utils.js78
1 files 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) {