Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

309 rader
9.9 KiB

  1. <template>
  2. <view class="u-toast">
  3. <u-overlay
  4. :show="isShow"
  5. :zIndex="tmpConfig.zIndex"
  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. <up-icon
  21. v-else-if="tmpConfig.type !== 'defalut' && iconName"
  22. :name="iconName"
  23. size="17"
  24. :color="tmpConfig.type"
  25. :customStyle="iconStyle"
  26. ></up-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://uview-plus.jiangruyi.com/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. // overlay=false时不阻止点击穿透
  115. if (!this.tmpConfig.overlay) {
  116. style.pointerEvents = 'none'
  117. }
  118. return style
  119. },
  120. iconStyle() {
  121. const style = {}
  122. // 图标需要一个右边距,以跟右边的文字有隔开的距离
  123. style.marginRight = '4px'
  124. // #ifdef APP-NVUE
  125. // iOSAPP下,图标有1px的向下偏移,这里进行修正
  126. if (os() === 'ios') {
  127. style.marginTop = '-1px'
  128. }
  129. // #endif
  130. return style
  131. },
  132. loadingIconColor() {
  133. let colorTmp = 'rgb(255, 255, 255)'
  134. if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) {
  135. // loading-icon组件内部会对color参数进行一个透明度处理,该方法要求传入的颜色值
  136. // 必须为rgb格式的,所以这里做一个处理
  137. colorTmp = hexToRgb(color[this.tmpConfig.type])
  138. }
  139. return colorTmp
  140. },
  141. // 内容盒子的样式
  142. contentStyle() {
  143. const windowHeight = getWindowInfo().windowHeight, style = {}
  144. let value = 0
  145. // 根据top和bottom,对Y轴进行窗体高度的百分比偏移
  146. if(this.tmpConfig.position === 'top') {
  147. value = - windowHeight * 0.25
  148. } else if(this.tmpConfig.position === 'bottom') {
  149. value = windowHeight * 0.25
  150. }
  151. style.transform = `translateY(${value}px)`
  152. return style
  153. }
  154. },
  155. created() {
  156. // 通过主题的形式调用toast,批量生成方法函数
  157. ['primary', 'success', 'error', 'warning', 'default', 'loading'].map(item => {
  158. this[item] = message => this.show({
  159. type: item,
  160. message
  161. })
  162. })
  163. },
  164. methods: {
  165. // 显示toast组件,由父组件通过this.$refs.xxx.show(options)形式调用
  166. show(options) {
  167. // 不将结果合并到this.config变量,避免多次调用u-toast,前后的配置造成混乱
  168. this.tmpConfig = deepMerge(this.config, options)
  169. // 清除定时器
  170. this.clearTimer()
  171. this.isShow = true
  172. // -1时不自动关闭
  173. if (this.tmpConfig.duration !== -1) {
  174. this.timer = setTimeout(() => {
  175. // 倒计时结束,清除定时器,隐藏toast组件
  176. this.clearTimer()
  177. // 判断是否存在callback方法,如果存在就执行
  178. typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete()
  179. }, this.tmpConfig.duration)
  180. }
  181. },
  182. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  183. hide() {
  184. this.clearTimer()
  185. },
  186. clearTimer() {
  187. this.isShow = false
  188. // 清除定时器
  189. clearTimeout(this.timer)
  190. this.timer = null
  191. }
  192. },
  193. beforeUnmount() {
  194. this.clearTimer()
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. $u-toast-color:#fff !default;
  200. $u-toast-border-radius:4px !default;
  201. $u-toast-border-background-color:#585858 !default;
  202. $u-toast-border-font-size:14px !default;
  203. $u-toast-border-padding:12px 20px !default;
  204. $u-toast-loading-border-padding: 20px 20px !default;
  205. $u-toast-content-text-color:#fff !default;
  206. $u-toast-content-text-font-size:15px !default;
  207. $u-toast-up-icon:10rpx !default;
  208. $u-toast-u-type-primary-color:$u-primary !default;
  209. $u-toast-u-type-primary-background-color:$u-primary-light !default;
  210. $u-toast-u-type-primary-border-color:$u-primary-disabled !default;
  211. $u-toast-u-type-primary-border-width:1px !default;
  212. $u-toast-u-type-success-color: $u-success !default;
  213. $u-toast-u-type-success-background-color: $u-success-light !default;
  214. $u-toast-u-type-success-border-color: $u-success-disabled !default;
  215. $u-toast-u-type-success-border-width: 1px !default;
  216. $u-toast-u-type-error-color:$u-error !default;
  217. $u-toast-u-type-error-background-color:$u-error-light !default;
  218. $u-toast-u-type-error-border-color:$u-error-disabled !default;
  219. $u-toast-u-type-error-border-width: 1px !default;
  220. $u-toast-u-type-warning-color:$u-warning !default;
  221. $u-toast-u-type-warning-background-color:$u-warning-light !default;
  222. $u-toast-u-type-warning-border-color:$u-warning-disabled !default;
  223. $u-toast-u-type-warning-border-width: 1px !default;
  224. $u-toast-u-type-default-color:#fff !default;
  225. $u-toast-u-type-default-background-color:#585858 !default;
  226. .u-toast {
  227. &__content {
  228. @include flex;
  229. padding: $u-toast-border-padding;
  230. border-radius: $u-toast-border-radius;
  231. background-color: $u-toast-border-background-color;
  232. color: $u-toast-color;
  233. align-items: center;
  234. /* #ifndef APP-NVUE */
  235. max-width: 600rpx;
  236. /* #endif */
  237. position: relative;
  238. &--loading {
  239. flex-direction: column;
  240. padding: $u-toast-loading-border-padding;
  241. }
  242. &__text {
  243. color: $u-toast-content-text-color;
  244. font-size: $u-toast-content-text-font-size;
  245. line-height: $u-toast-content-text-font-size;
  246. &--default {
  247. color: $u-toast-content-text-color;
  248. }
  249. &--error {
  250. color: $u-error;
  251. }
  252. &--primary {
  253. color: $u-primary;
  254. }
  255. &--success {
  256. color: $u-success;
  257. }
  258. &--warning {
  259. color: $u-warning;
  260. }
  261. }
  262. }
  263. }
  264. .u-type-primary {
  265. color: $u-toast-u-type-primary-color;
  266. background-color: $u-toast-u-type-primary-background-color;
  267. border-color: $u-toast-u-type-primary-border-color;
  268. border-width: $u-toast-u-type-primary-border-width;
  269. }
  270. .u-type-success {
  271. color: $u-toast-u-type-success-color;
  272. background-color: $u-toast-u-type-success-background-color;
  273. border-color: $u-toast-u-type-success-border-color;
  274. border-width: 1px;
  275. }
  276. .u-type-error {
  277. color: $u-toast-u-type-error-color;
  278. background-color: $u-toast-u-type-error-background-color;
  279. border-color: $u-toast-u-type-error-border-color;
  280. border-width: $u-toast-u-type-error-border-width;
  281. }
  282. .u-type-warning {
  283. color: $u-toast-u-type-warning-color;
  284. background-color: $u-toast-u-type-warning-background-color;
  285. border-color: $u-toast-u-type-warning-border-color;
  286. border-width: 1px;
  287. }
  288. .u-type-default {
  289. color: $u-toast-u-type-default-color;
  290. background-color: $u-toast-u-type-default-background-color;
  291. }
  292. </style>