uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.8 KiB

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