uni-tool.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * 复制内容
  64. * @param content - 要复制的内容
  65. */
  66. copy(content: string): void;
  67. /**
  68. * 格式化价格
  69. * @param price - 价格
  70. * @returns 格式化后的价格
  71. */
  72. formatPrice(price: number): number;
  73. /**
  74. * 格式化时间
  75. * @param time - 时间
  76. * @returns 格式化后的时间
  77. */
  78. formatTime(time: number | string, format: string): string;
  79. openBrowser(url: string): void;
  80. }
  81. const defaultModalOptions: IModalOptions = {
  82. title: '',
  83. content: '',
  84. showCancel: true,
  85. showConfirm: true,
  86. cancelColor: '#000000',
  87. confirmColor: '#31a0fc',
  88. cancelText: '取消',
  89. confirmText: '确认'
  90. };
  91. const tool: IeTool = {
  92. loading: false,
  93. loadingStartTime: 0,
  94. minLoadingTime: 500, // 最小显示时间,单位毫秒
  95. showToast(title: string = '') {
  96. // 先立即隐藏,避免上一个 toast存在导致下次的 toast 很快关闭
  97. uni.hideToast();
  98. setTimeout(() => {
  99. uni.showToast({
  100. title,
  101. icon: 'none'
  102. });
  103. }, 50);
  104. },
  105. showSuccess(title: string = '') {
  106. uni.showToast({
  107. title,
  108. icon: 'success'
  109. });
  110. },
  111. showError(title: string = '') {
  112. uni.showToast({
  113. title,
  114. icon: 'error'
  115. });
  116. },
  117. showLoading(title: string = '') {
  118. this.loadingStartTime = Date.now();
  119. uni.showLoading({
  120. title,
  121. mask: true,
  122. success: () => { }
  123. });
  124. },
  125. hideLoading() {
  126. const currentTime = Date.now();
  127. const elapsedTime = currentTime - this.loadingStartTime;
  128. const remainingTime = Math.max(0, this.minLoadingTime - elapsedTime);
  129. if (remainingTime > 0) {
  130. setTimeout(() => {
  131. uni.hideLoading();
  132. }, remainingTime);
  133. } else {
  134. uni.hideLoading();
  135. }
  136. },
  137. showModal(params: IModalOptions) {
  138. const { title, content, showCancel, confirmText, cancelText, cancelColor, confirmColor } = Object.assign(defaultModalOptions, params);
  139. return new Promise((resolve, reject) => {
  140. uni.showModal({
  141. title,
  142. content,
  143. showCancel,
  144. confirmText,
  145. cancelText,
  146. cancelColor,
  147. confirmColor,
  148. success: (res) => {
  149. resolve(res.confirm);
  150. },
  151. fail: reject
  152. });
  153. });
  154. },
  155. copy(content: string) {
  156. uni.setClipboardData({
  157. data: content,
  158. success: () => { }
  159. });
  160. },
  161. formatPrice(price: number) {
  162. return price / 100;
  163. },
  164. formatTime(time: number | string, format: string = 'yyyy-mm-dd hh:MM:ss') {
  165. if (!time) {
  166. return '';
  167. }
  168. // @ts-ignore
  169. return uni.$uv.timeFormat(time, format)
  170. },
  171. openBrowser(url: string) {
  172. const handleCopy = function () {
  173. uni.setClipboardData({
  174. data: url,
  175. success: () => {
  176. uni.showModal({
  177. title: '提示',
  178. content: '链接已复制,请在浏览器中打开',
  179. showCancel: false
  180. })
  181. }
  182. })
  183. }
  184. // 判断平台
  185. // #ifdef APP-PLUS
  186. plus.runtime.openURL(url)
  187. // #endif
  188. // #ifdef H5
  189. window.open(url, '_blank')
  190. // #endif
  191. // #ifdef MP-WEIXIN
  192. // 微信小程序
  193. try {
  194. if (url.toLowerCase().startsWith('https://mp.weixin.qq.com/')) {
  195. uni.openOfficialAccountArticle({
  196. url,
  197. fail: handleCopy
  198. })
  199. } else {
  200. handleCopy()
  201. }
  202. } catch (e){
  203. console.log('小程序打开链接降级处理', e)
  204. handleCopy()
  205. }
  206. // #endif
  207. // 其他平台,如App、快应用等,可以根据需要补充
  208. // 对于不支持直接打开浏览器的平台,使用降级方案
  209. // #ifndef H5 || MP-WEIXIN
  210. handleCopy()
  211. // #endif
  212. }
  213. };
  214. export default tool;