fab-sms-code-input.uvue 4.9 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3 4 5 6 7
<template>
	<uni-popup ref="popup" :mask-click="false">
		<view class="sms-code-content">
			<!-- 顶部文字 -->
			<text class="sms-code-title">{{title}}</text>
			<text class="sms-code-tip">短信验证码已发送至{{mobile}}</text>
			<view class="code-input-list">
A
Anne_LXM 已提交
8 9 10
				<template v-for="(_,i) in smsCodeList" :key="i">
          <!-- TODO APP :value="item" -->
					<input @input="setSmsCode(i,$event as InputEvent)" type="number" ref="code-input"
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
						class="code-input" maxlength="7" @focus="onFocus(i)" />
				</template>
			</view>
      <view class="fab-sms-code-input-foot">
        <text class="close" @click="clear();hide();">关闭</text>
        <text @click="reGetSmsCode" :class="'get-sms-code-tip'+ ((reverseNumber == 0) ? '-active' : '')">{{reGetSmsCodeTip}}</text>
      </view>
		</view>
	</uni-popup>
</template>

<script>
	export default {
A
Anne_LXM 已提交
24
		emits: ["update:modelValue","reGetSmsCode","blur", "focus" ],
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
25 26
		data() {
			return {
A
Anne_LXM 已提交
27
        smsCodeList: ["\u200b", "\u200b", "\u200b", "\u200b", "\u200b", "\u200b"],
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
				isOpen: false
			}
		},
		props: {
      title: {
        type: String,
        default: "请输入验证码"
      },
			mobile: {
				type: String,
				default: ""
			},
			modelValue: {
				type: String,
				default: ""
			},
			reverseNumber: {
				type: Number,
				default: 0
			},
		},
		computed: {
			reGetSmsCodeTip() : string {
				if (this.reverseNumber == 0) return "重新获取";
				return "重新发送" + '(' + this.reverseNumber + 's)';
			},
			smsCode() : string {
				return this.smsCodeList.map((item) : string => item.replace(/\u200b/g, '')).join("")
			}
		},
		watch: {
			smsCode(smsCode) {
				this.$emit('update:modelValue', smsCode);
			},
		},
		methods: {
			show() {
				(this.$refs['popup'] as UniPopupComponentPublicInstance).open();
				this.$nextTick(() => {
A
Anne_LXM 已提交
67
					(this.$refs["code-input"] as UniElement[])[0].focus();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
68 69 70 71 72 73 74 75 76 77 78
				})
				this.isOpen = true
			},
			hide() {
				(this.$refs['popup'] as UniPopupComponentPublicInstance).close();
				this.isOpen = false
				// console.log('hide success');
			},
			clear() {
				this.smsCodeList = ["\u200b", "\u200b", "\u200b", "\u200b", "\u200b", "\u200b"];
				if (this.isOpen) {
A
Anne_LXM 已提交
79
					(this.$refs["code-input"] as UniElement[])[0].focus();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
80 81 82 83 84 85
					// console.log('clear success');
				} else {
					// console.log('已经关了,不能清空');
				}
			},
			setSmsCode(i : number, e : InputEvent) {
A
Anne_LXM 已提交
86
				// console.log('setSmsCode--',e);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
87
				const { value } = e.detail
A
Anne_LXM 已提交
88
				console.log('~~',value,value.length);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
				// 已满6位数就直接调登录
				let $value = value.replace(/\u200b/g, '')
				if ($value.length == 6) {
					// (this.$refs["code-input"] as Element[])[0].focus();
					// console.log('~~~~~', $value, $value.split(''));
					$value.split('').forEach((item : string, index : number) => {
						this.smsCodeList[index] = "\u200b" + item
					})
					return
				}
				// 限制每个空格内的文字不超过2位
				if (value.length > 2) {
					this.$nextTick(() => {
						let newValue = value.slice(value.length - 1)
						// console.log('newValue', newValue);
						this.smsCodeList[i] = newValue
					})
				}
				// 被删除完就直接后退一格
				if (value.length == 0) {
					this.smsCodeList[i] = ""
					this.$nextTick(() => {
						this.smsCodeList[i] = '\u200b'
					})
					if (i != 0) {
A
Anne_LXM 已提交
114
						(this.$refs["code-input"] as UniElement[])[i - 1].focus();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
115 116 117 118 119
						this.smsCodeList[i - 1] = ""
					}
				} else if (value != "\u200b") {
					this.smsCodeList[i] = value;
					if (i != (this.smsCodeList.length - 1)) {
A
Anne_LXM 已提交
120
						(this.$refs["code-input"] as UniElement[])[i + 1].focus();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
121 122 123 124 125 126
					} else {
						// console.log(i, (this.smsCodeList.length - 1));
					}
				}
			},
			onFocus(i : number) {
A
Anne_LXM 已提交
127
        // console.log("onFocus",i);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
				if (this.smsCodeList[i].length == 0) {
					this.smsCodeList[i] = '\u200b'
				}
			},
			reGetSmsCode() {
				this.$emit("reGetSmsCode")
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sms-code-content {
		background-color: #FFF;
		width: 550rpx;
		justify-content: center;
		align-items: center;
		padding: 15px 20px;
		border-radius: 5px;
	}

	.sms-code-title,
	.sms-code-tip {
		text-align: left;
		padding-left:20px;
		width: 550rpx;
	}

	.sms-code-tip {
		color: #555;
		font-weight: 400;
		font-size: 12px;
		margin-top: 5px;
	}

	.code-input-list {
		flex-direction: row;
	}

	.code-input-list .code-input {
		width: 30px;
		height: 30px;
		border: 1px solid #eee;
		border-radius: 5px;
		margin: 10px 5px;
		text-align: center;
	}
A
Anne_LXM 已提交
175

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  .fab-sms-code-input-foot {
    margin-top: 5px;
    margin-left: 300rpx;
    position: relative;
    left: -15px;
    width: 250rpx;
    flex-direction: row;
    justify-content: flex-end;
  }
  .fab-sms-code-input-foot .close {
    width: 80rpx;
    font-size: 12px;
    color: #888;
    text-align: center;
    margin-right: 26rpx;
  }
A
Anne_LXM 已提交
192

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
193 194 195 196 197 198 199 200 201 202
	.get-sms-code-tip,
	.get-sms-code-tip-active {
		font-size: 12px;
		text-align: right;
		color: #AAA;
	}

	.get-sms-code-tip-active {
		color: #005eca;
	}
A
Anne_LXM 已提交
203
</style>