表单校验、发送验证码、本地校验验证码是否正确、表单提交——uniapp+uview组件

1.HTML

<u--form labelPosition="left" :model="model1" ref="form1">
					<u-form-item label="卡号" prop="cardNo" labelWidth="80" borderBottom>
						<u--input v-model="model1.cardNo" border="none" placeholder="请填写绑定卡号"></u--input>
						<!-- <u-button slot="right" @tap="getCode" :text="tips" type="primary" size="mini"
							:disabled="disabled1"></u-button> -->
					</u-form-item>
					<u-form-item label="手机号" prop="mobile" labelWidth="80" borderBottom>
						<u--input v-model="model1.mobile" border="none" placeholder="请填写手机号"></u--input>
						<!-- <u-button slot="right" @tap="getCode" :text="tips" type="primary" size="mini"
							:disabled="disabled1"></u-button> -->
						<view class="getCode" :class="{activeCode:codeFlag}" @click="getCode">
							{{codeTxt}}
						</view>
					</u-form-item>
					<u-form-item label="验证码" prop="code" labelWidth="80" borderBottom ref="item1">
						<u--input v-model="model1.code" border="none" placeholder="请填写验证码"></u--input>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="绑定" customStyle="margin-top: 50px" @click="submit"
					shape="circle"></u-button>

2.Script

data() {
			return {
				disabled1: false,
				tips: '',
				model1: {
					// cardNo: '57100677366', //卡号
					cardNo: '', //卡号
					mobile: '', //手机号
					code: ''
				},
				rules: {
					cardNo: [{
						type: 'string',
						required: true,
						message: '请填写绑定卡号',
						trigger: ['blur', 'change']
					}],
					mobile: [{
						type: 'string',
						required: true,
						message: '请填写手机号',
						trigger: ['blur', 'change']
					}],
					code: {
						type: 'string',
						required: true,
						message: '请填写验证码',
						trigger: ['blur', 'change']
					}
				},
				openid: '',
				verificationCode: '',
				codeTxt: '获取验证码',
				codeFlag: true, // 控制按钮是否可点击
			}
		},
		// 一定要设置,否则校验不生效
		onReady() {
			// 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
			this.$refs.form1.setRules(this.rules)
		},
// 发送验证码
			getCode() {
				let that = this
				if (this.codeFlag == false) {
					return
				}
				if (that.model1.cardNo == '') {
					uni.$u.toast('卡号不能为空');
				} else if (that.model1.mobile == '') {
					uni.$u.toast('手机号码不能为空');
				} else {
					uni.showToast({
						title: '验证码发送成功',
						icon: 'none',
						duration: 2000
					})
					this.codeFlag = false;
					var time = 60
					this.codeTxt = time + 'S' + '重新获取'

					var timer = setInterval(() => {
						this.codeTxt = '获取验证码'
						if (time == 1) {
							this.codeFlag = true;
							clearInterval(timer)
						} else {
							time--
							this.codeTxt = time + 'S' + '重新获取'
						}

					}, 1000)
					// 发布接口,注意post请求携带参数的方式,这里将对象重新赋值
					setTimeout(() => {
						that.model1 = {
							cardNo: that.model1.cardNo,
							mobile: that.model1.mobile
						}
						uni.showLoading({
							title: '加载中...',
							mask: true
						});
						uni.request({
							// header: {
							// 	'Content-Type': 'application/x-www-form-urlencoded'
							// },
							url: 'http://wx.cunkou.co/api/wx/sendSms', //仅为示例,并非真实接口地址。
							method: 'POST',
							data: {
								cardNo: that.model1.cardNo,
								mobile: that.model1.mobile
							},
							// dataType: 'json',
							success: (res) => {
								uni.hideLoading();
								if (res.data.code == 0) {
									that.verificationCode = res.data.data
								} else {
									uni.showToast({
										title: res.data.message,
										icon: 'none',
										duration: 2000
									})
								}

							}

						});
					}, 2000)

				}

			},
// 表单提交
			submit() {
				let that = this
				// 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {

					uni.showModal({
						content: '你确定要绑定吗?',
						success: function(res) {

							if (res.confirm) {
								if (that.model1.code !== that.verificationCode) {
									uni.$u.toast('验证码不正确');
								} else {
									that.model1 = {
										cardNo: that.model1.cardNo,
										mobile: that.model1.mobile,
										code: that.model1.code,
										openid: that.openid
									}
									uni.showLoading({
										title: '加载中...',
										mask: true
									});
									uni.request({
										// header: {
										// 	'Content-Type': 'application/x-www-form-urlencoded'
										// },
										url: 'http://wx.cunkou.co/api/wx/bindCard', //为示例,并非真实接口地址。
										method: 'POST', //一定要大写
										data: {
											cardNo: that.model1.cardNo,
											mobile: that.model1.mobile,
											code: that.model1.code,
											openid: that.openid
										},
										// dataType: 'json',
										success: (res) => {
											uni.hideLoading();
											if (res.data.code == 0) {
												uni.showToast({
													title: '绑定成功',
													icon: 'none',
													duration: 2000
												})
												// uni.setStorage({
												// 	key:'userInfo',
												// 	data:res.data
												// })
												// uni.setStorageSync(KEY,DATA)
												uni.setStorageSync('userInfo', res.data
													.data);

												setTimeout(function() {
													uni.reLaunch({
														url: '/pages/mine/mine'
													})
												}, 2000);

											} else {
												uni.showToast({
													title: res.data.message,
													icon: 'none',
													duration: 2000
												})
											}

										}

									});
								}

							} else if (res.cancel) {}
						}
					});

				}).catch(errors => {
					uni.$u.toast('请填写完整的信息')
				})
			},

3.Style

<style lang="scss">
	.u-demo-block__content {
		margin: 20rpx;
	}

	.getCode {
		height: 60rpx;
		width: 180rpx;
		background: #f7f7f7;
		color: #666666;
		line-height: 60rpx;
		font-size: 24rpx;
		text-align: center;
		border-radius: 50rpx;
	}

	.getCode.activeCode {
		background: #2D99F5;
		color: #FFFFFF;
	}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值