u-toast.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. import mpMixin from '../../libs/mixin/mpMixin.js';
  42. import mixin from '../../libs/mixin/mixin.js';
  43. /**
  44. * toast 消息提示
  45. * @description 此组件表现形式类似uni的uni.showToastAPI,但也有不同的地方。
  46. * @tutorial https://ijry.github.io/uview-plus/components/toast.html
  47. * @property {String | Number} zIndex toast展示时的zIndex值 (默认 10090 )
  48. * @property {Boolean} loading 是否加载中 (默认 false )
  49. * @property {String | Number} message 显示的文字内容
  50. * @property {String} icon 图标,或者绝对路径的图片
  51. * @property {String} type 主题类型 (默认 default)
  52. * @property {Boolean} show 是否显示该组件 (默认 false)
  53. * @property {Boolean} overlay 是否显示透明遮罩,防止点击穿透 (默认 false )
  54. * @property {String} position 位置 (默认 'center' )
  55. * @property {Object} params 跳转的参数
  56. * @property {String | Number} duration 展示时间,单位ms (默认 2000 )
  57. * @property {Boolean} isTab 是否返回的为tab页面 (默认 false )
  58. * @property {String} url toast消失后是否跳转页面,有则跳转,优先级高于back参数
  59. * @property {Function} complete 执行完后的回调函数
  60. * @property {Boolean} back 结束toast是否自动返回上一页 (默认 false )
  61. * @property {Object} customStyle 组件的样式,对象形式
  62. * @event {Function} show 显示toast,如需一进入页面就显示toast,请在onReady生命周期调用
  63. * @example <u-toast ref="uToast" />
  64. */
  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.color[this.tmpConfig.type])
  125. }
  126. return color
  127. },
  128. // 内容盒子的样式
  129. contentStyle() {
  130. const windowHeight = uni.$u.sys().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. this.timer = setTimeout(() => {
  160. // 倒计时结束,清除定时器,隐藏toast组件
  161. this.clearTimer()
  162. // 判断是否存在callback方法,如果存在就执行
  163. typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete()
  164. }, this.tmpConfig.duration)
  165. },
  166. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  167. hide() {
  168. this.clearTimer()
  169. },
  170. clearTimer() {
  171. this.isShow = false
  172. // 清除定时器
  173. clearTimeout(this.timer)
  174. this.timer = null
  175. }
  176. },
  177. beforeDestroy() {
  178. this.clearTimer()
  179. }
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. @import "../../libs/css/components.scss";
  184. $u-toast-color:#fff !default;
  185. $u-toast-border-radius:4px !default;
  186. $u-toast-border-background-color:#585858 !default;
  187. $u-toast-border-font-size:14px !default;
  188. $u-toast-border-padding:12px 20px !default;
  189. $u-toast-loading-border-padding: 20px 20px !default;
  190. $u-toast-content-text-color:#fff !default;
  191. $u-toast-content-text-font-size:15px !default;
  192. $u-toast-u-icon:10rpx !default;
  193. $u-toast-u-type-primary-color:$u-primary !default;
  194. $u-toast-u-type-primary-background-color:#ecf5ff !default;
  195. $u-toast-u-type-primary-border-color:rgb(215, 234, 254) !default;
  196. $u-toast-u-type-primary-border-width:1px !default;
  197. $u-toast-u-type-success-color: $u-success !default;
  198. $u-toast-u-type-success-background-color: #dbf1e1 !default;
  199. $u-toast-u-type-success-border-color: #BEF5C8 !default;
  200. $u-toast-u-type-success-border-width: 1px !default;
  201. $u-toast-u-type-error-color:$u-error !default;
  202. $u-toast-u-type-error-background-color:#fef0f0 !default;
  203. $u-toast-u-type-error-border-color:#fde2e2 !default;
  204. $u-toast-u-type-error-border-width: 1px !default;
  205. $u-toast-u-type-warning-color:$u-warning !default;
  206. $u-toast-u-type-warning-background-color:#fdf6ec !default;
  207. $u-toast-u-type-warning-border-color:#faecd8 !default;
  208. $u-toast-u-type-warning-border-width: 1px !default;
  209. $u-toast-u-type-default-color:#fff !default;
  210. $u-toast-u-type-default-background-color:#585858 !default;
  211. .u-toast {
  212. &__content {
  213. @include flex;
  214. padding: $u-toast-border-padding;
  215. border-radius: $u-toast-border-radius;
  216. background-color: $u-toast-border-background-color;
  217. color: $u-toast-color;
  218. align-items: center;
  219. /* #ifndef APP-NVUE */
  220. max-width: 600rpx;
  221. /* #endif */
  222. position: relative;
  223. &--loading {
  224. flex-direction: column;
  225. padding: $u-toast-loading-border-padding;
  226. }
  227. &__text {
  228. color: $u-toast-content-text-color;
  229. font-size: $u-toast-content-text-font-size;
  230. line-height: $u-toast-content-text-font-size;
  231. &--default {
  232. color: $u-toast-content-text-color;
  233. }
  234. &--error {
  235. color: $u-error;
  236. }
  237. &--primary {
  238. color: $u-primary;
  239. }
  240. &--success {
  241. color: $u-success;
  242. }
  243. &--warning {
  244. color: $u-warning;
  245. }
  246. }
  247. }
  248. }
  249. .u-type-primary {
  250. color: $u-toast-u-type-primary-color;
  251. background-color: $u-toast-u-type-primary-background-color;
  252. border-color: $u-toast-u-type-primary-border-color;
  253. border-width: $u-toast-u-type-primary-border-width;
  254. }
  255. .u-type-success {
  256. color: $u-toast-u-type-success-color;
  257. background-color: $u-toast-u-type-success-background-color;
  258. border-color: $u-toast-u-type-success-border-color;
  259. border-width: 1px;
  260. }
  261. .u-type-error {
  262. color: $u-toast-u-type-error-color;
  263. background-color: $u-toast-u-type-error-background-color;
  264. border-color: $u-toast-u-type-error-border-color;
  265. border-width: $u-toast-u-type-error-border-width;
  266. }
  267. .u-type-warning {
  268. color: $u-toast-u-type-warning-color;
  269. background-color: $u-toast-u-type-warning-background-color;
  270. border-color: $u-toast-u-type-warning-border-color;
  271. border-width: 1px;
  272. }
  273. .u-type-default {
  274. color: $u-toast-u-type-default-color;
  275. background-color: $u-toast-u-type-default-background-color;
  276. }
  277. </style>