hookehuyr

feat(网络): 添加弱网检测和离线模式处理逻辑

在应用启动时检测网络状态,当网络较弱时提示用户使用缓存的预约码进入离线模式
/*
* @Date: 2025-06-28 10:33:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-01-13 00:28:35
* @LastEditTime: 2026-01-13 13:49:42
* @FilePath: /xyxBooking-weapp/src/app.js
* @Description: 应用入口文件
*/
......@@ -14,6 +14,8 @@ import Taro from '@tarojs/taro'
import { qrcodeListAPI } from '@/api/index'
import { formatDatetime } from '@/utils/tools'
let has_shown_network_modal = false
/**
* 格式化支付记录,按 pay_id 分组,相同 pay_id 下的记录 sort 为 0,否则为 1
* @param {*} data 支付记录数组
......@@ -99,6 +101,69 @@ const App = createApp({
return ['wifi', '4g', '5g', '3g'].includes(network_type)
}
const get_network_type = async () => {
try {
const result = await new Promise((resolve, reject) => {
Taro.getNetworkType({
success: resolve,
fail: reject,
})
})
return result?.networkType || 'unknown'
} catch (e) {
return 'unknown'
}
}
const has_offline_qr_cache = () => {
try {
const data = Taro.getStorageSync('OFFLINE_QR_DATA')
return Array.isArray(data) && data.length > 0
} catch (e) {
return false
}
}
const handle_bad_network_on_launch = async () => {
if (has_shown_network_modal) return false
const network_type = await get_network_type()
const is_none_network = network_type === 'none'
const is_weak_network = !is_usable_network(network_type)
if (!is_weak_network) return false
has_shown_network_modal = true
if (has_offline_qr_cache()) {
try {
const modal_res = await Taro.showModal({
title: '网络连接不畅',
content: '当前网络信号较弱,可使用已缓存的预约码进入离线模式',
confirmText: '预约码',
cancelText: '知道了',
})
if (modal_res?.confirm) {
await Taro.reLaunch({ url: '/pages/offlineBookingCode/index' })
return true
}
} catch (e) {
return is_none_network
}
} else {
try {
await Taro.showToast({ title: '网络连接不畅', icon: 'none', duration: 2000 })
} catch (e) {
return is_none_network
}
}
return is_none_network
}
const should_stop = await handle_bad_network_on_launch()
if (should_stop) return
/**
* 尝试在网络可用时预加载二维码数据
* - 仅在有授权时调用
......