u-toast.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="u-toast">
  3. <u-overlay
  4. :show="isShow"
  5. :custom-style="overlayStyle"
  6. >
  7. <view
  8. class="u-toast__content"
  9. :style="[contentStyle]"
  10. :class="['u-type-' + tmpConfig.type, (tmpConfig.type === 'loading' || tmpConfig.loading) ? 'u-toast__content--loading' : '']"
  11. >
  12. <u-loading-icon
  13. v-if="tmpConfig.type === 'loading'"
  14. mode="circle"
  15. color="rgb(255, 255, 255)"
  16. inactiveColor="rgb(120, 120, 120)"
  17. size="25"
  18. ></u-loading-icon>
  19. <u-icon
  20. v-else-if="tmpConfig.type !== 'defalut' && iconName"
  21. :name="iconName"
  22. size="17"
  23. :color="tmpConfig.type"
  24. :customStyle="iconStyle"
  25. ></u-icon>
  26. <u-gap
  27. v-if="tmpConfig.type === 'loading' || tmpConfig.loading"
  28. height="12"
  29. bgColor="transparent"
  30. ></u-gap>
  31. <text
  32. class="u-toast__content__text"
  33. :class="['u-toast__content__text--' + tmpConfig.type]"
  34. style="max-width: 400rpx;"
  35. >{{ tmpConfig.message }}</text>
  36. </view>
  37. </u-overlay>
  38. </view>
  39. </template>
  40. <script>
  41. /**
  42. * toast 消息提示
  43. * @description 此组件表现形式类似uni的uni.showToastAPI,但也有不同的地方。
  44. * @tutorial https://uview.d3u.cn/components/toast.html
  45. * @property {String | Number} zIndex toast展示时的zIndex值 (默认 10090 )
  46. * @property {Boolean} loading 是否加载中 (默认 false )
  47. * @property {String | Number} message 显示的文字内容
  48. * @property {String} icon 图标,或者绝对路径的图片
  49. * @property {String} type 主题类型 (默认 default)
  50. * @property {Boolean} show 是否显示该组件 (默认 false)
  51. * @property {Boolean} overlay 是否显示透明遮罩,防止点击穿透 (默认 false )
  52. * @property {String} position 位置 (默认 'center' )
  53. * @property {Object} params 跳转的参数
  54. * @property {String | Number} duration 展示时间,单位ms (默认 2000 )
  55. * @property {Boolean} isTab 是否返回的为tab页面 (默认 false )
  56. * @property {String} url toast消失后是否跳转页面,有则跳转,优先级高于back参数
  57. * @property {Function} complete 执行完后的回调函数
  58. * @property {Boolean} back 结束toast是否自动返回上一页 (默认 false )
  59. * @property {Object} customStyle 组件的样式,对象形式
  60. * @event {Function} show 显示toast,如需一进入页面就显示toast,请在onReady生命周期调用
  61. * @example <u-toast ref="uToast" />
  62. */
  63. import mpMixin from '../../libs/mixin/mpMixin'
  64. import mixin from '../../libs/mixin/mixin'
  65. export default {
  66. name: 'u-toast',
  67. mixins: [mpMixin, mixin],
  68. data() {
  69. return {
  70. isShow: false,
  71. timer: null, // 定时器
  72. config: {
  73. message: '', // 显示文本
  74. type: '', // 主题类型,primary,success,error,warning,black
  75. duration: 2000, // 显示的时间,毫秒
  76. icon: true, // 显示的图标
  77. position: 'center', // toast出现的位置
  78. complete: null, // 执行完后的回调函数
  79. overlay: false, // 是否防止触摸穿透
  80. loading: false, // 是否加载中状态
  81. },
  82. tmpConfig: {}, // 将用户配置和内置配置合并后的临时配置变量
  83. }
  84. },
  85. computed: {
  86. iconName() {
  87. // 只有不为none,并且type为error|warning|succes|info时候,才显示图标
  88. if(!this.tmpConfig.icon || this.tmpConfig.icon == 'none') {
  89. return '';
  90. }
  91. if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) {
  92. return uni.$u.type2icon(this.tmpConfig.type)
  93. } else {
  94. return ''
  95. }
  96. },
  97. overlayStyle() {
  98. const style = {
  99. justifyContent: 'center',
  100. alignItems: 'center',
  101. display: 'flex'
  102. }
  103. // 将遮罩设置为100%透明度,避免出现灰色背景
  104. style.backgroundColor = 'rgba(0, 0, 0, 0)'
  105. return style
  106. },
  107. iconStyle() {
  108. const style = {}
  109. // 图标需要一个右边距,以跟右边的文字有隔开的距离
  110. style.marginRight = '4px'
  111. // #ifdef APP-NVUE
  112. // iOSAPP下,图标有1px的向下偏移,这里进行修正
  113. if (uni.$u.os() === 'ios') {
  114. style.marginTop = '-1px'
  115. }
  116. // #endif
  117. return style
  118. },
  119. loadingIconColor() {
  120. let color = 'rgb(255, 255, 255)'
  121. if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) {
  122. // loading-icon组件内部会对color参数进行一个透明度处理,该方法要求传入的颜色值
  123. // 必须为rgb格式的,所以这里做一个处理
  124. color = uni.$u.hexToRgb(uni.$u.theme[this.tmpConfig.type])
  125. }
  126. return color
  127. },
  128. // 内容盒子的样式
  129. contentStyle() {
  130. const windowHeight = uni.$u.window().windowHeight, style = {}
  131. let value = 0
  132. // 根据top和bottom,对Y轴进行窗体高度的百分比偏移
  133. if(this.tmpConfig.position === 'top') {
  134. value = - windowHeight * 0.25
  135. } else if(this.tmpConfig.position === 'bottom') {
  136. value = windowHeight * 0.25
  137. }
  138. style.transform = `translateY(${value}px)`
  139. return style
  140. }
  141. },
  142. created() {
  143. // 通过主题的形式调用toast,批量生成方法函数
  144. ['primary', 'success', 'error', 'warning', 'default', 'loading'].map(item => {
  145. this[item] = message => this.show({
  146. type: item,
  147. message
  148. })
  149. })
  150. },
  151. methods: {
  152. // 显示toast组件,由父组件通过this.$refs.xxx.show(options)形式调用
  153. show(options) {
  154. // 不将结果合并到this.config变量,避免多次调用u-toast,前后的配置造成混乱
  155. this.tmpConfig = uni.$u.deepMerge(this.config, options)
  156. // 清除定时器
  157. this.clearTimer()
  158. this.isShow = true
  159. if (this.tmpConfig.duration > 0) {
  160. this.timer = setTimeout(() => {
  161. // 倒计时结束,清除定时器,隐藏toast组件
  162. this.clearTimer()
  163. // 判断是否存在callback方法,如果存在就执行
  164. typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete()
  165. }, this.tmpConfig.duration)
  166. }
  167. },
  168. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  169. hide() {
  170. this.clearTimer()
  171. },
  172. clearTimer() {
  173. this.isShow = false
  174. // 清除定时器
  175. clearTimeout(this.timer)
  176. this.timer = null
  177. }
  178. },
  179. // #ifdef VUE2
  180. beforeDestroy() {
  181. this.clearTimer()
  182. }
  183. // #endif
  184. // #ifdef VUE3
  185. beforeUnmount() {
  186. this.clearTimer()
  187. }
  188. // #endif
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. @import "../../libs/css/components.scss";
  193. $u-toast-color:#fff !default;
  194. $u-toast-border-radius:4px !default;
  195. $u-toast-border-background-color:#585858 !default;
  196. $u-toast-border-font-size:14px !default;
  197. $u-toast-border-padding:12px 20px !default;
  198. $u-toast-loading-border-padding: 20px 20px !default;
  199. $u-toast-content-text-color:#fff !default;
  200. $u-toast-content-text-font-size:15px !default;
  201. $u-toast-u-icon:10rpx !default;
  202. $u-toast-u-type-primary-color:$u-primary !default;
  203. $u-toast-u-type-primary-background-color:#ecf5ff !default;
  204. $u-toast-u-type-primary-border-color:rgb(215, 234, 254) !default;
  205. $u-toast-u-type-primary-border-width:1px !default;
  206. $u-toast-u-type-success-color: $u-success !default;
  207. $u-toast-u-type-success-background-color: #dbf1e1 !default;
  208. $u-toast-u-type-success-border-color: #BEF5C8 !default;
  209. $u-toast-u-type-success-border-width: 1px !default;
  210. $u-toast-u-type-error-color:$u-error !default;
  211. $u-toast-u-type-error-background-color:#fef0f0 !default;
  212. $u-toast-u-type-error-border-color:#fde2e2 !default;
  213. $u-toast-u-type-error-border-width: 1px !default;
  214. $u-toast-u-type-warning-color:$u-warning !default;
  215. $u-toast-u-type-warning-background-color:#fdf6ec !default;
  216. $u-toast-u-type-warning-border-color:#faecd8 !default;
  217. $u-toast-u-type-warning-border-width: 1px !default;
  218. $u-toast-u-type-default-color:#fff !default;
  219. $u-toast-u-type-default-background-color:#585858 !default;
  220. .u-toast {
  221. &__content {
  222. @include flex;
  223. padding: $u-toast-border-padding;
  224. border-radius: $u-toast-border-radius;
  225. background-color: $u-toast-border-background-color;
  226. color: $u-toast-color;
  227. align-items: center;
  228. /* #ifndef APP-NVUE */
  229. max-width: 600rpx;
  230. /* #endif */
  231. position: relative;
  232. &--loading {
  233. flex-direction: column;
  234. padding: $u-toast-loading-border-padding;
  235. }
  236. &__text {
  237. color: $u-toast-content-text-color;
  238. font-size: $u-toast-content-text-font-size;
  239. line-height: $u-toast-content-text-font-size;
  240. &--default {
  241. color: $u-toast-content-text-color;
  242. }
  243. &--error {
  244. color: $u-error;
  245. }
  246. &--primary {
  247. color: $u-primary;
  248. }
  249. &--success {
  250. color: $u-success;
  251. }
  252. &--warning {
  253. color: $u-warning;
  254. }
  255. }
  256. }
  257. }
  258. .u-type-primary {
  259. color: $u-toast-u-type-primary-color;
  260. background-color: $u-toast-u-type-primary-background-color;
  261. border-color: $u-toast-u-type-primary-border-color;
  262. border-width: $u-toast-u-type-primary-border-width;
  263. }
  264. .u-type-success {
  265. color: $u-toast-u-type-success-color;
  266. background-color: $u-toast-u-type-success-background-color;
  267. border-color: $u-toast-u-type-success-border-color;
  268. border-width: 1px;
  269. }
  270. .u-type-error {
  271. color: $u-toast-u-type-error-color;
  272. background-color: $u-toast-u-type-error-background-color;
  273. border-color: $u-toast-u-type-error-border-color;
  274. border-width: $u-toast-u-type-error-border-width;
  275. }
  276. .u-type-warning {
  277. color: $u-toast-u-type-warning-color;
  278. background-color: $u-toast-u-type-warning-background-color;
  279. border-color: $u-toast-u-type-warning-border-color;
  280. border-width: 1px;
  281. }
  282. .u-type-default {
  283. color: $u-toast-u-type-default-color;
  284. background-color: $u-toast-u-type-default-background-color;
  285. }
  286. </style>