From 4fefe1019c4c45d7aea4f58819e6cd9fc93fa608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?21=E4=B8=96=E7=BA=AA=E5=B0=8F=E5=85=AB=E8=B7=AF?= <2529156631@qq.com> Date: Tue, 19 Nov 2024 22:38:11 +0800 Subject: [PATCH] =?UTF-8?q?uniapp=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91?= =?UTF-8?q?=EF=BC=9A=E5=85=A8=E5=B1=80=E6=A3=80=E6=B5=8B=E6=96=AD=E7=BD=91?= =?UTF-8?q?=E7=8A=B6=E6=80=81=EF=BC=8C=E6=96=AD=E7=BD=91=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E4=B8=8B=EF=BC=8C=E7=BB=99=E5=87=BA=E7=9B=B8=E5=BA=94=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Uni-App/CoreShop/App.vue | 26 ++++++---- .../CoreShop/core/hooks/index.ts | 1 + .../CoreShop/core/hooks/use-network-hook.ts | 50 +++++++++++++++++++ .../CoreShop/core/utils/http.ts | 17 ++++++- .../CoreShop/core/utils/uni-promise.ts | 11 +++- CoreCms.Net.Uni-App/CoreShop/manifest.json | 4 +- .../CoreShop/pages/home/home.vue | 10 ++++ .../pages/subpackage/notice/list/list.vue | 2 +- 8 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 CoreCms.Net.Uni-App/CoreShop/core/hooks/use-network-hook.ts diff --git a/CoreCms.Net.Uni-App/CoreShop/App.vue b/CoreCms.Net.Uni-App/CoreShop/App.vue index 2b6dd6b9..2a589d5f 100644 --- a/CoreCms.Net.Uni-App/CoreShop/App.vue +++ b/CoreCms.Net.Uni-App/CoreShop/App.vue @@ -3,18 +3,12 @@ import { queryShopConfigV2, queryAppVersions } from '@/core/api'; import type { Response, ShopConfigType } from '@/core/models'; import { useShopConfigStore } from '@/core/store'; - + import { useNetWorkHook } from '@/core/hooks'; + /** 获取项目配置 */ const shopConfigStore = useShopConfigStore(); - - onShow(() => { - console.log('App Show') - }) - - onHide(() => { - console.log('App Hide') - }) - + const useNetWork = useNetWorkHook(); + onLaunch(() => { getShopConfigV2(); /** 版本设置 */ @@ -34,6 +28,18 @@ checkVersion() // #endif }) + + onShow(() => { + console.log('App Show'); + useNetWork.checkNetworkStatus(); + useNetWork.subscriptionNetworkStatusChange(); + }) + + onHide(() => { + console.log('App Hide') + useNetWork.offNetworkStatusChange(); + useNetWork.setNetWorkStatusToDefault(); + }) const getShopConfigV2 = async () => { shopConfigStore.querySystemConfig(); diff --git a/CoreCms.Net.Uni-App/CoreShop/core/hooks/index.ts b/CoreCms.Net.Uni-App/CoreShop/core/hooks/index.ts index 015a01b7..ec2383be 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/hooks/index.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/hooks/index.ts @@ -1,3 +1,4 @@ /** 系统配置 */ export * from './systemInfo'; export * from './use-loading'; +export * from './use-network-hook'; diff --git a/CoreCms.Net.Uni-App/CoreShop/core/hooks/use-network-hook.ts b/CoreCms.Net.Uni-App/CoreShop/core/hooks/use-network-hook.ts new file mode 100644 index 00000000..d34aee12 --- /dev/null +++ b/CoreCms.Net.Uni-App/CoreShop/core/hooks/use-network-hook.ts @@ -0,0 +1,50 @@ +import { ref, watchEffect } from 'vue'; +import { getNetworkType } from '../utils/uni-promise'; + +export const isConnectedRef = ref(undefined); +export const useNetWorkHook = () => { + watchEffect(() => { + if (typeof isConnectedRef.value === 'boolean' && !isConnectedRef.value) { + uni.showToast({ title: '网络跑小差啦~', icon: 'none' }); + } + }); + const callBack = (res : UniNamespace.OnNetworkStatusChangeSuccess) => { + isConnectedRef.value = res.isConnected; + }; + + const subscriptionNetworkStatusChange = () => { + uni.onNetworkStatusChange(callBack); + }; + const offNetworkStatusChange = () => { + uni.offNetworkStatusChange(callBack); + }; + + const checkNetworkStatus = async () => { + if (typeof isConnectedRef.value === 'undefined') { + const result : UniNamespace.GetNetworkTypeSuccess = await getNetworkType(); + isConnectedRef.value = result.networkType !== 'none'; + } + }; + + const setNetWorkOffline = () => { + isConnectedRef.value = undefined; + isConnectedRef.value = false; + }; + + const setNetWorkStatusToDefault = () => { + isConnectedRef.value = undefined; + }; + + const checkNetWorkOffline = async () => { + if (typeof isConnectedRef.value === 'undefined') { + await checkNetworkStatus(); + } + + if (!isConnectedRef.value) { + setNetWorkOffline(); + } + return isConnectedRef.value; + }; + + return { subscriptionNetworkStatusChange, offNetworkStatusChange, setNetWorkOffline, checkNetworkStatus, setNetWorkStatusToDefault, checkNetWorkOffline }; +}; \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts index 4ae5d5e0..05332c7d 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts @@ -3,6 +3,7 @@ import { UserToken } from '@/core/consts' import UniP from "./uni-promise"; import { getRequestHost } from './host'; import { handleLoginGetToken } from '@/core/utils'; +import { useNetWorkHook } from '@/core/hooks'; type TReqOptions = Pick; type TReqMethodOptions = Omit; @@ -10,6 +11,7 @@ type TResData = UniApp.RequestSuccessCallbackResult['data']; let isRefreshing = true; let subscribers = []; +const useNetWork = useNetWorkHook(); function onAccessTokenFetched() { subscribers.forEach((callback) => { @@ -19,11 +21,24 @@ function onAccessTokenFetched() { isRefreshing = true; } -function addSubscriber(callback:Function) { +function addSubscriber(callback : Function) { subscribers.push(callback) } const request = async (url : string, { method, data, header = {} } : TReqOptions, auth : boolean = true, callBack ?: Function) : Promise> => { + + // 检查是否存在网络 + const isConnected : boolean = await useNetWork.checkNetWorkOffline(); + if (!isConnected) { + return Promise.reject({ + data: undefined, + code: 500, + msg: '网络跑小差啦~', + status: false, + otherData: '网络跑小差啦~' + }); + } + return new Promise(async (resolve, _) => { const res = await UniP.request({ url: url.indexOf('http') >= 0 ? url : `${getRequestHost()}/${url}`, diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts index 1adb56b9..9485acc1 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts @@ -187,6 +187,14 @@ export const showModal = (options : UniNamespace.ShowModalOptions) : Promise { + return new Promise((success, fail) => { + uni.getNetworkType({ + success, + fail + }); + }); +}; export default { @@ -207,5 +215,6 @@ export default { openSetting, authorize, showToast, - showModal + showModal, + getNetworkType } \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/manifest.json b/CoreCms.Net.Uni-App/CoreShop/manifest.json index 2d46a0e3..9db936f9 100644 --- a/CoreCms.Net.Uni-App/CoreShop/manifest.json +++ b/CoreCms.Net.Uni-App/CoreShop/manifest.json @@ -54,7 +54,7 @@ "setting" : { "urlCheck" : false, "checkSiteMap" : false, - "minified" : false, + "minified" : true, "postcss" : false, "es6" : true }, @@ -79,7 +79,7 @@ } }, "mp-alipay" : { - "appid" : "2021004107611929", + "appid" : "2021004107611929", "usingComponents" : true }, "mp-baidu" : { diff --git a/CoreCms.Net.Uni-App/CoreShop/pages/home/home.vue b/CoreCms.Net.Uni-App/CoreShop/pages/home/home.vue index 83986218..eeea7a87 100644 --- a/CoreCms.Net.Uni-App/CoreShop/pages/home/home.vue +++ b/CoreCms.Net.Uni-App/CoreShop/pages/home/home.vue @@ -74,6 +74,16 @@ onShow(() => { /** 触发自定义onshow事件,让后代组件监听页面是都进入 */ uni.$emit(onHomePageShow); + console.log('ddd') + uni.getNetworkType({ + success: function (res) { + console.log(res.networkType); + } + }); + uni.onNetworkStatusChange(function (res) { + console.log(res.isConnected); + console.log(res.networkType); + }); }) onPullDownRefresh(async () => { await queryHomePageConfig(); diff --git a/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/notice/list/list.vue b/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/notice/list/list.vue index 4824e933..e492cc78 100644 --- a/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/notice/list/list.vue +++ b/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/notice/list/list.vue @@ -10,7 +10,7 @@ 没有更多了 - +