Blame view

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