fab-sms-code-input.uvue 4.8 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
				<template v-for="(_,i) in smsCodeList" :key="i">
9
          <!-- TODO APP :value="item" 第一次不触发@input -->
DCloud_JSON's avatar
DCloud_JSON 已提交
10 11
					<input @input="setSmsCode(i,$event as InputEvent)" type="number" ref="code-input" :value="smsCodeList[i]"
						class="code-input" :maxlength="i == 0 ? 7 : 2" @focus="onFocus(i)" @blur="onBlur(i)" :focus="focusInputs[i]" />
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
12 13 14 15 16 17 18 19 20 21 22 23
				</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
DCloud_JSON 已提交
28
        focusInputs: [false, false, false, false, false, false],
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
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
				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);
			},
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
64 65 66
    // mounted() {
    //   this.show()
    // },
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
67
		methods: {
DCloud_JSON's avatar
DCloud_JSON 已提交
68 69 70
      onBlur(i : number) {
        this.focusInputs[i] = false
      },
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
71 72 73
			show() {
				(this.$refs['popup'] as UniPopupComponentPublicInstance).open();
				this.$nextTick(() => {
DCloud_JSON's avatar
DCloud_JSON 已提交
74
					this.focusInputs[0] = true;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
75 76 77 78 79 80 81 82 83 84
				})
				this.isOpen = true
			},
			hide() {
				(this.$refs['popup'] as UniPopupComponentPublicInstance).close();
				this.isOpen = false
			},
			clear() {
				this.smsCodeList = ["\u200b", "\u200b", "\u200b", "\u200b", "\u200b", "\u200b"];
				if (this.isOpen) {
DCloud_JSON's avatar
DCloud_JSON 已提交
85
					this.focusInputs[0] = true;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
86 87 88 89 90 91 92
					// console.log('clear success');
				} else {
					// console.log('已经关了,不能清空');
				}
			},
			setSmsCode(i : number, e : InputEvent) {
				const { value } = e.detail
93
				// console.log('~~',value,value.length);
DCloud_JSON's avatar
DCloud_JSON 已提交
94

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
				// 已满6位数就直接调登录
				let $value = value.replace(/\u200b/g, '')
				if ($value.length == 6) {
					$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)
						this.smsCodeList[i] = newValue
					})
				}
				// 被删除完就直接后退一格
				if (value.length == 0) {
					this.smsCodeList[i] = ""
					this.$nextTick(() => {
						this.smsCodeList[i] = '\u200b'
					})
					if (i != 0) {
DCloud_JSON's avatar
DCloud_JSON 已提交
117
						this.focusInputs[i - 1] = true;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
118 119 120 121 122
						this.smsCodeList[i - 1] = ""
					}
				} else if (value != "\u200b") {
					this.smsCodeList[i] = value;
					if (i != (this.smsCodeList.length - 1)) {
DCloud_JSON's avatar
DCloud_JSON 已提交
123
						this.focusInputs[i + 1] = true;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
124 125 126 127 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 175 176
					} else {
						// console.log(i, (this.smsCodeList.length - 1));
					}
				}
			},
			onFocus(i : number) {
				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 已提交
177

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  .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 已提交
194

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
195 196 197 198 199 200 201 202 203 204
	.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 已提交
205
</style>