This commit is contained in:
oneao committed 2025-09-30 17:26:30 +08:00
1 parent 92fbbf7c6d
commit b03e2c1e69
149 files changed
+925 -14628

No files matched your search

@@ -0,0 +1,38 @@
import { toast } from './toast'
export function joinUrl(base: string, path: string): string {
if (!base)
return path || ''
if (!path)
return base || ''
const baseEndsWithSlash = base.endsWith('/')
const pathStartsWithSlash = path.startsWith('/')
if (baseEndsWithSlash && pathStartsWithSlash) {
// 都有斜杠,去掉 base 末尾那个
return base.slice(0, -1) + path
}
else if (!baseEndsWithSlash && !pathStartsWithSlash) {
// 都没有斜杠,补一个
return `${base}/${path}`
}
else {
// 只有一个斜杠,直接拼接
return base + path
}
}
export function checkRequiredParams(options: Record<string, any>, keys: string[]): boolean {
for (const key of keys) {
const value = options[key]
if (!value || value === 'undefined' || value === 'null') {
toast.error(`参数 "${key}" 有误!`)
setTimeout(() => {
uni.navigateBack()
}, 1000)
return false
}
}
return true
}
@@ -0,0 +1,26 @@
/*
* @Author: 菲鸽
* @Date: 2024-03-28 19:13:55
* @Last Modified by: 菲鸽
* @Last Modified time: 2024-03-28 19:24:55
*/
export const platform = __UNI_PLATFORM__
export const isH5 = __UNI_PLATFORM__ === 'h5'
export const isApp = __UNI_PLATFORM__ === 'app'
export const isMp = __UNI_PLATFORM__.startsWith('mp-')
export const isMpWeixin = __UNI_PLATFORM__.startsWith('mp-weixin')
export const isMpAplipay = __UNI_PLATFORM__.startsWith('mp-alipay')
export const isMpToutiao = __UNI_PLATFORM__.startsWith('mp-toutiao')
export const isHarmony = __UNI_PLATFORM__.startsWith('app-harmony')
const PLATFORM = {
platform,
isH5,
isApp,
isMp,
isMpWeixin,
isMpAplipay,
isMpToutiao,
isHarmony,
}
export default PLATFORM
@@ -0,0 +1,28 @@
export function getServerBaseUrl(): string {
const origin = window.location.origin
const publicBase = import.meta.env.VITE_APP_PUBLIC_BASE || '/'
// 拼接时去掉 publicBase 开头和结尾的斜杠(只保留中间路径)
const cleanBase = publicBase.replace(/^\/+|\/+$/g, '')
return cleanBase ? `${origin}/${cleanBase}` : origin
}
export async function getDbConfigs(): Promise<Record<string, any> | null> {
const serverBaseUrl = getServerBaseUrl()
try {
const response = await fetch(`${serverBaseUrl}/static/db.json`)
if (!response.ok) {
console.error('Failed to fetch db.json:', response.status)
return null
}
const data: Record<string, any> = await response.json()
return data
}
catch (error) {
console.error('Error fetching db.json:', error)
return null
}
}
@@ -0,0 +1,38 @@
/* eslint-disable import/no-mutable-exports */
// 获取屏幕边界到安全区域距离
let systemInfo
let safeAreaInsets
// #ifdef MP-WEIXIN
// 微信小程序使用新的API
systemInfo = uni.getWindowInfo()
safeAreaInsets = systemInfo.safeArea
? {
top: systemInfo.safeArea.top,
right: systemInfo.windowWidth - systemInfo.safeArea.right,
bottom: systemInfo.windowHeight - systemInfo.safeArea.bottom,
left: systemInfo.safeArea.left,
}
: null
// #endif
// #ifndef MP-WEIXIN
// 其他平台继续使用uni API
systemInfo = uni.getSystemInfoSync()
safeAreaInsets = systemInfo.safeAreaInsets
// #endif
console.log('systemInfo', systemInfo)
// 微信里面打印
// pixelRatio: 3
// safeArea: {top: 47, left: 0, right: 390, bottom: 810, width: 390, …}
// safeAreaInsets: {top: 47, left: 0, right: 0, bottom: 34}
// screenHeight: 844
// screenTop: 91
// screenWidth: 390
// statusBarHeight: 47
// windowBottom: 0
// windowHeight: 753
// windowTop: 0
// windowWidth: 390
export { safeAreaInsets, systemInfo }
@@ -0,0 +1,65 @@
/**
* toast 弹窗组件
* 支持 success/error/warning/info 四种状态
* 可配置 duration, position 等参数
*/
type ToastType = 'success' | 'error' | 'warning' | 'info'
interface ToastOptions {
type?: ToastType
duration?: number
position?: 'top' | 'middle' | 'bottom'
icon?: 'success' | 'error' | 'none' | 'loading' | 'fail' | 'exception'
message: string
}
export function showToast(options: ToastOptions | string) {
const defaultOptions: ToastOptions = {
type: 'info',
duration: 2000,
position: 'middle',
message: '',
}
const mergedOptions
= typeof options === 'string'
? { ...defaultOptions, message: options }
: { ...defaultOptions, ...options }
// 映射position到uniapp支持的格式
const positionMap: Record<ToastOptions['position'], 'top' | 'bottom' | 'center'> = {
top: 'top',
middle: 'center',
bottom: 'bottom',
}
// 映射图标类型
const iconMap: Record<
ToastType,
'success' | 'error' | 'none' | 'loading' | 'fail' | 'exception'
> = {
success: 'success',
error: 'error',
warning: 'fail',
info: 'none',
}
// 调用uni.showToast显示提示
uni.showToast({
title: mergedOptions.message,
duration: mergedOptions.duration,
position: positionMap[mergedOptions.position],
icon: mergedOptions.icon || iconMap[mergedOptions.type],
mask: true,
})
}
export const toast = {
success: (message: string, options?: Omit<ToastOptions, 'type'>) =>
showToast({ ...options, type: 'success', message }),
error: (message: string, options?: Omit<ToastOptions, 'type'>) =>
showToast({ ...options, type: 'error', message }),
warning: (message: string, options?: Omit<ToastOptions, 'type'>) =>
showToast({ ...options, type: 'warning', message }),
info: (message: string, options?: Omit<ToastOptions, 'type'>) =>
showToast({ ...options, type: 'info', message }),
}