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