u-toast.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="u-toast" :class="[isShow ? 'u-show' : '', 'u-type-' + config.type, 'u-position-' + config.position]" :style="{
  3. zIndex: uZIndex
  4. }">
  5. <view class="u-icon-wrap">
  6. <u-icon v-if="config.icon" class="u-icon" :name="iconName" :size="30" :color="$u.color[config.type]"></u-icon>
  7. </view>
  8. <text class="u-text">{{config.title}}</text>
  9. </view>
  10. </template>
  11. <script>
  12. /**
  13. * toast 消息提示
  14. * @description 此组件表现形式类似uni的uni.showToastAPI,但也有不同的地方。
  15. * @tutorial https://www.uviewui.com/components/toast.html
  16. * @property {String} z-index toast展示时的z-index值
  17. * @event {Function} show 显示toast,如需一进入页面就显示toast,请在onReady生命周期调用
  18. * @example <u-toast ref="uToast" />
  19. */
  20. export default {
  21. name: "u-toast",
  22. props: {
  23. // z-index值
  24. zIndex: {
  25. type: [Number, String],
  26. default: ''
  27. },
  28. },
  29. data() {
  30. return {
  31. isShow: false,
  32. timer: null, // 定时器
  33. config: {
  34. params: {}, // URL跳转的参数,对象
  35. title: '', // 显示文本
  36. type: '', // 主题类型,primary,success,error,warning,black
  37. duration: 2000, // 显示的时间,毫秒
  38. isTab: false, // 是否跳转tab页面
  39. url: '', // toast消失后是否跳转页面,有则跳转
  40. icon: true, // 显示的图标
  41. position: 'center', // toast出现的位置
  42. }
  43. };
  44. },
  45. computed: {
  46. iconName() {
  47. // 只有不为none,并且type为error|warning|succes|info时候,才显示图标
  48. if (['error', 'warning', 'success', 'info'].indexOf(this.config.type) >= 0 && this.config.icon) {
  49. let icon = this.$u.type2icon(this.config.type);
  50. return icon;
  51. }
  52. },
  53. uZIndex() {
  54. // 显示toast时候,如果用户有传递z-index值,有限使用
  55. return this.isShow ? (this.zIndex ? this.zIndex : this.$u.zIndex.toast) : '999999';
  56. }
  57. },
  58. methods: {
  59. // 显示toast组件,由父组件通过this.$refs.xxx.show(options)形式调用
  60. show(options) {
  61. this.config = Object.assign(this.config, options);
  62. if (this.timer) {
  63. // 清除定时器
  64. clearTimeout(this.timer);
  65. this.timer = null;
  66. }
  67. this.isShow = true;
  68. this.timer = setTimeout(() => {
  69. // 倒计时结束,清除定时器,隐藏toast组件
  70. this.isShow = false;
  71. clearTimeout(this.timer);
  72. this.timer = null;
  73. this.timeEnd();
  74. }, this.config.duration);
  75. },
  76. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  77. hide() {
  78. this.isShow = false;
  79. if (this.timer) {
  80. // 清除定时器
  81. clearTimeout(this.timer);
  82. this.timer = null;
  83. }
  84. },
  85. // 倒计时结束之后,进行的一些操作
  86. timeEnd() {
  87. // 如果带有url值,根据isTab为true或者false进行跳转
  88. if (this.config.url) {
  89. // 如果url没有"/"开头,添加上,因为uni的路由跳转需要"/"开头
  90. if (this.config.url[0] != '/') this.config.url = '/' + this.config.url;
  91. // 判断是否有传递显式的参数
  92. if (Object.keys(this.config.params).length) {
  93. // 判断用户传递的url中,是否带有参数
  94. // 使用正则匹配,主要依据是判断是否有"/","?","="等,如“/page/index/index?name=mary"
  95. // 如果有params参数,转换后无需带上"?"
  96. let query = '';
  97. if (/.*\/.*\?.*=.*/.test(this.config.url)) {
  98. // object对象转为get类型的参数
  99. query = this.$u.queryParams(this.config.params, false);
  100. this.config.url = this.config.url + "&" + query;
  101. } else {
  102. query = this.$u.queryParams(this.config.params);
  103. this.config.url += query;
  104. }
  105. }
  106. // 如果是跳转tab页面,就使用uni.switchTab
  107. if (this.config.isTab) {
  108. uni.switchTab({
  109. url: this.config.url
  110. });
  111. } else {
  112. uni.navigateTo({
  113. url: this.config.url
  114. });
  115. }
  116. }
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="scss" scoped>
  122. .u-toast {
  123. position: fixed;
  124. z-index: -1;
  125. transition: opacity 0.3s;
  126. text-align: center;
  127. color: #fff;
  128. border-radius: 8rpx;
  129. background: #585858;
  130. height: 80rpx;
  131. display: flex;
  132. align-items: center;
  133. justify-content: center;
  134. font-size: 28rpx;
  135. opacity: 0;
  136. pointer-events: none;
  137. padding:0 40rpx;
  138. }
  139. .u-toast.u-show {
  140. opacity: 1;
  141. }
  142. .u-text {
  143. word-break: keep-all;
  144. white-space: nowrap;
  145. line-height: normal;
  146. }
  147. .u-icon {
  148. margin-right: 10rpx;
  149. display: flex;
  150. align-items: center;
  151. line-height: normal;
  152. }
  153. .u-position-center {
  154. left: 50%;
  155. top: 50%;
  156. transform: translateX(-50%) translateY(-50%);
  157. }
  158. .u-position-top {
  159. left: 50%;
  160. top: 20%;
  161. transform: translateX(-50%) translateY(-50%);
  162. }
  163. .u-position-bottom {
  164. left: 50%;
  165. bottom: 20%;
  166. transform: translateX(-50%) translateY(-50%);
  167. }
  168. .u-type-primary {
  169. color: $u-type-primary;
  170. background-color: $u-type-primary-light;
  171. border: 1px solid rgb(215, 234, 254);
  172. }
  173. .u-type-success {
  174. color: $u-type-success;
  175. background-color: $u-type-success-light;
  176. border: 1px solid #BEF5C8;
  177. }
  178. .u-type-error {
  179. color: $u-type-error;
  180. background-color: $u-type-error-light;
  181. border: 1px solid #fde2e2;
  182. }
  183. .u-type-warning {
  184. color: $u-type-warning;
  185. background-color: $u-type-warning-light;
  186. border: 1px solid #faecd8;
  187. }
  188. .u-type-info {
  189. color: $u-type-info;
  190. background-color: $u-type-info-light;
  191. border: 1px solid #ebeef5;
  192. }
  193. .u-type-default {
  194. color: #fff;
  195. background-color: #585858;
  196. }
  197. </style>