3
0

u-toast.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <view class="u-toast">
  3. <u-overlay
  4. :show="isShow"
  5. :zIndex="tmpConfig.overlay ? 10070 : -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, sys, 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. duration: 2000, // 显示的时间,毫秒
  80. icon: true, // 显示的图标
  81. position: 'center', // toast出现的位置
  82. complete: null, // 执行完后的回调函数
  83. overlay: true, // 是否防止触摸穿透
  84. loading: false, // 是否加载中状态
  85. },
  86. tmpConfig: {}, // 将用户配置和内置配置合并后的临时配置变量
  87. }
  88. },
  89. computed: {
  90. iconName() {
  91. // 只有不为none,并且type为error|warning|succes|info时候,才显示图标
  92. if(!this.tmpConfig.icon || this.tmpConfig.icon == 'none') {
  93. return '';
  94. }
  95. if (this.tmpConfig.icon === true) {
  96. if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) {
  97. return type2icon(this.tmpConfig.type)
  98. } else {
  99. return ''
  100. }
  101. } else {
  102. return this.tmpConfig.icon
  103. }
  104. },
  105. overlayStyle() {
  106. const style = {
  107. justifyContent: 'center',
  108. alignItems: 'center',
  109. display: 'flex'
  110. }
  111. // 将遮罩设置为100%透明度,避免出现灰色背景
  112. style.backgroundColor = 'rgba(0, 0, 0, 0)'
  113. return style
  114. },
  115. iconStyle() {
  116. const style = {}
  117. // 图标需要一个右边距,以跟右边的文字有隔开的距离
  118. style.marginRight = '4px'
  119. // #ifdef APP-NVUE
  120. // iOSAPP下,图标有1px的向下偏移,这里进行修正
  121. if (os() === 'ios') {
  122. style.marginTop = '-1px'
  123. }
  124. // #endif
  125. return style
  126. },
  127. loadingIconColor() {
  128. let colorTmp = 'rgb(255, 255, 255)'
  129. if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) {
  130. // loading-icon组件内部会对color参数进行一个透明度处理,该方法要求传入的颜色值
  131. // 必须为rgb格式的,所以这里做一个处理
  132. colorTmp = hexToRgb(color[this.tmpConfig.type])
  133. }
  134. return colorTmp
  135. },
  136. // 内容盒子的样式
  137. contentStyle() {
  138. const windowHeight = sys().windowHeight, style = {}
  139. let value = 0
  140. // 根据top和bottom,对Y轴进行窗体高度的百分比偏移
  141. if(this.tmpConfig.position === 'top') {
  142. value = - windowHeight * 0.25
  143. } else if(this.tmpConfig.position === 'bottom') {
  144. value = windowHeight * 0.25
  145. }
  146. style.transform = `translateY(${value}px)`
  147. return style
  148. }
  149. },
  150. created() {
  151. // 通过主题的形式调用toast,批量生成方法函数
  152. ['primary', 'success', 'error', 'warning', 'default', 'loading'].map(item => {
  153. this[item] = message => this.show({
  154. type: item,
  155. message
  156. })
  157. })
  158. },
  159. methods: {
  160. // 显示toast组件,由父组件通过this.$refs.xxx.show(options)形式调用
  161. show(options) {
  162. // 不将结果合并到this.config变量,避免多次调用u-toast,前后的配置造成混乱
  163. this.tmpConfig = deepMerge(this.config, options)
  164. // 清除定时器
  165. this.clearTimer()
  166. this.isShow = true
  167. // -1时不自动关闭
  168. if (this.tmpConfig.duration !== -1) {
  169. this.timer = setTimeout(() => {
  170. // 倒计时结束,清除定时器,隐藏toast组件
  171. this.clearTimer()
  172. // 判断是否存在callback方法,如果存在就执行
  173. typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete()
  174. }, this.tmpConfig.duration)
  175. }
  176. },
  177. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  178. hide() {
  179. this.clearTimer()
  180. },
  181. clearTimer() {
  182. this.isShow = false
  183. // 清除定时器
  184. clearTimeout(this.timer)
  185. this.timer = null
  186. }
  187. },
  188. beforeUnmount() {
  189. this.clearTimer()
  190. }
  191. }
  192. </script>
  193. <style lang="scss" scoped>
  194. @import "../../libs/css/components.scss";
  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>