Initial commit of akmon project

This commit is contained in:
2026-01-20 08:04:15 +08:00
commit 77a2bab985
1309 changed files with 343305 additions and 0 deletions

159
utils/utils.uts Normal file
View File

@@ -0,0 +1,159 @@
// 通用 UTSJSONObject 转任意 type 的函数
// UTS 2024
import i18n from '../i18n/index.uts';
/**
* 切换应用语言设置
* @param locale 语言代码,如 'zh-CN' 或 'en-US'
*/
export function switchLocale(locale: string) {
// 设置存储
uni.setStorageSync('uVueI18nLocale', locale);
// 设置 i18n 语言
try {
i18n.global.locale.value = locale;
} catch (err) {
console.error('Failed to switch locale:', err);
}
}
/**
* 获取当前语言设置
* @returns 当前语言代码
*/
export function getCurrentLocale(): string {
const locale = uni.getStorageSync('uVueI18nLocale') as string;
if (locale == null || locale == '') {
return 'zh-CN';
}
return locale;
}
/**
* 确保语言设置正确初始化
*/
export function ensureLocaleInitialized() {
const currentLocale = getCurrentLocale();
if (currentLocale == null || currentLocale == '') {
switchLocale('zh-CN');
}
}
/**
* 将任意错误对象转换为标准的 UniError
* @param error 任意类型的错误对象
* @param defaultMessage 默认错误消息
* @returns 标准化的 UniError 对象
*/
export function toUniError(error: any, defaultMessage: string = '操作失败'): UniError {
// 如果已经是 UniError直接返回
if (error instanceof UniError) {
return error
}
let errorMessage = defaultMessage
let errorCode = -1
try {
// 如果是普通 Error 对象
if (error instanceof Error) {
errorMessage = error.message != null && error.message != '' ? error.message : defaultMessage
}
// 如果是字符串
else if (typeof error === 'string') {
errorMessage = error
} // 如果是对象,尝试提取错误信息
else if (error != null && typeof error === 'object') {
const errorObj = error as UTSJSONObject
let message: string = ''
// 逐个检查字段,避免使用 || 操作符
if (errorObj['message'] != null) {
const msgValue = errorObj['message']
if (typeof msgValue === 'string') {
message = msgValue
}
} else if (errorObj['errMsg'] != null) {
const msgValue = errorObj['errMsg']
if (typeof msgValue === 'string') {
message = msgValue
}
} else if (errorObj['error'] != null) {
const msgValue = errorObj['error']
if (typeof msgValue === 'string') {
message = msgValue
}
} else if (errorObj['details'] != null) {
const msgValue = errorObj['details']
if (typeof msgValue === 'string') {
message = msgValue
}
} else if (errorObj['msg'] != null) {
const msgValue = errorObj['msg']
if (typeof msgValue === 'string') {
message = msgValue
}
}
if (message != '') {
errorMessage = message
}
// 尝试提取错误码
let code: number = 0
if (errorObj['code'] != null) {
const codeValue = errorObj['code']
if (typeof codeValue === 'number') {
code = codeValue
}
} else if (errorObj['errCode'] != null) {
const codeValue = errorObj['errCode']
if (typeof codeValue === 'number') {
code = codeValue
}
} else if (errorObj['status'] != null) {
const codeValue = errorObj['status']
if (typeof codeValue === 'number') {
code = codeValue
}
}
if (code != 0) {
errorCode = code
}
}
} catch (e) {
console.error('Error converting to UniError:', e)
errorMessage = defaultMessage
}
// 创建标准 UniError
const uniError = new UniError('AppError', errorCode, errorMessage)
return uniError
}
/**
* 响应式状态管理
* @returns 响应式状态对象
*/
export function responsiveState() {
const screenInfo = uni.getSystemInfoSync()
const screenWidth = screenInfo.screenWidth
return {
isLargeScreen: screenWidth >= 768,
isSmallScreen: screenWidth < 576,
screenWidth: screenWidth,
cardColumns: screenWidth >= 768 ? 3 : screenWidth >= 576 ? 2 : 1
}
}
/**
* 兼容 UTS Android 的剪贴板写入
* @param text 要写入剪贴板的文本
*/
export function setClipboard(text: string): void {
// #ifdef WEB
uni.setClipboardData({ data: text });
// #endif
}