uni-send-sms-code.vue 2.8 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 55 56 57
			}
		},
		data() {
			return {
				reverseNumber: 0,
				reverseTimer: null
			};
		},
		computed: {
			innerText() {
				if (this.reverseNumber == 0) return '获取验证码';
DCloud_JSON's avatar
DCloud_JSON 已提交
58
				return '重新发送('+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 74 75 76
			sendMsg() {
				let reg_phone = /^1\d{10}$/;
				if(!reg_phone.test(this.phone))return uni.showToast({
					title: '手机号格式错误',
					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 89 90 91 92 93 94 95 96 97 98 99 100 101
						if(result.code===0){
							uni.showToast({
								title: "短信验证码发送成功",
								icon: 'none'
							});
							this.reverseNumber = Number(this.count);
							this.getCode();
							this.$emit('getCode');
						}else{
							uni.showModal({
								content: result.msg,
								showCancel: false
							});
						}
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 121
<style lang="scss" scoped>
@import '@/common/all-flex.css';
芊里 已提交
122 123
	.short-code-btn {
		width: 200rpx;
芊里 已提交
124 125 126 127 128 129 130
		height: 85rpx;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
131 132 133
	.inner-text {
		font-size: 28rpx;
		color: #AAAAAA;
芊里 已提交
134 135 136
	}
	.inner-text-active {
		color: #007aff;
芊里 已提交
137
	}
138
</style>