fs-message.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view
  3. class="fs-message"
  4. :class="[{show:state.options.show},'bg-'+state.options.type]">
  5. {{state.options.message}}
  6. </view>
  7. </template>
  8. <script setup>
  9. import { reactive } from 'vue'
  10. const defaultOptions = {
  11. type: 'primary',
  12. duration: 3000
  13. }
  14. const state = reactive({
  15. options: {},
  16. timer: null
  17. })
  18. const formatOptions = options => {
  19. if (typeof options === 'string') {
  20. return {
  21. message: options
  22. }
  23. }
  24. if (options.type === 'error') {
  25. options.type = 'danger'
  26. }
  27. return options
  28. }
  29. const show = options => {
  30. state.options = {
  31. ...defaultOptions,
  32. ...formatOptions(options),
  33. show: true
  34. }
  35. if (state.timer) {
  36. clearTimeout(state.timer)
  37. }
  38. if (state.options.duration > 0) {
  39. state.timer = setTimeout(() => {
  40. handleHide()
  41. state.timer = null
  42. }, state.options.duration)
  43. }
  44. }
  45. const success = options => {
  46. show({
  47. ...formatOptions(options),
  48. type: 'success'
  49. })
  50. }
  51. const error = options => {
  52. show({
  53. ...formatOptions(options),
  54. type: 'danger'
  55. })
  56. }
  57. const warning = options => {
  58. show({
  59. ...formatOptions(options),
  60. type: 'warning'
  61. })
  62. }
  63. const info = options => {
  64. show({
  65. ...formatOptions(options),
  66. type: 'info'
  67. })
  68. }
  69. const handleHide = () => {
  70. state.options = {
  71. ...state.options,
  72. show: false
  73. }
  74. }
  75. defineExpose({
  76. show,
  77. success,
  78. error,
  79. warning,
  80. info,
  81. handleHide
  82. })
  83. </script>
  84. <style lang="scss" scoped>
  85. .fs-message{
  86. position: fixed;
  87. top: var(--window-top);
  88. left: 0;
  89. right: 0;
  90. padding: 20rpx;
  91. color: #fff;
  92. transition: all .1s;
  93. transform: translateY(-100%);
  94. text-align: center;
  95. z-index: 900;
  96. }
  97. .show{
  98. transform: translateY(0);
  99. }
  100. </style>