uni-tool.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * 模态框配置选项接口
  3. */
  4. export interface IModalOptions {
  5. /** 标题 */
  6. title?: string;
  7. /** 内容 */
  8. content: string;
  9. /** 是否显示取消按钮 */
  10. showCancel?: boolean;
  11. /** 是否显示确认按钮 */
  12. showConfirm?: boolean;
  13. /** 确认按钮文字 */
  14. confirmText?: string;
  15. /** 取消按钮文字 */
  16. cancelText?: string;
  17. /** 取消按钮颜色 */
  18. cancelColor?: string;
  19. /** 确认按钮颜色 */
  20. confirmColor?: string;
  21. }
  22. /**
  23. * 工具函数接口
  24. */
  25. export interface IeTool {
  26. /** 是否显示加载提示 */
  27. loading: boolean,
  28. /** 加载提示开始时间 */
  29. loadingStartTime: number,
  30. /** 最小显示时间 */
  31. minLoadingTime: number,
  32. /**
  33. * 显示普通提示
  34. * @param title - 提示内容,默认为空字符串
  35. */
  36. showToast(title?: string): void;
  37. /**
  38. * 显示成功提示
  39. * @param title - 提示内容,默认为空字符串
  40. */
  41. showSuccess(title?: string): void;
  42. /**
  43. * 显示错误提示
  44. * @param title - 提示内容,默认为空字符串
  45. */
  46. showError(title?: string): void;
  47. /**
  48. * 显示加载提示
  49. * @param title - 提示内容,默认为空字符串
  50. */
  51. showLoading(title?: string): void;
  52. /**
  53. * 隐藏加载提示
  54. */
  55. hideLoading(): void;
  56. /**
  57. * 显示模态对话框
  58. * @param params - 模态框配置选项
  59. * @returns Promise<boolean> - 用户点击确认返回 true,点击取消返回 false
  60. */
  61. showModal(params: IModalOptions): Promise<boolean>;
  62. /*
  63. * 显示模态对话框, 与showModal的差异在于,确认时命中resolve, 取消时命中reject.
  64. * // NOTE: 没有直接在showModal里改,是因为使用的地方太多,避免直接修改影响太广 2026.1.8
  65. * @param params - 模态框配置选项
  66. * @returns Promise<boolean> - 用户点击确认返回 true,点击取消返回 false
  67. * */
  68. showConfirm(params: IModalOptions): Promise<boolean>;
  69. /**
  70. * 复制内容
  71. * @param content - 要复制的内容
  72. */
  73. copy(content: string): void;
  74. /* 打开网址 */
  75. openBrowser(url: string): void;
  76. }
  77. const defaultModalOptions: IModalOptions = {
  78. title: '',
  79. content: '',
  80. showCancel: true,
  81. showConfirm: true,
  82. cancelColor: '#000000',
  83. confirmColor: '#31a0fc',
  84. cancelText: '取消',
  85. confirmText: '确认'
  86. };
  87. const tool: IeTool = {
  88. loading: false,
  89. loadingStartTime: 0,
  90. minLoadingTime: 500, // 最小显示时间,单位毫秒
  91. showToast(title: string = '') {
  92. // 先立即隐藏,避免上一个 toast存在导致下次的 toast 很快关闭
  93. uni.hideToast();
  94. setTimeout(() => {
  95. uni.showToast({
  96. title,
  97. icon: 'none'
  98. });
  99. }, 50);
  100. },
  101. showSuccess(title: string = '') {
  102. uni.showToast({
  103. title,
  104. icon: 'success'
  105. });
  106. },
  107. showError(title: string = '') {
  108. uni.showToast({
  109. title,
  110. icon: 'error'
  111. });
  112. },
  113. showLoading(title: string = '') {
  114. this.loadingStartTime = Date.now();
  115. uni.showLoading({
  116. title,
  117. mask: true,
  118. success: () => { }
  119. });
  120. },
  121. hideLoading() {
  122. const currentTime = Date.now();
  123. const elapsedTime = currentTime - this.loadingStartTime;
  124. const remainingTime = Math.max(0, this.minLoadingTime - elapsedTime);
  125. if (remainingTime > 0) {
  126. setTimeout(() => {
  127. uni.hideLoading();
  128. }, remainingTime);
  129. } else {
  130. uni.hideLoading();
  131. }
  132. },
  133. showModal(params: IModalOptions) {
  134. const { title, content, showCancel, confirmText, cancelText, cancelColor, confirmColor } = Object.assign(defaultModalOptions, params);
  135. return new Promise((resolve, reject) => {
  136. uni.showModal({
  137. title,
  138. content,
  139. showCancel,
  140. confirmText,
  141. cancelText,
  142. cancelColor,
  143. confirmColor,
  144. success: (res) => {
  145. resolve(res.confirm);
  146. },
  147. fail: reject
  148. });
  149. });
  150. },
  151. showConfirm(params: IModalOptions) {
  152. const { title, content, showCancel, confirmText, cancelText, cancelColor, confirmColor } = Object.assign(defaultModalOptions, params);
  153. return new Promise((resolve, reject) => {
  154. uni.showModal({
  155. title,
  156. content,
  157. showCancel,
  158. confirmText,
  159. cancelText,
  160. cancelColor,
  161. confirmColor,
  162. success: (res) => {
  163. /* 这里不要动,就是这样的。形成await showConfirm的效果 2026.1.13 */
  164. /* 如果有必须接收true/false的写法,请使用showModal */
  165. if(res.confirm)(resolve(true))
  166. else reject(false)
  167. },
  168. fail: reject
  169. });
  170. });
  171. },
  172. copy(content: string) {
  173. uni.setClipboardData({
  174. data: content,
  175. success: () => { }
  176. });
  177. },
  178. openBrowser(url: string) {
  179. // 判断平台
  180. // #ifdef H5
  181. window.open(url, '_blank')
  182. // #endif
  183. // #ifdef MP-WEIXIN
  184. // 微信小程序
  185. uni.setClipboardData({
  186. data: url,
  187. showToast: false,
  188. success: () => {
  189. uni.showModal({
  190. title: '提示',
  191. content: '链接已复制,请在浏览器中打开',
  192. showCancel: false
  193. })
  194. }
  195. })
  196. // #endif
  197. // #ifdef MP-ALIPAY
  198. // 支付宝小程序
  199. if (my.openBrowser) {
  200. my.openBrowser({ url: url })
  201. } else {
  202. // 降级处理
  203. uni.setClipboardData({
  204. data: url,
  205. success: () => {
  206. uni.showModal({
  207. title: '提示',
  208. content: '链接已复制,请在浏览器中打开',
  209. showCancel: false
  210. })
  211. }
  212. })
  213. }
  214. // #endif
  215. // 其他平台,如App、快应用等,可以根据需要补充
  216. // 对于不支持直接打开浏览器的平台,使用降级方案
  217. // #ifndef H5 || MP-WEIXIN || MP-ALIPAY
  218. uni.setClipboardData({
  219. data: url,
  220. success: () => {
  221. uni.showModal({
  222. title: '提示',
  223. content: '链接已复制,请在浏览器中打开',
  224. showCancel: false
  225. })
  226. }
  227. })
  228. // #endif
  229. }
  230. };
  231. export default tool;