From 9a290480056cb476abbc2d2cacbca25086406588 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?TOP=E7=B3=AF=E7=B1=B3?= <1130395124@qq.com>
Date: Sat, 8 Apr 2023 15:42:26 +0800
Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=8E=9F=E7=94=9Ftoast?=
=?UTF-8?q?=EF=BC=8Cloading=E4=BF=AE=E6=94=B9=E4=B8=BA=E8=87=AA=E5=AE=9A?=
=?UTF-8?q?=E4=B9=89=E7=BB=84=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/App.vue | 9 ---
src/components/layout/layout.vue | 6 +-
src/components/widgets/loading/loading.png | Bin 0 -> 2315 bytes
src/components/widgets/loading/loading.vue | 67 +++++++++++++++++++++
src/components/widgets/toast.vue | 50 ---------------
src/core/libs/loading.js | 13 ++++
src/core/libs/utils.js | 21 ++++---
src/core/request.js | 6 +-
src/core/upload.js | 6 +-
src/main.js | 2 +
src/store/index.js | 4 +-
src/store/modules/{toast.js => loading.js} | 2 +-
12 files changed, 103 insertions(+), 83 deletions(-)
create mode 100644 src/components/widgets/loading/loading.png
create mode 100644 src/components/widgets/loading/loading.vue
delete mode 100644 src/components/widgets/toast.vue
create mode 100644 src/core/libs/loading.js
rename src/store/modules/{toast.js => loading.js} (95%)
diff --git a/src/App.vue b/src/App.vue
index df9973f..2897209 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -4,15 +4,6 @@ export default {
pageConfig: {},
},
onLaunch: async function () {
- let that = this;
- for (let method of ["navigateTo", "redirectTo", "reLaunch", "switchTab", "navigateBack"]) {
- uni.addInterceptor(method, {
- success() {
- that.$store.commit("toast/show", false);
- that.$store.commit("toast/title", "");
- },
- });
- }
let pageConfig = this.$storage.get("system_config");
if (!pageConfig) {
const { windowWidth, windowHeight, statusBarHeight, safeAreaInsets } = uni.getSystemInfoSync();
diff --git a/src/components/layout/layout.vue b/src/components/layout/layout.vue
index 4089b61..1663583 100644
--- a/src/components/layout/layout.vue
+++ b/src/components/layout/layout.vue
@@ -31,13 +31,13 @@
-
+
+
+
\ No newline at end of file
diff --git a/src/components/widgets/toast.vue b/src/components/widgets/toast.vue
deleted file mode 100644
index 4c82f7e..0000000
--- a/src/components/widgets/toast.vue
+++ /dev/null
@@ -1,50 +0,0 @@
-
-
-
- {{ title }}
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/core/libs/loading.js b/src/core/libs/loading.js
new file mode 100644
index 0000000..ba95314
--- /dev/null
+++ b/src/core/libs/loading.js
@@ -0,0 +1,13 @@
+import $store from '@/store/index'
+export default {
+ show(title) {
+ $store.commit('loading/show', true);
+ if (title) {
+ $store.commit('loading/title', title);
+ }
+ },
+ hide() {
+ $store.commit('loading/show', false);
+ $store.commit('loading/title', "");
+ }
+}
\ No newline at end of file
diff --git a/src/core/libs/utils.js b/src/core/libs/utils.js
index 92100c7..5fceba1 100644
--- a/src/core/libs/utils.js
+++ b/src/core/libs/utils.js
@@ -1,5 +1,4 @@
import $storage from '@/core/storage'
-import $store from '@/store/index'
function time() {
return parseInt(Math.round(new Date() / 1000));
@@ -191,20 +190,22 @@ function toPage(url, options, type) {
}
let toastTimer;
-function toast(title, duration) {
- uni.hideLoading();
+function toast(title, options) {
+ if (typeof options === "undefined") {
+ options = {};
+ }
return new Promise((resolve, reject) => {
- if (typeof duration === "undefined") {
- duration = 1500;
+ if (!options.duration) {
+ options.duration = 1500;
}
- $store.commit('toast/show', true);
- $store.commit('toast/title', title);
+ uni.showToast({
+ ...{ title: title, icon: "none" },
+ ...options
+ });
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
- $store.commit('toast/show', false);
- $store.commit('toast/title', "");
resolve();
- }, duration);
+ }, options.duration);
})
}
diff --git a/src/core/request.js b/src/core/request.js
index c441377..bdd100b 100644
--- a/src/core/request.js
+++ b/src/core/request.js
@@ -32,9 +32,7 @@ const request = async (args) => {
throw "登录态失效";
}
- (rule.showLoading) && uni.showLoading({
- title: "加载中"
- });
+ (rule.showLoading) && prototype.$loading.show('加载中');
const contentType = (args.contentType) ? args.contentType : 'application/json';
@@ -51,7 +49,7 @@ const request = async (args) => {
}
});
- (rule.showLoading) && uni.hideLoading();
+ (rule.showLoading) && prototype.$loading.hide();
if (error || response.statusCode != 200) {
prototype.$utils.toast('网络错误');
diff --git a/src/core/upload.js b/src/core/upload.js
index 48664dc..a62c40b 100644
--- a/src/core/upload.js
+++ b/src/core/upload.js
@@ -24,9 +24,7 @@ let upload = async (args) => {
throw "找不到API:" + args.api;
}
- (rule.showLoading) && uni.showLoading({
- title: "上传中"
- });
+ (rule.showLoading) && prototype.$loading.show('加载中');
let [error, response] = await uni.uploadFile({
url: prototype.$config.root + rule.url,
@@ -35,7 +33,7 @@ let upload = async (args) => {
formData: args.data || {},
});
- (rule.showLoading) && uni.hideLoading();
+ (rule.showLoading) && prototype.$loading.hide();
if (error || response.statusCode != 200) {
prototype.$utils.toast('网络错误');
diff --git a/src/main.js b/src/main.js
index 5d077f8..86a6b53 100644
--- a/src/main.js
+++ b/src/main.js
@@ -5,6 +5,7 @@ import store from "./store/index"
import storage from './core/storage'
import request from './core/request'
import upload from './core/upload'
+import loading from './core/libs/loading'
import test from './core/libs/test'
import event from './core/libs/event'
import utils from './core/libs/utils'
@@ -18,6 +19,7 @@ Vue.use({
Vue.prototype.$storage = storage
Vue.prototype.$request = request
Vue.prototype.$upload = upload
+ Vue.prototype.$loading = loading
Vue.prototype.$test = test
Vue.prototype.$event = event
Vue.prototype.$utils = utils
diff --git a/src/store/index.js b/src/store/index.js
index 423a057..0f7db03 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -4,7 +4,7 @@ import Vuex from 'vuex'
import user from "@/store/modules/user"
import system from "@/store/modules/system"
import order from "@/store/modules/order"
-import toast from "@/store/modules/toast"
+import loading from "@/store/modules/loading"
Vue.use(Vuex)
export default new Vuex.Store({
@@ -12,6 +12,6 @@ export default new Vuex.Store({
user,
system,
order,
- toast,
+ loading,
}
})
diff --git a/src/store/modules/toast.js b/src/store/modules/loading.js
similarity index 95%
rename from src/store/modules/toast.js
rename to src/store/modules/loading.js
index 9e3d923..1dce469 100644
--- a/src/store/modules/toast.js
+++ b/src/store/modules/loading.js
@@ -11,7 +11,7 @@ export default {
},
title(state, data) {
state.title = data;
- }
+ },
},
actions: {}
}
\ No newline at end of file