u-toast.vue 9.7 KB

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