uni-id-pages-x-smsCode.uvue 7.1 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<template>
	<view class="mobile-box">
		<view @click="chooseArea" class="area">
			<text>+86</text>
			<uni-id-pages-x-icons :size="10" color="#666" @click="chooseArea" type="down" />
		</view>
		<view class="input-box">
			<uni-id-pages-x-input type="number" :border="false" v-model="mobile" :maxlength="11" placeholder="请输入11位手机号"
				ref="mobileInput"></uni-id-pages-x-input>
		</view>
	</view>
	<!-- 获取发送短信的验证码的图形验证码组件 -->
	<view class="send-sms-captcha-box">
		<uni-captcha ref="sendSmsCaptcha" scene="send-sms-code" v-model="sendSmsCaptcha" />
	</view>
	<!-- 悬浮的短信验证码输入框 -->
	<fab-sms-code-input v-model="smsCode" :mobile="mobile" :reverseNumber="reverseNumber" ref="fab-sms-code-input"
		@reGetSmsCode="reGetSmsCode" title="验证并登录" />
</template>

<script>
	import fabSmsCodeInput from './fab-sms-code-input.uvue';
	import { state } from '@/uni_modules/uni-id-pages-x/store.uts';
	export default {
		name: "uni-id-pages-x-smsCode",
		components: { fabSmsCodeInput },
A
Anne_LXM 已提交
27
    emits:["input"],
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
28 29 30 31 32
		data() {
			return {
				mobile: "",
				sendSmsCaptcha: "",
				smsCode: "",
A
Anne_LXM 已提交
33 34
				fabSmsCodeInputEl: null as null | ComponentPublicInstance,
				sendSmsCaptchaCP: null as null | UniCaptchaComponentPublicInstance,
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
				reverseNumber: 0
			}
		},
		props: {
			scene: {
				type: String,
				default: "login-by-sms"
			},
			autoSend: {
				type: Boolean,
				default: true
			}
		},
		computed: {
		},
		watch: {
			//  手机号输满11位时,自动给发送短信验证码的图形验证码“获取焦点”
			mobile(mobile) {
				this.emitInput()
				if (mobile.length == 11) {
A
Anne_LXM 已提交
55
					this.sendSmsCaptchaCP!.setFocus(true);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
					// 倒计时归零,允许再次发送
					this.reverseNumber = 0
				}
			},
			// 图形验证码填好直接自动发送 短信验证码
			sendSmsCaptcha() {
				this.emitInput()
				if (this.autoSend && this.sendSmsCaptcha.length == 4 && this.mobile.length == 11) {
					this.sendSmsCode()
				}
			},
			smsCode() {
				this.emitInput()
			}
		},
		mounted() {
A
Anne_LXM 已提交
72 73
			this.fabSmsCodeInputEl = this.$refs["fab-sms-code-input"] as ComponentPublicInstance;
			this.sendSmsCaptchaCP = this.$refs["sendSmsCaptcha"] as UniCaptchaComponentPublicInstance;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
74 75 76 77 78 79 80 81 82 83 84 85

			// 加载好,手机号码输入框就自动获取焦点
			// TO 临时方案解决 this.$nextTick 无效,由setTimeout 300 代替
			setTimeout(() => {
				this.setMobileInputFocus(true)
			}, 300)
		},
		methods: {
      setMobileInputFocus(val:boolean){
        (this.$refs['mobileInput'] as UniIdPagesXInputComponentPublicInstance).setFocus(val);
      },
			showCodeInput() {
A
Anne_LXM 已提交
86
				if (this.reverseNumber == 0) {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
					this.reverseNumber = 6
					//开始倒计时
					let reverseTimer = function () { }
					reverseTimer = () => {
						if (this.reverseNumber != 0) {
							this.reverseNumber--;
							setTimeout(() => {
								reverseTimer()
							}, 1000)
						}
					}
					reverseTimer()
				}
A
Anne_LXM 已提交
100
				this.fabSmsCodeInputEl?.$callMethod('show', true);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			},
			emitInput() {
				const param : UTSJSONObject = {
					"mobile": this.mobile,
					"code": this.smsCode,
					"sendSmsCaptcha": this.sendSmsCaptcha,
				}
				this.$emit("input", param)
			},
			chooseArea() {
				uni.showToast({
					title: "目前只支持中国",
					icon: "none"
				});
			},
			// 重新获取短信验证码
			reGetSmsCode() {
				this.hideCodeInput();
				this.clearCodeInput();
A
Anne_LXM 已提交
120
				this.sendSmsCaptchaCP!.getImageCaptcha(true);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
121 122
			},
			hideCodeInput() {
A
Anne_LXM 已提交
123
				this.fabSmsCodeInputEl!.$callMethod("hide");
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
124 125
			},
			clearCodeInput() {
A
Anne_LXM 已提交
126
				this.fabSmsCodeInputEl!.$callMethod('clear')
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
127 128 129 130 131 132 133 134 135
			},
			reset() {
				this.smsCode = ""
				this.sendSmsCaptcha = ""
				this.clearCodeInput()
				this.hideCodeInput()
				// console.log('reset');
			},
			sendSmsCode() {
A
Anne_LXM 已提交
136
				console.log("state.pendingAgreements", state.pendingAgreements);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
137 138
				if (state.pendingAgreements) {
          // uni.hideKeyboard();
A
Anne_LXM 已提交
139
          this.sendSmsCaptchaCP!.setFocus(false);
A
Anne_LXM 已提交
140 141 142 143 144 145
          // web端有问题
          // (this.$parent as ComponentPublicInstance).$callMethod("showPopupCaptcha", () => {
          // 	if (!state.pendingAgreements) {
          // 		this.sendSmsCode()
          // 	}
          // });
A
Anne_LXM 已提交
146 147 148 149 150 151
          // 临时方案
          uni.$emit('uni-id-pages-x-loginBySmsCode-showPopupCaptcha',()=>{
            if (!state.pendingAgreements) {
            	this.sendSmsCode()
            }
          })
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
					uni.showToast({
						title: '未同意隐私政策协议',
						icon: 'none'
					});
					return
				}
				// console.log('sendSmsCode');
				// 如果还在倒计时就显示出来输入框,阻止发送
				if (this.reverseNumber != 0) {
					return this.showCodeInput()
				}

				let reg_mobile = /^1\d{10}$/;
				if (!reg_mobile.test(this.mobile)) {
					(this.$refs['mobileInput'] as UniIdPagesXInputComponentPublicInstance).setFocus(true)
					uni.showToast({
						title: "手机号格式错误",
						icon: 'none',
						duration: 3000
					})
					return
				}

				if (this.sendSmsCaptcha.length != 4) {
A
Anne_LXM 已提交
176
					this.sendSmsCaptchaCP!.setFocus(true)
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
177 178 179 180 181 182 183 184
					uni.showToast({
						title: '请先输入图形验证码',
						icon: 'none',
						duration: 3000
					});
					return
				}

A
Anne_LXM 已提交
185
				// const param : UTSJSONObject
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
				// console.log('sendSmsCode',{
				// 	"mobile": this.mobile,
				// 	"scene": "login-by-sms",
				// 	"captcha": this.sendSmsCaptcha
				// });

				uni.showLoading({ "title": "发送中" })
				const uniIdCo = uniCloud.importObject("uni-id-co", { "customUI": true })
				uniIdCo.sendSmsCode({
					"mobile": this.mobile,
					"scene": this.scene,
					"captcha": this.sendSmsCaptcha
				})
					.finally(() : void => {
						uni.hideLoading()
					})
					.then((_ : UTSJSONObject) => {
						uni.showToast({
							title: "短信验证码发送成功",
							icon: 'none',
							duration: 3000
						});
						// console.log('result', result);
						this.showCodeInput()
					})
A
Anne_LXM 已提交
211
					.catch((err : any | null) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
212 213 214 215 216 217 218 219 220 221 222 223
						const error = err as UniCloudError
						// console.error(error.message)
						// console.error(error.code)

						switch (error.code) {
							case "uni-captcha-verify-fail":
								uni.showToast({
									title: error.message,
									icon: 'none',
									duration: 3000,
									mask: false
								});
A
Anne_LXM 已提交
224
								this.sendSmsCaptchaCP!.getImageCaptcha(true);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
								this.sendSmsCaptcha = "";
								break;
							case "uni-id-invalid-sms-template-id":
								this.showCodeInput()
								uni.showToast({
									title: ' 当前为测试模式,详情【控制台信息】',
									icon: 'none',
									duration: 3000
								});
								console.warn(error.message);
								break;
							default:
								uni.showToast({
									title: error.message,
									icon: 'none',
									duration: 3000
								});
								break;
						}
					})
			},
		}
	}
</script>

<style>
	.mobile-box {
		position: relative;
		flex-direction: row;
		background-color: #F8F8F8;
		justify-content: center;
		align-items: center;
		border-radius: 5rpx;
	}

	.area {
		width: 90rpx;
		justify-content: space-around;
		align-items: center;
		flex-direction: row;
		margin-left: 10px;
	}

	.mobile-box .input-box {
		flex: 1;
	}

	.send-sms-captcha-box {
		margin-top: 10px;
	}
A
Anne_LXM 已提交
275
</style>