uni-events-helper-wx
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.

299 lines
9.4 KiB

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