cl-message.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="cl-message__wrap">
  3. <template v-for="(item, index) in list">
  4. <view
  5. class="cl-message"
  6. v-if="!item.closed"
  7. :key="item.id"
  8. :class="[item.visible ? 'is-show' : '']"
  9. :style="{
  10. top: item.visible ? item.top : '-200rpx',
  11. }"
  12. >
  13. <icon
  14. class="cl-message__icon"
  15. :type="item.type"
  16. :size="item.iconSize"
  17. v-if="item.type"
  18. />
  19. <slot>
  20. <text class="cl-message__content">{{ item.message }}</text>
  21. </slot>
  22. </view>
  23. </template>
  24. </view>
  25. </template>
  26. <script>
  27. import { isObject, isString, isFunction } from "../../utils";
  28. let id = 0;
  29. /**
  30. * message 消息提示
  31. * @description 消息提示
  32. * @tutorial https://docs.cool-js.com/uni/components/feedback/message.html
  33. * @property {Boolean} single 是否单个显示,默认false
  34. * @example <cl-message ref="message"></cl-message>
  35. */
  36. export default {
  37. props: {
  38. // 是否单个显示
  39. single: Boolean,
  40. },
  41. data() {
  42. return {
  43. list: [],
  44. };
  45. },
  46. methods: {
  47. open(d) {
  48. // 默认配置
  49. let options = {
  50. id: id++,
  51. visible: false,
  52. closed: false,
  53. message: "",
  54. type: "success",
  55. duration: 3000,
  56. timer: null,
  57. onClose: null,
  58. iconSize: 22,
  59. top: "120rpx",
  60. };
  61. // 合并参数
  62. if (isObject(d)) {
  63. Object.assign(options, d);
  64. } else {
  65. options.message = d;
  66. }
  67. // 是否同时存在多个提示
  68. if (this.single) {
  69. this.list = [options];
  70. } else {
  71. this.list.push(options);
  72. }
  73. setTimeout(() => {
  74. this.create(options);
  75. }, 50);
  76. },
  77. close(item) {
  78. // 清空计时器
  79. clearTimeout(item.timer);
  80. item.visible = false;
  81. // 关闭回调
  82. if (isFunction(item.onClose)) {
  83. item.onClose(this);
  84. }
  85. setTimeout(() => {
  86. item.closed = true;
  87. }, 600);
  88. },
  89. create(item) {
  90. const { duration } = item || {};
  91. if (duration > 0) {
  92. item.visible = true;
  93. item.timer = setTimeout(() => {
  94. this.close(item);
  95. }, duration);
  96. }
  97. },
  98. },
  99. };
  100. </script>