retrieve.uvue 4.4 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
<template>
	<view class="page">
		<view class="form">
			<!-- 获取验证码组件(输入手机号码+发送短信验证码所需的图形验证码,获得短信验证码) -->
			<uni-id-pages-x-smsCode scene="reset-pwd-by-sms" ref="smsCode" @input="smsCodeInput"
				:autoSend="false"></uni-id-pages-x-smsCode>
			<uni-id-pages-x-input class="my-input" :border="false" v-model="password" placeholder="请输入新密码" ref="password"
				:password="true"></uni-id-pages-x-input>
			<uni-id-pages-x-input class="my-input" :border="false" v-model="password2" placeholder="请再次输入密码" ref="password2"
				:password="true"></uni-id-pages-x-input>
			<button class="uni-btn" type="primary" @click="doNext">下一步</button>
		</view>

		<!-- 多次重置输入错误时,需要额外输入图形验证码 (悬浮) -->
		<uni-popup-captcha ref="captcha" scene="reset-pwd-by-sms" v-model="captcha" title="请输入验证码"></uni-popup-captcha>
	</view>
</template>

<script>
	import { checkPassword } from '@/uni_modules/uni-id-pages-x/common/common.uts';
	const uniIdCo = uniCloud.importObject("uni-id-co", { "customUI": true })
	export default {
		data() {
			return {
				smsCode: "",
A
Anne_LXM 已提交
26
				smsCodeRef: null as null | UniIdPagesXSmsCodeComponentPublicInstance,
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
27 28 29 30 31 32
				password: "",
				password2: "",
				captcha: ""
			}
		},
		mounted() {
A
Anne_LXM 已提交
33
			this.smsCodeRef = (this.$refs["smsCode"] as UniIdPagesXSmsCodeComponentPublicInstance)
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
34
		},
A
Anne_LXM 已提交
35 36
		onLoad(param) {
			const mobile = param["mobile"]
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
37 38 39 40 41
			// const email = param.get("email")
			// console.log('mobile--', mobile);
			// console.log('email--', email);
			if (mobile != null) {
				this.$nextTick(() => {
A
Anne_LXM 已提交
42
					this.smsCodeRef!.mobile = mobile
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
				})
			}
		},
		methods: {
			smsCodeInput(param : UTSJSONObject) {
				// console.log('smsCodeInput param', param);
				const mobile = param.getString("mobile") as string;
				const code = param.getString("code") as string;
				const sendSmsCaptcha = param.getString("sendSmsCaptcha") as string;
				if (sendSmsCaptcha.length == 4 && code.length == 0) {
					(this.$refs["password"] as UniIdPagesXInputComponentPublicInstance).setFocus(true);
				}
				if (mobile.length == 11 && code.length == 6) {
					this.resetPwdBySms(param)
				}
			},
			doNext() {
				// 根据配置的密码强度校验
				let checkPasswordRes = checkPassword(this.password);
				let isPass = checkPasswordRes.getBoolean("pass") as Boolean
				if (!isPass) {
					const errMsg = checkPasswordRes.getString("errMsg")
					return uni.showModal({
						title:"提示",
						content: errMsg,
						showCancel: false,
						confirmText: "知道了",
						complete() {
							(this.$refs["password"] as UniIdPagesXInputComponentPublicInstance).setFocus(true);
						}
					});
				}

				if (this.password != this.password2) {
					return uni.showModal({
						title:"提示",
						content: "两次输入的密码不一致",
						showCancel: false,
						confirmText: "知道了",
						complete() {
							(this.$refs["password2"] as UniIdPagesXInputComponentPublicInstance).setFocus(true);
						}
					});
				}
A
Anne_LXM 已提交
87
				this.smsCodeRef!.sendSmsCode();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
88 89 90 91 92 93 94 95 96
			},
			resetPwdBySms(param : UTSJSONObject) {
				uni.showLoading({ "title": "请求中" })
				param.set("password", this.password)
				param.set("captcha", this.captcha)
				uniIdCo.resetPwdBySms(param)
					.finally(() : void => {
						uni.hideLoading()
					})
A
Anne_LXM 已提交
97
					.then((_ : UTSJSONObject) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
98 99
						// console.log('then');
						// console.log(e);
A
Anne_LXM 已提交
100
						this.smsCodeRef!.hideCodeInput();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
101 102 103 104 105 106
						uni.showToast({
							title: '重置成功',
							icon: 'none'
						});
						uni.navigateBack()
					})
A
Anne_LXM 已提交
107
					.catch((err : any | null) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120
						const error = err as UniCloudError
						console.error(error)
						console.error(error.code)
						if (["uni-id-captcha-required", "uni-captcha-verify-fail"].includes(error.code as string)) {
							(this.$refs["captcha"] as UniPopupCaptchaComponentPublicInstance).open(() => {
								this.resetPwdBySms(param)
							});
						} else {
							uni.showToast({
								title: error.message,
								icon: 'none',
								mask: false
							});
A
Anne_LXM 已提交
121
							this.smsCodeRef!.clearCodeInput()
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
							this.captcha = ""
						}
					})
			},
		}
	}
</script>

<style>
	@import url("/uni_modules/uni-id-pages-x/common/common.scss");

	.root {
		background-color: #FFF;
		width: 750rpx;
		flex: 1;
	}
	.form {
	  margin:50rpx 50rpx 0 50rpx;
	}
A
Anne_LXM 已提交
141
</style>