HarmonyOSBaseLibs/src/main/ets/utils/ToolsHelper.ets

357 行
9.5 KiB
Plaintext

2024-05-07 17:41:21 +08:00
import promptAction from '@ohos.promptAction';
2024-10-12 18:45:02 +08:00
import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
2024-10-14 16:02:55 +08:00
import { HashMap } from '@kit.ArkTS';
2024-10-12 18:45:02 +08:00
import { DeviceInfo } from '../bean/DeviceInfo';
2024-10-15 17:39:02 +08:00
import { common } from '@kit.AbilityKit';
2024-10-20 17:59:30 +08:00
import { md5_hex } from '../util/md5';
2024-05-07 17:41:21 +08:00
export interface Btn {
text?: string | Resource;
color?: string | Resource;
2024-10-20 17:59:30 +08:00
onClick?: () => void
2024-05-07 17:41:21 +08:00
}
export interface AlertOptions {
2024-10-21 19:54:10 +08:00
title?: string,
msg?: string,
2024-10-20 18:25:55 +08:00
action?: Btn
2024-05-07 17:41:21 +08:00
}
export interface ConfirmOptions {
title?: string
msg?: string
2024-10-20 17:59:30 +08:00
confirm?: Btn
cancel?: Btn
2024-05-07 17:41:21 +08:00
}
export interface ListOptions<T> {
title?: string
cancel?: Btn
values: Array<T>
onSelected: (index: number, value: T) => void
onError?: (msg: string) => void
}
interface ListItem {
content: string
}
@Builder
2024-10-22 12:00:46 +08:00
function customDialogBuilder<T>(option: ListOptions<T>, dialogTag: string) {
2024-05-07 17:41:21 +08:00
Column() {
Text(option.title)
.fontSize(13)
.textAlign(TextAlign.Center)
.width('60%')
.maxLines(2)
.ellipsisMode(EllipsisMode.END)
.textOverflow({
overflow: TextOverflow.Ellipsis
})
.visibility(option.title ? Visibility.Visible : Visibility.None)
List({ space: 20, initialIndex: 0 }) {
ForEach(option.values, (item: T, index: number) => {
ListItem() {
Text(typeof item === "string" ? item : (item as ListItem).content)
.width('100%')
.fontSize(16)
.textAlign(TextAlign.Center)
.onClick(() => {
2024-10-21 19:54:10 +08:00
if (ToolsHelper.mapDialog.get(dialogTag)) {
promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogTag))
ToolsHelper.mapDialog.remove(dialogTag)
2024-05-07 17:41:21 +08:00
}
option.onSelected(index, item)
})
}
}, (item: string) => item)
}
.listDirection(Axis.Vertical) // 排列方向
.scrollBar(BarState.Off)
.friction(0.6)
2024-10-12 18:45:02 +08:00
.divider({
strokeWidth: 1,
color: 0xEEEEEE,
startMargin: 20,
endMargin: 20
}) // 每行之间的分界线
2024-05-07 17:41:21 +08:00
.edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
.width('100%')
.height(option.values.length < 8 ? `${option.values.length / 16 * 100}%` : '50%')
.margin({ top: 20 })
2024-10-12 18:45:02 +08:00
}
.padding({
top: 20,
bottom: 20,
left: 20,
right: 20
})
2024-05-07 17:41:21 +08:00
}
2024-10-20 17:59:30 +08:00
interface ThrottleInterface {
startTime: number; //调用的时间
timeoutNumber: number; //setTimeout的句柄
}
2024-05-07 17:41:21 +08:00
/**
* 常用方法
*/
export class ToolsHelper {
2024-10-14 16:02:55 +08:00
/**
* 弹出Toast
* @param msg
*/
static log(...args: ESObject[]) {
const k = ToolsHelper.getStackKey()?.split('/')
2024-10-18 18:38:49 +08:00
console.log(`========>${k ? k[k.length-1].split('.')[0] : ''}::`, args, '\n')
2024-10-14 16:02:55 +08:00
}
2024-05-07 17:41:21 +08:00
/**
* 弹出Toast
* @param msg
*/
static showMessage(msg: string) {
console.info(msg);
promptAction.showToast({
message: msg,
duration: 1500
});
}
2024-10-15 17:39:02 +08:00
2024-10-18 18:38:49 +08:00
private static oTime: number = 0
2024-10-15 17:39:02 +08:00
/**
* 双击退出
* @param msg
*/
2024-10-18 18:38:49 +08:00
static doubleAndExit(event?: () => void) {
2024-10-15 17:39:02 +08:00
const cTime = new Date().getTime()
console.log('=====>', cTime)
console.log('=====>', cTime - ToolsHelper.oTime)
if (cTime - ToolsHelper.oTime > 800) {
ToolsHelper.oTime = cTime
ToolsHelper.showMessage('双击退出')
} else {
2024-10-18 10:01:31 +08:00
if (event) {
event()
2024-10-18 18:38:49 +08:00
} else {
2024-10-18 10:01:31 +08:00
(getContext() as common.UIAbilityContext).terminateSelf()
// getContext().getApplicationContext().killAllProcesses();
}
2024-10-15 17:39:02 +08:00
}
return true
}
2024-05-07 17:41:21 +08:00
/**kio9
* 弹出Alert弹窗
* @param options
*/
2024-10-21 19:54:10 +08:00
static showAlertDialog(options: AlertOptions) {
2024-05-07 17:41:21 +08:00
try {
promptAction.showDialog({
2024-10-21 19:54:10 +08:00
alignment: DialogAlignment.Center,
title: options.title,
message: options.msg,
2024-05-07 17:41:21 +08:00
buttons: [{
2024-10-21 19:54:10 +08:00
text: options.action?.text ?? "确定",
color: options.action?.color ?? "#000000",
2024-05-07 17:41:21 +08:00
}]
})
.then(() => {
2024-10-21 19:54:10 +08:00
options.action?.onClick && options.action?.onClick()
2024-05-07 17:41:21 +08:00
})
.catch((err: Error) => {
ToolsHelper.showMessage(err.message)
})
} catch (error) {
let message = (error as BusinessError).message
ToolsHelper.showMessage(message)
}
}
/**
* 弹出Confirm弹窗
* @param options
*/
static showConfirmDialog(options: ConfirmOptions): Promise<void> {
return new Promise((reject) => {
try {
promptAction.showDialog({
alignment: 1,
title: options.title,
message: options.msg,
buttons: [{
text: options.confirm?.text ?? "确定",
color: options.confirm?.color ?? "#000000",
}, {
text: options.cancel?.text ?? "取消",
color: options.cancel?.color ?? "#666666",
}]
2024-05-07 17:41:21 +08:00
})
.then((data) => {
if (data.index === 0) {
options.confirm?.onClick && options.confirm.onClick()
} else {
options.cancel?.onClick && options.cancel.onClick()
}
})
.catch((err: Error) => {
ToolsHelper.showMessage(err.message)
})
} catch (error) {
reject()
let message = (error as BusinessError).message
ToolsHelper.showMessage(message)
}
})
2024-05-07 17:41:21 +08:00
}
2024-10-21 19:54:10 +08:00
public static mapDialog = new HashMap<string, number>()
2024-05-07 17:41:21 +08:00
/**
* 弹出List弹窗
* @param options values 如果是非string列表的话,需要存在content字段
*/
static showListDialog<T = string>(options: ListOptions<T>, p: object) {
let isSuccess: Array<number> = []
options.values.forEach((item, index) => {
if (typeof item !== 'string') {
if (!(item as ListItem).content) {
isSuccess.push(index)
}
}
})
if (isSuccess.length > 0) {
options.onError && options.onError(`第(${isSuccess.join("、")})个数据中,没有content字段。`)
} else {
2024-10-21 19:54:10 +08:00
const dialogTag = ToolsHelper.getUuid()
2024-05-07 17:41:21 +08:00
promptAction.openCustomDialog({
alignment: 1,
builder: customDialogBuilder.bind(p, options, dialogTag)
}).then((dialogId: number) => {
ToolsHelper.mapDialog.set(dialogTag, dialogId)
})
}
}
/**
* 弹出自定义弹窗
* @param alignment 弹窗在竖直方向上的对齐方式
*/
2024-10-21 19:54:10 +08:00
static showCustomDialog(dialogTag: string, b: CustomBuilder, alignment?: DialogAlignment) {
2024-05-07 17:41:21 +08:00
promptAction.openCustomDialog({
alignment: alignment ?? DialogAlignment.Center,
builder: b
}).then((dialogId: number) => {
ToolsHelper.mapDialog.set(dialogTag, dialogId)
}).catch((error: Error) => {
console.log('>>>>>', JSON.stringify(error))
})
}
2024-10-10 20:01:58 +08:00
2024-10-21 19:54:10 +08:00
/**
* 关闭自定义弹窗
* @param dialogTag 开启时的tag
*/
static closeCustomDialog(dialogTag: string) {
if (ToolsHelper.mapDialog.get(dialogTag)) {
promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogTag))
ToolsHelper.mapDialog.remove(dialogTag)
}
}
static encryptStringToNumber(str: string): number {
let result = 0;
for (let i = 0; i < str.length; i++) {
result = result * 256 + str.charCodeAt(i);
}
return result;
}
static decryptNumberToString(num: number): string {
let result = '';
while (num > 0) {
result = String.fromCharCode(num & 255) + result;
num >>>= 8;
}
return result.replace(/[\s\0]+$/, ''); // 移除结尾的空白字符
}
2024-10-10 20:01:58 +08:00
/**
* 获取调用栈第一个类
*/
static getStackKey() {
let stack = new Error().stack
if (stack) {
let list = JSON.stringify(stack).split('\\n')
let a = list[list.length-2].split(':')[0].split('(')[1]
return a
}
return undefined
}
2024-10-12 18:45:02 +08:00
private static deviceInfo: DeviceInfo
2024-10-14 16:02:55 +08:00
/**
* 获取设备信息
* @returns {@link DeviceInfo}
*/
2024-10-12 18:45:02 +08:00
static getDeviceInfo() {
if (!ToolsHelper.deviceInfo) {
ToolsHelper.deviceInfo = new DeviceInfo()
ToolsHelper.deviceInfo.ODID = deviceInfo.ODID
ToolsHelper.deviceInfo.manufacture = deviceInfo.manufacture
ToolsHelper.deviceInfo.brand = deviceInfo.brand
ToolsHelper.deviceInfo.osFullName = deviceInfo.osFullName
}
return ToolsHelper.deviceInfo
}
2024-10-20 17:59:30 +08:00
/**
* 防抖动函数,调用后会延迟wait时间执行,当在wait时间内再次对同一函数调用,则会取消之前的定时器,重新定时
* @param fun
* @param wait
*/
static debounceHold(fun: Function, wait: number = 1500) {
let funcValue1 = ToolsHelper.getUniqueId(fun)
let hash = md5_hex(funcValue1)
if (ToolsHelper.setTimeOutMap.get(hash)) {
clearTimeout(ToolsHelper.setTimeOutMap.get(hash)?.timeoutNumber)
ToolsHelper.setTimeOutMap.delete(hash)
}
// ToolsHelper.checkTimeOutNumber()
let timeoutNumber = setTimeout(() => {
ToolsHelper.setTimeOutMap.get(hash) && clearTimeout(ToolsHelper.setTimeOutMap.get(hash)?.timeoutNumber)
ToolsHelper.setTimeOutMap.delete(hash)
// 执行函数调用
fun()
}, wait)
ToolsHelper.setTimeOutMap.set(hash, {
timeoutNumber: timeoutNumber,
startTime: new Date().getTime(),
})
}
private static setTimeOutMap: Map<string, ThrottleInterface> = new Map()
private static uniqueIdMap = new WeakMap<Function, string>();
public static getUuid() {
2024-10-20 17:59:30 +08:00
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
let r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
static getUniqueId(fun: Function): string {
2024-10-20 17:59:30 +08:00
if (!ToolsHelper.uniqueIdMap.has(fun)) {
ToolsHelper.uniqueIdMap.set(fun, ToolsHelper.getUuid());
}
return ToolsHelper.uniqueIdMap.get(fun)!;
}
2024-05-07 17:41:21 +08:00
}