u-notify.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <u-transition
  3. mode="slide-down"
  4. :customStyle="containerStyle"
  5. :show="open"
  6. >
  7. <view
  8. class="u-notify"
  9. :class="[`u-notify--${tmpConfig.type}`]"
  10. :style="[backgroundColor, $u.addStyle(customStyle)]"
  11. >
  12. <u-status-bar v-if="tmpConfig.safeAreaInsetTop"></u-status-bar>
  13. <view class="u-notify__warpper">
  14. <slot name="icon">
  15. <u-icon
  16. v-if="['success', 'warning', 'error'].includes(tmpConfig.type)"
  17. :name="tmpConfig.icon"
  18. :color="tmpConfig.color"
  19. :size="1.3 * tmpConfig.fontSize"
  20. :customStyle="{marginRight: '4px'}"
  21. ></u-icon>
  22. </slot>
  23. <text
  24. class="u-notify__warpper__text"
  25. :style="{
  26. fontSize: $u.addUnit(tmpConfig.fontSize),
  27. color: tmpConfig.color
  28. }"
  29. >{{ tmpConfig.message }}
  30. </text>
  31. </view>
  32. </view>
  33. </u-transition>
  34. </template>
  35. <script>
  36. import props from './props.js';
  37. /**
  38. * notify 顶部提示
  39. * @description 该组件一般用于页面顶部向下滑出一个提示,尔后自动收起的场景
  40. * @tutorial
  41. * @property {String | Number} top 到顶部的距离 ( 默认 0 )
  42. * @property {String} type 主题,primary,success,warning,error ( 默认 'primary' )
  43. * @property {String} color 字体颜色 ( 默认 '#ffffff' )
  44. * @property {String} bgColor 背景颜色
  45. * @property {String} message 展示的文字内容
  46. * @property {String | Number} duration 展示时长,为0时不消失,单位ms ( 默认 3000 )
  47. * @property {String | Number} fontSize 字体大小 ( 默认 15 )
  48. * @property {Boolean} safeAreaInsetTop 是否留出顶部安全距离(状态栏高度) ( 默认 false )
  49. * @property {Object} customStyle 组件的样式,对象形式
  50. * @event {Function} open 开启组件时调用的函数
  51. * @event {Function} close 关闭组件式调用的函数
  52. * @example <u-notify message="Hi uView"></u-notify>
  53. */
  54. export default {
  55. name: 'u-notify',
  56. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  57. data() {
  58. return {
  59. // 是否展示组件
  60. open: false,
  61. timer: null,
  62. config: {
  63. // 到顶部的距离
  64. top: uni.$u.props.notify.top,
  65. // type主题,primary,success,warning,error
  66. type: uni.$u.props.notify.type,
  67. // 字体颜色
  68. color: uni.$u.props.notify.color,
  69. // 背景颜色
  70. bgColor: uni.$u.props.notify.bgColor,
  71. // 展示的文字内容
  72. message: uni.$u.props.notify.message,
  73. // 展示时长,为0时不消失,单位ms
  74. duration: uni.$u.props.notify.duration,
  75. // 字体大小
  76. fontSize: uni.$u.props.notify.fontSize,
  77. // 是否留出顶部安全距离(状态栏高度)
  78. safeAreaInsetTop: uni.$u.props.notify.safeAreaInsetTop
  79. },
  80. // 合并后的配置,避免多次调用组件后,可能会复用之前使用的配置参数
  81. tmpConfig: {}
  82. }
  83. },
  84. computed: {
  85. containerStyle() {
  86. let top = 0
  87. if (this.tmpConfig.top === 0) {
  88. // #ifdef H5
  89. // H5端,导航栏为普通元素,需要将组件移动到导航栏的下边沿
  90. // H5的导航栏高度为44px
  91. top = 44
  92. // #endif
  93. }
  94. const style = {
  95. top: uni.$u.addUnit(this.tmpConfig.top === 0 ? top : this.tmpConfig.top),
  96. // 因为组件底层为u-transition组件,必须将其设置为fixed定位
  97. // 让其出现在导航栏底部
  98. position: 'fixed',
  99. left: 0,
  100. right: 0,
  101. zIndex: 10076
  102. }
  103. return style
  104. },
  105. // 组件背景颜色
  106. backgroundColor() {
  107. const style = {}
  108. if (this.tmpConfig.bgColor) {
  109. style.backgroundColor = this.tmpConfig.bgColor
  110. }
  111. return style
  112. },
  113. // 默认主题下的图标
  114. icon() {
  115. let icon
  116. if (this.tmpConfig.type === 'success') {
  117. icon = 'checkmark-circle'
  118. } else if (this.tmpConfig.type === 'error') {
  119. icon = 'close-circle'
  120. } else if (this.tmpConfig.type === 'warning') {
  121. icon = 'error-circle'
  122. }
  123. return icon
  124. }
  125. },
  126. created() {
  127. // 通过主题的形式调用toast,批量生成方法函数
  128. ['primary', 'success', 'error', 'warning'].map(item => {
  129. this[item] = message => this.show({
  130. type: item,
  131. message
  132. })
  133. })
  134. },
  135. methods: {
  136. show(options) {
  137. // 不将结果合并到this.config变量,避免多次调用u-toast,前后的配置造成混乱
  138. this.tmpConfig = uni.$u.deepMerge(this.config, options)
  139. // 任何定时器初始化之前,都要执行清除操作,否则可能会造成混乱
  140. this.clearTimer()
  141. this.open = true
  142. if (this.tmpConfig.duration > 0) {
  143. this.timer = setTimeout(() => {
  144. this.open = false
  145. // 倒计时结束,清除定时器,隐藏toast组件
  146. this.clearTimer()
  147. // 判断是否存在callback方法,如果存在就执行
  148. typeof (this.tmpConfig.complete) === 'function' && this.tmpConfig.complete()
  149. }, this.tmpConfig.duration)
  150. }
  151. },
  152. // 关闭notify
  153. close() {
  154. this.clearTimer()
  155. },
  156. clearTimer() {
  157. this.open = false
  158. // 清除定时器
  159. clearTimeout(this.timer)
  160. this.timer = null
  161. }
  162. },
  163. beforeDestroy() {
  164. this.clearTimer()
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. @import "../../libs/css/components.scss";
  170. $u-notify-padding: 8px 10px !default;
  171. $u-notify-text-font-size: 15px !default;
  172. $u-notify-primary-bgColor: $u-primary !default;
  173. $u-notify-success-bgColor: $u-success !default;
  174. $u-notify-error-bgColor: $u-error !default;
  175. $u-notify-warning-bgColor: $u-warning !default;
  176. .u-notify {
  177. padding: $u-notify-padding;
  178. &__warpper {
  179. @include flex;
  180. align-items: center;
  181. text-align: center;
  182. justify-content: center;
  183. &__text {
  184. font-size: $u-notify-text-font-size;
  185. text-align: center;
  186. }
  187. }
  188. &--primary {
  189. background-color: $u-notify-primary-bgColor;
  190. }
  191. &--success {
  192. background-color: $u-notify-success-bgColor;
  193. }
  194. &--error {
  195. background-color: $u-notify-error-bgColor;
  196. }
  197. &--warning {
  198. background-color: $u-notify-warning-bgColor;
  199. }
  200. }
  201. </style>