index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. module.exports = {
  2. showZanToast(title, timeout) {
  3. const options = formatParameter(title, timeout);
  4. // 清除上一轮的计时器
  5. const { timer = 0 } = this.data.zanToast || {};
  6. clearTimeout(timer);
  7. // 弹层设置~
  8. const zanToast = {
  9. show: true,
  10. icon: options.icon,
  11. image: options.image,
  12. title: options.title
  13. };
  14. this.setData({
  15. zanToast
  16. });
  17. // 传入的显示时长小于0,就认为需要一直显示
  18. if (timeout < 0) {
  19. return;
  20. }
  21. // 下一轮计时器
  22. const nextTimer = setTimeout(() => {
  23. this.clearZanToast();
  24. }, timeout || 3000);
  25. this.setData({
  26. 'zanToast.timer': nextTimer
  27. });
  28. },
  29. // 清除所有 toast
  30. clearZanToast() {
  31. const { timer = 0 } = this.data.zanToast || {};
  32. clearTimeout(timer);
  33. this.setData({
  34. 'zanToast.show': false
  35. });
  36. },
  37. // 快捷方法,显示 loading
  38. showZanLoading(title) {
  39. const options = formatParameter(title);
  40. this.showZanToast({
  41. ...options,
  42. icon: 'loading'
  43. });
  44. }
  45. };
  46. function formatParameter(title, timeout = 0) {
  47. // 如果传入的 title 是对象,那么认为所有的配置属性都在这个对象中了
  48. if (typeof title === 'object') {
  49. return title;
  50. }
  51. return {
  52. title,
  53. timeout
  54. };
  55. }