123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- import promptAction from '@ohos.promptAction';
- import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
- import { HashMap } from '@kit.ArkTS';
- import { DeviceInfo } from '../bean/DeviceInfo';
- import { common } from '@kit.AbilityKit';
- export interface Btn {
- text?: string | Resource;
- color?: string | Resource;
- onClick: () => void
- }
- export interface AlertOptions {
- title?: string
- msg?: string
- action: Btn
- }
- export interface ConfirmOptions {
- title?: string
- msg?: string
- confirm: Btn
- cancel: Btn
- }
- 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
- function customDialogBuilder<T>(option: ListOptions<T>, dialogId: number) {
- 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(() => {
- if (ToolsHelper.mapDialog.get(dialogId)) {
- promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogId))
- ToolsHelper.mapDialog.remove(dialogId)
- }
- option.onSelected(index, item)
- })
- }
- }, (item: string) => item)
- }
- .listDirection(Axis.Vertical) // 排列方向
- .scrollBar(BarState.Off)
- .friction(0.6)
- .divider({
- strokeWidth: 1,
- color: 0xEEEEEE,
- startMargin: 20,
- endMargin: 20
- }) // 每行之间的分界线
- .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
- .width('100%')
- .height(option.values.length < 8 ? `${option.values.length / 16 * 100}%` : '50%')
- .margin({ top: 20 })
- }
- .padding({
- top: 20,
- bottom: 20,
- left: 20,
- right: 20
- })
- }
- /**
- * 常用方法
- */
- export class ToolsHelper {
- /**
- * 弹出Toast
- * @param msg
- */
- static log(...args: ESObject[]) {
- const k = ToolsHelper.getStackKey()?.split('/')
- console.log(`========>${k?k[k.length-1].split('.')[0]:''}::`, args,'\n')
- }
- /**
- * 弹出Toast
- * @param msg
- */
- static showMessage(msg: string) {
- console.info(msg);
- promptAction.showToast({
- message: msg,
- duration: 1500
- });
- }
- private static oTime: number = 0
- /**
- * 双击退出
- * @param msg
- */
- static doubleAndExit() {
- const cTime = new Date().getTime()
- console.log('=====>', cTime)
- console.log('=====>', cTime - ToolsHelper.oTime)
- if (cTime - ToolsHelper.oTime > 800) {
- ToolsHelper.oTime = cTime
- ToolsHelper.showMessage('双击退出')
- } else {
- // getContext().getApplicationContext().killAllProcesses();
- (getContext() as common.UIAbilityContext).terminateSelf()
- }
- return true
- }
- /**kio9
- * 弹出Alert弹窗
- * @param options
- */
- static showAlertDialog(options: AlertOptions) {
- try {
- promptAction.showDialog({
- alignment: 1,
- title: options.title,
- message: options.msg,
- buttons: [{
- text: options.action.text ?? "确定",
- color: options.action.color ?? "#000000",
- }]
- })
- .then(() => {
- options.action.onClick()
- })
- .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) {
- 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",
- }]
- })
- .then((data) => {
- if (data.index === 0) {
- options.confirm.onClick()
- } else {
- options.cancel.onClick()
- }
- })
- .catch((err: Error) => {
- ToolsHelper.showMessage(err.message)
- })
- } catch (error) {
- let message = (error as BusinessError).message
- ToolsHelper.showMessage(message)
- }
- }
- public static mapDialog = new HashMap<number, number>()
- /**
- * 弹出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 {
- const dialogTag = new Date().getTime()
- promptAction.openCustomDialog({
- alignment: 1,
- builder: customDialogBuilder.bind(p, options, dialogTag)
- }).then((dialogId: number) => {
- ToolsHelper.mapDialog.set(dialogTag, dialogId)
- })
- }
- }
- /**
- * 弹出自定义弹窗
- * @param alignment 弹窗在竖直方向上的对齐方式
- */
- static showCustomDialog(b: CustomBuilder, alignment?: DialogAlignment) {
- const dialogTag = new Date().getTime()
- promptAction.openCustomDialog({
- alignment: alignment ?? DialogAlignment.Center,
- builder: b
- }).then((dialogId: number) => {
- ToolsHelper.mapDialog.set(dialogTag, dialogId)
- }).catch((error: Error) => {
- console.log('>>>>>', JSON.stringify(error))
- })
- }
- /**
- * 获取调用栈第一个类
- */
- 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
- }
- private static deviceInfo: DeviceInfo
- /**
- * 获取设备信息
- * @returns {@link DeviceInfo}
- */
- 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
- }
- }
|