ToolsHelper.ets 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import promptAction from '@ohos.promptAction';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { HashMap } from '@kit.ArkTS';
  4. export interface Btn {
  5. text?: string | Resource;
  6. color?: string | Resource;
  7. onClick: () => void
  8. }
  9. export interface AlertOptions {
  10. title?: string
  11. msg?: string
  12. action: Btn
  13. }
  14. export interface ConfirmOptions {
  15. title?: string
  16. msg?: string
  17. confirm: Btn
  18. cancel: Btn
  19. }
  20. export interface ListOptions<T> {
  21. title?: string
  22. cancel?: Btn
  23. values: Array<T>
  24. onSelected: (index: number, value: T) => void
  25. onError?: (msg: string) => void
  26. }
  27. interface ListItem {
  28. content: string
  29. }
  30. @Builder
  31. function customDialogBuilder<T>(option: ListOptions<T>, dialogId: number) {
  32. Column() {
  33. Text(option.title)
  34. .fontSize(13)
  35. .textAlign(TextAlign.Center)
  36. .width('60%')
  37. .maxLines(2)
  38. .ellipsisMode(EllipsisMode.END)
  39. .textOverflow({
  40. overflow: TextOverflow.Ellipsis
  41. })
  42. .visibility(option.title ? Visibility.Visible : Visibility.None)
  43. List({ space: 20, initialIndex: 0 }) {
  44. ForEach(option.values, (item: T, index: number) => {
  45. ListItem() {
  46. Text(typeof item === "string" ? item : (item as ListItem).content)
  47. .width('100%')
  48. .fontSize(16)
  49. .textAlign(TextAlign.Center)
  50. .onClick(() => {
  51. if (ToolsHelper.mapDialog.get(dialogId)) {
  52. promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogId))
  53. ToolsHelper.mapDialog.remove(dialogId)
  54. }
  55. option.onSelected(index, item)
  56. })
  57. }
  58. }, (item: string) => item)
  59. }
  60. .listDirection(Axis.Vertical) // 排列方向
  61. .scrollBar(BarState.Off)
  62. .friction(0.6)
  63. .divider({ strokeWidth: 1, color: 0xEEEEEE, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
  64. .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
  65. .width('100%')
  66. .height(option.values.length < 8 ? `${option.values.length / 16 * 100}%` : '50%')
  67. .margin({ top: 20 })
  68. }.padding({ top: 20, bottom: 20, left: 20, right: 20 })
  69. }
  70. /**
  71. * 常用方法
  72. */
  73. export class ToolsHelper {
  74. /**
  75. * 弹出Toast
  76. * @param msg
  77. */
  78. static showMessage(msg: string) {
  79. console.info(msg);
  80. promptAction.showToast({
  81. message: msg,
  82. duration: 1500
  83. });
  84. }
  85. /**kio9
  86. * 弹出Alert弹窗
  87. * @param options
  88. */
  89. static showAlertDialog(options: AlertOptions) {
  90. try {
  91. promptAction.showDialog({
  92. alignment: 1,
  93. title: options.title,
  94. message: options.msg,
  95. buttons: [{
  96. text: options.action.text ?? "确定",
  97. color: options.action.color ?? "#000000",
  98. }]
  99. })
  100. .then(() => {
  101. options.action.onClick()
  102. })
  103. .catch((err: Error) => {
  104. ToolsHelper.showMessage(err.message)
  105. })
  106. } catch (error) {
  107. let message = (error as BusinessError).message
  108. ToolsHelper.showMessage(message)
  109. }
  110. }
  111. /**
  112. * 弹出Confirm弹窗
  113. * @param options
  114. */
  115. static showConfirmDialog(options: ConfirmOptions) {
  116. try {
  117. promptAction.showDialog({
  118. alignment: 1,
  119. title: options.title,
  120. message: options.msg,
  121. buttons: [{
  122. text: options.confirm.text ?? "确定",
  123. color: options.confirm.color ?? "#000000",
  124. }, {
  125. text: options.cancel.text ?? "取消",
  126. color: options.cancel.color ?? "#666666",
  127. }]
  128. })
  129. .then((data) => {
  130. if (data.index === 0) {
  131. options.confirm.onClick()
  132. } else {
  133. options.cancel.onClick()
  134. }
  135. })
  136. .catch((err: Error) => {
  137. ToolsHelper.showMessage(err.message)
  138. })
  139. } catch (error) {
  140. let message = (error as BusinessError).message
  141. ToolsHelper.showMessage(message)
  142. }
  143. }
  144. public static mapDialog = new HashMap<number, number>()
  145. /**
  146. * 弹出List弹窗
  147. * @param options values 如果是非string列表的话,需要存在content字段
  148. */
  149. static showListDialog<T = string>(options: ListOptions<T>, p: object) {
  150. let isSuccess: Array<number> = []
  151. options.values.forEach((item, index) => {
  152. if (typeof item !== 'string') {
  153. if (!(item as ListItem).content) {
  154. isSuccess.push(index)
  155. }
  156. }
  157. })
  158. if (isSuccess.length > 0) {
  159. options.onError && options.onError(`第(${isSuccess.join("、")})个数据中,没有content字段。`)
  160. } else {
  161. const dialogTag = new Date().getTime()
  162. promptAction.openCustomDialog({
  163. alignment: 1,
  164. builder: customDialogBuilder.bind(p, options, dialogTag)
  165. }).then((dialogId: number) => {
  166. ToolsHelper.mapDialog.set(dialogTag, dialogId)
  167. })
  168. }
  169. }
  170. /**
  171. * 弹出自定义弹窗
  172. * @param alignment 弹窗在竖直方向上的对齐方式
  173. */
  174. static showCustomDialog(b: CustomBuilder, alignment?: DialogAlignment) {
  175. const dialogTag = new Date().getTime()
  176. promptAction.openCustomDialog({
  177. alignment: alignment ?? DialogAlignment.Center,
  178. builder: b
  179. }).then((dialogId: number) => {
  180. ToolsHelper.mapDialog.set(dialogTag, dialogId)
  181. }).catch((error: Error) => {
  182. console.log('>>>>>', JSON.stringify(error))
  183. })
  184. }
  185. /**
  186. * 获取调用栈第一个类
  187. */
  188. static getStackKey() {
  189. let stack = new Error().stack
  190. if (stack) {
  191. let list = JSON.stringify(stack).split('\\n')
  192. let a = list[list.length-2].split(':')[0].split('(')[1]
  193. return a
  194. }
  195. return undefined
  196. }
  197. }