工单小程序2024-11-20
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.

220 lines
6.1 KiB

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