uni-send-sms-code.vue 3.0 KB
Newer Older
芊里 已提交
1
<template>
芊里 已提交
2
	<view class="short-code-btn" hover-class="hover" @click="start">
芊里 已提交
3
		<text class="inner-text" :class="reverseNumber==0?'inner-text-active':''">{{innerText}}</text>
芊里 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	</view>
</template>

<script>
	function debounce(func, wait) {
		let timer;
		wait = wait || 500;
		return function() {
			let context = this;
			let args = arguments;
			if (timer) clearTimeout(timer);
			let callNow = !timer;
			timer = setTimeout(() => {
				timer = null;
			}, wait)
			if (callNow) func.apply(context, args);
		}
	}
	export default {
23
		name: "uni-send-sms-code",
芊里 已提交
24 25 26 27 28 29
		props: {
			/**
			 * 倒计时时长 s
			 */
			count: {
				type: [String, Number],
L
123  
linju 已提交
30
				default: 60
芊里 已提交
31 32 33 34 35 36 37
			},
			/**
			 * 手机号码
			 */
			phone: {
				type: [String, Number],
				default: ''
38 39 40 41 42 43 44 45 46
			},
			/*
				验证码类型,用于防止不同功能的验证码混用,目前支持的类型login登录、register注册、bind绑定手机、unbind解绑手机
			*/
			codeType:{
				type: String,
				default(){
					return 'login'
				}
芊里 已提交
47 48 49 50 51 52 53 54
			}
		},
		data() {
			return {
				reverseNumber: 0,
				reverseTimer: null
			};
		},
55
		computed: {
芊里 已提交
56
			innerText() {
57 58
				if (this.reverseNumber == 0) return this.$t('common').getVerifyCode;
				return this.$t('smsCode.resendVerifyCode ')+ '('+this.reverseNumber+'s)';
芊里 已提交
59 60 61 62 63 64 65
			}
		},
		created() {
			this.initClick();
		},
		methods: {
			initClick() {
芊里 已提交
66
				this.start = debounce(() => {
芊里 已提交
67
					if (this.reverseNumber != 0) return;
芊里 已提交
68 69 70
					this.sendMsg();
				})
			},
71 72 73
			sendMsg() {
				let reg_phone = /^1\d{10}$/;
				if(!reg_phone.test(this.phone))return uni.showToast({
74
					title: this.$t('smsCode.phoneErrTip'),
75 76
					icon: 'none'
				});
77 78 79 80 81 82 83 84 85 86 87
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'sendSmsCode',
						params:{
							"mobile": this.phone,
							"type": this.codeType
						},
					},
					success: ({result}) => {
						console.log(result);
88
						if(result.code===0){
89
							uni.showToast({
90
								title: this.$t('smsCode.sendSuccessTip'),
91 92 93 94
								icon: 'none'
							});
							this.reverseNumber = Number(this.count);
							this.getCode();
95 96 97 98 99 100
							this.$emit('getCode');
						}else{
							uni.showModal({
								content: result.msg,
								showCancel: false
							});
101 102 103
						}
					}
				})
芊里 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			},
			getCode() {
				if (this.reverseNumber == 0) {
					clearTimeout(this.reverseTimer);
					this.reverseTimer = null;
					return;
				}
				this.reverseNumber--;
				this.reverseTimer = setTimeout(() => {
					this.getCode();
				}, 1000)
			}
		}
	}
</script>

DCloud_JSON's avatar
DCloud_JSON 已提交
120
<style lang="scss" scoped>
121 122 123 124 125 126 127
/* #ifndef APP-NVUE */
view{
	display: flex;
	box-sizing: border-box;
	flex-direction: column;
}
/* #endif */
芊里 已提交
128 129
	.short-code-btn {
		width: 200rpx;
芊里 已提交
130 131 132 133 134 135 136
		height: 85rpx;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
137 138 139
	.inner-text {
		font-size: 28rpx;
		color: #AAAAAA;
芊里 已提交
140 141 142
	}
	.inner-text-active {
		color: #007aff;
芊里 已提交
143
	}
144
</style>