3
0

u-notify.vue 6.0 KB

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