From 4b93b50e504809433faf2d4f5a4e278168b27ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=8A=E5=B8=86?= Date: Fri, 16 Oct 2026 16:37:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AE=A2=E6=9C=8D=E5=92=8C=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 + src/api/auth.ts | 7 +- src/hooks/use-sms-code.ts | 7 +- .../bind/use-change-phone-bind.ts | 3 + .../mine/change-phone/change-phone.api.ts | 19 +- src/pages/mine/storage/index.vue | 36 ++- src/pages/mine/storage/storage.scss | 40 ++++ src/pages/mine/storage/storage.utils.ts | 214 ++++++++++++++++++ src/pages/mine/storage/use-storage.ts | 119 ++++++++-- src/pages/mine/support/cs-chat-socket.ts | 4 +- src/pages/mine/support/index.vue | 47 +++- src/pages/mine/support/support-runtime.ts | 17 ++ src/pages/mine/support/support.api.ts | 74 +++++- src/pages/mine/support/support.scss | 16 +- src/pages/mine/support/support.types.ts | 15 +- src/pages/mine/support/support.utils.ts | 39 +++- src/pages/mine/support/use-support.ts | 96 +++++++- src/static/图片.png | Bin 0 -> 4034 bytes src/static/组 98@2x.png | Bin 0 -> 681 bytes src/utils/api-flow-token.ts | 46 ++++ src/utils/static-assets.ts | 9 +- tmp-fetched-openapi.json | 1 + tmp-live-api.json | 1 + tmp-live-now.json | 1 + tmp-live-openapi.json | 1 + 25 files changed, 744 insertions(+), 71 deletions(-) create mode 100644 src/pages/mine/storage/storage.utils.ts create mode 100644 src/static/图片.png create mode 100644 src/static/组 98@2x.png create mode 100644 src/utils/api-flow-token.ts create mode 100644 tmp-fetched-openapi.json create mode 100644 tmp-live-api.json create mode 100644 tmp-live-now.json create mode 100644 tmp-live-openapi.json diff --git a/.env.example b/.env.example index a516cec..7acbbde 100644 --- a/.env.example +++ b/.env.example @@ -18,6 +18,9 @@ VITE_MESSAGE_WS_URL=ws://192.168.0.222:8082/app/ws/message # RuoYi-Vue-Plus App 端 clientId(请求头 clientId) VITE_API_CLIENT_ID=428a8310cd442757ae699df5d894f051 +# H5 唤起 App 的 URL Scheme(与 App 打包 scheme 一致,默认 czzj) +# VITE_APP_SCHEME=czzj + # Z2 BLE UUID(嵌入式文档默认 FFF2 写 / FFF1 通知,已内置,一般无需配置) # VITE_BLE_SERVICE_UUID=0000ffe0-0000-1000-8000-00805f9b34fb # VITE_BLE_WRITE_CHAR_UUID=0000fff2-0000-1000-8000-00805f9b34fb diff --git a/src/api/auth.ts b/src/api/auth.ts index ffe1858..b10b36b 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -1,22 +1,23 @@ import { request } from './request' import type { AuthTokenResult, SendSmsCodeBody, SmsCodeScene } from '@/types/api' import { ApiError } from '@/utils/api-error' +import { extractFlowToken } from '@/utils/api-flow-token' import { extractAuthToken, saveStoredToken } from '@/utils/auth-token' export type SendSmsCodeOptions = Pick -/** POST /app/auth/sendSmsCode */ +/** POST /app/auth/sendSmsCode — 换绑新号场景可能在响应 msg 中返回新的 changeToken */ export function sendSmsCode(phone: string, scene: SmsCodeScene, options?: SendSmsCodeOptions) { const data: SendSmsCodeBody = { phonenumber: phone.trim(), scene, ...(options?.changeToken ? { changeToken: options.changeToken.trim() } : {}), } - return request({ + return request({ url: '/app/auth/sendSmsCode', method: 'POST', data, - }) + }).then((payload) => extractFlowToken(payload) || undefined) } export function saveAuthToken(data: AuthTokenResult | unknown) { diff --git a/src/hooks/use-sms-code.ts b/src/hooks/use-sms-code.ts index 92fbe21..e38210f 100644 --- a/src/hooks/use-sms-code.ts +++ b/src/hooks/use-sms-code.ts @@ -7,6 +7,8 @@ import { useCountdown } from './use-countdown' export interface UseSmsCodeOptions { /** 换绑新手机号发码时携带第一步返回的 changeToken */ getChangeToken?: () => string | undefined + /** 发码接口在 msg 中返回新 changeToken 时回调 */ + onChangeToken?: (token: string) => void } export function useSmsCode(scene: SmsCodeScene, options?: UseSmsCodeOptions) { @@ -25,7 +27,10 @@ export function useSmsCode(scene: SmsCodeScene, options?: UseSmsCodeOptions) { return } try { - await sendSmsCode(phone, scene, changeToken ? { changeToken } : undefined) + const nextToken = await sendSmsCode(phone, scene, changeToken ? { changeToken } : undefined) + if (nextToken && scene === SMS_CODE_SCENE.changePhoneNew) { + options?.onChangeToken?.(nextToken) + } uni.showToast({ title: '验证码已发送', icon: 'none' }) start() } catch (err) { diff --git a/src/pages/mine/change-phone/bind/use-change-phone-bind.ts b/src/pages/mine/change-phone/bind/use-change-phone-bind.ts index 2bed53b..8ed7ea1 100644 --- a/src/pages/mine/change-phone/bind/use-change-phone-bind.ts +++ b/src/pages/mine/change-phone/bind/use-change-phone-bind.ts @@ -14,6 +14,9 @@ export function useChangePhoneBind() { const { counting, seconds, sendCode } = useSmsCode(SMS_CODE_SCENE.changePhoneNew, { getChangeToken: () => changeToken.value, + onChangeToken: (token) => { + changeToken.value = token + }, }) onLoad((query) => { diff --git a/src/pages/mine/change-phone/change-phone.api.ts b/src/pages/mine/change-phone/change-phone.api.ts index b8aef48..9800a88 100644 --- a/src/pages/mine/change-phone/change-phone.api.ts +++ b/src/pages/mine/change-phone/change-phone.api.ts @@ -1,4 +1,6 @@ import { request } from '@/api/request' +import { ApiError } from '@/utils/api-error' +import { extractFlowToken } from '@/utils/api-flow-token' import type { ChangePhoneBindForm, ChangePhoneVerifyForm, @@ -6,24 +8,23 @@ import type { VerifyOldPhoneBody, } from './change-phone.types' -type VerifyOldPhoneData = string | { changeToken: string } - -function normalizeChangeToken(data: VerifyOldPhoneData): string { - if (typeof data === 'string') return data - return data.changeToken -} - /** POST /app/auth/changePhone/verifyOld — 校验原手机号验证码,返回换绑令牌 */ export function verifyOldPhone(data: ChangePhoneVerifyForm) { const body: VerifyOldPhoneBody = { oldPhonenumber: data.phone.trim(), oldSmsCode: data.code.trim(), } - return request({ + return request({ url: '/app/auth/changePhone/verifyOld', method: 'POST', data: body, - }).then(normalizeChangeToken) + }).then((payload) => { + const changeToken = extractFlowToken(payload) + if (!changeToken) { + throw new ApiError('验证失败,未获取换绑令牌') + } + return changeToken + }) } /** POST /app/auth/changePhone/verifyNew — 校验新手机号验证码并完成换绑 */ diff --git a/src/pages/mine/storage/index.vue b/src/pages/mine/storage/index.vue index 83090e7..86b8d1e 100644 --- a/src/pages/mine/storage/index.vue +++ b/src/pages/mine/storage/index.vue @@ -6,7 +6,13 @@ - + + + 储存空间需在手机「虫子智家」App 中查看与管理,浏览器无法读取本机占用。 + + + + {{ usedPercent }}% @@ -35,11 +41,25 @@ - - - 一键清理全部缓存({{ totalCacheMb }}MB) - - + + @@ -54,7 +74,11 @@ const { usedPercent, totalCacheMb, ringStyle, + requiresApp, + clearing, onClearAll, + onOpenSystemSettings, + onOpenMobileApp, } = useStorage() function formatSize(sizeMb: number): string { diff --git a/src/pages/mine/storage/storage.scss b/src/pages/mine/storage/storage.scss index ce9602f..3f26f2a 100644 --- a/src/pages/mine/storage/storage.scss +++ b/src/pages/mine/storage/storage.scss @@ -14,6 +14,20 @@ padding: 8rpx 24rpx calc(32rpx + env(safe-area-inset-bottom)); } +.storage-page__tip { + margin-bottom: 24rpx; + padding: 20rpx 24rpx; + border-radius: 16rpx; + background: #fff8e6; + border: 1rpx solid #ffe4a8; +} + +.storage-page__tip-text { + font-size: 26rpx; + color: #8a6d3b; + line-height: 1.5; +} + .storage-page__section-label { display: block; padding: 8rpx 8rpx 24rpx; @@ -111,6 +125,9 @@ .storage-page__footer { padding: 0 8rpx; + display: flex; + flex-direction: column; + gap: 20rpx; } .storage-page__clear-btn { @@ -123,8 +140,31 @@ justify-content: center; } +.storage-page__clear-btn--disabled { + opacity: 0.45; +} + .storage-page__clear-text { font-size: 30rpx; color: #1a1a1a; line-height: 1.3; } + +.storage-page__settings-btn { + height: 88rpx; + border-radius: 20rpx; + background: #f5f7fa; + display: flex; + align-items: center; + justify-content: center; +} + +.storage-page__settings-btn--disabled { + opacity: 0.45; +} + +.storage-page__settings-text { + font-size: 28rpx; + color: #3d5a80; + line-height: 1.3; +} diff --git a/src/pages/mine/storage/storage.utils.ts b/src/pages/mine/storage/storage.utils.ts new file mode 100644 index 0000000..67eb958 --- /dev/null +++ b/src/pages/mine/storage/storage.utils.ts @@ -0,0 +1,214 @@ +import type { StorageCacheItem } from './storage.types' + +const TOKEN_STORAGE_KEY = 'token' +const STORAGE_APP_PATH = '/pages/mine/storage/index' + +export interface StorageOverview { + usedPercent: number + items: StorageCacheItem[] + totalCacheMb: number + /** 是否需在手机 App 端操作(H5 等) */ + requiresApp: boolean +} + +function kbToMb(kb: number): number { + return Math.round((kb / 1024) * 10) / 10 +} + +function bytesToMb(bytes: number): number { + return Math.round((bytes / (1024 * 1024)) * 10) / 10 +} + +export function isH5Platform(): boolean { + try { + const { uniPlatform } = uni.getSystemInfoSync() + return uniPlatform === 'web' || uniPlatform === 'h5' + } catch { + return typeof window !== 'undefined' + } +} + +export function isStorageManagedOnDevice(): boolean { + if (isH5Platform()) return false + try { + const { uniPlatform } = uni.getSystemInfoSync() + return uniPlatform === 'app' || uniPlatform === 'app-plus' + } catch { + return false + } +} + +function resolveAppDeepLink(path = STORAGE_APP_PATH): string { + const scheme = (import.meta.env.VITE_APP_SCHEME as string | undefined)?.trim() || 'czzj' + const normalized = path.replace(/^\//, '') + return `${scheme}://${normalized}` +} + +/** H5:尝试唤起已安装的「虫子智家」App 并打开储存空间页 */ +export function openMobileAppForStorage(): boolean { + if (!isH5Platform()) return false + const url = resolveAppDeepLink() + // #ifdef H5 + if (typeof window !== 'undefined') { + window.location.href = url + return true + } + // #endif + return false +} + +function readLocalStorageKb(): { currentKb: number; limitKb: number } { + try { + const info = uni.getStorageInfoSync() + return { + currentKb: Number(info.currentSize) || 0, + limitKb: Number(info.limitSize) || 0, + } + } catch { + return { currentKb: 0, limitKb: 0 } + } +} + +function measureAppFileCacheBytes(): Promise { + return new Promise((resolve) => { + // #ifdef APP-PLUS + if (typeof plus !== 'undefined' && plus.cache?.calculate) { + plus.cache.calculate((size: number) => { + resolve(Number(size) || 0) + }) + return + } + // #endif + resolve(0) + }) +} + +function buildItemPercents(items: StorageCacheItem[]): StorageCacheItem[] { + const total = items.reduce((sum, item) => sum + item.sizeMb, 0) + if (total <= 0) { + return items.map((item) => ({ ...item, percent: 0 })) + } + return items.map((item) => ({ + ...item, + percent: Math.min(100, Math.round((item.sizeMb / total) * 100)), + })) +} + +/** 读取本机 App 储存占用 */ +export async function fetchStorageOverview(): Promise { + const requiresApp = !isStorageManagedOnDevice() + if (requiresApp) { + return { + usedPercent: 0, + items: [], + totalCacheMb: 0, + requiresApp: true, + } + } + + const { currentKb, limitKb } = readLocalStorageKb() + const fileCacheBytes = await measureAppFileCacheBytes() + const localMb = kbToMb(currentKb) + const fileCacheMb = bytesToMb(fileCacheBytes) + + const items: StorageCacheItem[] = [] + if (fileCacheMb > 0) { + items.push({ id: 'file-cache', label: '图片与文件缓存', sizeMb: fileCacheMb, percent: 0 }) + } + items.push({ id: 'local-data', label: '应用本地数据', sizeMb: localMb, percent: 0 }) + + const totalCacheMb = Math.round((localMb + fileCacheMb) * 10) / 10 + const usedPercent = + limitKb > 0 + ? Math.min(100, Math.round((currentKb / limitKb) * 100)) + : totalCacheMb > 0 + ? 1 + : 0 + + return { + usedPercent, + items: buildItemPercents(items), + totalCacheMb, + requiresApp: false, + } +} + +function clearLocalCacheExceptAuth(): void { + try { + const { keys = [] } = uni.getStorageInfoSync() + for (const key of keys) { + if (key === TOKEN_STORAGE_KEY) continue + uni.removeStorageSync(key) + } + } catch { + // ignore + } +} + +function clearAppFileCache(): Promise { + return new Promise((resolve, reject) => { + // #ifdef APP-PLUS + if (typeof plus !== 'undefined' && plus.cache?.clear) { + plus.cache.clear( + () => resolve(), + (err: { message?: string }) => { + reject(new Error(err?.message || '清理失败')) + }, + ) + return + } + // #endif + resolve() + }) +} + +/** 清理可安全删除的缓存(保留登录令牌) */ +export async function clearDeviceCache(): Promise { + if (!isStorageManagedOnDevice()) { + throw new Error('请在手机 App 中清理储存空间') + } + await clearAppFileCache() + clearLocalCacheExceptAuth() +} + +/** 打开系统「应用详情 / 储存」设置(仅 App) */ +export function openSystemAppStorageSettings(): boolean { + if (!isStorageManagedOnDevice()) { + return false + } + + // #ifdef APP-PLUS + try { + const platform = uni.getSystemInfoSync().platform + if (platform === 'ios') { + plus.runtime.openURL('app-settings:') + return true + } + if (platform === 'android') { + const main = plus.android.runtimeMainActivity() + const Intent = plus.android.importClass('android.content.Intent') as { + new (action: string): PlusAndroidInstanceObject + } + const Settings = plus.android.importClass('android.provider.Settings') as { + ACTION_APPLICATION_DETAILS_SETTINGS: string + } + const Uri = plus.android.importClass('android.net.Uri') as { + fromParts: (scheme: string, path: string, fragment: string | null) => PlusAndroidInstanceObject + } + const intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + const uri = Uri.fromParts('package', main.getPackageName(), null) + intent.setData(uri) + main.startActivity(intent) + return true + } + } catch { + return false + } + // #endif + + return false +} + +export function getStorageAppDeepLink(): string { + return resolveAppDeepLink() +} diff --git a/src/pages/mine/storage/use-storage.ts b/src/pages/mine/storage/use-storage.ts index 45454fd..1155f5b 100644 --- a/src/pages/mine/storage/use-storage.ts +++ b/src/pages/mine/storage/use-storage.ts @@ -1,20 +1,22 @@ +import { onShow } from '@dcloudio/uni-app' import { computed, ref } from 'vue' +import { getErrorMessage } from '@/utils/api-error' import type { StorageCacheItem } from './storage.types' - -const DEFAULT_ITEMS: StorageCacheItem[] = [ - { id: 'image', label: '图片缓存', sizeMb: 12.8, percent: 72 }, - { id: 'doc', label: '文档数据', sizeMb: 12.8, percent: 58 }, - { id: 'health', label: '健康数据', sizeMb: 12.8, percent: 45 }, - { id: 'video', label: '视频缓存', sizeMb: 12.8, percent: 36 }, - { id: 'other', label: '其他数据', sizeMb: 12.8, percent: 28 }, -] - -const USAGE_PERCENT = 25 +import { + clearDeviceCache, + fetchStorageOverview, + getStorageAppDeepLink, + isStorageManagedOnDevice, + openMobileAppForStorage, + openSystemAppStorageSettings, +} from './storage.utils' export function useStorage() { - const items = ref(DEFAULT_ITEMS.map((item) => ({ ...item }))) - - const usedPercent = ref(USAGE_PERCENT) + const items = ref([]) + const usedPercent = ref(0) + const requiresApp = ref(!isStorageManagedOnDevice()) + const loading = ref(false) + const clearing = ref(false) const totalCacheMb = computed(() => { const sum = items.value.reduce((acc, item) => acc + item.sizeMb, 0) @@ -25,28 +27,99 @@ export function useStorage() { background: `conic-gradient(#6ea8ff 0% ${usedPercent.value}%, #e8ecf3 ${usedPercent.value}% 100%)`, })) - function onClearAll() { + async function refresh() { + loading.value = true + try { + const overview = await fetchStorageOverview() + requiresApp.value = overview.requiresApp + items.value = overview.items + usedPercent.value = overview.usedPercent + } catch (err) { + uni.showToast({ title: getErrorMessage(err, '读取储存信息失败'), icon: 'none' }) + } finally { + loading.value = false + } + } + + function promptOpenMobileApp() { uni.showModal({ - title: '清理缓存', - content: `确定清理全部缓存(${totalCacheMb.value}MB)吗?`, + title: '储存空间', + content: + '查看与清理储存空间需在手机「虫子智家」App 中进行。若未自动跳转,请手动打开 App 后进入「我的 → 储存空间」。', + confirmText: '打开手机 App', + cancelText: '知道了', success: (res) => { if (!res.confirm) return - items.value = items.value.map((item) => ({ - ...item, - sizeMb: 0, - percent: 0, - })) - usedPercent.value = 0 - uni.showToast({ title: '清理完成', icon: 'success' }) + const opened = openMobileAppForStorage() + if (!opened) { + uni.setClipboardData({ + data: getStorageAppDeepLink(), + success: () => { + uni.showToast({ title: '已复制打开链接', icon: 'none' }) + }, + }) + } }, }) } + function onClearAll() { + if (requiresApp.value) { + promptOpenMobileApp() + return + } + if (totalCacheMb.value <= 0) { + uni.showToast({ title: '暂无可清理缓存', icon: 'none' }) + return + } + + uni.showModal({ + title: '清理缓存', + content: `确定清理全部缓存(约 ${totalCacheMb.value}MB)吗?登录状态将保留。`, + success: (res) => { + if (!res.confirm) return + void (async () => { + clearing.value = true + try { + await clearDeviceCache() + uni.showToast({ title: '清理完成', icon: 'success' }) + await refresh() + } catch (err) { + uni.showToast({ title: getErrorMessage(err, '清理失败'), icon: 'none' }) + } finally { + clearing.value = false + } + })() + }, + }) + } + + function onOpenSystemSettings() { + if (requiresApp.value) { + promptOpenMobileApp() + return + } + const ok = openSystemAppStorageSettings() + if (!ok) { + uni.showToast({ title: '当前环境无法打开系统设置', icon: 'none' }) + } + } + + onShow(() => { + void refresh() + }) + return { items, usedPercent, totalCacheMb, ringStyle, + requiresApp, + loading, + clearing, onClearAll, + onOpenSystemSettings, + onOpenMobileApp: promptOpenMobileApp, + refresh, } } diff --git a/src/pages/mine/support/cs-chat-socket.ts b/src/pages/mine/support/cs-chat-socket.ts index 0ff70e5..85d7489 100644 --- a/src/pages/mine/support/cs-chat-socket.ts +++ b/src/pages/mine/support/cs-chat-socket.ts @@ -208,8 +208,8 @@ export function connectCsChatSocket(sessionId: string) { if (previousTask) { try { previousTask.close({}) - } catch { - // ignore + } catch { + // ignore } } diff --git a/src/pages/mine/support/index.vue b/src/pages/mine/support/index.vue index a49b3c4..eb3c1ff 100644 --- a/src/pages/mine/support/index.vue +++ b/src/pages/mine/support/index.vue @@ -120,7 +120,11 @@ - 🎧 + 转换人工客服 分配中 @@ -200,12 +204,35 @@ - - + + {{ item.role === 'bot' ? '智' : humanAvatarText }} + + + + + + + + 我 - 我 @@ -265,10 +292,10 @@