You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.6 KiB

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