toast.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view :style="{zIndex: zIndex}" class="container">
  3. <block v-for="message in messageQueue" :key="message.id">
  4. <view :class="[message.animation, backgroundClass(message)]" class="message">
  5. <text v-if="message.type === 'info'" class="bm-icon info">&#xe671;</text>
  6. <text v-if="message.type === 'success'" class="bm-icon success">&#xe62f;</text>
  7. <text v-if="message.type === 'warn'" class="bm-icon warn">&#xe671;</text>
  8. <text v-if="message.type === 'error'" class="bm-icon error">&#xe630;</text>
  9. <text>{{message.content}}</text>
  10. </view>
  11. </block>
  12. </view>
  13. </template>
  14. <script>
  15. // 需要支持的单独配置项:显示时长、是否启用背景、类型、内容
  16. export default {
  17. name: 'bobo-message-cpt',
  18. props: {
  19. zIndex: {
  20. type: Number,
  21. default: 10000
  22. },
  23. duration: {
  24. type: Number,
  25. default: 2000
  26. },
  27. background: {
  28. type: Boolean,
  29. default: false
  30. }
  31. },
  32. data() {
  33. return {
  34. messageQueue: [],
  35. lastId: 0
  36. }
  37. },
  38. computed: {
  39. backgroundClass() {
  40. return msg => {
  41. return this.background || msg.background ? `background-${msg.type}` : ''
  42. }
  43. }
  44. },
  45. methods: {
  46. /**
  47. * 展示普通提示信息
  48. * @param {Object} content
  49. */
  50. info(arg) {
  51. uni.hideLoading()
  52. const message = {
  53. type: 'info'
  54. }
  55. if (typeof arg === 'object' && arg) {
  56. message.content = arg.content
  57. message.duration = arg.duration
  58. message.background = arg.background
  59. } else if(typeof arg === 'string') {
  60. message.content = arg
  61. }
  62. this.fadeIn(message)
  63. },
  64. /**
  65. * 展示成功提示
  66. * @param {Object} content
  67. */
  68. success(arg) {
  69. uni.hideLoading()
  70. const message = {
  71. type: 'success'
  72. }
  73. if (typeof arg === 'object' && arg) {
  74. message.content = arg.content
  75. // 显示时长会用在 settimeout的参数中,必须保证类型正确
  76. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  77. message.duration = arg.duration
  78. }
  79. message.background = arg.background
  80. } else if(typeof arg === 'string') {
  81. message.content = arg
  82. }
  83. this.fadeIn(message)
  84. },
  85. /**
  86. * 展示警告提示
  87. */
  88. warn(arg) {
  89. uni.hideLoading()
  90. const message = {
  91. type: 'warn'
  92. }
  93. if (typeof arg === 'object' && arg) {
  94. message.content = arg.content
  95. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  96. message.duration = arg.duration
  97. }
  98. message.background = arg.background
  99. } else if(typeof arg === 'string') {
  100. message.content = arg
  101. }
  102. this.fadeIn(message)
  103. },
  104. /**
  105. * 展示错误提示
  106. * @param {Object} message
  107. */
  108. error(arg) {
  109. uni.hideLoading()
  110. const message = {
  111. type: 'error'
  112. }
  113. if (typeof arg === 'object' && arg) {
  114. message.content = arg.content
  115. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  116. message.duration = arg.duration
  117. }
  118. message.background = arg.background
  119. } else if(typeof arg === 'string') {
  120. message.content = arg
  121. }
  122. this.fadeIn(message)
  123. },
  124. fadeIn(message) {
  125. message.id = this.generateId()
  126. message.animation = 'fadeIn'
  127. this.messageQueue.push(message)
  128. // 动画执行完毕后取消动画效果,防止列表刷新时重新执行动画
  129. setTimeout(() => {
  130. message.animation = ''
  131. }, 410)
  132. // 显示一段时间后隐藏
  133. setTimeout(() => {
  134. this.fadeOut(message)
  135. }, message.duration || this.duration)
  136. },
  137. fadeOut(message) {
  138. message.animation = 'fadeOut'
  139. setTimeout(() => {
  140. let idx = 0
  141. this.messageQueue.some((msg, index) => {
  142. if (msg.id === message.id) {
  143. idx = index
  144. return true
  145. }
  146. })
  147. this.messageQueue.splice(idx, 1)
  148. }, 500)
  149. },
  150. generateId() {
  151. return (new Date()).getTime() * 1000 + (this.lastId++) % 1000
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. @font-face {
  158. font-family: 'bobo-message-iconfont'; /* project id 1477381 */
  159. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot');
  160. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot?#iefix') format('embedded-opentype'),
  161. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.ttf') format('truetype'),
  162. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.svg#iconfont') format('svg');
  163. }
  164. .container {
  165. left: 0;
  166. right: 0;
  167. top: 30%;
  168. min-width: 10rpx;
  169. max-width: 550rpx;
  170. position: fixed;
  171. margin: 0 auto;
  172. display: flex;
  173. flex-direction: column;
  174. align-items: center;
  175. // 透明层允许点击穿透
  176. pointer-events: none;
  177. .message {
  178. display: flex;
  179. // 内容区域禁止点击穿透
  180. pointer-events: all;
  181. align-items: center;
  182. padding: 8px 16px;
  183. border-radius: 4px;
  184. margin-top: 5px;
  185. box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
  186. background: #fff;
  187. min-height: 37px;
  188. transition: all .5s;
  189. box-sizing: border-box;
  190. .bm-icon {
  191. font-family: bobo-message-iconfont;
  192. margin-right: 10rpx;
  193. }
  194. .info {
  195. color: #288ced;
  196. }
  197. .success {
  198. color: #09be70;
  199. }
  200. .warn {
  201. color: #ff991f;
  202. }
  203. .error {
  204. color: #ef4017;
  205. }
  206. }
  207. .background-info {
  208. border: 2px solid #d4eefe;
  209. background: #f0faff;
  210. color: #288ced;
  211. box-shadow: none !important;
  212. }
  213. .background-success {
  214. border: 2px solid #baf2d1;
  215. background: #edfff4;
  216. color: #09be70;
  217. box-shadow: none !important;
  218. }
  219. .background-warn {
  220. border: 2px solid #ffe7a7;
  221. background: #fff9e7;
  222. color: #ff991f;
  223. box-shadow: none !important;
  224. }
  225. .background-error {
  226. border: 2px solid #ffcfb9;
  227. background: #ffefe6;
  228. color: #ef4017;
  229. box-shadow: none !important;
  230. }
  231. .fadeIn {
  232. animation: fadeIn 0.4s both;
  233. }
  234. @keyframes fadeIn {
  235. from {
  236. opacity: 0;
  237. transform: translate(0, -30px);
  238. }
  239. to {
  240. opacity: 1;
  241. transform: translate(0, 0);
  242. }
  243. }
  244. .fadeOut {
  245. animation: fadeOut 0.4s forwards;
  246. }
  247. @keyframes fadeOut {
  248. from {
  249. opacity: 1;
  250. transform: translate(0, 0);
  251. }
  252. to {
  253. opacity: 0;
  254. margin-top: -37px;
  255. }
  256. }
  257. }
  258. </style>